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 ("markup-list0"))
555 return MARKUP_LIST_HEAD_LIST0;
556 else if (tag == ly_symbol2scm ("scheme0-markup-list1"))
557 return MARKUP_LIST_HEAD_SCM0_LIST1;
558 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup-list2"))
559 return MARKUP_LIST_HEAD_SCM0_SCM1_LIST2;
561 programming_error ("no parser tag defined for this markup signature");
566 return scan_escaped_word (str);
571 [^#{}"\\ \t\n\r\f]+ {
572 string s (YYText ());
574 char c = s[s.length () - 1];
575 /* brace open is for not confusing dumb tools. */
576 if (c == '{' || c == '}')
577 here_input ().warning (
578 _ ("Brace found at end of markup. Did you forget a space?"));
579 yylval.scm = ly_string2scm (s);
592 /* 2 = init.ly + current file.
593 > because we're before closing, but is_main_input_ should
596 is_main_input_ = include_stack_.size () > 2;
598 /* Returns YY_NULL */
601 else if (!close_input ())
602 /* Returns YY_NULL */
608 return scan_bare_word (YYText ());
611 return scan_escaped_word (YYText () + 1);
616 return scan_bare_word (YYText ());
619 return scan_escaped_word (YYText () + 1);
623 int cnv = sscanf (YYText (), "%lf", &r);
627 yylval.scm = scm_from_double (r);
632 yylval.i = String_convert::dec2int (string (YYText ()));
642 char c = YYText ()[0];
647 <INITIAL,notes,figures>. {
651 <INITIAL,lyrics,notes,figures>\\. {
652 char c = YYText ()[1];
656 return E_ANGLE_CLOSE;
660 return E_EXCLAMATION;
666 return E_BRACKET_OPEN;
670 return E_BRACKET_CLOSE;
682 string msg = _f ("invalid character: `%c'", YYText ()[0]);
683 LexerError (msg.c_str ());
689 /* Make the lexer generate a token of the given type as the next token.
690 TODO: make it possible to define a value for the token as well */
692 Lily_lexer::push_extra_token (int token_type)
694 if (extra_token_types_.empty ())
696 if (YY_START != extratoken)
697 hidden_state_ = YY_START;
698 yy_push_state (extratoken);
700 extra_token_types_.push_back (token_type);
704 Lily_lexer::push_chord_state (SCM tab)
706 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
707 yy_push_state (chords);
711 Lily_lexer::push_figuredbass_state ()
713 yy_push_state (figures);
717 Lily_lexer::push_initial_state ()
719 yy_push_state (INITIAL);
723 Lily_lexer::push_lyric_state ()
725 yy_push_state (lyrics);
729 Lily_lexer::push_markup_state ()
731 yy_push_state (markup);
735 Lily_lexer::push_note_state (SCM tab)
737 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
738 yy_push_state (notes);
742 Lily_lexer::pop_state ()
744 if (YYSTATE == notes || YYSTATE == chords)
745 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
751 Lily_lexer::identifier_type (SCM sid)
753 int k = try_special_identifiers (&yylval.scm , sid);
754 return k >= 0 ? k : SCM_IDENTIFIER;
759 Lily_lexer::scan_escaped_word (string str)
761 // use more SCM for this.
763 // SCM sym = ly_symbol2scm (str.c_str ());
765 int i = lookup_keyword (str);
766 if (i == MARKUP && is_lyric_state ())
771 SCM sid = lookup_identifier (str);
772 if (is_music_function (sid))
774 yylval.scm = get_music_function_transform (sid);
776 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
777 push_extra_token (EXPECT_NO_MORE_ARGS);
778 for (; scm_is_pair (s); s = scm_cdr (s))
780 if (scm_car (s) == ly_music_p_proc)
781 push_extra_token (EXPECT_MUSIC);
782 else if (scm_car (s) == ly_lily_module_constant ("markup?"))
783 push_extra_token (EXPECT_MARKUP);
784 else if (ly_is_procedure (scm_car (s)))
785 push_extra_token (EXPECT_SCM);
786 else programming_error ("Function parameter without type-checking predicate");
788 return MUSIC_FUNCTION;
791 if (sid != SCM_UNDEFINED)
794 return identifier_type (sid);
797 string msg (_f ("unknown escaped string: `\\%s'", str));
798 LexerError (msg.c_str ());
800 yylval.scm = ly_string2scm (str);
806 Lily_lexer::scan_bare_word (string str)
808 SCM sym = ly_symbol2scm (str.c_str ());
809 if ((YYSTATE == notes) || (YYSTATE == chords)) {
810 SCM handle = SCM_BOOL_F;
811 if (scm_is_pair (pitchname_tab_stack_))
812 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
814 if (scm_is_pair (handle)) {
815 yylval.scm = scm_cdr (handle);
816 if (unsmob_pitch (yylval.scm))
817 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
818 else if (scm_is_symbol (yylval.scm))
821 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
823 yylval.scm = scm_cdr (handle);
824 return CHORD_MODIFIER;
828 yylval.scm = ly_string2scm (str);
833 Lily_lexer::get_state () const
835 if (YY_START == extratoken)
836 return hidden_state_;
842 Lily_lexer::is_note_state () const
844 return get_state () == notes;
848 Lily_lexer::is_chord_state () const
850 return get_state () == chords;
854 Lily_lexer::is_lyric_state () const
856 return get_state () == lyrics;
860 Lily_lexer::is_figure_state () const
862 return get_state () == figures;
866 urg, belong to string (_convert)
867 and should be generalised
870 strip_leading_white (string&s)
873 for (; i < s.length (); i++)
881 strip_trailing_white (string&s)
883 ssize i = s.length ();
888 s = s.substr (0, i + 1);
893 Lilypond_version oldest_version ("2.7.38");
897 is_valid_version (string s)
899 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
900 Lilypond_version ver (s);
901 if (int (ver) < oldest_version)
903 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
904 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
910 non_fatal_error (_f ("program too old: %s (file requires: %s)", current.to_string (), ver.to_string ()));
921 lyric_fudge (string s)
923 char *chars = string_copy (s);
925 for (char *p = chars; *p ; p++)
927 if (*p == '_' && (p == chars || *(p-1) != '\\'))
935 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
937 * (((char*)s.c_str ()) + i + 1) = 'c';
938 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
945 Convert "NUM/DEN" into a '(NUM . DEN) cons.
948 scan_fraction (string frac)
950 ssize i = frac.find ('/');
951 string left = frac.substr (0, i);
952 string right = frac.substr (i + 1, (frac.length () - i + 1));
954 int n = String_convert::dec2int (left);
955 int d = String_convert::dec2int (right);
956 return scm_cons (scm_from_int (n), scm_from_int (d));
960 lookup_markup_command (string s)
962 SCM proc = ly_lily_module_constant ("lookup-markup-command");
963 return scm_call_1 (proc, ly_string2scm (s));
967 lookup_markup_list_command (string s)
969 SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
970 return scm_call_1 (proc, ly_string2scm (s));
973 /* Shut up lexer warnings. */
993 silence_lexer_warnings ()
995 (void) yy_start_stack_ptr;
996 (void) yy_start_stack_depth;
997 (void) yy_start_stack;
998 (void) yy_push_state;
1000 (void) yy_top_state;
1001 (void) silence_lexer_warnings;