1 %{ // -*-Fundamental-*-
3 lexer.ll -- implement the Flex lexer
5 source file of the LilyPond music typesetter
7 (c) 1996--2006 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-input-version.hh"
46 #include "music-function.hh"
47 #include "parse-scm.hh"
49 #include "source-file.hh"
50 #include "std-string.hh"
51 #include "string-convert.hh"
58 #define isatty HORRIBLEKLUDGE
60 void strip_trailing_white (string&);
61 void strip_leading_white (string&);
62 string lyric_fudge (string s);
63 int music_function_type (SCM);
64 SCM lookup_markup_command (string s);
65 bool is_valid_version (string s);
68 #define start_quote() \
69 yy_push_state (quote);\
70 yylval.string = new string
72 #define start_lyric_quote() \
73 yy_push_state (lyric_quote);\
74 yylval.string = new string
82 #define YY_USER_ACTION add_lexed_char (YYLeng ());
85 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
90 SCM scan_fraction (string);
91 SCM (* scm_parse_error_handler) (void *);
101 %option yyclass="Lily_lexer"
103 %option never-interactive
124 NATIONAL [\001-\006\021-\027\031\036\200-\377]
125 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
127 DASHED_WORD {A}({AN}|-)*
128 DASHED_KEY_WORD \\{DASHED_WORD}
135 REAL ({INT}\.{N}*)|(-?\.{N}+)
138 HORIZONTALWHITE [ \t]
142 MARKUPCOMMAND \\({A}|[-_])+
143 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
147 BOM_UTF8 \357\273\277
152 // windows-suck-suck-suck
155 <INITIAL,chords,lyrics,figures,notes>{BOM_UTF8} {
156 if (this->lexloc->line_number () != 1 || this->lexloc->column_number () != 0)
158 LexerError (_ ("stray UTF-8 BOM encountered").c_str ());
161 if (be_verbose_global)
162 message (_ ("Skipping UTF-8 BOM"));
165 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
167 yy_push_state (longcomment);
171 %[^{\n\r] { // backup rule
182 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
183 yy_push_state (version);
185 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}* {
186 yy_push_state (sourcefilename);
188 <version>\"[^"]*\" { /* got the version number */
189 string s (YYText () + 1);
190 s = s.substr (0, s.rfind ('\"'));
194 SCM top_scope = scm_car (scm_last_pair (scopes_));
195 scm_module_define (top_scope, ly_symbol2scm ("version-seen"), SCM_BOOL_T);
197 if (!is_valid_version (s))
202 <sourcefilename>\"[^"]*\" {
203 string s (YYText () + 1);
204 s = s.substr (0, s.rfind ('\"'));
207 this->here_input().get_source_file ()->name_ = s;
208 message (_f ("Renaming input to: `%s'", s.c_str ()));
209 progress_indication ("\n");
210 scm_module_define (scm_car (scopes_),
211 ly_symbol2scm ("input-file-name"),
212 scm_makfrom0str (s.c_str ()));
216 LexerError (_ ("quoted string expected after \\version").c_str ());
220 LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
233 LexerError (_ ("EOF found inside a comment").c_str ());
234 is_main_input_ = false;
235 if (! close_input ())
236 yyterminate (); // can't move this, since it actually rets a YY_NULL
241 <INITIAL,chords,lyrics,notes,figures>\\maininput {
245 is_main_input_ = true;
248 error (_ ("\\maininput not allowed outside init files"));
251 <INITIAL,chords,lyrics,figures,notes>\\include {
252 yy_push_state (incl);
254 <incl>\"[^"]*\" { /* got the include file name */
255 string s (YYText ()+1);
256 s = s.substr (0, s.rfind ('"'));
258 new_input (s, sources_);
261 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
262 string s = YYText () + 1;
263 strip_trailing_white (s);
264 if (s.length () && (s[s.length () - 1] == ';'))
265 s = s.substr (0, s.length () - 1);
267 SCM sid = lookup_identifier (s);
268 if (scm_is_string (sid)) {
269 new_input (ly_scm2string (sid), sources_);
272 string msg (_f ("wrong or undefined identifier: `%s'", s ));
274 LexerError (msg.c_str ());
275 SCM err = scm_current_error_port ();
276 scm_puts ("This value was found in the table: ", err);
277 scm_display (sid, err);
280 <incl>\"[^"]* { // backup rule
281 error (_ ("end quote missing"));
284 <chords,notes,figures>{RESTNAME} {
285 char const *s = YYText ();
286 yylval.scm = scm_makfrom0str (s);
289 <chords,notes,figures>R {
290 return MULTI_MEASURE_REST;
292 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
294 Input hi = here_input();
296 SCM sval = ly_parse_scm (hi.start (), &n, hi,
297 be_safe_global && is_main_input_);
299 if (sval == SCM_UNDEFINED)
301 sval = SCM_UNSPECIFIED;
305 for (int i = 0; i < n; i++)
309 char_count_stack_.back () += n;
311 if (unpack_identifier (sval) != SCM_UNDEFINED)
313 yylval.scm = unpack_identifier(sval);
314 return identifier_type (yylval.scm);
320 <INITIAL,notes,lyrics>{
322 return DOUBLE_ANGLE_OPEN;
325 return DOUBLE_ANGLE_CLOSE;
342 return scan_bare_word (YYText ());
346 return scan_escaped_word (YYText () + 1);
349 yylval.scm = scan_fraction (YYText ());
354 yylval.i = String_convert::dec2int (string (YYText ()));
358 yylval.i = String_convert::dec2int (string (YYText ()));
362 yylval.i = String_convert::dec2int (string (YYText () +1));
375 *yylval.string += to_string (escaped_char (YYText ()[1]));
378 *yylval.string += YYText ();
384 /* yylval is union. Must remember STRING before setting SCM*/
385 string *sp = yylval.string;
386 yylval.scm = scm_makfrom0str (sp->c_str ());
391 *yylval.string += YYText ();
396 *yylval.string += to_string (escaped_char (YYText ()[1]));
399 *yylval.string += YYText ();
405 /* yylval is union. Must remember STRING before setting SCM*/
406 string *sp = yylval.string;
407 yylval.scm = scm_makfrom0str (sp->c_str ());
409 return LYRICS_STRING;
412 *yylval.string += YYText ();
418 start_lyric_quote ();
421 yylval.scm = scan_fraction (YYText ());
425 yylval.i = String_convert::dec2int (string (YYText ()));
429 return scan_escaped_word (YYText () + 1);
433 string s (YYText ());
435 return yylval.i = EXTENDER;
437 return yylval.i = HYPHEN;
440 char c = s[s.length () - 1];
441 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
442 here_input ().warning (
443 _ ("Brace found at end of lyric. Did you forget a space?"));
444 yylval.scm = scm_makfrom0str (s.c_str ());
447 return LYRICS_STRING;
455 return scan_bare_word (YYText ());
458 return scan_escaped_word (YYText () + 1);
461 yylval.scm = scan_fraction (YYText ());
465 yylval.i = String_convert::dec2int (string (YYText ()));
500 string str (YYText () + 1);
501 SCM s = lookup_markup_command (str);
503 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
504 yylval.scm = scm_car(s);
505 SCM tag = scm_cdr(s);
506 if (tag == ly_symbol2scm("markup0"))
507 return MARKUP_HEAD_MARKUP0;
508 if (tag == ly_symbol2scm("empty"))
509 return MARKUP_HEAD_EMPTY;
510 else if (tag == ly_symbol2scm ("markup0-markup1"))
511 return MARKUP_HEAD_MARKUP0_MARKUP1;
512 else if (tag == ly_symbol2scm ("markup-list0"))
513 return MARKUP_HEAD_LIST0;
514 else if (tag == ly_symbol2scm ("scheme0"))
515 return MARKUP_HEAD_SCM0;
516 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
517 return MARKUP_HEAD_SCM0_SCM1;
518 else if (tag == ly_symbol2scm ("scheme0-markup1"))
519 return MARKUP_HEAD_SCM0_MARKUP1;
520 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
521 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
522 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
523 return MARKUP_HEAD_SCM0_SCM1_SCM2;
525 programming_error ("no parser tag defined for this markup signature");
530 return scan_escaped_word (str);
535 [^#{}"\\ \t\n\r\f]+ {
536 string s (YYText ());
538 char c = s[s.length () - 1];
539 /* brace open is for not confusing dumb tools. */
540 if (c == '{' || c == '}')
541 here_input ().warning (
542 _ ("Brace found at end of markup. Did you forget a space?"));
543 yylval.scm = scm_makfrom0str (s.c_str ());
556 is_main_input_ = false;
558 /* Returns YY_NULL */
561 else if (!close_input ())
562 /* Returns YY_NULL */
568 return scan_bare_word (YYText ());
571 return scan_escaped_word (YYText () + 1);
576 return scan_bare_word (YYText ());
579 return scan_escaped_word (YYText () + 1);
583 int cnv = sscanf (YYText (), "%lf", &r);
587 yylval.scm = scm_from_double (r);
592 yylval.i = String_convert::dec2int (string (YYText ()));
602 char c = YYText ()[0];
607 <INITIAL,notes,figures>. {
611 <INITIAL,lyrics,notes,figures>\\. {
612 char c = YYText ()[1];
616 return E_ANGLE_CLOSE;
620 return E_EXCLAMATION;
626 return E_BRACKET_OPEN;
630 return E_BRACKET_CLOSE;
642 string msg = _f ("invalid character: `%c'", YYText ()[0]);
643 LexerError (msg.c_str ());
650 Lily_lexer::push_chord_state (SCM tab)
652 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
653 yy_push_state (chords);
657 Lily_lexer::push_figuredbass_state ()
659 yy_push_state (figures);
663 Lily_lexer::push_initial_state ()
665 yy_push_state (INITIAL);
669 Lily_lexer::push_lyric_state ()
671 yy_push_state (lyrics);
675 Lily_lexer::push_markup_state ()
677 yy_push_state (markup);
681 Lily_lexer::push_note_state (SCM tab)
683 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
684 yy_push_state (notes);
688 Lily_lexer::pop_state ()
690 if (YYSTATE == notes || YYSTATE == chords)
691 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
696 Lily_lexer::identifier_type (SCM sid)
698 int k = try_special_identifiers (&yylval.scm , sid);
699 return k >= 0 ? k : SCM_IDENTIFIER;
704 Lily_lexer::scan_escaped_word (string str)
706 // use more SCM for this.
708 // SCM sym = ly_symbol2scm (str.c_str ());
710 int i = lookup_keyword (str);
711 if (i == MARKUP && is_lyric_state ())
716 SCM sid = lookup_identifier (str);
717 if (is_music_function (sid))
719 yylval.scm = get_music_function_transform (sid);
720 return music_function_type (yylval.scm);
723 if (sid != SCM_UNDEFINED)
726 return identifier_type (sid);
729 string msg (_f ("unknown escaped string: `\\%s'", str));
730 LexerError (msg.c_str ());
732 yylval.scm = scm_makfrom0str (str.c_str ());
738 Lily_lexer::scan_bare_word (string str)
740 SCM sym = ly_symbol2scm (str.c_str ());
741 if ((YYSTATE == notes) || (YYSTATE == chords)) {
742 SCM handle = SCM_BOOL_F;
743 if (scm_is_pair (pitchname_tab_stack_))
744 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
746 if (scm_is_pair (handle)) {
747 yylval.scm = scm_cdr (handle);
748 if (unsmob_pitch (yylval.scm))
749 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
750 else if (scm_is_symbol (yylval.scm))
753 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
755 yylval.scm = scm_cdr (handle);
756 return CHORD_MODIFIER;
760 yylval.scm = scm_makfrom0str (str.c_str ());
765 Lily_lexer::is_note_state () const
767 return YY_START == notes;
771 Lily_lexer::is_chord_state () const
773 return YY_START == chords;
777 Lily_lexer::is_lyric_state () const
779 return YY_START == lyrics;
783 Lily_lexer::is_figure_state () const
785 return YY_START == figures;
789 urg, belong to string (_convert)
790 and should be generalised
793 strip_leading_white (string&s)
796 for (; i < s.length (); i++)
804 strip_trailing_white (string&s)
806 ssize i = s.length ();
811 s = s.substr (0, i + 1);
816 Lilypond_version oldest_version ("2.7.38");
820 is_valid_version (string s)
822 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
823 Lilypond_version ver (s);
824 if (! ((ver >= oldest_version) && (ver <= current)))
826 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
827 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
838 lyric_fudge (string s)
840 char *chars = string_copy (s);
842 for (char *p = chars; *p ; p++)
844 if (*p == '_' && (p == chars || *(p-1) != '\\'))
852 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
854 * (((char*)s.c_str ()) + i + 1) = 'c';
855 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
862 Convert "NUM/DEN" into a '(NUM . DEN) cons.
865 scan_fraction (string frac)
867 ssize i = frac.find ('/');
868 string left = frac.substr (0, i);
869 string right = frac.substr (i + 1, (frac.length () - i + 1));
871 int n = String_convert::dec2int (left);
872 int d = String_convert::dec2int (right);
873 return scm_cons (scm_from_int (n), scm_from_int (d));
877 lookup_markup_command (string s)
879 SCM proc = ly_lily_module_constant ("lookup-markup-command");
880 return scm_call_1 (proc, scm_makfrom0str (s.c_str ()));
883 struct Parser_signature
888 static SCM signature_hash_table;
890 static void init_signature_hash_table ()
892 signature_hash_table = scm_gc_protect_object (scm_c_make_hash_table (31));
893 Parser_signature sigs[] = {
894 {"scm", MUSIC_FUNCTION_SCM},
895 {"music", MUSIC_FUNCTION_MUSIC},
896 {"scm-music", MUSIC_FUNCTION_SCM_MUSIC},
897 {"scm-scm", MUSIC_FUNCTION_SCM_SCM},
898 {"music-music", MUSIC_FUNCTION_MUSIC_MUSIC},
899 {"scm-music-music", MUSIC_FUNCTION_SCM_MUSIC_MUSIC},
900 {"scm-scm-music-music", MUSIC_FUNCTION_SCM_SCM_MUSIC_MUSIC},
901 {"scm-scm-music", MUSIC_FUNCTION_SCM_SCM_MUSIC},
902 {"scm-scm-scm-music", MUSIC_FUNCTION_SCM_SCM_SCM_SCM_MUSIC},
903 {"scm-scm-scm-scm-music", MUSIC_FUNCTION_SCM_SCM_SCM_MUSIC},
904 {"scm-scm-scm", MUSIC_FUNCTION_SCM_SCM_SCM},
905 {"markup", MUSIC_FUNCTION_MARKUP},
906 {"markup-music", MUSIC_FUNCTION_MARKUP_MUSIC},
907 {"markup-markup", MUSIC_FUNCTION_MARKUP_MARKUP},
908 {"markup-music-music", MUSIC_FUNCTION_MARKUP_MUSIC_MUSIC},
909 {"markup-markup-music", MUSIC_FUNCTION_MARKUP_MARKUP_MUSIC},
910 {"noarg", MUSIC_FUNCTION},
914 for (int i = 0; sigs[i].symbol; i++)
915 scm_hashq_set_x (signature_hash_table, scm_gc_protect_object (ly_symbol2scm (sigs[i].symbol)),
916 scm_from_int (sigs[i].token_type));
920 music_function_type (SCM func)
922 if (!signature_hash_table)
923 init_signature_hash_table ();
925 SCM type = scm_object_property (func, ly_symbol2scm ("music-function-signature-keyword"));
926 SCM token_type = scm_hashq_ref (signature_hash_table, type, SCM_BOOL_F);
927 if (!scm_is_number (token_type))
929 programming_error (_ ("can't find signature for music function"));
930 return MUSIC_FUNCTION_SCM;
933 return scm_to_int (token_type);
936 /* Shut up lexer warnings. */
956 silence_lexer_warnings ()
958 (void) yy_start_stack_ptr;
959 (void) yy_start_stack_depth;
960 (void) yy_start_stack;
961 (void) yy_push_state;
964 (void) silence_lexer_warnings;