1 %{ // -*-Fundamental-*-
3 lexer.ll -- implement the Flex lexer
5 source file of the LilyPond music typesetter
7 (c) 1996--2004 Han-Wen Nienhuys <hanwen@cs.uu.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 :-)
29 /* Flex >= 2.5.29 fix; FlexLexer.h's multiple include bracing breaks
30 when building the actual lexer. */
38 #include "music-function.hh"
39 #include "source-file.hh"
40 #include "parse-scm.hh"
41 #include "lily-guile.hh"
43 #include "string-convert.hh"
44 #include "lily-lexer.hh"
45 #include "interval.hh"
46 #include "lily-guile.hh"
51 #include "lilypond-input-version.hh"
52 #include "context-def.hh"
53 #include "identifier-smob.hh"
58 #define isatty HORRIBLEKLUDGE
60 void strip_trailing_white (String&);
61 void strip_leading_white (String&);
62 String lyric_fudge (String s);
63 int music_function_type (SCM);
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
79 #define YY_USER_ACTION add_lexed_char (YYLeng ());
82 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
87 SCM scan_fraction (String);
88 SCM (* scm_parse_error_handler) (void *);
98 %option yyclass="Lily_lexer"
100 %option never-interactive
122 NATIONAL [\001-\006\021-\027\031\036\200-\377]
123 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
131 REAL ({INT}\.{N}*)|(-?\.{N}+)
134 HORIZONTALWHITE [ \t]
138 MARKUPCOMMAND \\({A}|[-_])+
139 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
147 // windows-suck-suck-suck
150 <INITIAL,chords,incl,markup,lyrics,notes,figures>{
152 yy_push_state (longcomment);
156 %[^{\n\r] { // backup rule
167 <INITIAL,lyrics,notes>\\encoding{WHITE}* {
168 yy_push_state (encoding);
170 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
171 yy_push_state (version);
173 <INITIAL,chords,lyrics,notes,figures>\\renameinput{WHITE}* {
174 yy_push_state (renameinput);
176 <encoding>\"[^"]*\" {
177 String s (YYText () + 1);
178 s = s.left_string (s.index_last ('\"'));
182 <version>\"[^"]*\" { /* got the version number */
183 String s (YYText () + 1);
184 s = s.left_string (s.index_last ('\"'));
187 if (!is_valid_version (s))
190 <renameinput>\"[^"]*\" {
191 String s (YYText () + 1);
192 s = s.left_string (s.index_last ('\"'));
195 this->here_input().source_file_->name_ = s;
196 progress_indication (_f ("Renaming input to: `%s'", s.to_str0 ()));
197 progress_indication ("\n");
198 scm_module_define (scm_car (scopes_),
199 ly_symbol2scm ("input-file-name"),
200 scm_makfrom0str (s.to_str0 ()));
204 LexerError (_ ("No quoted string found after \\encoding").to_str0 ());
208 LexerError (_ ("No quoted string found after \\version").to_str0 ());
212 LexerError (_ ("No quoted string found after \\renameinput").to_str0 ());
225 LexerError (_ ("EOF found inside a comment").to_str0 ());
226 main_input_b_ = false;
227 if (! close_input ())
228 yyterminate (); // can't move this, since it actually rets a YY_NULL
233 <INITIAL,chords,lyrics,notes,figures>\\maininput {
237 main_input_b_ = true;
240 error (_ ("\\maininput not allowed outside init files"));
243 <INITIAL,chords,lyrics,figures,notes>\\include {
244 yy_push_state (incl);
246 <incl>\"[^"]*\";? { /* got the include file name */
247 /* FIXME: semicolon? */
248 String s (YYText ()+1);
249 s = s.left_string (s.index_last ('"'));
251 new_input (s, sources_);
254 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
255 /* FIXME: semicolon? */
256 String s = YYText () + 1;
257 strip_trailing_white (s);
258 if (s.length () && (s[s.length () - 1] == ';'))
259 s = s.left_string (s.length () - 1);
261 SCM sid = lookup_identifier (s);
262 if (scm_is_string (sid)) {
263 new_input (ly_scm2string (sid), sources_);
266 String msg (_f ("wrong or undefined identifier: `%s'", s ));
268 LexerError (msg.to_str0 ());
269 SCM err = scm_current_error_port ();
270 scm_puts ("This value was found in the table: ", err);
271 scm_display (sid, err);
274 <incl>\"[^"]* { // backup rule
275 error (_ ("Missing end quote"));
278 <chords,notes,figures>{RESTNAME} {
279 const char *s = YYText ();
280 yylval.scm = scm_makfrom0str (s);
283 <chords,notes,figures>R {
284 return MULTI_MEASURE_REST;
286 <INITIAL,markup,chords,lyrics,notes,figures># { //embedded scm
287 //char const* s = YYText () + 1;
288 char const* s = here_str0 ();
290 SCM sval = ly_parse_scm (s, &n, here_input (),
291 safe_global_b && main_input_b_);
293 if (sval == SCM_UNDEFINED)
295 sval = SCM_UNSPECIFIED;
299 for (int i=0; i < n; i++)
303 char_count_stack_.top () += n;
305 if (unpack_identifier (sval) != SCM_UNDEFINED)
307 yylval.scm = unpack_identifier(sval);
308 return identifier_type (yylval.scm);
314 <INITIAL,notes,lyrics>{
316 return DOUBLE_ANGLE_OPEN;
319 return DOUBLE_ANGLE_CLOSE;
336 return scan_bare_word (YYText ());
340 return scan_escaped_word (YYText () + 1);
343 yylval.scm = scan_fraction (YYText ());
348 yylval.i = String_convert::dec2int (String (YYText ()));
352 yylval.i = String_convert::dec2int (String (YYText ()));
356 yylval.i = String_convert::dec2int (String (YYText () +1));
370 *yylval.string += to_string (escaped_char (YYText ()[1]));
373 *yylval.string += YYText ();
379 /* yylval is union. Must remember STRING before setting SCM*/
380 String *sp = yylval.string;
381 yylval.scm = scm_makfrom0str (sp->to_str0 ());
386 *yylval.string += YYText ();
391 *yylval.string += to_string (escaped_char (YYText ()[1]));
394 *yylval.string += YYText ();
400 /* yylval is union. Must remember STRING before setting SCM*/
401 String *sp = yylval.string;
402 yylval.scm = scm_makfrom0str (sp->to_str0 ());
404 return LYRICS_STRING;
407 *yylval.string += YYText ();
413 start_lyric_quote ();
416 yylval.scm = scan_fraction (YYText ());
420 yylval.i = String_convert::dec2int (String (YYText ()));
424 return scan_escaped_word (YYText () + 1);
428 String s (YYText ());
430 return yylval.i = EXTENDER;
432 return yylval.i = HYPHEN;
435 char c = s[s.length () - 1];
436 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
437 here_input ().warning (
438 _ ("Brace found at end of lyric. Did you forget a space?"));
439 yylval.scm = scm_makfrom0str (s.to_str0 ());
442 return LYRICS_STRING;
450 return scan_bare_word (YYText ());
453 return scan_escaped_word (YYText () + 1);
456 yylval.scm = scan_fraction (YYText ());
460 yylval.i = String_convert::dec2int (String (YYText ()));
501 String str (YYText () + 1);
502 SCM s = lookup_markup_command (str);
504 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
505 yylval.scm = scm_car(s);
506 SCM tag = scm_cdr(s);
507 if (tag == ly_symbol2scm("markup0"))
508 return MARKUP_HEAD_MARKUP0;
509 if (tag == ly_symbol2scm("empty"))
510 return MARKUP_HEAD_EMPTY;
511 else if (tag == ly_symbol2scm ("markup0-markup1"))
512 return MARKUP_HEAD_MARKUP0_MARKUP1;
513 else if (tag == ly_symbol2scm ("markup-list0"))
514 return MARKUP_HEAD_LIST0;
515 else if (tag == ly_symbol2scm ("scheme0"))
516 return MARKUP_HEAD_SCM0;
517 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
518 return MARKUP_HEAD_SCM0_SCM1;
519 else if (tag == ly_symbol2scm ("scheme0-markup1"))
520 return MARKUP_HEAD_SCM0_MARKUP1;
521 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
522 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
523 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
524 return MARKUP_HEAD_SCM0_SCM1_SCM2;
526 programming_error ("No parser tag defined for this signature. Abort");
531 return scan_escaped_word (str);
536 [^#{}"\\ \t\n\r\f]+ {
537 String s (YYText ());
539 char c = s[s.length () - 1];
540 /* brace open is for not confusing dumb tools. */
541 if (c == '{' || c == '}')
542 here_input ().warning (
543 _ ("Brace found at end of markup. Did you forget a space?"));
544 yylval.scm = scm_makfrom0str (s.to_str0 ());
557 main_input_b_ = false;
559 /* Returns YY_NULL */
562 else if (!close_input ())
563 /* Returns YY_NULL */
569 return scan_bare_word (YYText ());
572 return scan_escaped_word (YYText () + 1);
576 int cnv=sscanf (YYText (), "%lf", &r);
579 yylval.scm = scm_make_real (r);
584 yylval.i = String_convert::dec2int (String (YYText ()));
594 char c = YYText ()[0];
599 <INITIAL,notes,figures>. {
603 <INITIAL,lyrics,notes,figures>\\. {
604 char c= YYText ()[1];
612 return E_EXCLAMATION;
620 return E_RIGHTSQUARE;
632 String msg = _f ("invalid character: `%c'", YYText ()[0]);
633 LexerError (msg.to_str0 ());
640 Lily_lexer::push_chord_state (SCM tab)
642 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
643 yy_push_state (chords);
647 Lily_lexer::push_figuredbass_state ()
649 yy_push_state (figures);
653 Lily_lexer::push_initial_state ()
655 yy_push_state (INITIAL);
659 Lily_lexer::push_lyric_state ()
661 yy_push_state (lyrics);
665 Lily_lexer::push_markup_state ()
667 yy_push_state (markup);
671 Lily_lexer::push_note_state (SCM tab)
673 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
674 yy_push_state (notes);
678 Lily_lexer::pop_state ()
680 if (YYSTATE == notes || YYSTATE == chords)
681 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
686 Lily_lexer::identifier_type (SCM sid)
688 int k = try_special_identifiers (&yylval.scm , sid);
689 return k >= 0 ? k : SCM_IDENTIFIER;
694 Lily_lexer::scan_escaped_word (String str)
696 // use more SCM for this.
698 // SCM sym = ly_symbol2scm (str.to_str0 ());
700 int l = lookup_keyword (str);
704 SCM sid = lookup_identifier (str);
705 if (is_music_function (sid))
707 yylval.scm = get_music_function_transform (sid);
709 return music_function_type (yylval.scm);
712 if (sid != SCM_UNDEFINED)
715 return identifier_type (sid);
718 String msg (_f ("unknown escaped string: `\\%s'", str));
719 LexerError (msg.to_str0 ());
721 yylval.scm = scm_makfrom0str (str.to_str0 ());
727 Lily_lexer::scan_bare_word (String str)
729 SCM sym = ly_symbol2scm (str.to_str0 ());
730 if ((YYSTATE == notes) || (YYSTATE == chords)) {
731 SCM handle = SCM_BOOL_F;
732 if (scm_is_pair (pitchname_tab_stack_))
733 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
735 if (scm_is_pair (handle)) {
736 yylval.scm = scm_cdr (handle);
737 if (unsmob_pitch (yylval.scm))
738 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
739 else if (scm_is_symbol (yylval.scm))
742 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
744 yylval.scm = scm_cdr (handle);
745 return CHORD_MODIFIER;
749 yylval.scm = scm_makfrom0str (str.to_str0 ());
754 Lily_lexer::is_note_state () const
756 return YY_START == notes;
760 Lily_lexer::is_chord_state () const
762 return YY_START == chords;
766 Lily_lexer::is_lyric_state () const
768 return YY_START == lyrics;
772 Lily_lexer::is_figure_state () const
774 return YY_START == figures;
778 urg, belong to String (_convert)
779 and should be generalised
782 strip_leading_white (String&s)
785 for (; i < s.length (); i++)
789 s = s.nomid_string (0, i);
793 strip_trailing_white (String&s)
800 s = s.left_string (i+1);
805 /* 2.1.2x something -> \property -> \set. */
806 Lilypond_version oldest_version ("2.3.22");
810 is_valid_version (String s)
812 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
813 Lilypond_version ver (s);
814 if (! ((ver >= oldest_version) && (ver <= current)))
816 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
817 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
828 lyric_fudge (String s)
830 char * chars =s.get_copy_str0 ();
832 for (char * p = chars; *p ; p++)
834 if (*p == '_' && (p == chars || *(p-1) != '\\'))
842 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
844 * (s.get_str0 () + i + 1) = 'c';
845 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
852 Convert "NUM/DEN" into a '(NUM . DEN) cons.
855 scan_fraction (String frac)
857 int i = frac.index ('/');
858 int l = frac.length ();
859 String left = frac.left_string (i);
860 String right = frac.right_string (l - i - 1);
862 int n = String_convert::dec2int (left);
863 int d = String_convert::dec2int (right);
864 return scm_cons (scm_int2num (n), scm_int2num (d));
867 // Breaks for flex 2.5.31
869 /* avoid silly flex induced gcc warnings */
870 static void yy_push_state (int) {;}
871 static void yy_pop_state () {;}
872 static int yy_top_state () { return 0; }
875 avoid_silly_flex_induced_gcc_warnings ()
877 (void)yy_start_stack_ptr;
878 (void)yy_start_stack_depth;
879 (void)yy_start_stack;
883 avoid_silly_flex_induced_gcc_warnings ();
888 lookup_markup_command (String s)
890 SCM proc = ly_scheme_function ("lookup-markup-command");
891 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));
896 music_function_type (SCM func)
898 SCM type= scm_object_property (func, ly_symbol2scm ("music-function-signature-keyword"));
899 if (type == ly_symbol2scm ("scm"))
901 return MUSIC_FUNCTION_SCM;
903 else if (type == ly_symbol2scm ("music"))
905 return MUSIC_FUNCTION_MUSIC;
907 else if (type == ly_symbol2scm ("scm-music"))
909 return MUSIC_FUNCTION_SCM_MUSIC;
911 else if (type == ly_symbol2scm ("scm-scm"))
913 return MUSIC_FUNCTION_SCM_SCM;
915 else if (type == ly_symbol2scm ("music-music"))
917 return MUSIC_FUNCTION_MUSIC_MUSIC;
919 else if (type == ly_symbol2scm ("scm-music-music"))
921 return MUSIC_FUNCTION_SCM_MUSIC_MUSIC;
923 else if (type == ly_symbol2scm ("scm-scm-music"))
925 return MUSIC_FUNCTION_SCM_SCM_MUSIC;
927 else if (type == ly_symbol2scm ("markup"))
929 return MUSIC_FUNCTION_MARKUP;
931 else if (type == ly_symbol2scm ("markup-music"))
933 return MUSIC_FUNCTION_MARKUP_MUSIC;
935 else if (type == ly_symbol2scm ("markup-markup"))
937 return MUSIC_FUNCTION_MARKUP_MARKUP;
939 else if (type == ly_symbol2scm ("markup-music-music"))
941 return MUSIC_FUNCTION_MARKUP_MUSIC_MUSIC;
943 else if (type == ly_symbol2scm ("markup-markup-music"))
945 return MUSIC_FUNCTION_MARKUP_MARKUP_MUSIC;
947 else if (type == ly_symbol2scm ("noarg"))
949 return MUSIC_FUNCTION;
953 /* TODO: print location */
954 error ("Can not find signature for music function.");
957 return MUSIC_FUNCTION_SCM;