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 <INITIAL,chords,lyrics,figures,notes>{BOM_UTF8} {
175 if (this->lexloc->line_number () != 1 || this->lexloc->column_number () != 0)
177 LexerError (_ ("stray UTF-8 BOM encountered").c_str ());
180 if (be_verbose_global)
181 message (_ ("Skipping UTF-8 BOM"));
184 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
186 yy_push_state (longcomment);
190 %[^{\n\r] { // backup rule
201 <INITIAL,notes,figures,chords,markup>{
207 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
208 yy_push_state (version);
210 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}* {
211 yy_push_state (sourcefilename);
213 <INITIAL,chords,lyrics,notes,figures>\\sourcefileline{WHITE}* {
214 yy_push_state (sourcefileline);
216 <version>\"[^"]*\" { /* got the version number */
217 string s (YYText () + 1);
218 s = s.substr (0, s.rfind ('\"'));
222 SCM top_scope = scm_car (scm_last_pair (scopes_));
223 scm_module_define (top_scope, ly_symbol2scm ("version-seen"), SCM_BOOL_T);
225 if (!is_valid_version (s))
230 <sourcefilename>\"[^"]*\" {
231 string s (YYText () + 1);
232 s = s.substr (0, s.rfind ('\"'));
235 this->here_input().get_source_file ()->name_ = s;
236 message (_f ("Renaming input to: `%s'", s.c_str ()));
237 progress_indication ("\n");
238 scm_module_define (scm_car (scopes_),
239 ly_symbol2scm ("input-file-name"),
244 <sourcefileline>{INT} {
246 sscanf (YYText (), "%d", &i);
249 this->here_input ().get_source_file ()->set_line (here_input ().start (), i);
253 LexerError (_ ("quoted string expected after \\version").c_str ());
257 LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
261 LexerError (_ ("integer expected after \\sourcefileline").c_str ());
274 LexerError (_ ("EOF found inside a comment").c_str ());
275 is_main_input_ = false; // should be safe , can't have \include in --safe.
276 if (! close_input ())
277 yyterminate (); // can't move this, since it actually rets a YY_NULL
282 <INITIAL,chords,lyrics,notes,figures>\\maininput {
286 is_main_input_ = true;
289 error (_ ("\\maininput not allowed outside init files"));
292 <INITIAL,chords,lyrics,figures,notes>\\include {
293 yy_push_state (incl);
295 <incl>\"[^"]*\" { /* got the include file name */
296 string s (YYText ()+1);
297 s = s.substr (0, s.rfind ('"'));
299 new_input (s, sources_);
302 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
303 string s = YYText () + 1;
304 strip_trailing_white (s);
305 if (s.length () && (s[s.length () - 1] == ';'))
306 s = s.substr (0, s.length () - 1);
308 SCM sid = lookup_identifier (s);
309 if (scm_is_string (sid)) {
310 new_input (ly_scm2string (sid), sources_);
313 string msg (_f ("wrong or undefined identifier: `%s'", s ));
315 LexerError (msg.c_str ());
316 SCM err = scm_current_error_port ();
317 scm_puts ("This value was found in the table: ", err);
318 scm_display (sid, err);
321 <incl>\"[^"]* { // backup rule
322 error (_ ("end quote missing"));
325 <chords,notes,figures>{RESTNAME} {
326 char const *s = YYText ();
327 yylval.scm = scm_from_locale_string (s);
330 <chords,notes,figures>R {
331 return MULTI_MEASURE_REST;
333 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
335 Input hi = here_input();
337 SCM sval = ly_parse_scm (hi.start (), &n, hi,
338 be_safe_global && is_main_input_);
340 if (sval == SCM_UNDEFINED)
342 sval = SCM_UNSPECIFIED;
346 for (int i = 0; i < n; i++)
350 char_count_stack_.back () += n;
352 if (unpack_identifier (sval) != SCM_UNDEFINED)
354 yylval.scm = unpack_identifier(sval);
355 return identifier_type (yylval.scm);
361 <INITIAL,notes,lyrics>{
363 return DOUBLE_ANGLE_OPEN;
366 return DOUBLE_ANGLE_CLOSE;
393 return scan_bare_word (YYText ());
397 return scan_escaped_word (YYText () + 1);
400 yylval.scm = scan_fraction (YYText ());
405 yylval.i = String_convert::dec2int (string (YYText ()));
409 yylval.i = String_convert::dec2int (string (YYText ()));
413 yylval.i = String_convert::dec2int (string (YYText () +1));
420 *yylval.string += to_string (escaped_char (YYText ()[1]));
423 *yylval.string += YYText ();
429 /* yylval is union. Must remember STRING before setting SCM*/
430 string *sp = yylval.string;
431 yylval.scm = ly_string2scm (*sp);
433 return is_lyric_state () ? LYRICS_STRING : STRING;
436 *yylval.string += YYText ();
442 start_lyric_quote ();
445 yylval.scm = scan_fraction (YYText ());
449 yylval.i = String_convert::dec2int (string (YYText ()));
453 return scan_escaped_word (YYText () + 1);
457 string s (YYText ());
459 return yylval.i = EXTENDER;
461 return yylval.i = HYPHEN;
464 char c = s[s.length () - 1];
465 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
466 here_input ().warning (
467 _ ("Brace found at end of lyric. Did you forget a space?"));
468 yylval.scm = ly_string2scm (s);
471 return LYRICS_STRING;
479 return scan_bare_word (YYText ());
482 return scan_escaped_word (YYText () + 1);
485 yylval.scm = scan_fraction (YYText ());
489 yylval.i = String_convert::dec2int (string (YYText ()));
518 string str (YYText () + 1);
519 SCM s = lookup_markup_command (str);
520 SCM s2 = lookup_markup_list_command (str);
521 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
522 yylval.scm = scm_car(s);
523 SCM tag = scm_cdr(s);
524 if (tag == ly_symbol2scm("markup0"))
525 return MARKUP_HEAD_MARKUP0;
526 if (tag == ly_symbol2scm("empty"))
527 return MARKUP_HEAD_EMPTY;
528 else if (tag == ly_symbol2scm ("markup0-markup1"))
529 return MARKUP_HEAD_MARKUP0_MARKUP1;
530 else if (tag == ly_symbol2scm ("markup-list0"))
531 return MARKUP_HEAD_LIST0;
532 else if (tag == ly_symbol2scm ("scheme0"))
533 return MARKUP_HEAD_SCM0;
534 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
535 return MARKUP_HEAD_SCM0_SCM1;
536 else if (tag == ly_symbol2scm ("scheme0-markup1"))
537 return MARKUP_HEAD_SCM0_MARKUP1;
538 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
539 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
540 else if (tag == ly_symbol2scm ("scheme0-markup1-markup2"))
541 return MARKUP_HEAD_SCM0_MARKUP1_MARKUP2;
542 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
543 return MARKUP_HEAD_SCM0_SCM1_SCM2;
545 programming_error ("no parser tag defined for this markup signature");
549 } else if (scm_is_pair (s2) && scm_is_symbol (scm_cdr (s2))) {
550 yylval.scm = scm_car(s2);
551 SCM tag = scm_cdr(s2);
552 if (tag == ly_symbol2scm("empty"))
553 return MARKUP_LIST_HEAD_EMPTY;
554 else if (tag == ly_symbol2scm ("scheme0"))
555 return MARKUP_LIST_HEAD_SCM0;
556 else if (tag == ly_symbol2scm ("markup-list0"))
557 return MARKUP_LIST_HEAD_LIST0;
558 else if (tag == ly_symbol2scm ("scheme0-markup-list1"))
559 return MARKUP_LIST_HEAD_SCM0_LIST1;
560 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup-list2"))
561 return MARKUP_LIST_HEAD_SCM0_SCM1_LIST2;
563 programming_error ("no parser tag defined for this markup list signature");
568 return scan_escaped_word (str);
573 [^#{}"\\ \t\n\r\f]+ {
574 string s (YYText ());
576 char c = s[s.length () - 1];
577 /* brace open is for not confusing dumb tools. */
578 if (c == '{' || c == '}')
579 here_input ().warning (
580 _ ("Brace found at end of markup. Did you forget a space?"));
581 yylval.scm = ly_string2scm (s);
594 /* 2 = init.ly + current file.
595 > because we're before closing, but is_main_input_ should
598 is_main_input_ = include_stack_.size () > 2;
600 /* Returns YY_NULL */
603 else if (!close_input ())
604 /* Returns YY_NULL */
610 return scan_bare_word (YYText ());
613 return scan_escaped_word (YYText () + 1);
618 return scan_bare_word (YYText ());
621 return scan_escaped_word (YYText () + 1);
625 int cnv = sscanf (YYText (), "%lf", &r);
629 yylval.scm = scm_from_double (r);
634 yylval.i = String_convert::dec2int (string (YYText ()));
644 char c = YYText ()[0];
649 <INITIAL,notes,figures>. {
653 <INITIAL,lyrics,notes,figures>\\. {
654 char c = YYText ()[1];
658 return E_ANGLE_CLOSE;
662 return E_EXCLAMATION;
668 return E_BRACKET_OPEN;
672 return E_BRACKET_CLOSE;
684 string msg = _f ("invalid character: `%c'", YYText ()[0]);
685 LexerError (msg.c_str ());
691 /* Make the lexer generate a token of the given type as the next token.
692 TODO: make it possible to define a value for the token as well */
694 Lily_lexer::push_extra_token (int token_type)
696 if (extra_token_types_.empty ())
698 if (YY_START != extratoken)
699 hidden_state_ = YY_START;
700 yy_push_state (extratoken);
702 extra_token_types_.push_back (token_type);
706 Lily_lexer::push_chord_state (SCM tab)
708 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
709 yy_push_state (chords);
713 Lily_lexer::push_figuredbass_state ()
715 yy_push_state (figures);
719 Lily_lexer::push_initial_state ()
721 yy_push_state (INITIAL);
725 Lily_lexer::push_lyric_state ()
727 yy_push_state (lyrics);
731 Lily_lexer::push_markup_state ()
733 yy_push_state (markup);
737 Lily_lexer::push_note_state (SCM tab)
739 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
740 yy_push_state (notes);
744 Lily_lexer::pop_state ()
746 if (YYSTATE == notes || YYSTATE == chords)
747 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
753 Lily_lexer::identifier_type (SCM sid)
755 int k = try_special_identifiers (&yylval.scm , sid);
756 return k >= 0 ? k : SCM_IDENTIFIER;
761 Lily_lexer::scan_escaped_word (string str)
763 // use more SCM for this.
765 // SCM sym = ly_symbol2scm (str.c_str ());
767 int i = lookup_keyword (str);
768 if (i == MARKUP && is_lyric_state ())
773 SCM sid = lookup_identifier (str);
774 if (is_music_function (sid))
776 yylval.scm = get_music_function_transform (sid);
778 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
779 push_extra_token (EXPECT_NO_MORE_ARGS);
780 for (; scm_is_pair (s); s = scm_cdr (s))
782 if (scm_car (s) == ly_music_p_proc)
783 push_extra_token (EXPECT_MUSIC);
784 else if (scm_car (s) == ly_lily_module_constant ("markup?"))
785 push_extra_token (EXPECT_MARKUP);
786 else if (ly_is_procedure (scm_car (s)))
787 push_extra_token (EXPECT_SCM);
788 else programming_error ("Function parameter without type-checking predicate");
790 return MUSIC_FUNCTION;
793 if (sid != SCM_UNDEFINED)
796 return identifier_type (sid);
799 string msg (_f ("unknown escaped string: `\\%s'", str));
800 LexerError (msg.c_str ());
802 yylval.scm = ly_string2scm (str);
808 Lily_lexer::scan_bare_word (string str)
810 SCM sym = ly_symbol2scm (str.c_str ());
811 if ((YYSTATE == notes) || (YYSTATE == chords)) {
812 SCM handle = SCM_BOOL_F;
813 if (scm_is_pair (pitchname_tab_stack_))
814 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
816 if (scm_is_pair (handle)) {
817 yylval.scm = scm_cdr (handle);
818 if (unsmob_pitch (yylval.scm))
819 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
820 else if (scm_is_symbol (yylval.scm))
823 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
825 yylval.scm = scm_cdr (handle);
826 return CHORD_MODIFIER;
830 yylval.scm = ly_string2scm (str);
835 Lily_lexer::get_state () const
837 if (YY_START == extratoken)
838 return hidden_state_;
844 Lily_lexer::is_note_state () const
846 return get_state () == notes;
850 Lily_lexer::is_chord_state () const
852 return get_state () == chords;
856 Lily_lexer::is_lyric_state () const
858 return get_state () == lyrics;
862 Lily_lexer::is_figure_state () const
864 return get_state () == figures;
868 urg, belong to string (_convert)
869 and should be generalised
872 strip_leading_white (string&s)
875 for (; i < s.length (); i++)
883 strip_trailing_white (string&s)
885 ssize i = s.length ();
890 s = s.substr (0, i + 1);
895 Lilypond_version oldest_version ("2.7.38");
899 is_valid_version (string s)
901 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
902 Lilypond_version ver (s);
903 if (int (ver) < oldest_version)
905 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
906 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
912 non_fatal_error (_f ("program too old: %s (file requires: %s)", current.to_string (), ver.to_string ()));
923 lyric_fudge (string s)
925 char *chars = string_copy (s);
927 for (char *p = chars; *p ; p++)
929 if (*p == '_' && (p == chars || *(p-1) != '\\'))
937 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
939 * (((char*)s.c_str ()) + i + 1) = 'c';
940 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
947 Convert "NUM/DEN" into a '(NUM . DEN) cons.
950 scan_fraction (string frac)
952 ssize i = frac.find ('/');
953 string left = frac.substr (0, i);
954 string right = frac.substr (i + 1, (frac.length () - i + 1));
956 int n = String_convert::dec2int (left);
957 int d = String_convert::dec2int (right);
958 return scm_cons (scm_from_int (n), scm_from_int (d));
962 lookup_markup_command (string s)
964 SCM proc = ly_lily_module_constant ("lookup-markup-command");
965 return scm_call_1 (proc, ly_string2scm (s));
969 lookup_markup_list_command (string s)
971 SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
972 return scm_call_1 (proc, ly_string2scm (s));
975 /* Shut up lexer warnings. */
995 silence_lexer_warnings ()
997 (void) yy_start_stack_ptr;
998 (void) yy_start_stack_depth;
999 (void) yy_start_stack;
1000 (void) yy_push_state;
1001 (void) yy_pop_state;
1002 (void) yy_top_state;
1003 (void) silence_lexer_warnings;