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. */
35 #include "music-function.hh"
36 #include "source-file.hh"
37 #include "parse-scm.hh"
38 #include "lily-guile.hh"
40 #include "string-convert.hh"
41 #include "lily-lexer.hh"
42 #include "interval.hh"
43 #include "lily-guile.hh"
48 #include "lilypond-input-version.hh"
49 #include "context-def.hh"
50 #include "identifier-smob.hh"
55 #define isatty HORRIBLEKLUDGE
57 void strip_trailing_white (String&);
58 void strip_leading_white (String&);
59 String lyric_fudge (String s);
60 int music_function_type (SCM);
61 SCM lookup_markup_command (String s);
62 bool is_valid_version (String s);
65 #define start_quote() \
66 yy_push_state (quote);\
67 yylval.string = new String
69 #define start_lyric_quote() \
70 yy_push_state (lyric_quote);\
71 yylval.string = new String
76 #define YY_USER_ACTION add_lexed_char (YYLeng ());
79 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
84 SCM scan_fraction (String);
85 SCM (* scm_parse_error_handler) (void *);
95 %option yyclass="Lily_lexer"
97 %option never-interactive
119 NATIONAL [\001-\006\021-\027\031\036\200-\377]
120 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
128 REAL ({INT}\.{N}*)|(-?\.{N}+)
131 HORIZONTALWHITE [ \t]
135 MARKUPCOMMAND \\({A}|[-_])+
136 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
144 // windows-suck-suck-suck
147 <INITIAL,chords,incl,markup,lyrics,notes,figures>{
149 yy_push_state (longcomment);
153 %[^{\n\r] { // backup rule
164 <INITIAL,notes>\\encoding{WHITE}* {
165 yy_push_state (encoding);
167 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
168 yy_push_state (version);
170 <INITIAL,chords,lyrics,notes,figures>\\renameinput{WHITE}* {
171 yy_push_state (renameinput);
173 <encoding>\"[^"]*\" {
174 String s (YYText () + 1);
175 s = s.left_string (s.index_last ('\"'));
179 <version>\"[^"]*\" { /* got the version number */
180 String s (YYText () + 1);
181 s = s.left_string (s.index_last ('\"'));
184 if (!is_valid_version (s))
187 <renameinput>\"[^"]*\" {
188 String s (YYText ()+1);
189 s = s.left_string (s.index_last ('\"'));
192 this->here_input().source_file_->name_ = s;
193 progress_indication ("\n");
194 progress_indication (_f ("input renamed to: `%s'", s.to_str0 ()));
195 progress_indication ("\n");
196 scm_module_define (scm_car (scopes_),
197 ly_symbol2scm ("input-file-name"),
198 scm_makfrom0str (s.to_str0()));
202 LexerError (_ ("No quoted string found after \\encoding").to_str0 ());
206 LexerError (_ ("No quoted string found after \\version").to_str0 ());
210 LexerError (_ ("No quoted string found after \\renameinput").to_str0 ());
223 LexerError (_ ("EOF found inside a comment").to_str0 ());
224 main_input_b_ = false;
225 if (! close_input ())
226 yyterminate (); // can't move this, since it actually rets a YY_NULL
231 <INITIAL,chords,lyrics,notes,figures>\\maininput {
235 main_input_b_ = true;
238 error (_ ("\\maininput not allowed outside init files"));
241 <INITIAL,chords,lyrics,figures,notes>\\include {
242 yy_push_state (incl);
244 <incl>\"[^"]*\";? { /* got the include file name */
245 /* FIXME: semicolon? */
246 String s (YYText ()+1);
247 s = s.left_string (s.index_last ('"'));
249 new_input (s, sources_);
252 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
253 /* FIXME: semicolon? */
254 String s = YYText () + 1;
255 strip_trailing_white (s);
256 if (s.length () && (s[s.length () - 1] == ';'))
257 s = s.left_string (s.length () - 1);
259 SCM sid = lookup_identifier (s);
260 if (scm_is_string (sid)) {
261 new_input (ly_scm2string (sid), sources_);
264 String msg (_f ("wrong or undefined identifier: `%s'", s ));
266 LexerError (msg.to_str0 ());
267 SCM err = scm_current_error_port ();
268 scm_puts ("This value was found in the table: ", err);
269 scm_display (sid, err);
272 <incl>\"[^"]* { // backup rule
273 error (_ ("Missing end quote"));
276 <chords,notes,figures>{RESTNAME} {
277 const char *s = YYText ();
278 yylval.scm = scm_makfrom0str (s);
281 <chords,notes,figures>R {
282 return MULTI_MEASURE_REST;
284 <INITIAL,markup,chords,lyrics,notes,figures># { //embedded scm
285 //char const* s = YYText () + 1;
286 char const* s = here_str0 ();
288 SCM sval = ly_parse_scm (s, &n, here_input (),
289 safe_global_b && main_input_b_);
291 if (sval == SCM_UNDEFINED)
293 sval = SCM_UNSPECIFIED;
297 for (int i=0; i < n; i++)
301 char_count_stack_.top () += n;
303 if (unpack_identifier (sval) != SCM_UNDEFINED)
305 yylval.scm = unpack_identifier(sval);
306 return identifier_type (yylval.scm);
312 <INITIAL,notes,lyrics>{
314 return DOUBLE_ANGLE_OPEN;
317 return DOUBLE_ANGLE_CLOSE;
334 return scan_bare_word (YYText ());
338 return scan_escaped_word (YYText () + 1);
341 yylval.scm = scan_fraction (YYText ());
346 yylval.i = String_convert::dec2int (String (YYText ()));
350 yylval.i = String_convert::dec2int (String (YYText ()));
354 yylval.i = String_convert::dec2int (String (YYText () +1));
368 *yylval.string += to_string (escaped_char (YYText ()[1]));
371 *yylval.string += YYText ();
377 /* yylval is union. Must remember STRING before setting SCM*/
378 String *sp = yylval.string;
379 yylval.scm = scm_makfrom0str (sp->to_str0 ());
384 *yylval.string += YYText ();
389 *yylval.string += to_string (escaped_char (YYText ()[1]));
392 *yylval.string += YYText ();
398 /* yylval is union. Must remember STRING before setting SCM*/
399 String *sp = yylval.string;
400 yylval.scm = scm_makfrom0str (sp->to_str0 ());
402 return LYRICS_STRING;
405 *yylval.string += YYText ();
411 start_lyric_quote ();
414 yylval.scm = scan_fraction (YYText ());
418 yylval.i = String_convert::dec2int (String (YYText ()));
422 return scan_escaped_word (YYText () + 1);
426 String s (YYText ());
428 return yylval.i = EXTENDER;
430 return yylval.i = HYPHEN;
433 char c = s[s.length () - 1];
434 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
435 here_input ().warning (
436 _ ("Brace found at end of lyric. Did you forget a space?"));
437 yylval.scm = scm_makfrom0str (s.to_str0 ());
440 return LYRICS_STRING;
448 return scan_bare_word (YYText ());
451 return scan_escaped_word (YYText () + 1);
454 yylval.scm = scan_fraction (YYText ());
458 yylval.i = String_convert::dec2int (String (YYText ()));
499 String str (YYText () + 1);
500 SCM s = lookup_markup_command (str);
502 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
503 yylval.scm = scm_car(s);
504 SCM tag = scm_cdr(s);
505 if (tag == ly_symbol2scm("markup0"))
506 return MARKUP_HEAD_MARKUP0;
507 if (tag == ly_symbol2scm("empty"))
508 return MARKUP_HEAD_EMPTY;
509 else if (tag == ly_symbol2scm ("markup0-markup1"))
510 return MARKUP_HEAD_MARKUP0_MARKUP1;
511 else if (tag == ly_symbol2scm ("markup-list0"))
512 return MARKUP_HEAD_LIST0;
513 else if (tag == ly_symbol2scm ("scheme0"))
514 return MARKUP_HEAD_SCM0;
515 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
516 return MARKUP_HEAD_SCM0_SCM1;
517 else if (tag == ly_symbol2scm ("scheme0-markup1"))
518 return MARKUP_HEAD_SCM0_MARKUP1;
519 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
520 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
521 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
522 return MARKUP_HEAD_SCM0_SCM1_SCM2;
524 programming_error ("No parser tag defined for this signature. Abort");
529 return scan_escaped_word (str);
534 [^#{}"\\ \t\n\r\f]+ {
535 String s (YYText ());
537 char c = s[s.length () - 1];
538 /* brace open is for not confusing dumb tools. */
539 if (c == '{' || c == '}')
540 here_input ().warning (
541 _ ("Brace found at end of markup. Did you forget a space?"));
542 yylval.scm = scm_makfrom0str (s.to_str0 ());
555 main_input_b_ = false;
557 /* Returns YY_NULL */
560 else if (!close_input ())
561 /* Returns YY_NULL */
567 return scan_bare_word (YYText ());
570 return scan_escaped_word (YYText () + 1);
574 int cnv=sscanf (YYText (), "%lf", &r);
577 yylval.scm = scm_make_real (r);
582 yylval.i = String_convert::dec2int (String (YYText ()));
592 char c = YYText ()[0];
597 <INITIAL,notes,figures>. {
601 <INITIAL,lyrics,notes,figures>\\. {
602 char c= YYText ()[1];
610 return E_EXCLAMATION;
618 return E_RIGHTSQUARE;
630 String msg = _f ("invalid character: `%c'", YYText ()[0]);
631 LexerError (msg.to_str0 ());
638 Lily_lexer::push_chord_state (SCM tab)
640 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
641 yy_push_state (chords);
645 Lily_lexer::push_figuredbass_state ()
647 yy_push_state (figures);
651 Lily_lexer::push_initial_state ()
653 yy_push_state (INITIAL);
657 Lily_lexer::push_lyric_state ()
659 yy_push_state (lyrics);
663 Lily_lexer::push_markup_state ()
665 yy_push_state (markup);
669 Lily_lexer::push_note_state (SCM tab)
671 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
672 yy_push_state (notes);
676 Lily_lexer::pop_state ()
678 if (YYSTATE == notes || YYSTATE == chords)
679 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
684 Lily_lexer::identifier_type (SCM sid)
686 int k = try_special_identifiers (&yylval.scm , sid);
687 return k >= 0 ? k : SCM_IDENTIFIER;
692 Lily_lexer::scan_escaped_word (String str)
694 // use more SCM for this.
696 // SCM sym = ly_symbol2scm (str.to_str0 ());
698 int l = lookup_keyword (str);
702 SCM sid = lookup_identifier (str);
703 if (is_music_function (sid))
705 yylval.scm = get_music_function_transform (sid);
707 return music_function_type (yylval.scm);
710 if (sid != SCM_UNDEFINED)
713 return identifier_type (sid);
716 String msg (_f ("unknown escaped string: `\\%s'", str));
717 LexerError (msg.to_str0 ());
719 yylval.scm = scm_makfrom0str (str.to_str0 ());
725 Lily_lexer::scan_bare_word (String str)
727 SCM sym = ly_symbol2scm (str.to_str0 ());
728 if ((YYSTATE == notes) || (YYSTATE == chords)) {
729 SCM handle = SCM_BOOL_F;
730 if (scm_is_pair (pitchname_tab_stack_))
731 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
733 if (scm_is_pair (handle)) {
734 yylval.scm = scm_cdr (handle);
735 if (unsmob_pitch (yylval.scm))
736 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
737 else if (scm_is_symbol (yylval.scm))
740 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
742 yylval.scm = scm_cdr (handle);
743 return CHORD_MODIFIER;
747 yylval.scm = scm_makfrom0str (str.to_str0 ());
752 Lily_lexer::is_note_state () const
754 return YY_START == notes;
758 Lily_lexer::is_chord_state () const
760 return YY_START == chords;
764 Lily_lexer::is_lyric_state () const
766 return YY_START == lyrics;
770 Lily_lexer::is_figure_state () const
772 return YY_START == figures;
776 urg, belong to String (_convert)
777 and should be generalised
780 strip_leading_white (String&s)
783 for (; i < s.length (); i++)
787 s = s.nomid_string (0, i);
791 strip_trailing_white (String&s)
798 s = s.left_string (i+1);
803 /* 2.1.2x something -> \property -> \set. */
804 Lilypond_version oldest_version ("2.2.0");
808 is_valid_version (String s)
810 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
811 Lilypond_version ver (s);
812 if (! ((ver >= oldest_version) && (ver <= current)))
814 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
815 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
826 lyric_fudge (String s)
828 char * chars =s.get_copy_str0 ();
830 for (char * p = chars; *p ; p++)
832 if (*p == '_' && (p == chars || *(p-1) != '\\'))
840 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
842 * (s.get_str0 () + i + 1) = 'c';
843 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
850 Convert "NUM/DEN" into a '(NUM . DEN) cons.
853 scan_fraction (String frac)
855 int i = frac.index ('/');
856 int l = frac.length ();
857 String left = frac.left_string (i);
858 String right = frac.right_string (l - i - 1);
860 int n = String_convert::dec2int (left);
861 int d = String_convert::dec2int (right);
862 return scm_cons (scm_int2num (n), scm_int2num (d));
865 // Breaks for flex 2.5.31
867 /* avoid silly flex induced gcc warnings */
868 static void yy_push_state (int) {;}
869 static void yy_pop_state () {;}
870 static int yy_top_state () { return 0; }
873 avoid_silly_flex_induced_gcc_warnings ()
875 (void)yy_start_stack_ptr;
876 (void)yy_start_stack_depth;
877 (void)yy_start_stack;
881 avoid_silly_flex_induced_gcc_warnings ();
886 lookup_markup_command (String s)
888 SCM proc = ly_scheme_function ("lookup-markup-command");
889 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));
894 music_function_type (SCM func)
896 SCM type= scm_object_property (func, ly_symbol2scm ("music-function-signature-keyword"));
897 if (type == ly_symbol2scm ("scm"))
899 return MUSIC_FUNCTION_SCM;
901 else if (type == ly_symbol2scm ("music"))
903 return MUSIC_FUNCTION_MUSIC;
905 else if (type == ly_symbol2scm ("scm-music"))
907 return MUSIC_FUNCTION_SCM_MUSIC;
909 else if (type == ly_symbol2scm ("scm-scm"))
911 return MUSIC_FUNCTION_SCM_SCM;
913 else if (type == ly_symbol2scm ("music-music"))
915 return MUSIC_FUNCTION_MUSIC_MUSIC;
917 else if (type == ly_symbol2scm ("scm-music-music"))
919 return MUSIC_FUNCTION_SCM_MUSIC_MUSIC;
921 else if (type == ly_symbol2scm ("scm-scm-music"))
923 return MUSIC_FUNCTION_SCM_SCM_MUSIC;
925 else if (type == ly_symbol2scm ("noarg"))
927 return MUSIC_FUNCTION;
931 /* TODO: print location */
932 error ("Can not find signature for music function.");
935 return MUSIC_FUNCTION_SCM;