1 %{ // -*-Fundamental-*-
3 lexer.ll -- implement the Flex lexer
5 source file of the LilyPond music typesetter
7 (c) 1996--2005 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
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
125 NATIONAL [\001-\006\021-\027\031\036\200-\377]
126 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
134 REAL ({INT}\.{N}*)|(-?\.{N}+)
137 HORIZONTALWHITE [ \t]
141 MARKUPCOMMAND \\({A}|[-_])+
142 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
150 // windows-suck-suck-suck
153 <INITIAL,chords,figures,incl,lyrics,lyric_markup,markup,notes>{
155 yy_push_state (longcomment);
159 %[^{\n\r] { // backup rule
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 <version>\"[^"]*\" { /* got the version number */
177 String s (YYText () + 1);
178 s = s.left_string (s.index_last ('\"'));
181 if (!is_valid_version (s))
184 SCM top_scope = scm_car (scm_last_pair (scopes_));
185 scm_module_define (top_scope, ly_symbol2scm ("version-seen?"), SCM_BOOL_T);
188 <renameinput>\"[^"]*\" {
189 String s (YYText () + 1);
190 s = s.left_string (s.index_last ('\"'));
193 this->here_input().source_file_->name_ = s;
194 progress_indication (_f ("Renaming input 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 \\version").to_str0 ());
206 LexerError (_ ("No quoted string found after \\renameinput").to_str0 ());
219 LexerError (_ ("EOF found inside a comment").to_str0 ());
220 is_main_input_ = false;
221 if (! close_input ())
222 yyterminate (); // can't move this, since it actually rets a YY_NULL
227 <INITIAL,chords,lyrics,notes,figures>\\maininput {
231 is_main_input_ = true;
234 error (_ ("\\maininput not allowed outside init files"));
237 <INITIAL,chords,lyrics,figures,notes>\\include {
238 yy_push_state (incl);
240 <incl>\"[^"]*\" { /* got the include file name */
241 String s (YYText ()+1);
242 s = s.left_string (s.index_last ('"'));
244 new_input (s, sources_);
247 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
248 String s = YYText () + 1;
249 strip_trailing_white (s);
250 if (s.length () && (s[s.length () - 1] == ';'))
251 s = s.left_string (s.length () - 1);
253 SCM sid = lookup_identifier (s);
254 if (scm_is_string (sid)) {
255 new_input (ly_scm2string (sid), sources_);
258 String msg (_f ("wrong or undefined identifier: `%s'", s ));
260 LexerError (msg.to_str0 ());
261 SCM err = scm_current_error_port ();
262 scm_puts ("This value was found in the table: ", err);
263 scm_display (sid, err);
266 <incl>\"[^"]* { // backup rule
267 error (_ ("Missing end quote"));
270 <chords,notes,figures>{RESTNAME} {
271 const char *s = YYText ();
272 yylval.scm = scm_makfrom0str (s);
275 <chords,notes,figures>R {
276 return MULTI_MEASURE_REST;
278 <INITIAL,chords,figures,lyrics,lyric_markup,markup,notes># { //embedded scm
280 Input hi = here_input();
282 SCM sval = ly_parse_scm (hi.start_, &n, hi,
283 be_safe_global && is_main_input_);
285 if (sval == SCM_UNDEFINED)
287 sval = SCM_UNSPECIFIED;
291 for (int i = 0; i < n; i++)
295 char_count_stack_.top () += n;
297 if (unpack_identifier (sval) != SCM_UNDEFINED)
299 yylval.scm = unpack_identifier(sval);
300 return identifier_type (yylval.scm);
306 <INITIAL,notes,lyrics>{
308 return DOUBLE_ANGLE_OPEN;
311 return DOUBLE_ANGLE_CLOSE;
328 return scan_bare_word (YYText ());
332 return scan_escaped_word (YYText () + 1);
335 yylval.scm = scan_fraction (YYText ());
340 yylval.i = String_convert::dec2int (String (YYText ()));
344 yylval.i = String_convert::dec2int (String (YYText ()));
348 yylval.i = String_convert::dec2int (String (YYText () +1));
361 *yylval.string += to_string (escaped_char (YYText ()[1]));
364 *yylval.string += YYText ();
370 /* yylval is union. Must remember STRING before setting SCM*/
371 String *sp = yylval.string;
372 yylval.scm = scm_makfrom0str (sp->to_str0 ());
377 *yylval.string += YYText ();
382 *yylval.string += to_string (escaped_char (YYText ()[1]));
385 *yylval.string += YYText ();
391 /* yylval is union. Must remember STRING before setting SCM*/
392 String *sp = yylval.string;
393 yylval.scm = scm_makfrom0str (sp->to_str0 ());
395 return LYRICS_STRING;
398 *yylval.string += YYText ();
404 start_lyric_quote ();
407 yylval.scm = scan_fraction (YYText ());
411 yylval.i = String_convert::dec2int (String (YYText ()));
415 return scan_escaped_word (YYText () + 1);
419 String s (YYText ());
421 return yylval.i = EXTENDER;
423 return yylval.i = HYPHEN;
426 char c = s[s.length () - 1];
427 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
428 here_input ().warning (
429 _ ("Brace found at end of lyric. Did you forget a space?"));
430 yylval.scm = scm_makfrom0str (s.to_str0 ());
433 return LYRICS_STRING;
441 return scan_bare_word (YYText ());
444 return scan_escaped_word (YYText () + 1);
447 yylval.scm = scan_fraction (YYText ());
451 yylval.i = String_convert::dec2int (String (YYText ()));
478 <markup,lyric_markup>{
486 String str (YYText () + 1);
487 SCM s = lookup_markup_command (str);
489 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
490 yylval.scm = scm_car(s);
491 SCM tag = scm_cdr(s);
492 if (tag == ly_symbol2scm("markup0"))
493 return MARKUP_HEAD_MARKUP0;
494 if (tag == ly_symbol2scm("empty"))
495 return MARKUP_HEAD_EMPTY;
496 else if (tag == ly_symbol2scm ("markup0-markup1"))
497 return MARKUP_HEAD_MARKUP0_MARKUP1;
498 else if (tag == ly_symbol2scm ("markup-list0"))
499 return MARKUP_HEAD_LIST0;
500 else if (tag == ly_symbol2scm ("scheme0"))
501 return MARKUP_HEAD_SCM0;
502 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
503 return MARKUP_HEAD_SCM0_SCM1;
504 else if (tag == ly_symbol2scm ("scheme0-markup1"))
505 return MARKUP_HEAD_SCM0_MARKUP1;
506 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
507 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
508 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
509 return MARKUP_HEAD_SCM0_SCM1_SCM2;
511 programming_error ("No parser tag defined for this signature. Abort");
516 return scan_escaped_word (str);
521 [^#{}"\\ \t\n\r\f]+ {
522 String s (YYText ());
524 char c = s[s.length () - 1];
525 /* brace open is for not confusing dumb tools. */
526 if (c == '{' || c == '}')
527 here_input ().warning (
528 _ ("Brace found at end of markup. Did you forget a space?"));
529 yylval.scm = scm_makfrom0str (s.to_str0 ());
542 is_main_input_ = false;
544 /* Returns YY_NULL */
547 else if (!close_input ())
548 /* Returns YY_NULL */
554 return scan_bare_word (YYText ());
557 return scan_escaped_word (YYText () + 1);
561 int cnv = sscanf (YYText (), "%lf", &r);
564 yylval.scm = scm_make_real (r);
569 yylval.i = String_convert::dec2int (String (YYText ()));
579 char c = YYText ()[0];
584 <INITIAL,notes,figures>. {
588 <INITIAL,lyrics,notes,figures>\\. {
589 char c = YYText ()[1];
597 return E_EXCLAMATION;
605 return E_RIGHTSQUARE;
617 String msg = _f ("invalid character: `%c'", YYText ()[0]);
618 LexerError (msg.to_str0 ());
625 Lily_lexer::push_chord_state (SCM tab)
627 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
628 yy_push_state (chords);
632 Lily_lexer::push_figuredbass_state ()
634 yy_push_state (figures);
638 Lily_lexer::push_initial_state ()
640 yy_push_state (INITIAL);
644 Lily_lexer::push_lyric_state ()
646 yy_push_state (lyrics);
650 Lily_lexer::push_markup_state ()
652 yy_push_state (markup);
656 Lily_lexer::push_lyric_markup_state ()
658 yy_push_state (lyric_markup);
662 Lily_lexer::push_note_state (SCM tab)
664 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
665 yy_push_state (notes);
669 Lily_lexer::pop_state ()
671 if (YYSTATE == notes || YYSTATE == chords)
672 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
677 Lily_lexer::identifier_type (SCM sid)
679 int k = try_special_identifiers (&yylval.scm , sid);
680 return k >= 0 ? k : SCM_IDENTIFIER;
685 Lily_lexer::scan_escaped_word (String str)
687 // use more SCM for this.
689 // SCM sym = ly_symbol2scm (str.to_str0 ());
691 int i = lookup_keyword (str);
692 if (i == MARKUP && is_lyric_state ())
697 SCM sid = lookup_identifier (str);
698 if (is_music_function (sid))
700 yylval.scm = get_music_function_transform (sid);
701 return music_function_type (yylval.scm);
704 if (sid != SCM_UNDEFINED)
707 return identifier_type (sid);
710 String msg (_f ("unknown escaped string: `\\%s'", str));
711 LexerError (msg.to_str0 ());
713 yylval.scm = scm_makfrom0str (str.to_str0 ());
719 Lily_lexer::scan_bare_word (String str)
721 SCM sym = ly_symbol2scm (str.to_str0 ());
722 if ((YYSTATE == notes) || (YYSTATE == chords)) {
723 SCM handle = SCM_BOOL_F;
724 if (scm_is_pair (pitchname_tab_stack_))
725 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
727 if (scm_is_pair (handle)) {
728 yylval.scm = scm_cdr (handle);
729 if (unsmob_pitch (yylval.scm))
730 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
731 else if (scm_is_symbol (yylval.scm))
734 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
736 yylval.scm = scm_cdr (handle);
737 return CHORD_MODIFIER;
741 yylval.scm = scm_makfrom0str (str.to_str0 ());
746 Lily_lexer::is_note_state () const
748 return YY_START == notes;
752 Lily_lexer::is_chord_state () const
754 return YY_START == chords;
758 Lily_lexer::is_lyric_state () const
760 return YY_START == lyrics;
764 Lily_lexer::is_figure_state () const
766 return YY_START == figures;
770 urg, belong to String (_convert)
771 and should be generalised
774 strip_leading_white (String&s)
777 for (; i < s.length (); i++)
781 s = s.nomid_string (0, i);
785 strip_trailing_white (String&s)
792 s = s.left_string (i+1);
797 /* 2.1.2x something -> \property -> \set. */
798 Lilypond_version oldest_version ("2.3.22");
802 is_valid_version (String s)
804 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
805 Lilypond_version ver (s);
806 if (! ((ver >= oldest_version) && (ver <= current)))
808 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
809 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
820 lyric_fudge (String s)
822 char * chars = s.get_copy_str0 ();
824 for (char * p = chars; *p ; p++)
826 if (*p == '_' && (p == chars || *(p-1) != '\\'))
834 if ((i = s.index ("\\,")) != -1) // change "\," to TeX's "\c "
836 * (s.get_str0 () + i + 1) = 'c';
837 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
844 Convert "NUM/DEN" into a '(NUM . DEN) cons.
847 scan_fraction (String frac)
849 int i = frac.index ('/');
850 int l = frac.length ();
851 String left = frac.left_string (i);
852 String right = frac.right_string (l - i - 1);
854 int n = String_convert::dec2int (left);
855 int d = String_convert::dec2int (right);
856 return scm_cons (scm_int2num (n), scm_int2num (d));
859 // Breaks for flex 2.5.31
861 /* avoid silly flex induced gcc warnings */
862 static void yy_push_state (int) {;}
863 static void yy_pop_state () {;}
864 static int yy_top_state () { return 0; }
867 avoid_silly_flex_induced_gcc_warnings ()
869 (void)yy_start_stack_ptr;
870 (void)yy_start_stack_depth;
871 (void)yy_start_stack;
875 avoid_silly_flex_induced_gcc_warnings ();
880 lookup_markup_command (String s)
882 SCM proc = ly_lily_module_constant ("lookup-markup-command");
883 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));
888 music_function_type (SCM func)
890 SCM type = scm_object_property (func, ly_symbol2scm ("music-function-signature-keyword"));
891 if (type == ly_symbol2scm ("scm"))
893 return MUSIC_FUNCTION_SCM;
895 else if (type == ly_symbol2scm ("music"))
897 return MUSIC_FUNCTION_MUSIC;
899 else if (type == ly_symbol2scm ("scm-music"))
901 return MUSIC_FUNCTION_SCM_MUSIC;
903 else if (type == ly_symbol2scm ("scm-scm"))
905 return MUSIC_FUNCTION_SCM_SCM;
907 else if (type == ly_symbol2scm ("music-music"))
909 return MUSIC_FUNCTION_MUSIC_MUSIC;
911 else if (type == ly_symbol2scm ("scm-music-music"))
913 return MUSIC_FUNCTION_SCM_MUSIC_MUSIC;
915 else if (type == ly_symbol2scm ("scm-scm-music"))
917 return MUSIC_FUNCTION_SCM_SCM_MUSIC;
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 not find signature for music function.");
949 return MUSIC_FUNCTION_SCM;