1 %{ // -*-Fundamental-*-
3 lexer.l -- implement the Flex lexer
5 source file of the LilyPond music typesetter
7 (c) 1996--2003 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 :-)
31 #include "parse-scm.hh"
32 #include "lily-guile.hh"
34 #include "string-convert.hh"
35 #include "my-lily-lexer.hh"
36 #include "input-file-results.hh"
37 #include "interval.hh"
38 #include "lily-guile.hh"
43 #include "lilypond-input-version.hh"
44 #include "translator-def.hh"
45 #include "identifier-smob.hh"
50 #define isatty HORRIBLEKLUDGE
52 void strip_trailing_white (String&);
53 void strip_leading_white (String&);
54 String lyric_fudge (String s);
57 lookup_markup_command (String s);
60 valid_version_b (String s);
64 #define start_quote() \
65 yy_push_state (quote);\
66 yylval.string = new String
71 #define YY_USER_ACTION add_lexed_char (YYLeng ());
74 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
79 SCM scan_fraction (String);
80 SCM (* scm_parse_error_handler) (void *);
90 %option yyclass="My_lily_lexer"
92 %option never-interactive
111 NATIONAL [\001-\006\021-\027\031\036\200-\377]
112 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
120 REAL ({INT}\.{N}*)|(-?\.{N}+)
123 HORIZONTALWHITE [ \t]
127 MARKUPCOMMAND \\({A}|[-_])+
128 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
136 // windows-suck-suck-suck
139 <INITIAL,chords,incl,markup,lyrics,notes,figures>{
141 yy_push_state (longcomment);
145 %[^{\n] { // backup rule
156 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
157 yy_push_state (version);
159 <version>\"[^"]*\" { /* got the version number */
160 String s (YYText ()+1);
161 s = s.left_string (s.index_last ('\"'));
164 if (!valid_version_b (s))
168 LexerError ("No quoted string found after \\version");
181 LexerError (_ ("EOF found inside a comment").to_str0 ());
182 if (! close_input ())
183 yyterminate (); // can't move this, since it actually rets a YY_NULL
188 <INITIAL,chords,lyrics,notes,figures>\\maininput {
192 main_input_b_ = true;
195 error (_ ("\\maininput disallowed outside init files"));
198 <INITIAL,chords,lyrics,figures,notes>\\include {
199 yy_push_state (incl);
201 <incl>\"[^"]*\";? { /* got the include file name */
202 String s (YYText ()+1);
203 s = s.left_string (s.index_last ('"'));
205 new_input (s, &global_input_file->sources_ );
208 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
209 String s = YYText () + 1;
210 strip_trailing_white (s);
211 if (s.length () && (s[s.length () - 1] == ';'))
212 s = s.left_string (s.length () - 1);
214 SCM sid = lookup_identifier (s);
215 if (gh_string_p (sid)) {
216 new_input (ly_scm2string (sid), &global_input_file->sources_);
219 String msg (_f ("wrong or undefined identifier: `%s'", s ));
221 LexerError (msg.to_str0 ());
222 SCM err = scm_current_error_port ();
223 scm_puts ("This value was found in the table: ", err);
224 scm_display (sid, err);
227 <incl>\"[^"]* { // backup rule
228 error (_ ("Missing end quote"));
231 <chords,notes,figures>{RESTNAME} {
232 const char *s = YYText ();
233 yylval.scm = scm_makfrom0str (s);
236 <chords,notes,figures>R {
237 return MULTI_MEASURE_REST;
239 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}*{WHITE} {
240 String s=YYText () + 2;
241 s=s.left_string (s.length () - 1);
242 return scan_escaped_word (s);
244 <INITIAL,chords,lyrics,notes,figures>\${BLACK}*{WHITE} {
245 String s=YYText () + 1;
246 s=s.left_string (s.length () - 1);
247 return scan_bare_word (s);
249 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}* { // backup rule
250 error (_("white expected"));
253 <INITIAL,chords,lyrics,notes,figures>\${BLACK}* { // backup rule
254 error (_("white expected"));
258 <INITIAL,markup,chords,lyrics,notes,figures># { //embedded scm
259 //char const* s = YYText () + 1;
260 char const* s = here_str0 ();
262 if (main_input_b_ && safe_global_b) {
263 error (_ ("Can't evaluate Scheme in safe mode"));
264 yylval.scm = SCM_EOL;
267 SCM sval = ly_parse_scm (s, &n, here_input());
268 if (sval == SCM_UNDEFINED)
270 sval = SCM_UNSPECIFIED;
274 for (int i=0; i < n; i++)
278 char_count_stack_.top () += n;
280 if (unpack_identifier (sval) != SCM_UNDEFINED)
282 yylval.scm = unpack_identifier(sval);
283 return identifier_type (yylval.scm);
289 <INITIAL,notes,markup>{
311 return scan_bare_word (YYText ());
315 return scan_escaped_word (YYText () + 1);
318 yylval.scm = scan_fraction (YYText ());
323 yylval.i = String_convert::dec2int (String (YYText ()));
327 yylval.i = String_convert::dec2int (String (YYText ()));
331 yylval.i = String_convert::dec2int (String (YYText () +1));
345 *yylval.string += to_string (escaped_char (YYText ()[1]));
348 *yylval.string += YYText ();
354 /* yylval is union. Must remember STRING before setting SCM*/
355 String *sp = yylval.string;
356 yylval.scm = scm_makfrom0str (sp->to_str0 ());
361 *yylval.string += YYText ();
370 yylval.scm = scan_fraction (YYText ());
374 yylval.i = String_convert::dec2int (String (YYText ()));
378 return scan_escaped_word (YYText () + 1);
382 String s (YYText ());
384 return yylval.i = EXTENDER;
386 return yylval.i = HYPHEN;
389 char c = s[s.length () - 1];
390 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
391 here_input ().warning (
392 _ ("Brace found at end of lyric. Did you forget a space?"));
393 yylval.scm = scm_makfrom0str (s.to_str0 ());
404 return scan_bare_word (YYText ());
407 return scan_escaped_word (YYText () + 1);
410 yylval.scm = scan_fraction (YYText ());
414 yylval.i = String_convert::dec2int (String (YYText ()));
446 String str (YYText() + 1);
447 SCM s = lookup_markup_command (str);
449 if (gh_pair_p (s) && gh_symbol_p (gh_cdr (s)) ) {
450 yylval.scm = gh_car(s);
452 if (tag == ly_symbol2scm("markup0"))
453 return MARKUP_HEAD_MARKUP0;
454 else if (tag == ly_symbol2scm ("markup0-markup1"))
455 return MARKUP_HEAD_MARKUP0_MARKUP1;
456 else if (tag == ly_symbol2scm ("markup-list0"))
457 return MARKUP_HEAD_LIST0;
458 else if (tag == ly_symbol2scm ("scheme0"))
459 return MARKUP_HEAD_SCM0;
460 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
461 return MARKUP_HEAD_SCM0_SCM1;
462 else if (tag == ly_symbol2scm ("scheme0-markup1"))
463 return MARKUP_HEAD_SCM0_MARKUP1;
464 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
465 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
471 return scan_escaped_word (str);
474 String s (YYText ());
476 char c = s[s.length () - 1];
477 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
478 here_input ().warning (
479 _ ("Brace found at end of lyric. Did you forget a space?"));
480 yylval.scm = scm_makfrom0str (s.to_str0 ());
496 if (! close_input ()) {
497 yyterminate (); // can't move this, since it actually rets a YY_NULL
503 return scan_bare_word (YYText ());
506 return scan_escaped_word (YYText () + 1);
510 int cnv=sscanf (YYText (), "%lf", &r);
513 yylval.scm = gh_double2scm (r);
518 yylval.i = String_convert::dec2int (String (YYText ()));
528 char c = YYText ()[0];
533 <INITIAL,notes,figures>. {
537 <INITIAL,lyrics,notes,figures>\\. {
538 char c= YYText ()[1];
546 return E_EXCLAMATION;
554 return E_RIGHTSQUARE;
566 String msg = _f ("invalid character: `%c'", YYText ()[0]);
567 LexerError (msg.to_str0 ());
574 My_lily_lexer::push_note_state ()
576 yy_push_state (notes);
580 My_lily_lexer::push_figuredbass_state()
582 yy_push_state (figures);
585 My_lily_lexer::push_chord_state ()
587 yy_push_state (chords);
591 My_lily_lexer::push_lyric_state ()
593 yy_push_state (lyrics);
597 My_lily_lexer::push_markup_state ()
599 yy_push_state (markup);
603 My_lily_lexer::pop_state ()
609 My_lily_lexer::identifier_type(SCM sid)
611 int k = try_special_identifiers(&yylval.scm , sid);
612 return k >= 0 ? k : SCM_IDENTIFIER;
617 My_lily_lexer::scan_escaped_word (String str)
619 // use more SCM for this.
621 SCM sym = ly_symbol2scm (str.to_str0 ());
623 int l = lookup_keyword (str);
627 SCM sid = lookup_identifier (str);
628 if (sid != SCM_UNDEFINED)
631 return identifier_type (sid);
634 if ((YYSTATE != notes) && (YYSTATE != chords)) {
635 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
637 if (gh_pair_p (pitch))
639 yylval.scm = ly_cdr (pitch);
640 return NOTENAME_PITCH;
643 String msg (_f ("unknown escaped string: `\\%s'", str));
644 LexerError (msg.to_str0 ());
646 yylval.scm = scm_makfrom0str (str.to_str0 ());
652 My_lily_lexer::scan_bare_word (String str)
654 SCM sym = ly_symbol2scm (str.to_str0 ());
655 if ((YYSTATE == notes) || (YYSTATE == chords)) {
656 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
657 if (gh_pair_p (pitch)) {
658 yylval.scm = ly_cdr (pitch);
659 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
660 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
662 yylval.scm = ly_cdr (pitch);
663 return CHORDMODIFIER_PITCH;
667 yylval.scm = scm_makfrom0str (str.to_str0 ());
672 My_lily_lexer::note_state_b () const
674 return YY_START == notes;
678 My_lily_lexer::chord_state_b () const
680 return YY_START == chords;
684 My_lily_lexer::lyric_state_b () const
686 return YY_START == lyrics;
690 My_lily_lexer::figure_state_b () const
692 return YY_START == figures;
696 urg, belong to String (_convert)
697 and should be generalised
700 strip_leading_white (String&s)
703 for (; i < s.length (); i++)
707 s = s.nomid_string (0, i);
711 strip_trailing_white (String&s)
718 s = s.left_string (i+1);
723 /* 1.3.146 == removal of ; */
724 Lilypond_version oldest_version ("1.3.146");
728 valid_version_b (String s)
730 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
731 Lilypond_version ver (s);
732 if (! ((ver >= oldest_version) && (ver <= current)))
734 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
735 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
746 lyric_fudge (String s)
748 char * chars =s.get_copy_str0 ();
750 for (char * p = chars; *p ; p++)
752 if (*p == '_' && (p == chars || *(p-1) != '\\'))
760 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
762 * (s.get_str0 () + i + 1) = 'c';
763 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
770 Convert "NUM/DEN" into a '(NUM . DEN) cons.
773 scan_fraction (String frac)
775 int i = frac.index ('/');
776 int l = frac.length ();
777 String left = frac.left_string (i);
778 String right = frac.right_string (l - i - 1);
780 int n = String_convert::dec2int (left);
781 int d = String_convert::dec2int (right);
782 return gh_cons (gh_int2scm (n), gh_int2scm (d));
785 /* avoid silly flex induced gcc warnings */
786 static void yy_push_state (int) {;}
787 static void yy_pop_state () {;}
788 static int yy_top_state () { return 0; }
791 avoid_silly_flex_induced_gcc_warnings ()
793 (void)yy_start_stack_ptr;
794 (void)yy_start_stack_depth;
795 (void)yy_start_stack;
799 avoid_silly_flex_induced_gcc_warnings ();
803 lookup_markup_command (String s)
807 proc = scm_c_eval_string ("lookup-markup-command");
809 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));