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 SCM top_scope = scm_car (scm_last_pair (scopes_));
191 scm_module_define (top_scope, ly_symbol2scm ("version-seen?"), SCM_BOOL_T);
194 <renameinput>\"[^"]*\" {
195 String s (YYText () + 1);
196 s = s.left_string (s.index_last ('\"'));
199 this->here_input().source_file_->name_ = s;
200 progress_indication (_f ("Renaming input to: `%s'", s.to_str0 ()));
201 progress_indication ("\n");
202 scm_module_define (scm_car (scopes_),
203 ly_symbol2scm ("input-file-name"),
204 scm_makfrom0str (s.to_str0 ()));
208 LexerError (_ ("No quoted string found after \\encoding").to_str0 ());
212 LexerError (_ ("No quoted string found after \\version").to_str0 ());
216 LexerError (_ ("No quoted string found after \\renameinput").to_str0 ());
229 LexerError (_ ("EOF found inside a comment").to_str0 ());
230 main_input_b_ = false;
231 if (! close_input ())
232 yyterminate (); // can't move this, since it actually rets a YY_NULL
237 <INITIAL,chords,lyrics,notes,figures>\\maininput {
241 main_input_b_ = true;
244 error (_ ("\\maininput not allowed outside init files"));
247 <INITIAL,chords,lyrics,figures,notes>\\include {
248 yy_push_state (incl);
250 <incl>\"[^"]*\" { /* got the include file name */
251 String s (YYText ()+1);
252 s = s.left_string (s.index_last ('"'));
254 new_input (s, sources_);
257 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
258 String s = YYText () + 1;
259 strip_trailing_white (s);
260 if (s.length () && (s[s.length () - 1] == ';'))
261 s = s.left_string (s.length () - 1);
263 SCM sid = lookup_identifier (s);
264 if (scm_is_string (sid)) {
265 new_input (ly_scm2string (sid), sources_);
268 String msg (_f ("wrong or undefined identifier: `%s'", s ));
270 LexerError (msg.to_str0 ());
271 SCM err = scm_current_error_port ();
272 scm_puts ("This value was found in the table: ", err);
273 scm_display (sid, err);
276 <incl>\"[^"]* { // backup rule
277 error (_ ("Missing end quote"));
280 <chords,notes,figures>{RESTNAME} {
281 const char *s = YYText ();
282 yylval.scm = scm_makfrom0str (s);
285 <chords,notes,figures>R {
286 return MULTI_MEASURE_REST;
288 <INITIAL,markup,chords,lyrics,notes,figures># { //embedded scm
289 //char const* s = YYText () + 1;
290 char const* s = here_str0 ();
292 SCM sval = ly_parse_scm (s, &n, here_input (),
293 safe_global_b && main_input_b_);
295 if (sval == SCM_UNDEFINED)
297 sval = SCM_UNSPECIFIED;
301 for (int i = 0; i < n; i++)
305 char_count_stack_.top () += n;
307 if (unpack_identifier (sval) != SCM_UNDEFINED)
309 yylval.scm = unpack_identifier(sval);
310 return identifier_type (yylval.scm);
316 <INITIAL,notes,lyrics>{
318 return DOUBLE_ANGLE_OPEN;
321 return DOUBLE_ANGLE_CLOSE;
338 return scan_bare_word (YYText ());
342 return scan_escaped_word (YYText () + 1);
345 yylval.scm = scan_fraction (YYText ());
350 yylval.i = String_convert::dec2int (String (YYText ()));
354 yylval.i = String_convert::dec2int (String (YYText ()));
358 yylval.i = String_convert::dec2int (String (YYText () +1));
371 *yylval.string += to_string (escaped_char (YYText ()[1]));
374 *yylval.string += YYText ();
380 /* yylval is union. Must remember STRING before setting SCM*/
381 String *sp = yylval.string;
382 yylval.scm = scm_makfrom0str (sp->to_str0 ());
387 *yylval.string += YYText ();
392 *yylval.string += to_string (escaped_char (YYText ()[1]));
395 *yylval.string += YYText ();
401 /* yylval is union. Must remember STRING before setting SCM*/
402 String *sp = yylval.string;
403 yylval.scm = scm_makfrom0str (sp->to_str0 ());
405 return LYRICS_STRING;
408 *yylval.string += YYText ();
414 start_lyric_quote ();
417 yylval.scm = scan_fraction (YYText ());
421 yylval.i = String_convert::dec2int (String (YYText ()));
425 return scan_escaped_word (YYText () + 1);
429 String s (YYText ());
431 return yylval.i = EXTENDER;
433 return yylval.i = HYPHEN;
436 char c = s[s.length () - 1];
437 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
438 here_input ().warning (
439 _ ("Brace found at end of lyric. Did you forget a space?"));
440 yylval.scm = scm_makfrom0str (s.to_str0 ());
443 return LYRICS_STRING;
451 return scan_bare_word (YYText ());
454 return scan_escaped_word (YYText () + 1);
457 yylval.scm = scan_fraction (YYText ());
461 yylval.i = String_convert::dec2int (String (YYText ()));
496 String str (YYText () + 1);
497 SCM s = lookup_markup_command (str);
499 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
500 yylval.scm = scm_car(s);
501 SCM tag = scm_cdr(s);
502 if (tag == ly_symbol2scm("markup0"))
503 return MARKUP_HEAD_MARKUP0;
504 if (tag == ly_symbol2scm("empty"))
505 return MARKUP_HEAD_EMPTY;
506 else if (tag == ly_symbol2scm ("markup0-markup1"))
507 return MARKUP_HEAD_MARKUP0_MARKUP1;
508 else if (tag == ly_symbol2scm ("markup-list0"))
509 return MARKUP_HEAD_LIST0;
510 else if (tag == ly_symbol2scm ("scheme0"))
511 return MARKUP_HEAD_SCM0;
512 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
513 return MARKUP_HEAD_SCM0_SCM1;
514 else if (tag == ly_symbol2scm ("scheme0-markup1"))
515 return MARKUP_HEAD_SCM0_MARKUP1;
516 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
517 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
518 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
519 return MARKUP_HEAD_SCM0_SCM1_SCM2;
521 programming_error ("No parser tag defined for this signature. Abort");
526 return scan_escaped_word (str);
531 [^#{}"\\ \t\n\r\f]+ {
532 String s (YYText ());
534 char c = s[s.length () - 1];
535 /* brace open is for not confusing dumb tools. */
536 if (c == '{' || c == '}')
537 here_input ().warning (
538 _ ("Brace found at end of markup. Did you forget a space?"));
539 yylval.scm = scm_makfrom0str (s.to_str0 ());
552 main_input_b_ = false;
554 /* Returns YY_NULL */
557 else if (!close_input ())
558 /* Returns YY_NULL */
564 return scan_bare_word (YYText ());
567 return scan_escaped_word (YYText () + 1);
571 int cnv = sscanf (YYText (), "%lf", &r);
574 yylval.scm = scm_make_real (r);
579 yylval.i = String_convert::dec2int (String (YYText ()));
589 char c = YYText ()[0];
594 <INITIAL,notes,figures>. {
598 <INITIAL,lyrics,notes,figures>\\. {
599 char c = YYText ()[1];
607 return E_EXCLAMATION;
615 return E_RIGHTSQUARE;
627 String msg = _f ("invalid character: `%c'", YYText ()[0]);
628 LexerError (msg.to_str0 ());
635 Lily_lexer::push_chord_state (SCM tab)
637 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
638 yy_push_state (chords);
642 Lily_lexer::push_figuredbass_state ()
644 yy_push_state (figures);
648 Lily_lexer::push_initial_state ()
650 yy_push_state (INITIAL);
654 Lily_lexer::push_lyric_state ()
656 yy_push_state (lyrics);
660 Lily_lexer::push_markup_state ()
662 yy_push_state (markup);
666 Lily_lexer::push_note_state (SCM tab)
668 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
669 yy_push_state (notes);
673 Lily_lexer::pop_state ()
675 if (YYSTATE == notes || YYSTATE == chords)
676 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
681 Lily_lexer::identifier_type (SCM sid)
683 int k = try_special_identifiers (&yylval.scm , sid);
684 return k >= 0 ? k : SCM_IDENTIFIER;
689 Lily_lexer::scan_escaped_word (String str)
691 // use more SCM for this.
693 // SCM sym = ly_symbol2scm (str.to_str0 ());
695 int l = lookup_keyword (str);
699 SCM sid = lookup_identifier (str);
700 if (is_music_function (sid))
702 yylval.scm = get_music_function_transform (sid);
704 return music_function_type (yylval.scm);
707 if (sid != SCM_UNDEFINED)
710 return identifier_type (sid);
713 String msg (_f ("unknown escaped string: `\\%s'", str));
714 LexerError (msg.to_str0 ());
716 yylval.scm = scm_makfrom0str (str.to_str0 ());
722 Lily_lexer::scan_bare_word (String str)
724 SCM sym = ly_symbol2scm (str.to_str0 ());
725 if ((YYSTATE == notes) || (YYSTATE == chords)) {
726 SCM handle = SCM_BOOL_F;
727 if (scm_is_pair (pitchname_tab_stack_))
728 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
730 if (scm_is_pair (handle)) {
731 yylval.scm = scm_cdr (handle);
732 if (unsmob_pitch (yylval.scm))
733 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
734 else if (scm_is_symbol (yylval.scm))
737 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
739 yylval.scm = scm_cdr (handle);
740 return CHORD_MODIFIER;
744 yylval.scm = scm_makfrom0str (str.to_str0 ());
749 Lily_lexer::is_note_state () const
751 return YY_START == notes;
755 Lily_lexer::is_chord_state () const
757 return YY_START == chords;
761 Lily_lexer::is_lyric_state () const
763 return YY_START == lyrics;
767 Lily_lexer::is_figure_state () const
769 return YY_START == figures;
773 urg, belong to String (_convert)
774 and should be generalised
777 strip_leading_white (String&s)
780 for (; i < s.length (); i++)
784 s = s.nomid_string (0, i);
788 strip_trailing_white (String&s)
795 s = s.left_string (i+1);
800 /* 2.1.2x something -> \property -> \set. */
801 Lilypond_version oldest_version ("2.3.22");
805 is_valid_version (String s)
807 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
808 Lilypond_version ver (s);
809 if (! ((ver >= oldest_version) && (ver <= current)))
811 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
812 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
823 lyric_fudge (String s)
825 char * chars = s.get_copy_str0 ();
827 for (char * p = chars; *p ; p++)
829 if (*p == '_' && (p == chars || *(p-1) != '\\'))
837 if ((i = s.index ("\\,")) != -1) // change "\," to TeX's "\c "
839 * (s.get_str0 () + i + 1) = 'c';
840 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
847 Convert "NUM/DEN" into a '(NUM . DEN) cons.
850 scan_fraction (String frac)
852 int i = frac.index ('/');
853 int l = frac.length ();
854 String left = frac.left_string (i);
855 String right = frac.right_string (l - i - 1);
857 int n = String_convert::dec2int (left);
858 int d = String_convert::dec2int (right);
859 return scm_cons (scm_int2num (n), scm_int2num (d));
862 // Breaks for flex 2.5.31
864 /* avoid silly flex induced gcc warnings */
865 static void yy_push_state (int) {;}
866 static void yy_pop_state () {;}
867 static int yy_top_state () { return 0; }
870 avoid_silly_flex_induced_gcc_warnings ()
872 (void)yy_start_stack_ptr;
873 (void)yy_start_stack_depth;
874 (void)yy_start_stack;
878 avoid_silly_flex_induced_gcc_warnings ();
883 lookup_markup_command (String s)
885 SCM proc = ly_lily_module_constant ("lookup-markup-command");
886 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));
891 music_function_type (SCM func)
893 SCM type = scm_object_property (func, ly_symbol2scm ("music-function-signature-keyword"));
894 if (type == ly_symbol2scm ("scm"))
896 return MUSIC_FUNCTION_SCM;
898 else if (type == ly_symbol2scm ("music"))
900 return MUSIC_FUNCTION_MUSIC;
902 else if (type == ly_symbol2scm ("scm-music"))
904 return MUSIC_FUNCTION_SCM_MUSIC;
906 else if (type == ly_symbol2scm ("scm-scm"))
908 return MUSIC_FUNCTION_SCM_SCM;
910 else if (type == ly_symbol2scm ("music-music"))
912 return MUSIC_FUNCTION_MUSIC_MUSIC;
914 else if (type == ly_symbol2scm ("scm-music-music"))
916 return MUSIC_FUNCTION_SCM_MUSIC_MUSIC;
918 else if (type == ly_symbol2scm ("scm-scm-music"))
920 return MUSIC_FUNCTION_SCM_SCM_MUSIC;
922 else if (type == ly_symbol2scm ("markup"))
924 return MUSIC_FUNCTION_MARKUP;
926 else if (type == ly_symbol2scm ("markup-music"))
928 return MUSIC_FUNCTION_MARKUP_MUSIC;
930 else if (type == ly_symbol2scm ("markup-markup"))
932 return MUSIC_FUNCTION_MARKUP_MARKUP;
934 else if (type == ly_symbol2scm ("markup-music-music"))
936 return MUSIC_FUNCTION_MARKUP_MUSIC_MUSIC;
938 else if (type == ly_symbol2scm ("markup-markup-music"))
940 return MUSIC_FUNCTION_MARKUP_MARKUP_MUSIC;
942 else if (type == ly_symbol2scm ("noarg"))
944 return MUSIC_FUNCTION;
948 /* TODO: print location */
949 error ("Can not find signature for music function.");
952 return MUSIC_FUNCTION_SCM;