1 %{ // -*-Fundamental-*-
3 lexer.l -- implement the Flex lexer
5 source file of the LilyPond music typesetter
7 (c) 1996--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
15 after making a change to the lexer rules, run
16 flex -b <this lexer file>
19 contains no backup states, but only the reminder
20 Compressed tables always back up.
21 (don-t forget to rm lex.yy.cc :-)
27 #include <iostream.h> /* gcc 3.0 */
32 #include "lily-guile.hh"
34 #include "string-convert.hh"
35 #include "my-lily-lexer.hh"
37 #include "interval.hh"
38 #include "lily-guile.hh"
42 #include "musical-request.hh"
44 #include "lilypond-input-version.hh"
45 #include "translator-def.hh"
46 #include "music-output-def.hh"
51 #define isatty HORRIBLEKLUDGE
53 void strip_trailing_white (String&);
54 void strip_leading_white (String&);
55 String lyric_fudge (String s);
59 valid_version_b (String s);
63 #define start_quote() \
64 yy_push_state (quote);\
65 yylval.string = new String
70 #define YY_USER_ACTION add_lexed_char (YYLeng ());
73 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
78 SCM scan_fraction (String);
86 %option yyclass="My_lily_lexer"
88 %option never-interactive
107 NATIONAL [\001-\006\021-\027\031\036\200-\377]
108 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
115 REAL ({INT}\.{N}*)|(-?\.{N}+)
118 HORIZONTALWHITE [ \t]
122 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
130 // windows-suck-suck-suck
133 <INITIAL,chords,incl,lyrics,notes,figures>{
135 yy_push_state (longcomment);
139 %[^{\n] { // backup rule
150 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
151 yy_push_state (version);
153 <version>\"[^"]*\" { /* got the version number */
154 String s (YYText ()+1);
155 s = s.left_str (s.index_last_i ('"'));
158 if (!valid_version_b (s))
162 LexerError ("No quoted string found after \\version");
175 LexerError (_ ("EOF found inside a comment").ch_C ());
176 if (! close_input ())
177 yyterminate (); // can't move this, since it actually rets a YY_NULL
182 <INITIAL,chords,lyrics,notes,figures>\\maininput {
186 main_input_b_ = true;
189 error (_ ("\\maininput disallowed outside init files"));
192 <INITIAL,chords,lyrics,figures,notes>\\include {
193 yy_push_state (incl);
195 <incl>\"[^"]*\";? { /* got the include file name */
196 String s (YYText ()+1);
197 s = s.left_str (s.index_last_i ('"'));
199 new_input (s,source_global_l);
202 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
203 String s = YYText () + 1;
204 strip_trailing_white (s);
205 if (s.length_i () && (s[s.length_i () - 1] == ';'))
206 s = s.left_str (s.length_i () - 1);
208 SCM sid = lookup_identifier (s);
209 if (gh_string_p (sid)) {
210 new_input (ly_scm2string (sid), source_global_l);
213 String msg (_f ("wrong or undefined identifier: `%s'", s ));
215 LexerError (msg.ch_C ());
216 SCM err = scm_current_error_port ();
217 scm_puts ("This value was found in the table: ", err);
218 scm_display (sid, err);
221 <incl>\"[^"]* { // backup rule
222 cerr << _ ("Missing end quote") << endl;
225 <chords,notes,figures>{RESTNAME} {
226 const char *s = YYText ();
227 yylval.scm = ly_str02scm (s);
230 <chords,notes,figures>R {
231 return MULTI_MEASURE_REST;
233 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}*{WHITE} {
234 String s=YYText () + 2;
235 s=s.left_str (s.length_i () - 1);
236 return scan_escaped_word (s);
238 <INITIAL,chords,lyrics,notes,figures>\${BLACK}*{WHITE} {
239 String s=YYText () + 1;
240 s=s.left_str (s.length_i () - 1);
241 return scan_bare_word (s);
243 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}* { // backup rule
244 cerr << _ ("white expected") << endl;
247 <INITIAL,chords,lyrics,notes,figures>\${BLACK}* { // backup rule
248 cerr << _ ("white expected") << endl;
252 <INITIAL,chords,lyrics,notes,figures># { //embedded scm
253 //char const* s = YYText () + 1;
254 char const* s = here_ch_C ();
256 if (main_input_b_ && safe_global_b) {
257 error (_ ("Can't evaluate Scheme in safe mode"));
258 yylval.scm = SCM_EOL;
261 yylval.scm = ly_parse_scm (s, &n);
263 for (int i=0; i < n; i++)
267 char_count_stack_.top () += n;
281 return scan_bare_word (YYText ());
285 return scan_escaped_word (YYText () + 1);
288 yylval.scm = scan_fraction (YYText ());
293 yylval.i = String_convert::dec2_i (String (YYText ()));
297 yylval.i = String_convert::dec2_i (String (YYText ()));
311 *yylval.string += to_str (escaped_char (YYText ()[1]));
314 *yylval.string += YYText ();
320 /* yylval is union. Must remember STRING before setting SCM*/
321 String *sp = yylval.string;
322 yylval.scm = ly_str02scm (sp->ch_C ());
327 *yylval.string += YYText ();
336 yylval.scm = scan_fraction (YYText ());
340 yylval.i = String_convert::dec2_i (String (YYText ()));
344 return scan_escaped_word (YYText () + 1);
348 String s (YYText ());
350 return yylval.i = EXTENDER;
352 return yylval.i = HYPHEN;
355 char c = s[s.length_i () - 1];
356 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
357 here_input ().warning (
358 _ ("Brace found at end of lyric. Did you forget a space?"));
359 yylval.scm = ly_str02scm (s.ch_C ());
370 return scan_bare_word (YYText ());
373 return scan_escaped_word (YYText () + 1);
376 yylval.scm = scan_fraction (YYText ());
380 yylval.i = String_convert::dec2_i (String (YYText ()));
406 if (! close_input ()) {
407 yyterminate (); // can't move this, since it actually rets a YY_NULL
413 return scan_bare_word (YYText ());
416 return scan_escaped_word (YYText () + 1);
420 int cnv=sscanf (YYText (), "%lf", &r);
423 yylval.scm = gh_double2scm (r);
428 yylval.i = String_convert::dec2_i (String (YYText ()));
437 char c = YYText ()[0];
442 <INITIAL,notes,figures>. {
446 <INITIAL,lyrics,notes,figures>\\. {
447 char c= YYText ()[1];
455 return E_EXCLAMATION;
470 String msg = _f ("invalid character: `%c'", YYText ()[0]);
471 LexerError (msg.ch_C ());
478 My_lily_lexer::push_note_state ()
480 yy_push_state (notes);
484 My_lily_lexer::push_figuredbass_state()
486 yy_push_state (figures);
489 My_lily_lexer::push_chord_state ()
491 yy_push_state (chords);
495 My_lily_lexer::push_lyric_state ()
497 yy_push_state (lyrics);
501 My_lily_lexer::pop_state ()
507 My_lily_lexer::scan_escaped_word (String str)
509 // use more SCM for this.
511 SCM sym = ly_symbol2scm (str.ch_C ());
513 int l = lookup_keyword (str);
517 SCM sid = lookup_identifier (str);
518 if (gh_string_p (sid)) {
520 return STRING_IDENTIFIER;
521 } else if (gh_number_p (sid)) {
523 return NUMBER_IDENTIFIER;
524 } else if (unsmob_translator_def (sid)) {
526 return TRANSLATOR_IDENTIFIER;
527 } else if (unsmob_score (sid)) {
529 return SCORE_IDENTIFIER;
530 } else if (Music * mus =unsmob_music (sid)) {
533 return dynamic_cast<Request*> (mus) ? REQUEST_IDENTIFIER : MUSIC_IDENTIFIER;
534 } else if (unsmob_duration (sid)) {
536 return DURATION_IDENTIFIER;
537 } else if (unsmob_music_output_def (sid)) {
539 return MUSIC_OUTPUT_DEF_IDENTIFIER;
542 if (sid != SCM_UNDEFINED) {
544 return SCM_IDENTIFIER;
547 if ((YYSTATE != notes) && (YYSTATE != chords)) {
548 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
550 if (gh_pair_p (pitch))
552 yylval.scm = ly_cdr (pitch);
553 return NOTENAME_PITCH;
556 String msg (_f ("unknown escaped string: `\\%s'", str));
557 LexerError (msg.ch_C ());
559 yylval.scm = ly_str02scm (str.ch_C ());
565 My_lily_lexer::scan_bare_word (String str)
567 SCM sym = ly_symbol2scm (str.ch_C ());
568 if ((YYSTATE == notes) || (YYSTATE == chords)) {
569 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
570 if (gh_pair_p (pitch)) {
571 yylval.scm = ly_cdr (pitch);
572 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
573 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
575 yylval.scm = ly_cdr (pitch);
576 return CHORDMODIFIER_PITCH;
580 yylval.scm = ly_str02scm (str.ch_C ());
585 My_lily_lexer::note_state_b () const
587 return YY_START == notes;
591 My_lily_lexer::chord_state_b () const
593 return YY_START == chords;
597 My_lily_lexer::lyric_state_b () const
599 return YY_START == lyrics;
603 My_lily_lexer::figure_state_b () const
605 return YY_START == figures;
609 urg, belong to String (_convert)
610 and should be generalised
613 strip_leading_white (String&s)
616 for (; i < s.length_i (); i++)
620 s = s.nomid_str (0, i);
624 strip_trailing_white (String&s)
631 s = s.left_str (i+1);
636 Lilypond_version oldest_version ("1.3.59");
639 print_lilypond_versions (ostream &os)
641 os << _f ("Oldest supported input version: %s", oldest_version.str ())
647 valid_version_b (String s)
649 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
650 Lilypond_version ver (s);
651 if (! ((ver >= oldest_version) && (ver <= current)))
653 non_fatal_error (_f ("incorrect lilypond version: %s (%s, %s)", ver.str (), oldest_version.str (), current.str ()));
654 non_fatal_error (_ ("Consider converting the input with the convert-ly script"));
662 lyric_fudge (String s)
664 char * chars =s.copy_ch_p ();
666 for (char * p = chars; *p ; p++)
668 if (*p == '_' && (p == chars || *(p-1) != '\\'))
676 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
678 * (s.ch_l () + i + 1) = 'c';
679 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
686 Convert "NUM/DEN" into a '(NUM . DEN) cons.
689 scan_fraction (String frac)
691 int i = frac.index_i ('/');
692 int l = frac.length_i ();
693 String left = frac.left_str (i);
694 String right = frac.right_str (l - i - 1);
696 int n = String_convert::dec2_i (left);
697 int d = String_convert::dec2_i (right);
698 return gh_cons (gh_int2scm (n), gh_int2scm (d));