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 ()));
492 String str (YYText () + 1);
493 SCM s = lookup_markup_command (str);
495 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
496 yylval.scm = scm_car(s);
497 SCM tag = scm_cdr(s);
498 if (tag == ly_symbol2scm("markup0"))
499 return MARKUP_HEAD_MARKUP0;
500 if (tag == ly_symbol2scm("empty"))
501 return MARKUP_HEAD_EMPTY;
502 else if (tag == ly_symbol2scm ("markup0-markup1"))
503 return MARKUP_HEAD_MARKUP0_MARKUP1;
504 else if (tag == ly_symbol2scm ("markup-list0"))
505 return MARKUP_HEAD_LIST0;
506 else if (tag == ly_symbol2scm ("scheme0"))
507 return MARKUP_HEAD_SCM0;
508 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
509 return MARKUP_HEAD_SCM0_SCM1;
510 else if (tag == ly_symbol2scm ("scheme0-markup1"))
511 return MARKUP_HEAD_SCM0_MARKUP1;
512 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
513 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
514 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
515 return MARKUP_HEAD_SCM0_SCM1_SCM2;
517 programming_error ("No parser tag defined for this signature. Abort");
522 return scan_escaped_word (str);
527 [^#{}"\\ \t\n\r\f]+ {
528 String s (YYText ());
530 char c = s[s.length () - 1];
531 /* brace open is for not confusing dumb tools. */
532 if (c == '{' || c == '}')
533 here_input ().warning (
534 _ ("Brace found at end of markup. Did you forget a space?"));
535 yylval.scm = scm_makfrom0str (s.to_str0 ());
548 main_input_b_ = false;
550 /* Returns YY_NULL */
553 else if (!close_input ())
554 /* Returns YY_NULL */
560 return scan_bare_word (YYText ());
563 return scan_escaped_word (YYText () + 1);
567 int cnv=sscanf (YYText (), "%lf", &r);
570 yylval.scm = scm_make_real (r);
575 yylval.i = String_convert::dec2int (String (YYText ()));
585 char c = YYText ()[0];
590 <INITIAL,notes,figures>. {
594 <INITIAL,lyrics,notes,figures>\\. {
595 char c= YYText ()[1];
603 return E_EXCLAMATION;
611 return E_RIGHTSQUARE;
623 String msg = _f ("invalid character: `%c'", YYText ()[0]);
624 LexerError (msg.to_str0 ());
631 Lily_lexer::push_chord_state (SCM tab)
633 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
634 yy_push_state (chords);
638 Lily_lexer::push_figuredbass_state ()
640 yy_push_state (figures);
644 Lily_lexer::push_initial_state ()
646 yy_push_state (INITIAL);
650 Lily_lexer::push_lyric_state ()
652 yy_push_state (lyrics);
656 Lily_lexer::push_markup_state ()
658 yy_push_state (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 l = lookup_keyword (str);
695 SCM sid = lookup_identifier (str);
696 if (is_music_function (sid))
698 yylval.scm = get_music_function_transform (sid);
700 return music_function_type (yylval.scm);
703 if (sid != SCM_UNDEFINED)
706 return identifier_type (sid);
709 String msg (_f ("unknown escaped string: `\\%s'", str));
710 LexerError (msg.to_str0 ());
712 yylval.scm = scm_makfrom0str (str.to_str0 ());
718 Lily_lexer::scan_bare_word (String str)
720 SCM sym = ly_symbol2scm (str.to_str0 ());
721 if ((YYSTATE == notes) || (YYSTATE == chords)) {
722 SCM handle = SCM_BOOL_F;
723 if (scm_is_pair (pitchname_tab_stack_))
724 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
726 if (scm_is_pair (handle)) {
727 yylval.scm = scm_cdr (handle);
728 if (unsmob_pitch (yylval.scm))
729 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
730 else if (scm_is_symbol (yylval.scm))
733 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
735 yylval.scm = scm_cdr (handle);
736 return CHORD_MODIFIER;
740 yylval.scm = scm_makfrom0str (str.to_str0 ());
745 Lily_lexer::is_note_state () const
747 return YY_START == notes;
751 Lily_lexer::is_chord_state () const
753 return YY_START == chords;
757 Lily_lexer::is_lyric_state () const
759 return YY_START == lyrics;
763 Lily_lexer::is_figure_state () const
765 return YY_START == figures;
769 urg, belong to String (_convert)
770 and should be generalised
773 strip_leading_white (String&s)
776 for (; i < s.length (); i++)
780 s = s.nomid_string (0, i);
784 strip_trailing_white (String&s)
791 s = s.left_string (i+1);
796 /* 2.1.2x something -> \property -> \set. */
797 Lilypond_version oldest_version ("2.3.22");
801 is_valid_version (String s)
803 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
804 Lilypond_version ver (s);
805 if (! ((ver >= oldest_version) && (ver <= current)))
807 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
808 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
819 lyric_fudge (String s)
821 char * chars =s.get_copy_str0 ();
823 for (char * p = chars; *p ; p++)
825 if (*p == '_' && (p == chars || *(p-1) != '\\'))
833 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
835 * (s.get_str0 () + i + 1) = 'c';
836 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
843 Convert "NUM/DEN" into a '(NUM . DEN) cons.
846 scan_fraction (String frac)
848 int i = frac.index ('/');
849 int l = frac.length ();
850 String left = frac.left_string (i);
851 String right = frac.right_string (l - i - 1);
853 int n = String_convert::dec2int (left);
854 int d = String_convert::dec2int (right);
855 return scm_cons (scm_int2num (n), scm_int2num (d));
858 // Breaks for flex 2.5.31
860 /* avoid silly flex induced gcc warnings */
861 static void yy_push_state (int) {;}
862 static void yy_pop_state () {;}
863 static int yy_top_state () { return 0; }
866 avoid_silly_flex_induced_gcc_warnings ()
868 (void)yy_start_stack_ptr;
869 (void)yy_start_stack_depth;
870 (void)yy_start_stack;
874 avoid_silly_flex_induced_gcc_warnings ();
879 lookup_markup_command (String s)
881 SCM proc = ly_scheme_function ("lookup-markup-command");
882 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));
887 music_function_type (SCM func)
889 SCM type= scm_object_property (func, ly_symbol2scm ("music-function-signature-keyword"));
890 if (type == ly_symbol2scm ("scm"))
892 return MUSIC_FUNCTION_SCM;
894 else if (type == ly_symbol2scm ("music"))
896 return MUSIC_FUNCTION_MUSIC;
898 else if (type == ly_symbol2scm ("scm-music"))
900 return MUSIC_FUNCTION_SCM_MUSIC;
902 else if (type == ly_symbol2scm ("scm-scm"))
904 return MUSIC_FUNCTION_SCM_SCM;
906 else if (type == ly_symbol2scm ("music-music"))
908 return MUSIC_FUNCTION_MUSIC_MUSIC;
910 else if (type == ly_symbol2scm ("scm-music-music"))
912 return MUSIC_FUNCTION_SCM_MUSIC_MUSIC;
914 else if (type == ly_symbol2scm ("scm-scm-music"))
916 return MUSIC_FUNCTION_SCM_SCM_MUSIC;
918 else if (type == ly_symbol2scm ("markup"))
920 return MUSIC_FUNCTION_MARKUP;
922 else if (type == ly_symbol2scm ("markup-music"))
924 return MUSIC_FUNCTION_MARKUP_MUSIC;
926 else if (type == ly_symbol2scm ("markup-markup"))
928 return MUSIC_FUNCTION_MARKUP_MARKUP;
930 else if (type == ly_symbol2scm ("markup-music-music"))
932 return MUSIC_FUNCTION_MARKUP_MUSIC_MUSIC;
934 else if (type == ly_symbol2scm ("markup-markup-music"))
936 return MUSIC_FUNCTION_MARKUP_MARKUP_MUSIC;
938 else if (type == ly_symbol2scm ("noarg"))
940 return MUSIC_FUNCTION;
944 /* TODO: print location */
945 error ("Can not find signature for music function.");
948 return MUSIC_FUNCTION_SCM;