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
125 NATIONAL [\001-\006\021-\027\031\036\200-\377]
126 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
128 DASHED_WORD {A}({AN}|-)*
129 DASHED_KEY_WORD \\{DASHED_WORD}
136 REAL ({INT}\.{N}*)|(-?\.{N}+)
139 HORIZONTALWHITE [ \t]
143 MARKUPCOMMAND \\({A}|[-_])+
144 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
148 BOM_UTF8 \357\273\277
153 // windows-suck-suck-suck
156 <INITIAL,chords,lyrics,figures,notes>{BOM_UTF8} {
157 if (this->lexloc->line_number () != 1 || this->lexloc->column_number () != 0)
159 LexerError (_ ("stray UTF-8 BOM encountered").c_str ());
162 if (be_verbose_global)
163 message (_ ("Skipping UTF-8 BOM"));
166 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
168 yy_push_state (longcomment);
172 %[^{\n\r] { // backup rule
183 <INITIAL,notes,figures,chords,markup>{
189 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
190 yy_push_state (version);
192 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}* {
193 yy_push_state (sourcefilename);
195 <INITIAL,chords,lyrics,notes,figures>\\sourcefileline{WHITE}* {
196 yy_push_state (sourcefileline);
198 <version>\"[^"]*\" { /* got the version number */
199 string s (YYText () + 1);
200 s = s.substr (0, s.rfind ('\"'));
204 SCM top_scope = scm_car (scm_last_pair (scopes_));
205 scm_module_define (top_scope, ly_symbol2scm ("version-seen"), SCM_BOOL_T);
207 if (!is_valid_version (s))
212 <sourcefilename>\"[^"]*\" {
213 string s (YYText () + 1);
214 s = s.substr (0, s.rfind ('\"'));
217 this->here_input().get_source_file ()->name_ = s;
218 message (_f ("Renaming input to: `%s'", s.c_str ()));
219 progress_indication ("\n");
220 scm_module_define (scm_car (scopes_),
221 ly_symbol2scm ("input-file-name"),
222 scm_makfrom0str (s.c_str ()));
226 <sourcefileline>{INT} {
228 sscanf (YYText (), "%d", &i);
230 // this->set_debug (1);
232 this->here_input ().get_source_file ()->set_line (here_input ().start (), i);
236 LexerError (_ ("quoted string expected after \\version").c_str ());
240 LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
244 LexerError (_ ("integer expected after \\sourcefileline").c_str ());
257 LexerError (_ ("EOF found inside a comment").c_str ());
258 is_main_input_ = false;
259 if (! close_input ())
260 yyterminate (); // can't move this, since it actually rets a YY_NULL
265 <INITIAL,chords,lyrics,notes,figures>\\maininput {
269 is_main_input_ = true;
272 error (_ ("\\maininput not allowed outside init files"));
275 <INITIAL,chords,lyrics,figures,notes>\\include {
276 yy_push_state (incl);
278 <incl>\"[^"]*\" { /* got the include file name */
279 string s (YYText ()+1);
280 s = s.substr (0, s.rfind ('"'));
282 new_input (s, sources_);
285 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
286 string s = YYText () + 1;
287 strip_trailing_white (s);
288 if (s.length () && (s[s.length () - 1] == ';'))
289 s = s.substr (0, s.length () - 1);
291 SCM sid = lookup_identifier (s);
292 if (scm_is_string (sid)) {
293 new_input (ly_scm2string (sid), sources_);
296 string msg (_f ("wrong or undefined identifier: `%s'", s ));
298 LexerError (msg.c_str ());
299 SCM err = scm_current_error_port ();
300 scm_puts ("This value was found in the table: ", err);
301 scm_display (sid, err);
304 <incl>\"[^"]* { // backup rule
305 error (_ ("end quote missing"));
308 <chords,notes,figures>{RESTNAME} {
309 char const *s = YYText ();
310 yylval.scm = scm_makfrom0str (s);
313 <chords,notes,figures>R {
314 return MULTI_MEASURE_REST;
316 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
318 Input hi = here_input();
320 SCM sval = ly_parse_scm (hi.start (), &n, hi,
321 be_safe_global && is_main_input_);
323 if (sval == SCM_UNDEFINED)
325 sval = SCM_UNSPECIFIED;
329 for (int i = 0; i < n; i++)
333 char_count_stack_.back () += n;
335 if (unpack_identifier (sval) != SCM_UNDEFINED)
337 yylval.scm = unpack_identifier(sval);
338 return identifier_type (yylval.scm);
344 <INITIAL,notes,lyrics>{
346 return DOUBLE_ANGLE_OPEN;
349 return DOUBLE_ANGLE_CLOSE;
376 return scan_bare_word (YYText ());
380 return scan_escaped_word (YYText () + 1);
383 yylval.scm = scan_fraction (YYText ());
388 yylval.i = String_convert::dec2int (string (YYText ()));
392 yylval.i = String_convert::dec2int (string (YYText ()));
396 yylval.i = String_convert::dec2int (string (YYText () +1));
403 *yylval.string += to_string (escaped_char (YYText ()[1]));
406 *yylval.string += YYText ();
412 /* yylval is union. Must remember STRING before setting SCM*/
413 string *sp = yylval.string;
414 yylval.scm = scm_makfrom0str (sp->c_str ());
416 return is_lyric_state () ? LYRICS_STRING : STRING;
419 *yylval.string += YYText ();
425 start_lyric_quote ();
428 yylval.scm = scan_fraction (YYText ());
432 yylval.i = String_convert::dec2int (string (YYText ()));
436 return scan_escaped_word (YYText () + 1);
440 string s (YYText ());
442 return yylval.i = EXTENDER;
444 return yylval.i = HYPHEN;
447 char c = s[s.length () - 1];
448 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
449 here_input ().warning (
450 _ ("Brace found at end of lyric. Did you forget a space?"));
451 yylval.scm = scm_makfrom0str (s.c_str ());
454 return LYRICS_STRING;
462 return scan_bare_word (YYText ());
465 return scan_escaped_word (YYText () + 1);
468 yylval.scm = scan_fraction (YYText ());
472 yylval.i = String_convert::dec2int (string (YYText ()));
501 string str (YYText () + 1);
502 SCM s = lookup_markup_command (str);
504 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
505 yylval.scm = scm_car(s);
506 SCM tag = scm_cdr(s);
507 if (tag == ly_symbol2scm("markup0"))
508 return MARKUP_HEAD_MARKUP0;
509 if (tag == ly_symbol2scm("empty"))
510 return MARKUP_HEAD_EMPTY;
511 else if (tag == ly_symbol2scm ("markup0-markup1"))
512 return MARKUP_HEAD_MARKUP0_MARKUP1;
513 else if (tag == ly_symbol2scm ("markup-list0"))
514 return MARKUP_HEAD_LIST0;
515 else if (tag == ly_symbol2scm ("scheme0"))
516 return MARKUP_HEAD_SCM0;
517 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
518 return MARKUP_HEAD_SCM0_SCM1;
519 else if (tag == ly_symbol2scm ("scheme0-markup1"))
520 return MARKUP_HEAD_SCM0_MARKUP1;
521 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
522 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
523 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
524 return MARKUP_HEAD_SCM0_SCM1_SCM2;
526 programming_error ("no parser tag defined for this markup signature");
531 return scan_escaped_word (str);
536 [^#{}"\\ \t\n\r\f]+ {
537 string s (YYText ());
539 char c = s[s.length () - 1];
540 /* brace open is for not confusing dumb tools. */
541 if (c == '{' || c == '}')
542 here_input ().warning (
543 _ ("Brace found at end of markup. Did you forget a space?"));
544 yylval.scm = scm_makfrom0str (s.c_str ());
557 is_main_input_ = false;
559 /* Returns YY_NULL */
562 else if (!close_input ())
563 /* Returns YY_NULL */
569 return scan_bare_word (YYText ());
572 return scan_escaped_word (YYText () + 1);
577 return scan_bare_word (YYText ());
580 return scan_escaped_word (YYText () + 1);
584 int cnv = sscanf (YYText (), "%lf", &r);
588 yylval.scm = scm_from_double (r);
593 yylval.i = String_convert::dec2int (string (YYText ()));
603 char c = YYText ()[0];
608 <INITIAL,notes,figures>. {
612 <INITIAL,lyrics,notes,figures>\\. {
613 char c = YYText ()[1];
617 return E_ANGLE_CLOSE;
621 return E_EXCLAMATION;
627 return E_BRACKET_OPEN;
631 return E_BRACKET_CLOSE;
643 string msg = _f ("invalid character: `%c'", YYText ()[0]);
644 LexerError (msg.c_str ());
651 Lily_lexer::push_chord_state (SCM tab)
653 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
654 yy_push_state (chords);
658 Lily_lexer::push_figuredbass_state ()
660 yy_push_state (figures);
664 Lily_lexer::push_initial_state ()
666 yy_push_state (INITIAL);
670 Lily_lexer::push_lyric_state ()
672 yy_push_state (lyrics);
676 Lily_lexer::push_markup_state ()
678 yy_push_state (markup);
682 Lily_lexer::push_note_state (SCM tab)
684 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
685 yy_push_state (notes);
689 Lily_lexer::pop_state ()
691 if (YYSTATE == notes || YYSTATE == chords)
692 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
697 Lily_lexer::identifier_type (SCM sid)
699 int k = try_special_identifiers (&yylval.scm , sid);
700 return k >= 0 ? k : SCM_IDENTIFIER;
705 Lily_lexer::scan_escaped_word (string str)
707 // use more SCM for this.
709 // SCM sym = ly_symbol2scm (str.c_str ());
711 int i = lookup_keyword (str);
712 if (i == MARKUP && is_lyric_state ())
717 SCM sid = lookup_identifier (str);
718 if (is_music_function (sid))
720 yylval.scm = get_music_function_transform (sid);
721 return music_function_type (yylval.scm);
724 if (sid != SCM_UNDEFINED)
727 return identifier_type (sid);
730 string msg (_f ("unknown escaped string: `\\%s'", str));
731 LexerError (msg.c_str ());
733 yylval.scm = scm_makfrom0str (str.c_str ());
739 Lily_lexer::scan_bare_word (string str)
741 SCM sym = ly_symbol2scm (str.c_str ());
742 if ((YYSTATE == notes) || (YYSTATE == chords)) {
743 SCM handle = SCM_BOOL_F;
744 if (scm_is_pair (pitchname_tab_stack_))
745 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
747 if (scm_is_pair (handle)) {
748 yylval.scm = scm_cdr (handle);
749 if (unsmob_pitch (yylval.scm))
750 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
751 else if (scm_is_symbol (yylval.scm))
754 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
756 yylval.scm = scm_cdr (handle);
757 return CHORD_MODIFIER;
761 yylval.scm = scm_makfrom0str (str.c_str ());
766 Lily_lexer::is_note_state () const
768 return YY_START == notes;
772 Lily_lexer::is_chord_state () const
774 return YY_START == chords;
778 Lily_lexer::is_lyric_state () const
780 return YY_START == lyrics;
784 Lily_lexer::is_figure_state () const
786 return YY_START == figures;
790 urg, belong to string (_convert)
791 and should be generalised
794 strip_leading_white (string&s)
797 for (; i < s.length (); i++)
805 strip_trailing_white (string&s)
807 ssize i = s.length ();
812 s = s.substr (0, i + 1);
817 Lilypond_version oldest_version ("2.7.38");
821 is_valid_version (string s)
823 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
824 Lilypond_version ver (s);
825 if (! ((ver >= oldest_version) && (ver <= current)))
827 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
828 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
839 lyric_fudge (string s)
841 char *chars = string_copy (s);
843 for (char *p = chars; *p ; p++)
845 if (*p == '_' && (p == chars || *(p-1) != '\\'))
853 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
855 * (((char*)s.c_str ()) + i + 1) = 'c';
856 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
863 Convert "NUM/DEN" into a '(NUM . DEN) cons.
866 scan_fraction (string frac)
868 ssize i = frac.find ('/');
869 string left = frac.substr (0, i);
870 string right = frac.substr (i + 1, (frac.length () - i + 1));
872 int n = String_convert::dec2int (left);
873 int d = String_convert::dec2int (right);
874 return scm_cons (scm_from_int (n), scm_from_int (d));
878 lookup_markup_command (string s)
880 SCM proc = ly_lily_module_constant ("lookup-markup-command");
881 return scm_call_1 (proc, scm_makfrom0str (s.c_str ()));
884 struct Parser_signature
889 static SCM signature_hash_table;
891 static void init_signature_hash_table ()
893 signature_hash_table = scm_gc_protect_object (scm_c_make_hash_table (31));
894 Parser_signature sigs[] = {
895 {"scm", MUSIC_FUNCTION_SCM},
896 {"music", MUSIC_FUNCTION_MUSIC},
897 {"scm-music", MUSIC_FUNCTION_SCM_MUSIC},
898 {"scm-scm", MUSIC_FUNCTION_SCM_SCM},
899 {"music-music", MUSIC_FUNCTION_MUSIC_MUSIC},
900 {"scm-music-music", MUSIC_FUNCTION_SCM_MUSIC_MUSIC},
901 {"scm-scm-music-music", MUSIC_FUNCTION_SCM_SCM_MUSIC_MUSIC},
902 {"scm-scm-music", MUSIC_FUNCTION_SCM_SCM_MUSIC},
903 {"scm-scm-scm-music", MUSIC_FUNCTION_SCM_SCM_SCM_SCM_MUSIC},
904 {"scm-scm-scm-scm-music", MUSIC_FUNCTION_SCM_SCM_SCM_MUSIC},
905 {"scm-scm-scm", MUSIC_FUNCTION_SCM_SCM_SCM},
906 {"markup", MUSIC_FUNCTION_MARKUP},
907 {"markup-music", MUSIC_FUNCTION_MARKUP_MUSIC},
908 {"markup-markup", MUSIC_FUNCTION_MARKUP_MARKUP},
909 {"markup-music-music", MUSIC_FUNCTION_MARKUP_MUSIC_MUSIC},
910 {"markup-markup-music", MUSIC_FUNCTION_MARKUP_MARKUP_MUSIC},
911 {"noarg", MUSIC_FUNCTION},
915 for (int i = 0; sigs[i].symbol; i++)
916 scm_hashq_set_x (signature_hash_table, ly_symbol2scm (sigs[i].symbol),
917 scm_from_int (sigs[i].token_type));
921 music_function_type (SCM func)
923 if (!signature_hash_table)
924 init_signature_hash_table ();
926 SCM type = scm_object_property (func, ly_symbol2scm ("music-function-signature-keyword"));
927 SCM token_type = scm_hashq_ref (signature_hash_table, type, SCM_BOOL_F);
928 if (!scm_is_number (token_type))
930 programming_error (_ ("can't find signature for music function"));
931 return MUSIC_FUNCTION_SCM;
934 return scm_to_int (token_type);
937 /* Shut up lexer warnings. */
957 silence_lexer_warnings ()
959 (void) yy_start_stack_ptr;
960 (void) yy_start_stack_depth;
961 (void) yy_start_stack;
962 (void) yy_push_state;
965 (void) silence_lexer_warnings;