1 %{ // -*-Fundamental-*-
3 lexer.ll -- implement the Flex lexer
5 source file of the LilyPond music typesetter
7 (c) 1996--2009 Han-Wen Nienhuys <hanwen@xs4all.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 :-)
30 /* Flex >= 2.5.29 fix; FlexLexer.h's multiple include bracing breaks
31 when building the actual lexer. */
38 #include "context-def.hh"
39 #include "identifier-smob.hh"
40 #include "international.hh"
41 #include "interval.hh"
42 #include "lily-guile.hh"
43 #include "lily-lexer.hh"
44 #include "lilypond-version.hh"
47 #include "music-function.hh"
48 #include "parse-scm.hh"
50 #include "source-file.hh"
51 #include "std-string.hh"
52 #include "string-convert.hh"
59 #define isatty HORRIBLEKLUDGE
61 void strip_trailing_white (string&);
62 void strip_leading_white (string&);
63 string lyric_fudge (string s);
64 SCM lookup_markup_command (string s);
65 SCM lookup_markup_list_command (string s);
66 bool is_valid_version (string s);
69 #define start_quote() \
70 yy_push_state (quote);\
71 yylval.string = new string
73 #define start_lyric_quote() \
74 yy_push_state (lyric_quote);\
75 yylval.string = new string
83 #define YY_USER_ACTION add_lexed_char (YYLeng ());
86 SCM scan_fraction (string);
87 SCM (* scm_parse_error_handler) (void *);
97 %option yyclass="Lily_lexer"
99 %option never-interactive
123 NATIONAL [\001-\006\021-\027\031\036]
124 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
126 DASHED_WORD {A}({AN}|-)*
127 DASHED_KEY_WORD \\{DASHED_WORD}
137 REAL ({INT}\.{N}*)|(-?\.{N}+)
140 HORIZONTALWHITE [ \t]
144 MARKUPCOMMAND \\({A}|[-_])+
145 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
149 BOM_UTF8 \357\273\277
155 // windows-suck-suck-suck
158 <extratoken>{ANY_CHAR} {
159 /* Generate a token without swallowing anything */
161 /* First unswallow the eaten character */
162 add_lexed_char (-YYLeng ());
165 /* produce requested token */
166 int type = extra_token_types_.back ();
167 extra_token_types_.pop_back ();
168 if (extra_token_types_.empty ())
174 /* Use the trailing context feature. Otherwise, the BOM will not be
175 found if the file starts with an identifier definition. */
176 <INITIAL,chords,lyrics,figures,notes>{BOM_UTF8}/.* {
177 if (this->lexloc->line_number () != 1 || this->lexloc->column_number () != 0)
179 LexerError (_ ("stray UTF-8 BOM encountered").c_str ());
182 if (be_verbose_global)
183 message (_ ("Skipping UTF-8 BOM"));
186 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
188 yy_push_state (longcomment);
192 %[^{\n\r] { // backup rule
203 <INITIAL,notes,figures,chords,markup>{
209 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
210 yy_push_state (version);
212 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}* {
213 yy_push_state (sourcefilename);
215 <INITIAL,chords,lyrics,notes,figures>\\sourcefileline{WHITE}* {
216 yy_push_state (sourcefileline);
218 <version>\"[^"]*\" { /* got the version number */
219 string s (YYText () + 1);
220 s = s.substr (0, s.rfind ('\"'));
224 SCM top_scope = scm_car (scm_last_pair (scopes_));
225 scm_module_define (top_scope, ly_symbol2scm ("version-seen"), SCM_BOOL_T);
227 if (!is_valid_version (s))
232 <sourcefilename>\"[^"]*\" {
233 string s (YYText () + 1);
234 s = s.substr (0, s.rfind ('\"'));
237 this->here_input().get_source_file ()->name_ = s;
238 message (_f ("Renaming input to: `%s'", s.c_str ()));
239 progress_indication ("\n");
240 scm_module_define (scm_car (scopes_),
241 ly_symbol2scm ("input-file-name"),
246 <sourcefileline>{INT} {
248 sscanf (YYText (), "%d", &i);
251 this->here_input ().get_source_file ()->set_line (here_input ().start (), i);
255 LexerError (_ ("quoted string expected after \\version").c_str ());
259 LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
263 LexerError (_ ("integer expected after \\sourcefileline").c_str ());
276 LexerError (_ ("EOF found inside a comment").c_str ());
277 is_main_input_ = false; // should be safe , can't have \include in --safe.
278 if (! close_input ())
279 yyterminate (); // can't move this, since it actually rets a YY_NULL
284 <INITIAL,chords,lyrics,notes,figures>\\maininput {
288 is_main_input_ = true;
291 error (_ ("\\maininput not allowed outside init files"));
294 <INITIAL,chords,lyrics,figures,notes>\\include {
295 yy_push_state (incl);
297 <incl>\"[^"]*\" { /* got the include file name */
298 string s (YYText ()+1);
299 s = s.substr (0, s.rfind ('"'));
301 new_input (s, sources_);
304 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
305 string s = YYText () + 1;
306 strip_trailing_white (s);
307 if (s.length () && (s[s.length () - 1] == ';'))
308 s = s.substr (0, s.length () - 1);
310 SCM sid = lookup_identifier (s);
311 if (scm_is_string (sid)) {
312 new_input (ly_scm2string (sid), sources_);
315 string msg (_f ("wrong or undefined identifier: `%s'", s ));
317 LexerError (msg.c_str ());
318 SCM err = scm_current_error_port ();
319 scm_puts ("This value was found in the table: ", err);
320 scm_display (sid, err);
323 <incl>\"[^"]* { // backup rule
324 error (_ ("end quote missing"));
327 <chords,notes,figures>{RESTNAME} {
328 char const *s = YYText ();
329 yylval.scm = scm_from_locale_string (s);
332 <chords,notes,figures>R {
333 return MULTI_MEASURE_REST;
335 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
337 Input hi = here_input();
339 SCM sval = ly_parse_scm (hi.start (), &n, hi,
340 be_safe_global && is_main_input_, parser_);
342 if (sval == SCM_UNDEFINED)
344 sval = SCM_UNSPECIFIED;
348 for (int i = 0; i < n; i++)
352 char_count_stack_.back () += n;
354 if (unpack_identifier (sval) != SCM_UNDEFINED)
356 yylval.scm = unpack_identifier(sval);
357 return identifier_type (yylval.scm);
363 <INITIAL,notes,lyrics>{
365 return DOUBLE_ANGLE_OPEN;
368 return DOUBLE_ANGLE_CLOSE;
395 return scan_bare_word (YYText ());
399 return scan_escaped_word (YYText () + 1);
402 yylval.scm = scan_fraction (YYText ());
407 yylval.i = String_convert::dec2int (string (YYText ()));
411 yylval.i = String_convert::dec2int (string (YYText ()));
415 yylval.i = String_convert::dec2int (string (YYText () +1));
422 *yylval.string += to_string (escaped_char (YYText ()[1]));
425 *yylval.string += YYText ();
431 /* yylval is union. Must remember STRING before setting SCM*/
432 string *sp = yylval.string;
433 yylval.scm = ly_string2scm (*sp);
435 return is_lyric_state () ? LYRICS_STRING : STRING;
438 *yylval.string += YYText ();
444 start_lyric_quote ();
447 yylval.scm = scan_fraction (YYText ());
451 yylval.i = String_convert::dec2int (string (YYText ()));
455 return scan_escaped_word (YYText () + 1);
459 string s (YYText ());
461 return yylval.i = EXTENDER;
463 return yylval.i = HYPHEN;
466 char c = s[s.length () - 1];
467 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
468 here_input ().warning (
469 _ ("Brace found at end of lyric. Did you forget a space?"));
470 yylval.scm = ly_string2scm (s);
473 return LYRICS_STRING;
481 return scan_bare_word (YYText ());
484 return scan_escaped_word (YYText () + 1);
487 yylval.scm = scan_fraction (YYText ());
491 yylval.i = String_convert::dec2int (string (YYText ()));
520 string str (YYText () + 1);
521 SCM s = lookup_markup_command (str);
522 SCM s2 = lookup_markup_list_command (str);
523 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
524 yylval.scm = scm_car(s);
525 SCM tag = scm_cdr(s);
526 if (tag == ly_symbol2scm("markup0"))
527 return MARKUP_HEAD_MARKUP0;
528 if (tag == ly_symbol2scm("empty"))
529 return MARKUP_HEAD_EMPTY;
530 else if (tag == ly_symbol2scm ("markup0-markup1"))
531 return MARKUP_HEAD_MARKUP0_MARKUP1;
532 else if (tag == ly_symbol2scm ("markup-list0"))
533 return MARKUP_HEAD_LIST0;
534 else if (tag == ly_symbol2scm ("scheme0"))
535 return MARKUP_HEAD_SCM0;
536 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
537 return MARKUP_HEAD_SCM0_SCM1;
538 else if (tag == ly_symbol2scm ("scheme0-markup1"))
539 return MARKUP_HEAD_SCM0_MARKUP1;
540 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
541 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
542 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2-markup3"))
543 return MARKUP_HEAD_SCM0_SCM1_MARKUP2_MARKUP3;
544 else if (tag == ly_symbol2scm ("scheme0-markup1-markup2"))
545 return MARKUP_HEAD_SCM0_MARKUP1_MARKUP2;
546 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
547 return MARKUP_HEAD_SCM0_SCM1_SCM2;
549 programming_error ("no parser tag defined for this markup signature");
553 } else if (scm_is_pair (s2) && scm_is_symbol (scm_cdr (s2))) {
554 yylval.scm = scm_car(s2);
555 SCM tag = scm_cdr(s2);
556 if (tag == ly_symbol2scm("empty"))
557 return MARKUP_LIST_HEAD_EMPTY;
558 else if (tag == ly_symbol2scm ("scheme0"))
559 return MARKUP_LIST_HEAD_SCM0;
560 else if (tag == ly_symbol2scm ("markup-list0"))
561 return MARKUP_LIST_HEAD_LIST0;
562 else if (tag == ly_symbol2scm ("scheme0-markup-list1"))
563 return MARKUP_LIST_HEAD_SCM0_LIST1;
564 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup-list2"))
565 return MARKUP_LIST_HEAD_SCM0_SCM1_LIST2;
567 programming_error ("no parser tag defined for this markup list signature");
572 return scan_escaped_word (str);
577 [^#{}"\\ \t\n\r\f]+ {
578 string s (YYText ());
580 char c = s[s.length () - 1];
581 /* brace open is for not confusing dumb tools. */
582 if (c == '{' || c == '}')
583 here_input ().warning (
584 _ ("Brace found at end of markup. Did you forget a space?"));
585 yylval.scm = ly_string2scm (s);
598 /* 2 = init.ly + current file.
599 > because we're before closing, but is_main_input_ should
602 is_main_input_ = include_stack_.size () > 2;
604 /* Returns YY_NULL */
607 else if (!close_input ())
608 /* Returns YY_NULL */
614 return scan_bare_word (YYText ());
617 return scan_escaped_word (YYText () + 1);
622 return scan_bare_word (YYText ());
625 return scan_escaped_word (YYText () + 1);
629 int cnv = sscanf (YYText (), "%lf", &r);
633 yylval.scm = scm_from_double (r);
638 yylval.i = String_convert::dec2int (string (YYText ()));
648 char c = YYText ()[0];
653 <INITIAL,notes,figures>. {
657 <INITIAL,lyrics,notes,figures>\\. {
658 char c = YYText ()[1];
662 return E_ANGLE_CLOSE;
666 return E_EXCLAMATION;
672 return E_BRACKET_OPEN;
676 return E_BRACKET_CLOSE;
688 string msg = _f ("invalid character: `%c'", YYText ()[0]);
689 LexerError (msg.c_str ());
695 /* Make the lexer generate a token of the given type as the next token.
696 TODO: make it possible to define a value for the token as well */
698 Lily_lexer::push_extra_token (int token_type)
700 if (extra_token_types_.empty ())
702 if (YY_START != extratoken)
703 hidden_state_ = YY_START;
704 yy_push_state (extratoken);
706 extra_token_types_.push_back (token_type);
710 Lily_lexer::push_chord_state (SCM tab)
712 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
713 yy_push_state (chords);
717 Lily_lexer::push_figuredbass_state ()
719 yy_push_state (figures);
723 Lily_lexer::push_initial_state ()
725 yy_push_state (INITIAL);
729 Lily_lexer::push_lyric_state ()
731 yy_push_state (lyrics);
735 Lily_lexer::push_markup_state ()
737 yy_push_state (markup);
741 Lily_lexer::push_note_state (SCM tab)
743 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
744 yy_push_state (notes);
748 Lily_lexer::pop_state ()
750 if (YYSTATE == notes || YYSTATE == chords)
751 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
757 Lily_lexer::identifier_type (SCM sid)
759 int k = try_special_identifiers (&yylval.scm , sid);
760 return k >= 0 ? k : SCM_IDENTIFIER;
765 Lily_lexer::scan_escaped_word (string str)
767 // use more SCM for this.
769 // SCM sym = ly_symbol2scm (str.c_str ());
771 int i = lookup_keyword (str);
772 if (i == MARKUP && is_lyric_state ())
777 SCM sid = lookup_identifier (str);
778 if (is_music_function (sid))
780 yylval.scm = get_music_function_transform (sid);
782 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
783 push_extra_token (EXPECT_NO_MORE_ARGS);
784 for (; scm_is_pair (s); s = scm_cdr (s))
786 if (scm_car (s) == ly_music_p_proc)
787 push_extra_token (EXPECT_MUSIC);
788 else if (scm_car (s) == ly_lily_module_constant ("markup?"))
789 push_extra_token (EXPECT_MARKUP);
790 else if (ly_is_procedure (scm_car (s)))
791 push_extra_token (EXPECT_SCM);
792 else programming_error ("Function parameter without type-checking predicate");
794 return MUSIC_FUNCTION;
797 if (sid != SCM_UNDEFINED)
800 return identifier_type (sid);
803 string msg (_f ("unknown escaped string: `\\%s'", str));
804 LexerError (msg.c_str ());
806 yylval.scm = ly_string2scm (str);
812 Lily_lexer::scan_bare_word (string str)
814 SCM sym = ly_symbol2scm (str.c_str ());
815 if ((YYSTATE == notes) || (YYSTATE == chords)) {
816 SCM handle = SCM_BOOL_F;
817 if (scm_is_pair (pitchname_tab_stack_))
818 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
820 if (scm_is_pair (handle)) {
821 yylval.scm = scm_cdr (handle);
822 if (unsmob_pitch (yylval.scm))
823 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
824 else if (scm_is_symbol (yylval.scm))
827 else if ((YYSTATE == chords)
828 && (handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
830 yylval.scm = scm_cdr (handle);
831 return CHORD_MODIFIER;
833 if ((chord_repetition_.repetition_symbol_ != SCM_EOL)
834 && to_boolean (scm_equal_p (chord_repetition_.repetition_symbol_, sym)))
835 return CHORD_REPETITION;
838 yylval.scm = ly_string2scm (str);
843 Lily_lexer::get_state () const
845 if (YY_START == extratoken)
846 return hidden_state_;
852 Lily_lexer::is_note_state () const
854 return get_state () == notes;
858 Lily_lexer::is_chord_state () const
860 return get_state () == chords;
864 Lily_lexer::is_lyric_state () const
866 return get_state () == lyrics;
870 Lily_lexer::is_figure_state () const
872 return get_state () == figures;
876 urg, belong to string (_convert)
877 and should be generalised
880 strip_leading_white (string&s)
883 for (; i < s.length (); i++)
891 strip_trailing_white (string&s)
893 ssize i = s.length ();
898 s = s.substr (0, i + 1);
903 Lilypond_version oldest_version ("2.7.38");
907 is_valid_version (string s)
909 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
910 Lilypond_version ver (s);
911 if (int (ver) < oldest_version)
913 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
914 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
920 non_fatal_error (_f ("program too old: %s (file requires: %s)", current.to_string (), ver.to_string ()));
931 lyric_fudge (string s)
933 char *chars = string_copy (s);
935 for (char *p = chars; *p ; p++)
937 if (*p == '_' && (p == chars || *(p-1) != '\\'))
945 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
947 * (((char*)s.c_str ()) + i + 1) = 'c';
948 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
955 Convert "NUM/DEN" into a '(NUM . DEN) cons.
958 scan_fraction (string frac)
960 ssize i = frac.find ('/');
961 string left = frac.substr (0, i);
962 string right = frac.substr (i + 1, (frac.length () - i + 1));
964 int n = String_convert::dec2int (left);
965 int d = String_convert::dec2int (right);
966 return scm_cons (scm_from_int (n), scm_from_int (d));
970 lookup_markup_command (string s)
972 SCM proc = ly_lily_module_constant ("lookup-markup-command");
973 return scm_call_1 (proc, ly_string2scm (s));
977 lookup_markup_list_command (string s)
979 SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
980 return scm_call_1 (proc, ly_string2scm (s));
983 /* Shut up lexer warnings. */
1003 silence_lexer_warnings ()
1005 (void) yy_start_stack_ptr;
1006 (void) yy_start_stack_depth;
1007 (void) yy_start_stack;
1008 (void) yy_push_state;
1009 (void) yy_pop_state;
1010 (void) yy_top_state;
1011 (void) silence_lexer_warnings;