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"
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 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
127 NATIONAL [\001-\006\021-\027\031\036\200-\377]
128 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
130 DASHED_WORD {A}({AN}|-)*
131 DASHED_KEY_WORD \\{DASHED_WORD}
138 REAL ({INT}\.{N}*)|(-?\.{N}+)
141 HORIZONTALWHITE [ \t]
145 MARKUPCOMMAND \\({A}|[-_])+
146 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
150 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"),
240 scm_makfrom0str (s.c_str ()));
244 <sourcefileline>{INT} {
246 sscanf (YYText (), "%d", &i);
248 // this->set_debug (1);
250 this->here_input ().get_source_file ()->set_line (here_input ().start (), i);
254 LexerError (_ ("quoted string expected after \\version").c_str ());
258 LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
262 LexerError (_ ("integer expected after \\sourcefileline").c_str ());
275 LexerError (_ ("EOF found inside a comment").c_str ());
276 is_main_input_ = false;
277 if (! close_input ())
278 yyterminate (); // can't move this, since it actually rets a YY_NULL
283 <INITIAL,chords,lyrics,notes,figures>\\maininput {
287 is_main_input_ = true;
290 error (_ ("\\maininput not allowed outside init files"));
293 <INITIAL,chords,lyrics,figures,notes>\\include {
294 yy_push_state (incl);
296 <incl>\"[^"]*\" { /* got the include file name */
297 string s (YYText ()+1);
298 s = s.substr (0, s.rfind ('"'));
300 new_input (s, sources_);
303 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
304 string s = YYText () + 1;
305 strip_trailing_white (s);
306 if (s.length () && (s[s.length () - 1] == ';'))
307 s = s.substr (0, s.length () - 1);
309 SCM sid = lookup_identifier (s);
310 if (scm_is_string (sid)) {
311 new_input (ly_scm2string (sid), sources_);
314 string msg (_f ("wrong or undefined identifier: `%s'", s ));
316 LexerError (msg.c_str ());
317 SCM err = scm_current_error_port ();
318 scm_puts ("This value was found in the table: ", err);
319 scm_display (sid, err);
322 <incl>\"[^"]* { // backup rule
323 error (_ ("end quote missing"));
326 <chords,notes,figures>{RESTNAME} {
327 char const *s = YYText ();
328 yylval.scm = scm_makfrom0str (s);
331 <chords,notes,figures>R {
332 return MULTI_MEASURE_REST;
334 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
336 Input hi = here_input();
338 SCM sval = ly_parse_scm (hi.start (), &n, hi,
339 be_safe_global && is_main_input_);
341 if (sval == SCM_UNDEFINED)
343 sval = SCM_UNSPECIFIED;
347 for (int i = 0; i < n; i++)
351 char_count_stack_.back () += n;
353 if (unpack_identifier (sval) != SCM_UNDEFINED)
355 yylval.scm = unpack_identifier(sval);
356 return identifier_type (yylval.scm);
362 <INITIAL,notes,lyrics>{
364 return DOUBLE_ANGLE_OPEN;
367 return DOUBLE_ANGLE_CLOSE;
394 return scan_bare_word (YYText ());
398 return scan_escaped_word (YYText () + 1);
401 yylval.scm = scan_fraction (YYText ());
406 yylval.i = String_convert::dec2int (string (YYText ()));
410 yylval.i = String_convert::dec2int (string (YYText ()));
414 yylval.i = String_convert::dec2int (string (YYText () +1));
421 *yylval.string += to_string (escaped_char (YYText ()[1]));
424 *yylval.string += YYText ();
430 /* yylval is union. Must remember STRING before setting SCM*/
431 string *sp = yylval.string;
432 yylval.scm = scm_makfrom0str (sp->c_str ());
434 return is_lyric_state () ? LYRICS_STRING : STRING;
437 *yylval.string += YYText ();
443 start_lyric_quote ();
446 yylval.scm = scan_fraction (YYText ());
450 yylval.i = String_convert::dec2int (string (YYText ()));
454 return scan_escaped_word (YYText () + 1);
458 string s (YYText ());
460 return yylval.i = EXTENDER;
462 return yylval.i = HYPHEN;
465 char c = s[s.length () - 1];
466 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
467 here_input ().warning (
468 _ ("Brace found at end of lyric. Did you forget a space?"));
469 yylval.scm = scm_makfrom0str (s.c_str ());
472 return LYRICS_STRING;
480 return scan_bare_word (YYText ());
483 return scan_escaped_word (YYText () + 1);
486 yylval.scm = scan_fraction (YYText ());
490 yylval.i = String_convert::dec2int (string (YYText ()));
519 string str (YYText () + 1);
520 SCM s = lookup_markup_command (str);
522 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
523 yylval.scm = scm_car(s);
524 SCM tag = scm_cdr(s);
525 if (tag == ly_symbol2scm("markup0"))
526 return MARKUP_HEAD_MARKUP0;
527 if (tag == ly_symbol2scm("empty"))
528 return MARKUP_HEAD_EMPTY;
529 else if (tag == ly_symbol2scm ("markup0-markup1"))
530 return MARKUP_HEAD_MARKUP0_MARKUP1;
531 else if (tag == ly_symbol2scm ("markup-list0"))
532 return MARKUP_HEAD_LIST0;
533 else if (tag == ly_symbol2scm ("scheme0"))
534 return MARKUP_HEAD_SCM0;
535 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
536 return MARKUP_HEAD_SCM0_SCM1;
537 else if (tag == ly_symbol2scm ("scheme0-markup1"))
538 return MARKUP_HEAD_SCM0_MARKUP1;
539 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
540 return MARKUP_HEAD_SCM0_SCM1_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 = scm_makfrom0str (s.c_str ());
575 is_main_input_ = false;
577 /* Returns YY_NULL */
580 else if (!close_input ())
581 /* Returns YY_NULL */
587 return scan_bare_word (YYText ());
590 return scan_escaped_word (YYText () + 1);
595 return scan_bare_word (YYText ());
598 return scan_escaped_word (YYText () + 1);
602 int cnv = sscanf (YYText (), "%lf", &r);
606 yylval.scm = scm_from_double (r);
611 yylval.i = String_convert::dec2int (string (YYText ()));
621 char c = YYText ()[0];
626 <INITIAL,notes,figures>. {
630 <INITIAL,lyrics,notes,figures>\\. {
631 char c = YYText ()[1];
635 return E_ANGLE_CLOSE;
639 return E_EXCLAMATION;
645 return E_BRACKET_OPEN;
649 return E_BRACKET_CLOSE;
661 string msg = _f ("invalid character: `%c'", YYText ()[0]);
662 LexerError (msg.c_str ());
668 /* Make the lexer generate a token of the given type as the next token.
669 TODO: make it possible to define a value for the token as well */
671 Lily_lexer::push_extra_token (int token_type)
673 if (extra_token_types_.empty ())
675 if (YY_START != extratoken)
676 hidden_state_ = YY_START;
677 yy_push_state (extratoken);
679 extra_token_types_.push_back (token_type);
683 Lily_lexer::push_chord_state (SCM tab)
685 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
686 yy_push_state (chords);
690 Lily_lexer::push_figuredbass_state ()
692 yy_push_state (figures);
696 Lily_lexer::push_initial_state ()
698 yy_push_state (INITIAL);
702 Lily_lexer::push_lyric_state ()
704 yy_push_state (lyrics);
708 Lily_lexer::push_markup_state ()
710 yy_push_state (markup);
714 Lily_lexer::push_note_state (SCM tab)
716 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
717 yy_push_state (notes);
721 Lily_lexer::pop_state ()
723 if (YYSTATE == notes || YYSTATE == chords)
724 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
730 Lily_lexer::identifier_type (SCM sid)
732 int k = try_special_identifiers (&yylval.scm , sid);
733 return k >= 0 ? k : SCM_IDENTIFIER;
738 Lily_lexer::scan_escaped_word (string str)
740 // use more SCM for this.
742 // SCM sym = ly_symbol2scm (str.c_str ());
744 int i = lookup_keyword (str);
745 if (i == MARKUP && is_lyric_state ())
750 SCM sid = lookup_identifier (str);
751 if (is_music_function (sid))
753 yylval.scm = get_music_function_transform (sid);
755 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
756 for (; scm_is_pair (s); s = scm_cdr (s))
758 if (scm_car (s) == ly_music_p_proc)
759 push_extra_token (EXPECT_MUSIC);
760 else if (scm_car (s) == ly_lily_module_constant ("markup?"))
761 push_extra_token (EXPECT_MARKUP);
762 else if (ly_is_procedure (scm_car (s)))
763 push_extra_token (EXPECT_SCM);
764 else programming_error ("Function parameter without type-checking predicate");
766 return MUSIC_FUNCTION;
769 if (sid != SCM_UNDEFINED)
772 return identifier_type (sid);
775 string msg (_f ("unknown escaped string: `\\%s'", str));
776 LexerError (msg.c_str ());
778 yylval.scm = scm_makfrom0str (str.c_str ());
784 Lily_lexer::scan_bare_word (string str)
786 SCM sym = ly_symbol2scm (str.c_str ());
787 if ((YYSTATE == notes) || (YYSTATE == chords)) {
788 SCM handle = SCM_BOOL_F;
789 if (scm_is_pair (pitchname_tab_stack_))
790 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
792 if (scm_is_pair (handle)) {
793 yylval.scm = scm_cdr (handle);
794 if (unsmob_pitch (yylval.scm))
795 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
796 else if (scm_is_symbol (yylval.scm))
799 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
801 yylval.scm = scm_cdr (handle);
802 return CHORD_MODIFIER;
806 yylval.scm = scm_makfrom0str (str.c_str ());
811 Lily_lexer::get_state () const
813 if (YY_START == extratoken)
814 return hidden_state_;
820 Lily_lexer::is_note_state () const
822 return get_state () == notes;
826 Lily_lexer::is_chord_state () const
828 return get_state () == chords;
832 Lily_lexer::is_lyric_state () const
834 return get_state () == lyrics;
838 Lily_lexer::is_figure_state () const
840 return get_state () == figures;
844 urg, belong to string (_convert)
845 and should be generalised
848 strip_leading_white (string&s)
851 for (; i < s.length (); i++)
859 strip_trailing_white (string&s)
861 ssize i = s.length ();
866 s = s.substr (0, i + 1);
871 Lilypond_version oldest_version ("2.7.38");
875 is_valid_version (string s)
877 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
878 Lilypond_version ver (s);
879 if (! ((ver >= oldest_version) && (ver <= current)))
881 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
882 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
893 lyric_fudge (string s)
895 char *chars = string_copy (s);
897 for (char *p = chars; *p ; p++)
899 if (*p == '_' && (p == chars || *(p-1) != '\\'))
907 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
909 * (((char*)s.c_str ()) + i + 1) = 'c';
910 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
917 Convert "NUM/DEN" into a '(NUM . DEN) cons.
920 scan_fraction (string frac)
922 ssize i = frac.find ('/');
923 string left = frac.substr (0, i);
924 string right = frac.substr (i + 1, (frac.length () - i + 1));
926 int n = String_convert::dec2int (left);
927 int d = String_convert::dec2int (right);
928 return scm_cons (scm_from_int (n), scm_from_int (d));
932 lookup_markup_command (string s)
934 SCM proc = ly_lily_module_constant ("lookup-markup-command");
935 return scm_call_1 (proc, scm_makfrom0str (s.c_str ()));
938 /* Shut up lexer warnings. */
958 silence_lexer_warnings ()
960 (void) yy_start_stack_ptr;
961 (void) yy_start_stack_depth;
962 (void) yy_start_stack;
963 (void) yy_push_state;
966 (void) silence_lexer_warnings;