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 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 SCM scan_fraction (string);
86 SCM (* scm_parse_error_handler) (void *);
96 %option yyclass="Lily_lexer"
98 %option never-interactive
122 NATIONAL [\001-\006\021-\027\031\036]
123 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
125 DASHED_WORD {A}({AN}|-)*
126 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
154 // windows-suck-suck-suck
157 <extratoken>{ANY_CHAR} {
158 /* Generate a token without swallowing anything */
160 /* First unswallow the eaten character */
161 add_lexed_char (-YYLeng ());
164 /* produce requested token */
165 int type = extra_token_types_.back ();
166 extra_token_types_.pop_back ();
167 if (extra_token_types_.empty ())
173 <INITIAL,chords,lyrics,figures,notes>{BOM_UTF8} {
174 if (this->lexloc->line_number () != 1 || this->lexloc->column_number () != 0)
176 LexerError (_ ("stray UTF-8 BOM encountered").c_str ());
179 if (be_verbose_global)
180 message (_ ("Skipping UTF-8 BOM"));
183 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
185 yy_push_state (longcomment);
189 %[^{\n\r] { // backup rule
200 <INITIAL,notes,figures,chords,markup>{
206 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
207 yy_push_state (version);
209 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}* {
210 yy_push_state (sourcefilename);
212 <INITIAL,chords,lyrics,notes,figures>\\sourcefileline{WHITE}* {
213 yy_push_state (sourcefileline);
215 <version>\"[^"]*\" { /* got the version number */
216 string s (YYText () + 1);
217 s = s.substr (0, s.rfind ('\"'));
221 SCM top_scope = scm_car (scm_last_pair (scopes_));
222 scm_module_define (top_scope, ly_symbol2scm ("version-seen"), SCM_BOOL_T);
224 if (!is_valid_version (s))
229 <sourcefilename>\"[^"]*\" {
230 string s (YYText () + 1);
231 s = s.substr (0, s.rfind ('\"'));
234 this->here_input().get_source_file ()->name_ = s;
235 message (_f ("Renaming input to: `%s'", s.c_str ()));
236 progress_indication ("\n");
237 scm_module_define (scm_car (scopes_),
238 ly_symbol2scm ("input-file-name"),
243 <sourcefileline>{INT} {
245 sscanf (YYText (), "%d", &i);
248 this->here_input ().get_source_file ()->set_line (here_input ().start (), i);
252 LexerError (_ ("quoted string expected after \\version").c_str ());
256 LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
260 LexerError (_ ("integer expected after \\sourcefileline").c_str ());
273 LexerError (_ ("EOF found inside a comment").c_str ());
274 is_main_input_ = false; // should be safe , can't have \include in --safe.
275 if (! close_input ())
276 yyterminate (); // can't move this, since it actually rets a YY_NULL
281 <INITIAL,chords,lyrics,notes,figures>\\maininput {
285 is_main_input_ = true;
288 error (_ ("\\maininput not allowed outside init files"));
291 <INITIAL,chords,lyrics,figures,notes>\\include {
292 yy_push_state (incl);
294 <incl>\"[^"]*\" { /* got the include file name */
295 string s (YYText ()+1);
296 s = s.substr (0, s.rfind ('"'));
298 new_input (s, sources_);
301 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
302 string s = YYText () + 1;
303 strip_trailing_white (s);
304 if (s.length () && (s[s.length () - 1] == ';'))
305 s = s.substr (0, s.length () - 1);
307 SCM sid = lookup_identifier (s);
308 if (scm_is_string (sid)) {
309 new_input (ly_scm2string (sid), sources_);
312 string msg (_f ("wrong or undefined identifier: `%s'", s ));
314 LexerError (msg.c_str ());
315 SCM err = scm_current_error_port ();
316 scm_puts ("This value was found in the table: ", err);
317 scm_display (sid, err);
320 <incl>\"[^"]* { // backup rule
321 error (_ ("end quote missing"));
324 <chords,notes,figures>{RESTNAME} {
325 char const *s = YYText ();
326 yylval.scm = scm_from_locale_string (s);
329 <chords,notes,figures>R {
330 return MULTI_MEASURE_REST;
332 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
334 Input hi = here_input();
336 SCM sval = ly_parse_scm (hi.start (), &n, hi,
337 be_safe_global && is_main_input_);
339 if (sval == SCM_UNDEFINED)
341 sval = SCM_UNSPECIFIED;
345 for (int i = 0; i < n; i++)
349 char_count_stack_.back () += n;
351 if (unpack_identifier (sval) != SCM_UNDEFINED)
353 yylval.scm = unpack_identifier(sval);
354 return identifier_type (yylval.scm);
360 <INITIAL,notes,lyrics>{
362 return DOUBLE_ANGLE_OPEN;
365 return DOUBLE_ANGLE_CLOSE;
392 return scan_bare_word (YYText ());
396 return scan_escaped_word (YYText () + 1);
399 yylval.scm = scan_fraction (YYText ());
404 yylval.i = String_convert::dec2int (string (YYText ()));
408 yylval.i = String_convert::dec2int (string (YYText ()));
412 yylval.i = String_convert::dec2int (string (YYText () +1));
419 *yylval.string += to_string (escaped_char (YYText ()[1]));
422 *yylval.string += YYText ();
428 /* yylval is union. Must remember STRING before setting SCM*/
429 string *sp = yylval.string;
430 yylval.scm = ly_string2scm (*sp);
432 return is_lyric_state () ? LYRICS_STRING : STRING;
435 *yylval.string += YYText ();
441 start_lyric_quote ();
444 yylval.scm = scan_fraction (YYText ());
448 yylval.i = String_convert::dec2int (string (YYText ()));
452 return scan_escaped_word (YYText () + 1);
456 string s (YYText ());
458 return yylval.i = EXTENDER;
460 return yylval.i = HYPHEN;
463 char c = s[s.length () - 1];
464 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
465 here_input ().warning (
466 _ ("Brace found at end of lyric. Did you forget a space?"));
467 yylval.scm = ly_string2scm (s);
470 return LYRICS_STRING;
478 return scan_bare_word (YYText ());
481 return scan_escaped_word (YYText () + 1);
484 yylval.scm = scan_fraction (YYText ());
488 yylval.i = String_convert::dec2int (string (YYText ()));
517 string str (YYText () + 1);
518 SCM s = lookup_markup_command (str);
520 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
521 yylval.scm = scm_car(s);
522 SCM tag = scm_cdr(s);
523 if (tag == ly_symbol2scm("markup0"))
524 return MARKUP_HEAD_MARKUP0;
525 if (tag == ly_symbol2scm("empty"))
526 return MARKUP_HEAD_EMPTY;
527 else if (tag == ly_symbol2scm ("markup0-markup1"))
528 return MARKUP_HEAD_MARKUP0_MARKUP1;
529 else if (tag == ly_symbol2scm ("markup-list0"))
530 return MARKUP_HEAD_LIST0;
531 else if (tag == ly_symbol2scm ("scheme0"))
532 return MARKUP_HEAD_SCM0;
533 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
534 return MARKUP_HEAD_SCM0_SCM1;
535 else if (tag == ly_symbol2scm ("scheme0-markup1"))
536 return MARKUP_HEAD_SCM0_MARKUP1;
537 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
538 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
539 else if (tag == ly_symbol2scm ("scheme0-markup1-markup2"))
540 return MARKUP_HEAD_SCM0_MARKUP1_MARKUP2;
541 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
542 return MARKUP_HEAD_SCM0_SCM1_SCM2;
544 programming_error ("no parser tag defined for this markup signature");
549 return scan_escaped_word (str);
554 [^#{}"\\ \t\n\r\f]+ {
555 string s (YYText ());
557 char c = s[s.length () - 1];
558 /* brace open is for not confusing dumb tools. */
559 if (c == '{' || c == '}')
560 here_input ().warning (
561 _ ("Brace found at end of markup. Did you forget a space?"));
562 yylval.scm = ly_string2scm (s);
575 /* 2 = init.ly + current file.
576 > because we're before closing, but is_main_input_ should
579 is_main_input_ = include_stack_.size () > 2;
581 /* Returns YY_NULL */
584 else if (!close_input ())
585 /* Returns YY_NULL */
591 return scan_bare_word (YYText ());
594 return scan_escaped_word (YYText () + 1);
599 return scan_bare_word (YYText ());
602 return scan_escaped_word (YYText () + 1);
606 int cnv = sscanf (YYText (), "%lf", &r);
610 yylval.scm = scm_from_double (r);
615 yylval.i = String_convert::dec2int (string (YYText ()));
625 char c = YYText ()[0];
630 <INITIAL,notes,figures>. {
634 <INITIAL,lyrics,notes,figures>\\. {
635 char c = YYText ()[1];
639 return E_ANGLE_CLOSE;
643 return E_EXCLAMATION;
649 return E_BRACKET_OPEN;
653 return E_BRACKET_CLOSE;
665 string msg = _f ("invalid character: `%c'", YYText ()[0]);
666 LexerError (msg.c_str ());
672 /* Make the lexer generate a token of the given type as the next token.
673 TODO: make it possible to define a value for the token as well */
675 Lily_lexer::push_extra_token (int token_type)
677 if (extra_token_types_.empty ())
679 if (YY_START != extratoken)
680 hidden_state_ = YY_START;
681 yy_push_state (extratoken);
683 extra_token_types_.push_back (token_type);
687 Lily_lexer::push_chord_state (SCM tab)
689 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
690 yy_push_state (chords);
694 Lily_lexer::push_figuredbass_state ()
696 yy_push_state (figures);
700 Lily_lexer::push_initial_state ()
702 yy_push_state (INITIAL);
706 Lily_lexer::push_lyric_state ()
708 yy_push_state (lyrics);
712 Lily_lexer::push_markup_state ()
714 yy_push_state (markup);
718 Lily_lexer::push_note_state (SCM tab)
720 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
721 yy_push_state (notes);
725 Lily_lexer::pop_state ()
727 if (YYSTATE == notes || YYSTATE == chords)
728 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
734 Lily_lexer::identifier_type (SCM sid)
736 int k = try_special_identifiers (&yylval.scm , sid);
737 return k >= 0 ? k : SCM_IDENTIFIER;
742 Lily_lexer::scan_escaped_word (string str)
744 // use more SCM for this.
746 // SCM sym = ly_symbol2scm (str.c_str ());
748 int i = lookup_keyword (str);
749 if (i == MARKUP && is_lyric_state ())
754 SCM sid = lookup_identifier (str);
755 if (is_music_function (sid))
757 yylval.scm = get_music_function_transform (sid);
759 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
760 push_extra_token (EXPECT_NO_MORE_ARGS);
761 for (; scm_is_pair (s); s = scm_cdr (s))
763 if (scm_car (s) == ly_music_p_proc)
764 push_extra_token (EXPECT_MUSIC);
765 else if (scm_car (s) == ly_lily_module_constant ("markup?"))
766 push_extra_token (EXPECT_MARKUP);
767 else if (ly_is_procedure (scm_car (s)))
768 push_extra_token (EXPECT_SCM);
769 else programming_error ("Function parameter without type-checking predicate");
771 return MUSIC_FUNCTION;
774 if (sid != SCM_UNDEFINED)
777 return identifier_type (sid);
780 string msg (_f ("unknown escaped string: `\\%s'", str));
781 LexerError (msg.c_str ());
783 yylval.scm = ly_string2scm (str);
789 Lily_lexer::scan_bare_word (string str)
791 SCM sym = ly_symbol2scm (str.c_str ());
792 if ((YYSTATE == notes) || (YYSTATE == chords)) {
793 SCM handle = SCM_BOOL_F;
794 if (scm_is_pair (pitchname_tab_stack_))
795 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
797 if (scm_is_pair (handle)) {
798 yylval.scm = scm_cdr (handle);
799 if (unsmob_pitch (yylval.scm))
800 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
801 else if (scm_is_symbol (yylval.scm))
804 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
806 yylval.scm = scm_cdr (handle);
807 return CHORD_MODIFIER;
811 yylval.scm = ly_string2scm (str);
816 Lily_lexer::get_state () const
818 if (YY_START == extratoken)
819 return hidden_state_;
825 Lily_lexer::is_note_state () const
827 return get_state () == notes;
831 Lily_lexer::is_chord_state () const
833 return get_state () == chords;
837 Lily_lexer::is_lyric_state () const
839 return get_state () == lyrics;
843 Lily_lexer::is_figure_state () const
845 return get_state () == figures;
849 urg, belong to string (_convert)
850 and should be generalised
853 strip_leading_white (string&s)
856 for (; i < s.length (); i++)
864 strip_trailing_white (string&s)
866 ssize i = s.length ();
871 s = s.substr (0, i + 1);
876 Lilypond_version oldest_version ("2.7.38");
880 is_valid_version (string s)
882 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
883 Lilypond_version ver (s);
884 if (int (ver) < oldest_version)
886 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
887 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
893 non_fatal_error (_f ("program too old: %s (file requires: %s)", current.to_string (), ver.to_string ()));
904 lyric_fudge (string s)
906 char *chars = string_copy (s);
908 for (char *p = chars; *p ; p++)
910 if (*p == '_' && (p == chars || *(p-1) != '\\'))
918 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
920 * (((char*)s.c_str ()) + i + 1) = 'c';
921 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
928 Convert "NUM/DEN" into a '(NUM . DEN) cons.
931 scan_fraction (string frac)
933 ssize i = frac.find ('/');
934 string left = frac.substr (0, i);
935 string right = frac.substr (i + 1, (frac.length () - i + 1));
937 int n = String_convert::dec2int (left);
938 int d = String_convert::dec2int (right);
939 return scm_cons (scm_from_int (n), scm_from_int (d));
943 lookup_markup_command (string s)
945 SCM proc = ly_lily_module_constant ("lookup-markup-command");
946 return scm_call_1 (proc, ly_string2scm (s));
949 /* Shut up lexer warnings. */
969 silence_lexer_warnings ()
971 (void) yy_start_stack_ptr;
972 (void) yy_start_stack_depth;
973 (void) yy_start_stack;
974 (void) yy_push_state;
977 (void) silence_lexer_warnings;