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,figures,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 String s (YYText ()+1);
248 s = s.left_string (s.index_last ('"'));
250 new_input (s, sources_);
253 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
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));
367 *yylval.string += to_string (escaped_char (YYText ()[1]));
370 *yylval.string += YYText ();
376 /* yylval is union. Must remember STRING before setting SCM*/
377 String *sp = yylval.string;
378 yylval.scm = scm_makfrom0str (sp->to_str0 ());
383 *yylval.string += YYText ();
388 *yylval.string += to_string (escaped_char (YYText ()[1]));
391 *yylval.string += YYText ();
397 /* yylval is union. Must remember STRING before setting SCM*/
398 String *sp = yylval.string;
399 yylval.scm = scm_makfrom0str (sp->to_str0 ());
401 return LYRICS_STRING;
404 *yylval.string += YYText ();
410 start_lyric_quote ();
413 yylval.scm = scan_fraction (YYText ());
417 yylval.i = String_convert::dec2int (String (YYText ()));
421 return scan_escaped_word (YYText () + 1);
425 String s (YYText ());
427 return yylval.i = EXTENDER;
429 return yylval.i = HYPHEN;
432 char c = s[s.length () - 1];
433 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
434 here_input ().warning (
435 _ ("Brace found at end of lyric. Did you forget a space?"));
436 yylval.scm = scm_makfrom0str (s.to_str0 ());
439 return LYRICS_STRING;
447 return scan_bare_word (YYText ());
450 return scan_escaped_word (YYText () + 1);
453 yylval.scm = scan_fraction (YYText ());
457 yylval.i = String_convert::dec2int (String (YYText ()));
498 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 signature. Abort");
528 return scan_escaped_word (str);
533 [^#{}"\\ \t\n\r\f]+ {
534 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.to_str0 ());
554 main_input_b_ = 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);
573 int cnv=sscanf (YYText (), "%lf", &r);
576 yylval.scm = scm_make_real (r);
581 yylval.i = String_convert::dec2int (String (YYText ()));
591 char c = YYText ()[0];
596 <INITIAL,notes,figures>. {
600 <INITIAL,lyrics,notes,figures>\\. {
601 char c= YYText ()[1];
609 return E_EXCLAMATION;
617 return E_RIGHTSQUARE;
629 String msg = _f ("invalid character: `%c'", YYText ()[0]);
630 LexerError (msg.to_str0 ());
637 Lily_lexer::push_chord_state (SCM tab)
639 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
640 yy_push_state (chords);
644 Lily_lexer::push_figuredbass_state ()
646 yy_push_state (figures);
650 Lily_lexer::push_initial_state ()
652 yy_push_state (INITIAL);
656 Lily_lexer::push_lyric_state ()
658 yy_push_state (lyrics);
662 Lily_lexer::push_markup_state ()
664 yy_push_state (markup);
668 Lily_lexer::push_note_state (SCM tab)
670 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
671 yy_push_state (notes);
675 Lily_lexer::pop_state ()
677 if (YYSTATE == notes || YYSTATE == chords)
678 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
683 Lily_lexer::identifier_type (SCM sid)
685 int k = try_special_identifiers (&yylval.scm , sid);
686 return k >= 0 ? k : SCM_IDENTIFIER;
691 Lily_lexer::scan_escaped_word (String str)
693 // use more SCM for this.
695 // SCM sym = ly_symbol2scm (str.to_str0 ());
697 int l = lookup_keyword (str);
701 SCM sid = lookup_identifier (str);
702 if (is_music_function (sid))
704 yylval.scm = get_music_function_transform (sid);
706 return music_function_type (yylval.scm);
709 if (sid != SCM_UNDEFINED)
712 return identifier_type (sid);
715 String msg (_f ("unknown escaped string: `\\%s'", str));
716 LexerError (msg.to_str0 ());
718 yylval.scm = scm_makfrom0str (str.to_str0 ());
724 Lily_lexer::scan_bare_word (String str)
726 SCM sym = ly_symbol2scm (str.to_str0 ());
727 if ((YYSTATE == notes) || (YYSTATE == chords)) {
728 SCM handle = SCM_BOOL_F;
729 if (scm_is_pair (pitchname_tab_stack_))
730 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
732 if (scm_is_pair (handle)) {
733 yylval.scm = scm_cdr (handle);
734 if (unsmob_pitch (yylval.scm))
735 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
736 else if (scm_is_symbol (yylval.scm))
739 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
741 yylval.scm = scm_cdr (handle);
742 return CHORD_MODIFIER;
746 yylval.scm = scm_makfrom0str (str.to_str0 ());
751 Lily_lexer::is_note_state () const
753 return YY_START == notes;
757 Lily_lexer::is_chord_state () const
759 return YY_START == chords;
763 Lily_lexer::is_lyric_state () const
765 return YY_START == lyrics;
769 Lily_lexer::is_figure_state () const
771 return YY_START == figures;
775 urg, belong to String (_convert)
776 and should be generalised
779 strip_leading_white (String&s)
782 for (; i < s.length (); i++)
786 s = s.nomid_string (0, i);
790 strip_trailing_white (String&s)
797 s = s.left_string (i+1);
802 /* 2.1.2x something -> \property -> \set. */
803 Lilypond_version oldest_version ("2.3.22");
807 is_valid_version (String s)
809 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
810 Lilypond_version ver (s);
811 if (! ((ver >= oldest_version) && (ver <= current)))
813 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
814 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
825 lyric_fudge (String s)
827 char * chars =s.get_copy_str0 ();
829 for (char * p = chars; *p ; p++)
831 if (*p == '_' && (p == chars || *(p-1) != '\\'))
839 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
841 * (s.get_str0 () + i + 1) = 'c';
842 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
849 Convert "NUM/DEN" into a '(NUM . DEN) cons.
852 scan_fraction (String frac)
854 int i = frac.index ('/');
855 int l = frac.length ();
856 String left = frac.left_string (i);
857 String right = frac.right_string (l - i - 1);
859 int n = String_convert::dec2int (left);
860 int d = String_convert::dec2int (right);
861 return scm_cons (scm_int2num (n), scm_int2num (d));
864 // Breaks for flex 2.5.31
866 /* avoid silly flex induced gcc warnings */
867 static void yy_push_state (int) {;}
868 static void yy_pop_state () {;}
869 static int yy_top_state () { return 0; }
872 avoid_silly_flex_induced_gcc_warnings ()
874 (void)yy_start_stack_ptr;
875 (void)yy_start_stack_depth;
876 (void)yy_start_stack;
880 avoid_silly_flex_induced_gcc_warnings ();
885 lookup_markup_command (String s)
887 SCM proc = ly_scheme_function ("lookup-markup-command");
888 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));
893 music_function_type (SCM func)
895 SCM type= scm_object_property (func, ly_symbol2scm ("music-function-signature-keyword"));
896 if (type == ly_symbol2scm ("scm"))
898 return MUSIC_FUNCTION_SCM;
900 else if (type == ly_symbol2scm ("music"))
902 return MUSIC_FUNCTION_MUSIC;
904 else if (type == ly_symbol2scm ("scm-music"))
906 return MUSIC_FUNCTION_SCM_MUSIC;
908 else if (type == ly_symbol2scm ("scm-scm"))
910 return MUSIC_FUNCTION_SCM_SCM;
912 else if (type == ly_symbol2scm ("music-music"))
914 return MUSIC_FUNCTION_MUSIC_MUSIC;
916 else if (type == ly_symbol2scm ("scm-music-music"))
918 return MUSIC_FUNCTION_SCM_MUSIC_MUSIC;
920 else if (type == ly_symbol2scm ("scm-scm-music"))
922 return MUSIC_FUNCTION_SCM_SCM_MUSIC;
924 else if (type == ly_symbol2scm ("markup"))
926 return MUSIC_FUNCTION_MARKUP;
928 else if (type == ly_symbol2scm ("markup-music"))
930 return MUSIC_FUNCTION_MARKUP_MUSIC;
932 else if (type == ly_symbol2scm ("markup-markup"))
934 return MUSIC_FUNCTION_MARKUP_MARKUP;
936 else if (type == ly_symbol2scm ("markup-music-music"))
938 return MUSIC_FUNCTION_MARKUP_MUSIC_MUSIC;
940 else if (type == ly_symbol2scm ("markup-markup-music"))
942 return MUSIC_FUNCTION_MARKUP_MARKUP_MUSIC;
944 else if (type == ly_symbol2scm ("noarg"))
946 return MUSIC_FUNCTION;
950 /* TODO: print location */
951 error ("Can not find signature for music function.");
954 return MUSIC_FUNCTION_SCM;