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 (std::string&);
61 void strip_leading_white (std::string&);
62 std::string lyric_fudge (std::string s);
63 int music_function_type (SCM);
64 SCM lookup_markup_command (std::string s);
65 bool is_valid_version (std::string s);
68 #define start_quote() \
69 yy_push_state (quote);\
70 yylval.string = new std::string
72 #define start_lyric_quote() \
73 yy_push_state (lyric_quote);\
74 yylval.string = new std::string
82 #define YY_USER_ACTION add_lexed_char (YYLeng ());
85 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
90 SCM scan_fraction (std::string);
91 SCM (* scm_parse_error_handler) (void *);
101 %option yyclass="Lily_lexer"
103 %option never-interactive
124 NATIONAL [\001-\006\021-\027\031\036\200-\377]
125 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
127 DASHED_WORD {A}({AN}|-)*
128 DASHED_KEY_WORD \\{DASHED_WORD}
135 REAL ({INT}\.{N}*)|(-?\.{N}+)
138 HORIZONTALWHITE [ \t]
142 MARKUPCOMMAND \\({A}|[-_])+
143 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
147 BOM_UTF8 \357\273\277
152 // windows-suck-suck-suck
155 <INITIAL,chords,lyrics,figures,notes>{BOM_UTF8} {
156 if (this->lexloc->line_number () != 1 || this->lexloc->column_number () != 0)
158 LexerError (_ ("stray UTF-8 BOM encountered").c_str ());
161 if (be_verbose_global)
162 message (_ ("Skipping UTF-8 BOM"));
165 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
167 yy_push_state (longcomment);
171 %[^{\n\r] { // backup rule
182 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
183 yy_push_state (version);
185 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}* {
186 yy_push_state (sourcefilename);
188 <version>\"[^"]*\" { /* got the version number */
189 std::string s (YYText () + 1);
190 s = s.substr (0, s.rfind ('\"'));
193 if (!is_valid_version (s))
196 SCM top_scope = scm_car (scm_last_pair (scopes_));
197 scm_module_define (top_scope, ly_symbol2scm ("version-seen?"), SCM_BOOL_T);
200 <sourcefilename>\"[^"]*\" {
201 std::string s (YYText () + 1);
202 s = s.substr (0, s.rfind ('\"'));
205 this->here_input().get_source_file ()->name_ = s;
206 message (_f ("Renaming input to: `%s'", s.c_str ()));
207 progress_indication ("\n");
208 scm_module_define (scm_car (scopes_),
209 ly_symbol2scm ("input-file-name"),
210 scm_makfrom0str (s.c_str ()));
214 LexerError (_ ("quoted string expected after \\version").c_str ());
218 LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
231 LexerError (_ ("EOF found inside a comment").c_str ());
232 is_main_input_ = false;
233 if (! close_input ())
234 yyterminate (); // can't move this, since it actually rets a YY_NULL
239 <INITIAL,chords,lyrics,notes,figures>\\maininput {
243 is_main_input_ = true;
246 error (_ ("\\maininput not allowed outside init files"));
249 <INITIAL,chords,lyrics,figures,notes>\\include {
250 yy_push_state (incl);
252 <incl>\"[^"]*\" { /* got the include file name */
253 std::string s (YYText ()+1);
254 s = s.substr (0, s.rfind ('"'));
256 new_input (s, sources_);
259 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
260 std::string s = YYText () + 1;
261 strip_trailing_white (s);
262 if (s.length () && (s[s.length () - 1] == ';'))
263 s = s.substr (0, s.length () - 1);
265 SCM sid = lookup_identifier (s);
266 if (scm_is_string (sid)) {
267 new_input (ly_scm2string (sid), sources_);
270 std::string msg (_f ("wrong or undefined identifier: `%s'", s ));
272 LexerError (msg.c_str ());
273 SCM err = scm_current_error_port ();
274 scm_puts ("This value was found in the table: ", err);
275 scm_display (sid, err);
278 <incl>\"[^"]* { // backup rule
279 error (_ ("end quote missing"));
282 <chords,notes,figures>{RESTNAME} {
283 char const *s = YYText ();
284 yylval.scm = scm_makfrom0str (s);
287 <chords,notes,figures>R {
288 return MULTI_MEASURE_REST;
290 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
292 Input hi = here_input();
294 SCM sval = ly_parse_scm (hi.start (), &n, hi,
295 be_safe_global && is_main_input_);
297 if (sval == SCM_UNDEFINED)
299 sval = SCM_UNSPECIFIED;
303 for (int i = 0; i < n; i++)
307 char_count_stack_.back () += n;
309 if (unpack_identifier (sval) != SCM_UNDEFINED)
311 yylval.scm = unpack_identifier(sval);
312 return identifier_type (yylval.scm);
318 <INITIAL,notes,lyrics>{
320 return DOUBLE_ANGLE_OPEN;
323 return DOUBLE_ANGLE_CLOSE;
340 return scan_bare_word (YYText ());
344 return scan_escaped_word (YYText () + 1);
347 yylval.scm = scan_fraction (YYText ());
352 yylval.i = String_convert::dec2int (std::string (YYText ()));
356 yylval.i = String_convert::dec2int (std::string (YYText ()));
360 yylval.i = String_convert::dec2int (std::string (YYText () +1));
373 *yylval.string += to_string (escaped_char (YYText ()[1]));
376 *yylval.string += YYText ();
382 /* yylval is union. Must remember STRING before setting SCM*/
383 std::string *sp = yylval.string;
384 yylval.scm = scm_makfrom0str (sp->c_str ());
389 *yylval.string += YYText ();
394 *yylval.string += to_string (escaped_char (YYText ()[1]));
397 *yylval.string += YYText ();
403 /* yylval is union. Must remember STRING before setting SCM*/
404 std::string *sp = yylval.string;
405 yylval.scm = scm_makfrom0str (sp->c_str ());
407 return LYRICS_STRING;
410 *yylval.string += YYText ();
416 start_lyric_quote ();
419 yylval.scm = scan_fraction (YYText ());
423 yylval.i = String_convert::dec2int (std::string (YYText ()));
427 return scan_escaped_word (YYText () + 1);
431 std::string s (YYText ());
433 return yylval.i = EXTENDER;
435 return yylval.i = HYPHEN;
438 char c = s[s.length () - 1];
439 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
440 here_input ().warning (
441 _ ("Brace found at end of lyric. Did you forget a space?"));
442 yylval.scm = scm_makfrom0str (s.c_str ());
445 return LYRICS_STRING;
453 return scan_bare_word (YYText ());
456 return scan_escaped_word (YYText () + 1);
459 yylval.scm = scan_fraction (YYText ());
463 yylval.i = String_convert::dec2int (std::string (YYText ()));
498 std::string str (YYText () + 1);
499 SCM s = lookup_markup_command (str);
501 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
502 yylval.scm = scm_car(s);
503 SCM tag = scm_cdr(s);
504 if (tag == ly_symbol2scm("markup0"))
505 return MARKUP_HEAD_MARKUP0;
506 if (tag == ly_symbol2scm("empty"))
507 return MARKUP_HEAD_EMPTY;
508 else if (tag == ly_symbol2scm ("markup0-markup1"))
509 return MARKUP_HEAD_MARKUP0_MARKUP1;
510 else if (tag == ly_symbol2scm ("markup-list0"))
511 return MARKUP_HEAD_LIST0;
512 else if (tag == ly_symbol2scm ("scheme0"))
513 return MARKUP_HEAD_SCM0;
514 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
515 return MARKUP_HEAD_SCM0_SCM1;
516 else if (tag == ly_symbol2scm ("scheme0-markup1"))
517 return MARKUP_HEAD_SCM0_MARKUP1;
518 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
519 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
520 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
521 return MARKUP_HEAD_SCM0_SCM1_SCM2;
523 programming_error ("no parser tag defined for this markup signature");
528 return scan_escaped_word (str);
533 [^#{}"\\ \t\n\r\f]+ {
534 std::string s (YYText ());
536 char c = s[s.length () - 1];
537 /* brace open is for not confusing dumb tools. */
538 if (c == '{' || c == '}')
539 here_input ().warning (
540 _ ("Brace found at end of markup. Did you forget a space?"));
541 yylval.scm = scm_makfrom0str (s.c_str ());
554 is_main_input_ = false;
556 /* Returns YY_NULL */
559 else if (!close_input ())
560 /* Returns YY_NULL */
566 return scan_bare_word (YYText ());
569 return scan_escaped_word (YYText () + 1);
574 return scan_bare_word (YYText ());
577 return scan_escaped_word (YYText () + 1);
581 int cnv = sscanf (YYText (), "%lf", &r);
585 yylval.scm = scm_from_double (r);
590 yylval.i = String_convert::dec2int (std::string (YYText ()));
600 char c = YYText ()[0];
605 <INITIAL,notes,figures>. {
609 <INITIAL,lyrics,notes,figures>\\. {
610 char c = YYText ()[1];
614 return E_ANGLE_CLOSE;
618 return E_EXCLAMATION;
624 return E_BRACKET_OPEN;
628 return E_BRACKET_CLOSE;
640 std::string msg = _f ("invalid character: `%c'", YYText ()[0]);
641 LexerError (msg.c_str ());
648 Lily_lexer::push_chord_state (SCM tab)
650 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
651 yy_push_state (chords);
655 Lily_lexer::push_figuredbass_state ()
657 yy_push_state (figures);
661 Lily_lexer::push_initial_state ()
663 yy_push_state (INITIAL);
667 Lily_lexer::push_lyric_state ()
669 yy_push_state (lyrics);
673 Lily_lexer::push_markup_state ()
675 yy_push_state (markup);
679 Lily_lexer::push_note_state (SCM tab)
681 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
682 yy_push_state (notes);
686 Lily_lexer::pop_state ()
688 if (YYSTATE == notes || YYSTATE == chords)
689 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
694 Lily_lexer::identifier_type (SCM sid)
696 int k = try_special_identifiers (&yylval.scm , sid);
697 return k >= 0 ? k : SCM_IDENTIFIER;
702 Lily_lexer::scan_escaped_word (std::string str)
704 // use more SCM for this.
706 // SCM sym = ly_symbol2scm (str.c_str ());
708 int i = lookup_keyword (str);
709 if (i == MARKUP && is_lyric_state ())
714 SCM sid = lookup_identifier (str);
715 if (is_music_function (sid))
717 yylval.scm = get_music_function_transform (sid);
718 return music_function_type (yylval.scm);
721 if (sid != SCM_UNDEFINED)
724 return identifier_type (sid);
727 std::string msg (_f ("unknown escaped string: `\\%s'", str));
728 LexerError (msg.c_str ());
730 yylval.scm = scm_makfrom0str (str.c_str ());
736 Lily_lexer::scan_bare_word (std::string str)
738 SCM sym = ly_symbol2scm (str.c_str ());
739 if ((YYSTATE == notes) || (YYSTATE == chords)) {
740 SCM handle = SCM_BOOL_F;
741 if (scm_is_pair (pitchname_tab_stack_))
742 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
744 if (scm_is_pair (handle)) {
745 yylval.scm = scm_cdr (handle);
746 if (unsmob_pitch (yylval.scm))
747 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
748 else if (scm_is_symbol (yylval.scm))
751 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
753 yylval.scm = scm_cdr (handle);
754 return CHORD_MODIFIER;
758 yylval.scm = scm_makfrom0str (str.c_str ());
763 Lily_lexer::is_note_state () const
765 return YY_START == notes;
769 Lily_lexer::is_chord_state () const
771 return YY_START == chords;
775 Lily_lexer::is_lyric_state () const
777 return YY_START == lyrics;
781 Lily_lexer::is_figure_state () const
783 return YY_START == figures;
787 urg, belong to std::string (_convert)
788 and should be generalised
791 strip_leading_white (std::string&s)
794 for (; i < s.length (); i++)
802 strip_trailing_white (std::string&s)
804 ssize i = s.length ();
809 s = s.substr (0, i + 1);
814 /* 2.1.2x something -> \property -> \set. */
815 Lilypond_version oldest_version ("2.3.22");
819 is_valid_version (std::string s)
821 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
822 Lilypond_version ver (s);
823 if (! ((ver >= oldest_version) && (ver <= current)))
825 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
826 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
837 lyric_fudge (std::string s)
839 char *chars = string_copy (s);
841 for (char *p = chars; *p ; p++)
843 if (*p == '_' && (p == chars || *(p-1) != '\\'))
847 s = std::string (chars);
851 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
853 * (((char*)s.c_str ()) + i + 1) = 'c';
854 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
861 Convert "NUM/DEN" into a '(NUM . DEN) cons.
864 scan_fraction (std::string frac)
866 ssize i = frac.find ('/');
867 std::string left = frac.substr (0, i);
868 std::string right = frac.substr (i + 1, (frac.length () - i + 1));
870 int n = String_convert::dec2int (left);
871 int d = String_convert::dec2int (right);
872 return scm_cons (scm_from_int (n), scm_from_int (d));
876 lookup_markup_command (std::string s)
878 SCM proc = ly_lily_module_constant ("lookup-markup-command");
879 return scm_call_1 (proc, scm_makfrom0str (s.c_str ()));
884 music_function_type (SCM func)
886 SCM type = scm_object_property (func, ly_symbol2scm ("music-function-signature-keyword"));
887 if (type == ly_symbol2scm ("scm"))
889 return MUSIC_FUNCTION_SCM;
891 else if (type == ly_symbol2scm ("music"))
893 return MUSIC_FUNCTION_MUSIC;
895 else if (type == ly_symbol2scm ("scm-music"))
897 return MUSIC_FUNCTION_SCM_MUSIC;
899 else if (type == ly_symbol2scm ("scm-scm"))
901 return MUSIC_FUNCTION_SCM_SCM;
903 else if (type == ly_symbol2scm ("music-music"))
905 return MUSIC_FUNCTION_MUSIC_MUSIC;
907 else if (type == ly_symbol2scm ("scm-music-music"))
909 return MUSIC_FUNCTION_SCM_MUSIC_MUSIC;
911 else if (type == ly_symbol2scm ("scm-scm-music"))
913 return MUSIC_FUNCTION_SCM_SCM_MUSIC;
915 else if (type == ly_symbol2scm ("scm-scm-scm"))
917 return MUSIC_FUNCTION_SCM_SCM_SCM;
919 else if (type == ly_symbol2scm ("markup"))
921 return MUSIC_FUNCTION_MARKUP;
923 else if (type == ly_symbol2scm ("markup-music"))
925 return MUSIC_FUNCTION_MARKUP_MUSIC;
927 else if (type == ly_symbol2scm ("markup-markup"))
929 return MUSIC_FUNCTION_MARKUP_MARKUP;
931 else if (type == ly_symbol2scm ("markup-music-music"))
933 return MUSIC_FUNCTION_MARKUP_MUSIC_MUSIC;
935 else if (type == ly_symbol2scm ("markup-markup-music"))
937 return MUSIC_FUNCTION_MARKUP_MARKUP_MUSIC;
939 else if (type == ly_symbol2scm ("noarg"))
941 return MUSIC_FUNCTION;
945 /* TODO: print location */
946 error (_ ("can't find signature for music function"));
949 return MUSIC_FUNCTION_SCM;
952 /* Shut up lexer warnings. */
972 silence_lexer_warnings ()
974 (void) yy_start_stack_ptr;
975 (void) yy_start_stack_depth;
976 (void) yy_start_stack;
977 (void) yy_push_state;
980 (void) silence_lexer_warnings;