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
124 NATIONAL [\001-\006\021-\027\031\036\200-\377]
125 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
133 REAL ({INT}\.{N}*)|(-?\.{N}+)
136 HORIZONTALWHITE [ \t]
140 MARKUPCOMMAND \\({A}|[-_])+
141 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
149 // windows-suck-suck-suck
152 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
154 yy_push_state (longcomment);
158 %[^{\n\r] { // backup rule
169 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
170 yy_push_state (version);
172 <INITIAL,chords,lyrics,notes,figures>\\renameinput{WHITE}* {
173 yy_push_state (renameinput);
175 <version>\"[^"]*\" { /* got the version number */
176 String s (YYText () + 1);
177 s = s.left_string (s.index_last ('\"'));
180 if (!is_valid_version (s))
183 SCM top_scope = scm_car (scm_last_pair (scopes_));
184 scm_module_define (top_scope, ly_symbol2scm ("version-seen?"), SCM_BOOL_T);
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 (_f ("Renaming input to: `%s'", s.to_str0 ()));
194 progress_indication ("\n");
195 scm_module_define (scm_car (scopes_),
196 ly_symbol2scm ("input-file-name"),
197 scm_makfrom0str (s.to_str0 ()));
201 LexerError (_ ("No quoted string found after \\version").to_str0 ());
205 LexerError (_ ("No quoted string found after \\renameinput").to_str0 ());
218 LexerError (_ ("EOF found inside a comment").to_str0 ());
219 is_main_input_ = false;
220 if (! close_input ())
221 yyterminate (); // can't move this, since it actually rets a YY_NULL
226 <INITIAL,chords,lyrics,notes,figures>\\maininput {
230 is_main_input_ = true;
233 error (_ ("\\maininput not allowed outside init files"));
236 <INITIAL,chords,lyrics,figures,notes>\\include {
237 yy_push_state (incl);
239 <incl>\"[^"]*\" { /* got the include file name */
240 String s (YYText ()+1);
241 s = s.left_string (s.index_last ('"'));
243 new_input (s, sources_);
246 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
247 String s = YYText () + 1;
248 strip_trailing_white (s);
249 if (s.length () && (s[s.length () - 1] == ';'))
250 s = s.left_string (s.length () - 1);
252 SCM sid = lookup_identifier (s);
253 if (scm_is_string (sid)) {
254 new_input (ly_scm2string (sid), sources_);
257 String msg (_f ("wrong or undefined identifier: `%s'", s ));
259 LexerError (msg.to_str0 ());
260 SCM err = scm_current_error_port ();
261 scm_puts ("This value was found in the table: ", err);
262 scm_display (sid, err);
265 <incl>\"[^"]* { // backup rule
266 error (_ ("Missing end quote"));
269 <chords,notes,figures>{RESTNAME} {
270 const char *s = YYText ();
271 yylval.scm = scm_makfrom0str (s);
274 <chords,notes,figures>R {
275 return MULTI_MEASURE_REST;
277 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
279 Input hi = here_input();
281 SCM sval = ly_parse_scm (hi.start_, &n, hi,
282 be_safe_global && is_main_input_);
284 if (sval == SCM_UNDEFINED)
286 sval = SCM_UNSPECIFIED;
290 for (int i = 0; i < n; i++)
294 char_count_stack_.top () += n;
296 if (unpack_identifier (sval) != SCM_UNDEFINED)
298 yylval.scm = unpack_identifier(sval);
299 return identifier_type (yylval.scm);
305 <INITIAL,notes,lyrics>{
307 return DOUBLE_ANGLE_OPEN;
310 return DOUBLE_ANGLE_CLOSE;
327 return scan_bare_word (YYText ());
331 return scan_escaped_word (YYText () + 1);
334 yylval.scm = scan_fraction (YYText ());
339 yylval.i = String_convert::dec2int (String (YYText ()));
343 yylval.i = String_convert::dec2int (String (YYText ()));
347 yylval.i = String_convert::dec2int (String (YYText () +1));
360 *yylval.string += to_string (escaped_char (YYText ()[1]));
363 *yylval.string += YYText ();
369 /* yylval is union. Must remember STRING before setting SCM*/
370 String *sp = yylval.string;
371 yylval.scm = scm_makfrom0str (sp->to_str0 ());
376 *yylval.string += YYText ();
381 *yylval.string += to_string (escaped_char (YYText ()[1]));
384 *yylval.string += YYText ();
390 /* yylval is union. Must remember STRING before setting SCM*/
391 String *sp = yylval.string;
392 yylval.scm = scm_makfrom0str (sp->to_str0 ());
394 return LYRICS_STRING;
397 *yylval.string += YYText ();
403 start_lyric_quote ();
406 yylval.scm = scan_fraction (YYText ());
410 yylval.i = String_convert::dec2int (String (YYText ()));
414 return scan_escaped_word (YYText () + 1);
418 String s (YYText ());
420 return yylval.i = EXTENDER;
422 return yylval.i = HYPHEN;
425 char c = s[s.length () - 1];
426 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
427 here_input ().warning (
428 _ ("Brace found at end of lyric. Did you forget a space?"));
429 yylval.scm = scm_makfrom0str (s.to_str0 ());
432 return LYRICS_STRING;
440 return scan_bare_word (YYText ());
443 return scan_escaped_word (YYText () + 1);
446 yylval.scm = scan_fraction (YYText ());
450 yylval.i = String_convert::dec2int (String (YYText ()));
485 String str (YYText () + 1);
486 SCM s = lookup_markup_command (str);
488 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
489 yylval.scm = scm_car(s);
490 SCM tag = scm_cdr(s);
491 if (tag == ly_symbol2scm("markup0"))
492 return MARKUP_HEAD_MARKUP0;
493 if (tag == ly_symbol2scm("empty"))
494 return MARKUP_HEAD_EMPTY;
495 else if (tag == ly_symbol2scm ("markup0-markup1"))
496 return MARKUP_HEAD_MARKUP0_MARKUP1;
497 else if (tag == ly_symbol2scm ("markup-list0"))
498 return MARKUP_HEAD_LIST0;
499 else if (tag == ly_symbol2scm ("scheme0"))
500 return MARKUP_HEAD_SCM0;
501 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
502 return MARKUP_HEAD_SCM0_SCM1;
503 else if (tag == ly_symbol2scm ("scheme0-markup1"))
504 return MARKUP_HEAD_SCM0_MARKUP1;
505 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
506 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
507 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
508 return MARKUP_HEAD_SCM0_SCM1_SCM2;
510 programming_error ("No parser tag defined for this signature. Abort");
515 return scan_escaped_word (str);
520 [^#{}"\\ \t\n\r\f]+ {
521 String s (YYText ());
523 char c = s[s.length () - 1];
524 /* brace open is for not confusing dumb tools. */
525 if (c == '{' || c == '}')
526 here_input ().warning (
527 _ ("Brace found at end of markup. Did you forget a space?"));
528 yylval.scm = scm_makfrom0str (s.to_str0 ());
541 is_main_input_ = false;
543 /* Returns YY_NULL */
546 else if (!close_input ())
547 /* Returns YY_NULL */
553 return scan_bare_word (YYText ());
556 return scan_escaped_word (YYText () + 1);
560 int cnv = sscanf (YYText (), "%lf", &r);
563 yylval.scm = scm_make_real (r);
568 yylval.i = String_convert::dec2int (String (YYText ()));
578 char c = YYText ()[0];
583 <INITIAL,notes,figures>. {
587 <INITIAL,lyrics,notes,figures>\\. {
588 char c = YYText ()[1];
596 return E_EXCLAMATION;
604 return E_RIGHTSQUARE;
616 String msg = _f ("invalid character: `%c'", YYText ()[0]);
617 LexerError (msg.to_str0 ());
624 Lily_lexer::push_chord_state (SCM tab)
626 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
627 yy_push_state (chords);
631 Lily_lexer::push_figuredbass_state ()
633 yy_push_state (figures);
637 Lily_lexer::push_initial_state ()
639 yy_push_state (INITIAL);
643 Lily_lexer::push_lyric_state ()
645 yy_push_state (lyrics);
649 Lily_lexer::push_markup_state ()
651 yy_push_state (markup);
655 Lily_lexer::push_note_state (SCM tab)
657 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
658 yy_push_state (notes);
662 Lily_lexer::pop_state ()
664 if (YYSTATE == notes || YYSTATE == chords)
665 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
670 Lily_lexer::identifier_type (SCM sid)
672 int k = try_special_identifiers (&yylval.scm , sid);
673 return k >= 0 ? k : SCM_IDENTIFIER;
678 Lily_lexer::scan_escaped_word (String str)
680 // use more SCM for this.
682 // SCM sym = ly_symbol2scm (str.to_str0 ());
684 int i = lookup_keyword (str);
685 if (i == MARKUP && is_lyric_state ())
690 SCM sid = lookup_identifier (str);
691 if (is_music_function (sid))
693 yylval.scm = get_music_function_transform (sid);
694 return music_function_type (yylval.scm);
697 if (sid != SCM_UNDEFINED)
700 return identifier_type (sid);
703 String msg (_f ("unknown escaped string: `\\%s'", str));
704 LexerError (msg.to_str0 ());
706 yylval.scm = scm_makfrom0str (str.to_str0 ());
712 Lily_lexer::scan_bare_word (String str)
714 SCM sym = ly_symbol2scm (str.to_str0 ());
715 if ((YYSTATE == notes) || (YYSTATE == chords)) {
716 SCM handle = SCM_BOOL_F;
717 if (scm_is_pair (pitchname_tab_stack_))
718 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
720 if (scm_is_pair (handle)) {
721 yylval.scm = scm_cdr (handle);
722 if (unsmob_pitch (yylval.scm))
723 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
724 else if (scm_is_symbol (yylval.scm))
727 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
729 yylval.scm = scm_cdr (handle);
730 return CHORD_MODIFIER;
734 yylval.scm = scm_makfrom0str (str.to_str0 ());
739 Lily_lexer::is_note_state () const
741 return YY_START == notes;
745 Lily_lexer::is_chord_state () const
747 return YY_START == chords;
751 Lily_lexer::is_lyric_state () const
753 return YY_START == lyrics;
757 Lily_lexer::is_figure_state () const
759 return YY_START == figures;
763 urg, belong to String (_convert)
764 and should be generalised
767 strip_leading_white (String&s)
770 for (; i < s.length (); i++)
774 s = s.nomid_string (0, i);
778 strip_trailing_white (String&s)
785 s = s.left_string (i+1);
790 /* 2.1.2x something -> \property -> \set. */
791 Lilypond_version oldest_version ("2.3.22");
795 is_valid_version (String s)
797 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
798 Lilypond_version ver (s);
799 if (! ((ver >= oldest_version) && (ver <= current)))
801 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
802 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
813 lyric_fudge (String s)
815 char * chars = s.get_copy_str0 ();
817 for (char * p = chars; *p ; p++)
819 if (*p == '_' && (p == chars || *(p-1) != '\\'))
827 if ((i = s.index ("\\,")) != -1) // change "\," to TeX's "\c "
829 * (s.get_str0 () + i + 1) = 'c';
830 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
837 Convert "NUM/DEN" into a '(NUM . DEN) cons.
840 scan_fraction (String frac)
842 int i = frac.index ('/');
843 int l = frac.length ();
844 String left = frac.left_string (i);
845 String right = frac.right_string (l - i - 1);
847 int n = String_convert::dec2int (left);
848 int d = String_convert::dec2int (right);
849 return scm_cons (scm_int2num (n), scm_int2num (d));
852 // Breaks for flex 2.5.31
854 /* avoid silly flex induced gcc warnings */
855 static void yy_push_state (int) {;}
856 static void yy_pop_state () {;}
857 static int yy_top_state () { return 0; }
860 avoid_silly_flex_induced_gcc_warnings ()
862 (void)yy_start_stack_ptr;
863 (void)yy_start_stack_depth;
864 (void)yy_start_stack;
868 avoid_silly_flex_induced_gcc_warnings ();
873 lookup_markup_command (String s)
875 SCM proc = ly_lily_module_constant ("lookup-markup-command");
876 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));
881 music_function_type (SCM func)
883 SCM type = scm_object_property (func, ly_symbol2scm ("music-function-signature-keyword"));
884 if (type == ly_symbol2scm ("scm"))
886 return MUSIC_FUNCTION_SCM;
888 else if (type == ly_symbol2scm ("music"))
890 return MUSIC_FUNCTION_MUSIC;
892 else if (type == ly_symbol2scm ("scm-music"))
894 return MUSIC_FUNCTION_SCM_MUSIC;
896 else if (type == ly_symbol2scm ("scm-scm"))
898 return MUSIC_FUNCTION_SCM_SCM;
900 else if (type == ly_symbol2scm ("music-music"))
902 return MUSIC_FUNCTION_MUSIC_MUSIC;
904 else if (type == ly_symbol2scm ("scm-music-music"))
906 return MUSIC_FUNCTION_SCM_MUSIC_MUSIC;
908 else if (type == ly_symbol2scm ("scm-scm-music"))
910 return MUSIC_FUNCTION_SCM_SCM_MUSIC;
912 else if (type == ly_symbol2scm ("markup"))
914 return MUSIC_FUNCTION_MARKUP;
916 else if (type == ly_symbol2scm ("markup-music"))
918 return MUSIC_FUNCTION_MARKUP_MUSIC;
920 else if (type == ly_symbol2scm ("markup-markup"))
922 return MUSIC_FUNCTION_MARKUP_MARKUP;
924 else if (type == ly_symbol2scm ("markup-music-music"))
926 return MUSIC_FUNCTION_MARKUP_MUSIC_MUSIC;
928 else if (type == ly_symbol2scm ("markup-markup-music"))
930 return MUSIC_FUNCTION_MARKUP_MARKUP_MUSIC;
932 else if (type == ly_symbol2scm ("noarg"))
934 return MUSIC_FUNCTION;
938 /* TODO: print location */
939 error ("Can not find signature for music function.");
942 return MUSIC_FUNCTION_SCM;