1 %{ // -*-Fundamental-*-
3 lexer.ll -- implement the Flex lexer
5 source file of the LilyPond music typesetter
7 (c) 1996--2007 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_);
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-markup1-markup2"))
543 return MARKUP_HEAD_SCM0_MARKUP1_MARKUP2;
544 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
545 return MARKUP_HEAD_SCM0_SCM1_SCM2;
547 programming_error ("no parser tag defined for this markup signature");
551 } else if (scm_is_pair (s2) && scm_is_symbol (scm_cdr (s2))) {
552 yylval.scm = scm_car(s2);
553 SCM tag = scm_cdr(s2);
554 if (tag == ly_symbol2scm("empty"))
555 return MARKUP_LIST_HEAD_EMPTY;
556 else if (tag == ly_symbol2scm ("scheme0"))
557 return MARKUP_LIST_HEAD_SCM0;
558 else if (tag == ly_symbol2scm ("markup-list0"))
559 return MARKUP_LIST_HEAD_LIST0;
560 else if (tag == ly_symbol2scm ("scheme0-markup-list1"))
561 return MARKUP_LIST_HEAD_SCM0_LIST1;
562 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup-list2"))
563 return MARKUP_LIST_HEAD_SCM0_SCM1_LIST2;
565 programming_error ("no parser tag defined for this markup list signature");
570 return scan_escaped_word (str);
575 [^#{}"\\ \t\n\r\f]+ {
576 string s (YYText ());
578 char c = s[s.length () - 1];
579 /* brace open is for not confusing dumb tools. */
580 if (c == '{' || c == '}')
581 here_input ().warning (
582 _ ("Brace found at end of markup. Did you forget a space?"));
583 yylval.scm = ly_string2scm (s);
596 /* 2 = init.ly + current file.
597 > because we're before closing, but is_main_input_ should
600 is_main_input_ = include_stack_.size () > 2;
602 /* Returns YY_NULL */
605 else if (!close_input ())
606 /* Returns YY_NULL */
612 return scan_bare_word (YYText ());
615 return scan_escaped_word (YYText () + 1);
620 return scan_bare_word (YYText ());
623 return scan_escaped_word (YYText () + 1);
627 int cnv = sscanf (YYText (), "%lf", &r);
631 yylval.scm = scm_from_double (r);
636 yylval.i = String_convert::dec2int (string (YYText ()));
646 char c = YYText ()[0];
651 <INITIAL,notes,figures>. {
655 <INITIAL,lyrics,notes,figures>\\. {
656 char c = YYText ()[1];
660 return E_ANGLE_CLOSE;
664 return E_EXCLAMATION;
670 return E_BRACKET_OPEN;
674 return E_BRACKET_CLOSE;
686 string msg = _f ("invalid character: `%c'", YYText ()[0]);
687 LexerError (msg.c_str ());
693 /* Make the lexer generate a token of the given type as the next token.
694 TODO: make it possible to define a value for the token as well */
696 Lily_lexer::push_extra_token (int token_type)
698 if (extra_token_types_.empty ())
700 if (YY_START != extratoken)
701 hidden_state_ = YY_START;
702 yy_push_state (extratoken);
704 extra_token_types_.push_back (token_type);
708 Lily_lexer::push_chord_state (SCM tab)
710 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
711 yy_push_state (chords);
715 Lily_lexer::push_figuredbass_state ()
717 yy_push_state (figures);
721 Lily_lexer::push_initial_state ()
723 yy_push_state (INITIAL);
727 Lily_lexer::push_lyric_state ()
729 yy_push_state (lyrics);
733 Lily_lexer::push_markup_state ()
735 yy_push_state (markup);
739 Lily_lexer::push_note_state (SCM tab)
741 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
742 yy_push_state (notes);
746 Lily_lexer::pop_state ()
748 if (YYSTATE == notes || YYSTATE == chords)
749 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
755 Lily_lexer::identifier_type (SCM sid)
757 int k = try_special_identifiers (&yylval.scm , sid);
758 return k >= 0 ? k : SCM_IDENTIFIER;
763 Lily_lexer::scan_escaped_word (string str)
765 // use more SCM for this.
767 // SCM sym = ly_symbol2scm (str.c_str ());
769 int i = lookup_keyword (str);
770 if (i == MARKUP && is_lyric_state ())
775 SCM sid = lookup_identifier (str);
776 if (is_music_function (sid))
778 yylval.scm = get_music_function_transform (sid);
780 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
781 push_extra_token (EXPECT_NO_MORE_ARGS);
782 for (; scm_is_pair (s); s = scm_cdr (s))
784 if (scm_car (s) == ly_music_p_proc)
785 push_extra_token (EXPECT_MUSIC);
786 else if (scm_car (s) == ly_lily_module_constant ("markup?"))
787 push_extra_token (EXPECT_MARKUP);
788 else if (ly_is_procedure (scm_car (s)))
789 push_extra_token (EXPECT_SCM);
790 else programming_error ("Function parameter without type-checking predicate");
792 return MUSIC_FUNCTION;
795 if (sid != SCM_UNDEFINED)
798 return identifier_type (sid);
801 string msg (_f ("unknown escaped string: `\\%s'", str));
802 LexerError (msg.c_str ());
804 yylval.scm = ly_string2scm (str);
810 Lily_lexer::scan_bare_word (string str)
812 SCM sym = ly_symbol2scm (str.c_str ());
813 if ((YYSTATE == notes) || (YYSTATE == chords)) {
814 SCM handle = SCM_BOOL_F;
815 if (scm_is_pair (pitchname_tab_stack_))
816 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
818 if (scm_is_pair (handle)) {
819 yylval.scm = scm_cdr (handle);
820 if (unsmob_pitch (yylval.scm))
821 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
822 else if (scm_is_symbol (yylval.scm))
825 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
827 yylval.scm = scm_cdr (handle);
828 return CHORD_MODIFIER;
832 yylval.scm = ly_string2scm (str);
837 Lily_lexer::get_state () const
839 if (YY_START == extratoken)
840 return hidden_state_;
846 Lily_lexer::is_note_state () const
848 return get_state () == notes;
852 Lily_lexer::is_chord_state () const
854 return get_state () == chords;
858 Lily_lexer::is_lyric_state () const
860 return get_state () == lyrics;
864 Lily_lexer::is_figure_state () const
866 return get_state () == figures;
870 urg, belong to string (_convert)
871 and should be generalised
874 strip_leading_white (string&s)
877 for (; i < s.length (); i++)
885 strip_trailing_white (string&s)
887 ssize i = s.length ();
892 s = s.substr (0, i + 1);
897 Lilypond_version oldest_version ("2.7.38");
901 is_valid_version (string s)
903 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
904 Lilypond_version ver (s);
905 if (int (ver) < oldest_version)
907 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
908 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
914 non_fatal_error (_f ("program too old: %s (file requires: %s)", current.to_string (), ver.to_string ()));
925 lyric_fudge (string s)
927 char *chars = string_copy (s);
929 for (char *p = chars; *p ; p++)
931 if (*p == '_' && (p == chars || *(p-1) != '\\'))
939 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
941 * (((char*)s.c_str ()) + i + 1) = 'c';
942 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
949 Convert "NUM/DEN" into a '(NUM . DEN) cons.
952 scan_fraction (string frac)
954 ssize i = frac.find ('/');
955 string left = frac.substr (0, i);
956 string right = frac.substr (i + 1, (frac.length () - i + 1));
958 int n = String_convert::dec2int (left);
959 int d = String_convert::dec2int (right);
960 return scm_cons (scm_from_int (n), scm_from_int (d));
964 lookup_markup_command (string s)
966 SCM proc = ly_lily_module_constant ("lookup-markup-command");
967 return scm_call_1 (proc, ly_string2scm (s));
971 lookup_markup_list_command (string s)
973 SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
974 return scm_call_1 (proc, ly_string2scm (s));
977 /* Shut up lexer warnings. */
997 silence_lexer_warnings ()
999 (void) yy_start_stack_ptr;
1000 (void) yy_start_stack_depth;
1001 (void) yy_start_stack;
1002 (void) yy_push_state;
1003 (void) yy_pop_state;
1004 (void) yy_top_state;
1005 (void) silence_lexer_warnings;