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-scheme1-scheme2"))
540 return MARKUP_HEAD_SCM0_SCM1_SCM2;
542 programming_error ("no parser tag defined for this markup signature");
547 return scan_escaped_word (str);
552 [^#{}"\\ \t\n\r\f]+ {
553 string s (YYText ());
555 char c = s[s.length () - 1];
556 /* brace open is for not confusing dumb tools. */
557 if (c == '{' || c == '}')
558 here_input ().warning (
559 _ ("Brace found at end of markup. Did you forget a space?"));
560 yylval.scm = ly_string2scm (s);
573 /* 2 = init.ly + current file.
574 > because we're before closing, but is_main_input_ should
577 is_main_input_ = include_stack_.size () > 2;
579 /* Returns YY_NULL */
582 else if (!close_input ())
583 /* Returns YY_NULL */
589 return scan_bare_word (YYText ());
592 return scan_escaped_word (YYText () + 1);
597 return scan_bare_word (YYText ());
600 return scan_escaped_word (YYText () + 1);
604 int cnv = sscanf (YYText (), "%lf", &r);
608 yylval.scm = scm_from_double (r);
613 yylval.i = String_convert::dec2int (string (YYText ()));
623 char c = YYText ()[0];
628 <INITIAL,notes,figures>. {
632 <INITIAL,lyrics,notes,figures>\\. {
633 char c = YYText ()[1];
637 return E_ANGLE_CLOSE;
641 return E_EXCLAMATION;
647 return E_BRACKET_OPEN;
651 return E_BRACKET_CLOSE;
663 string msg = _f ("invalid character: `%c'", YYText ()[0]);
664 LexerError (msg.c_str ());
670 /* Make the lexer generate a token of the given type as the next token.
671 TODO: make it possible to define a value for the token as well */
673 Lily_lexer::push_extra_token (int token_type)
675 if (extra_token_types_.empty ())
677 if (YY_START != extratoken)
678 hidden_state_ = YY_START;
679 yy_push_state (extratoken);
681 extra_token_types_.push_back (token_type);
685 Lily_lexer::push_chord_state (SCM tab)
687 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
688 yy_push_state (chords);
692 Lily_lexer::push_figuredbass_state ()
694 yy_push_state (figures);
698 Lily_lexer::push_initial_state ()
700 yy_push_state (INITIAL);
704 Lily_lexer::push_lyric_state ()
706 yy_push_state (lyrics);
710 Lily_lexer::push_markup_state ()
712 yy_push_state (markup);
716 Lily_lexer::push_note_state (SCM tab)
718 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
719 yy_push_state (notes);
723 Lily_lexer::pop_state ()
725 if (YYSTATE == notes || YYSTATE == chords)
726 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
732 Lily_lexer::identifier_type (SCM sid)
734 int k = try_special_identifiers (&yylval.scm , sid);
735 return k >= 0 ? k : SCM_IDENTIFIER;
740 Lily_lexer::scan_escaped_word (string str)
742 // use more SCM for this.
744 // SCM sym = ly_symbol2scm (str.c_str ());
746 int i = lookup_keyword (str);
747 if (i == MARKUP && is_lyric_state ())
752 SCM sid = lookup_identifier (str);
753 if (is_music_function (sid))
755 yylval.scm = get_music_function_transform (sid);
757 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
758 push_extra_token (EXPECT_NO_MORE_ARGS);
759 for (; scm_is_pair (s); s = scm_cdr (s))
761 if (scm_car (s) == ly_music_p_proc)
762 push_extra_token (EXPECT_MUSIC);
763 else if (scm_car (s) == ly_lily_module_constant ("markup?"))
764 push_extra_token (EXPECT_MARKUP);
765 else if (ly_is_procedure (scm_car (s)))
766 push_extra_token (EXPECT_SCM);
767 else programming_error ("Function parameter without type-checking predicate");
769 return MUSIC_FUNCTION;
772 if (sid != SCM_UNDEFINED)
775 return identifier_type (sid);
778 string msg (_f ("unknown escaped string: `\\%s'", str));
779 LexerError (msg.c_str ());
781 yylval.scm = ly_string2scm (str);
787 Lily_lexer::scan_bare_word (string str)
789 SCM sym = ly_symbol2scm (str.c_str ());
790 if ((YYSTATE == notes) || (YYSTATE == chords)) {
791 SCM handle = SCM_BOOL_F;
792 if (scm_is_pair (pitchname_tab_stack_))
793 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
795 if (scm_is_pair (handle)) {
796 yylval.scm = scm_cdr (handle);
797 if (unsmob_pitch (yylval.scm))
798 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
799 else if (scm_is_symbol (yylval.scm))
802 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
804 yylval.scm = scm_cdr (handle);
805 return CHORD_MODIFIER;
809 yylval.scm = ly_string2scm (str);
814 Lily_lexer::get_state () const
816 if (YY_START == extratoken)
817 return hidden_state_;
823 Lily_lexer::is_note_state () const
825 return get_state () == notes;
829 Lily_lexer::is_chord_state () const
831 return get_state () == chords;
835 Lily_lexer::is_lyric_state () const
837 return get_state () == lyrics;
841 Lily_lexer::is_figure_state () const
843 return get_state () == figures;
847 urg, belong to string (_convert)
848 and should be generalised
851 strip_leading_white (string&s)
854 for (; i < s.length (); i++)
862 strip_trailing_white (string&s)
864 ssize i = s.length ();
869 s = s.substr (0, i + 1);
874 Lilypond_version oldest_version ("2.7.38");
878 is_valid_version (string s)
880 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
881 Lilypond_version ver (s);
882 if (int (ver) < oldest_version)
884 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
885 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
891 non_fatal_error (_f ("program too old: %s (file requires: %s)", current.to_string (), ver.to_string ()));
902 lyric_fudge (string s)
904 char *chars = string_copy (s);
906 for (char *p = chars; *p ; p++)
908 if (*p == '_' && (p == chars || *(p-1) != '\\'))
916 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
918 * (((char*)s.c_str ()) + i + 1) = 'c';
919 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
926 Convert "NUM/DEN" into a '(NUM . DEN) cons.
929 scan_fraction (string frac)
931 ssize i = frac.find ('/');
932 string left = frac.substr (0, i);
933 string right = frac.substr (i + 1, (frac.length () - i + 1));
935 int n = String_convert::dec2int (left);
936 int d = String_convert::dec2int (right);
937 return scm_cons (scm_from_int (n), scm_from_int (d));
941 lookup_markup_command (string s)
943 SCM proc = ly_lily_module_constant ("lookup-markup-command");
944 return scm_call_1 (proc, ly_string2scm (s));
947 /* Shut up lexer warnings. */
967 silence_lexer_warnings ()
969 (void) yy_start_stack_ptr;
970 (void) yy_start_stack_depth;
971 (void) yy_start_stack;
972 (void) yy_push_state;
975 (void) silence_lexer_warnings;