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,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 ("\n");
197 progress_indication (_f ("input renamed to: `%s'", s.to_str0 ()));
198 progress_indication ("\n");
199 scm_module_define (scm_car (scopes_),
200 ly_symbol2scm ("input-file-name"),
201 scm_makfrom0str (s.to_str0()));
205 LexerError (_ ("No quoted string found after \\encoding").to_str0 ());
209 LexerError (_ ("No quoted string found after \\version").to_str0 ());
213 LexerError (_ ("No quoted string found after \\renameinput").to_str0 ());
226 LexerError (_ ("EOF found inside a comment").to_str0 ());
227 main_input_b_ = false;
228 if (! close_input ())
229 yyterminate (); // can't move this, since it actually rets a YY_NULL
234 <INITIAL,chords,lyrics,notes,figures>\\maininput {
238 main_input_b_ = true;
241 error (_ ("\\maininput not allowed outside init files"));
244 <INITIAL,chords,lyrics,figures,notes>\\include {
245 yy_push_state (incl);
247 <incl>\"[^"]*\";? { /* got the include file name */
248 /* FIXME: semicolon? */
249 String s (YYText ()+1);
250 s = s.left_string (s.index_last ('"'));
252 new_input (s, sources_);
255 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
256 /* FIXME: semicolon? */
257 String s = YYText () + 1;
258 strip_trailing_white (s);
259 if (s.length () && (s[s.length () - 1] == ';'))
260 s = s.left_string (s.length () - 1);
262 SCM sid = lookup_identifier (s);
263 if (scm_is_string (sid)) {
264 new_input (ly_scm2string (sid), sources_);
267 String msg (_f ("wrong or undefined identifier: `%s'", s ));
269 LexerError (msg.to_str0 ());
270 SCM err = scm_current_error_port ();
271 scm_puts ("This value was found in the table: ", err);
272 scm_display (sid, err);
275 <incl>\"[^"]* { // backup rule
276 error (_ ("Missing end quote"));
279 <chords,notes,figures>{RESTNAME} {
280 const char *s = YYText ();
281 yylval.scm = scm_makfrom0str (s);
284 <chords,notes,figures>R {
285 return MULTI_MEASURE_REST;
287 <INITIAL,markup,chords,lyrics,notes,figures># { //embedded scm
288 //char const* s = YYText () + 1;
289 char const* s = here_str0 ();
291 SCM sval = ly_parse_scm (s, &n, here_input (),
292 safe_global_b && main_input_b_);
294 if (sval == SCM_UNDEFINED)
296 sval = SCM_UNSPECIFIED;
300 for (int i=0; i < n; i++)
304 char_count_stack_.top () += n;
306 if (unpack_identifier (sval) != SCM_UNDEFINED)
308 yylval.scm = unpack_identifier(sval);
309 return identifier_type (yylval.scm);
315 <INITIAL,notes,lyrics>{
317 return DOUBLE_ANGLE_OPEN;
320 return DOUBLE_ANGLE_CLOSE;
337 return scan_bare_word (YYText ());
341 return scan_escaped_word (YYText () + 1);
344 yylval.scm = scan_fraction (YYText ());
349 yylval.i = String_convert::dec2int (String (YYText ()));
353 yylval.i = String_convert::dec2int (String (YYText ()));
357 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 ()));
502 String str (YYText () + 1);
503 SCM s = lookup_markup_command (str);
505 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
506 yylval.scm = scm_car(s);
507 SCM tag = scm_cdr(s);
508 if (tag == ly_symbol2scm("markup0"))
509 return MARKUP_HEAD_MARKUP0;
510 if (tag == ly_symbol2scm("empty"))
511 return MARKUP_HEAD_EMPTY;
512 else if (tag == ly_symbol2scm ("markup0-markup1"))
513 return MARKUP_HEAD_MARKUP0_MARKUP1;
514 else if (tag == ly_symbol2scm ("markup-list0"))
515 return MARKUP_HEAD_LIST0;
516 else if (tag == ly_symbol2scm ("scheme0"))
517 return MARKUP_HEAD_SCM0;
518 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
519 return MARKUP_HEAD_SCM0_SCM1;
520 else if (tag == ly_symbol2scm ("scheme0-markup1"))
521 return MARKUP_HEAD_SCM0_MARKUP1;
522 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
523 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
524 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
525 return MARKUP_HEAD_SCM0_SCM1_SCM2;
527 programming_error ("No parser tag defined for this signature. Abort");
532 return scan_escaped_word (str);
537 [^#{}"\\ \t\n\r\f]+ {
538 String s (YYText ());
540 char c = s[s.length () - 1];
541 /* brace open is for not confusing dumb tools. */
542 if (c == '{' || c == '}')
543 here_input ().warning (
544 _ ("Brace found at end of markup. Did you forget a space?"));
545 yylval.scm = scm_makfrom0str (s.to_str0 ());
558 main_input_b_ = false;
560 /* Returns YY_NULL */
563 else if (!close_input ())
564 /* Returns YY_NULL */
570 return scan_bare_word (YYText ());
573 return scan_escaped_word (YYText () + 1);
577 int cnv=sscanf (YYText (), "%lf", &r);
580 yylval.scm = scm_make_real (r);
585 yylval.i = String_convert::dec2int (String (YYText ()));
595 char c = YYText ()[0];
600 <INITIAL,notes,figures>. {
604 <INITIAL,lyrics,notes,figures>\\. {
605 char c= YYText ()[1];
613 return E_EXCLAMATION;
621 return E_RIGHTSQUARE;
633 String msg = _f ("invalid character: `%c'", YYText ()[0]);
634 LexerError (msg.to_str0 ());
641 Lily_lexer::push_chord_state (SCM tab)
643 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
644 yy_push_state (chords);
648 Lily_lexer::push_figuredbass_state ()
650 yy_push_state (figures);
654 Lily_lexer::push_initial_state ()
656 yy_push_state (INITIAL);
660 Lily_lexer::push_lyric_state ()
662 yy_push_state (lyrics);
666 Lily_lexer::push_markup_state ()
668 yy_push_state (markup);
672 Lily_lexer::push_note_state (SCM tab)
674 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
675 yy_push_state (notes);
679 Lily_lexer::pop_state ()
681 if (YYSTATE == notes || YYSTATE == chords)
682 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
687 Lily_lexer::identifier_type (SCM sid)
689 int k = try_special_identifiers (&yylval.scm , sid);
690 return k >= 0 ? k : SCM_IDENTIFIER;
695 Lily_lexer::scan_escaped_word (String str)
697 // use more SCM for this.
699 // SCM sym = ly_symbol2scm (str.to_str0 ());
701 int l = lookup_keyword (str);
705 SCM sid = lookup_identifier (str);
706 if (is_music_function (sid))
708 yylval.scm = get_music_function_transform (sid);
710 return music_function_type (yylval.scm);
713 if (sid != SCM_UNDEFINED)
716 return identifier_type (sid);
719 String msg (_f ("unknown escaped string: `\\%s'", str));
720 LexerError (msg.to_str0 ());
722 yylval.scm = scm_makfrom0str (str.to_str0 ());
728 Lily_lexer::scan_bare_word (String str)
730 SCM sym = ly_symbol2scm (str.to_str0 ());
731 if ((YYSTATE == notes) || (YYSTATE == chords)) {
732 SCM handle = SCM_BOOL_F;
733 if (scm_is_pair (pitchname_tab_stack_))
734 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
736 if (scm_is_pair (handle)) {
737 yylval.scm = scm_cdr (handle);
738 if (unsmob_pitch (yylval.scm))
739 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
740 else if (scm_is_symbol (yylval.scm))
743 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
745 yylval.scm = scm_cdr (handle);
746 return CHORD_MODIFIER;
750 yylval.scm = scm_makfrom0str (str.to_str0 ());
755 Lily_lexer::is_note_state () const
757 return YY_START == notes;
761 Lily_lexer::is_chord_state () const
763 return YY_START == chords;
767 Lily_lexer::is_lyric_state () const
769 return YY_START == lyrics;
773 Lily_lexer::is_figure_state () const
775 return YY_START == figures;
779 urg, belong to String (_convert)
780 and should be generalised
783 strip_leading_white (String&s)
786 for (; i < s.length (); i++)
790 s = s.nomid_string (0, i);
794 strip_trailing_white (String&s)
801 s = s.left_string (i+1);
806 /* 2.1.2x something -> \property -> \set. */
807 Lilypond_version oldest_version ("2.3.22");
811 is_valid_version (String s)
813 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
814 Lilypond_version ver (s);
815 if (! ((ver >= oldest_version) && (ver <= current)))
817 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
818 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
829 lyric_fudge (String s)
831 char * chars =s.get_copy_str0 ();
833 for (char * p = chars; *p ; p++)
835 if (*p == '_' && (p == chars || *(p-1) != '\\'))
843 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
845 * (s.get_str0 () + i + 1) = 'c';
846 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
853 Convert "NUM/DEN" into a '(NUM . DEN) cons.
856 scan_fraction (String frac)
858 int i = frac.index ('/');
859 int l = frac.length ();
860 String left = frac.left_string (i);
861 String right = frac.right_string (l - i - 1);
863 int n = String_convert::dec2int (left);
864 int d = String_convert::dec2int (right);
865 return scm_cons (scm_int2num (n), scm_int2num (d));
868 // Breaks for flex 2.5.31
870 /* avoid silly flex induced gcc warnings */
871 static void yy_push_state (int) {;}
872 static void yy_pop_state () {;}
873 static int yy_top_state () { return 0; }
876 avoid_silly_flex_induced_gcc_warnings ()
878 (void)yy_start_stack_ptr;
879 (void)yy_start_stack_depth;
880 (void)yy_start_stack;
884 avoid_silly_flex_induced_gcc_warnings ();
889 lookup_markup_command (String s)
891 SCM proc = ly_scheme_function ("lookup-markup-command");
892 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));
897 music_function_type (SCM func)
899 SCM type= scm_object_property (func, ly_symbol2scm ("music-function-signature-keyword"));
900 if (type == ly_symbol2scm ("scm"))
902 return MUSIC_FUNCTION_SCM;
904 else if (type == ly_symbol2scm ("music"))
906 return MUSIC_FUNCTION_MUSIC;
908 else if (type == ly_symbol2scm ("scm-music"))
910 return MUSIC_FUNCTION_SCM_MUSIC;
912 else if (type == ly_symbol2scm ("scm-scm"))
914 return MUSIC_FUNCTION_SCM_SCM;
916 else if (type == ly_symbol2scm ("music-music"))
918 return MUSIC_FUNCTION_MUSIC_MUSIC;
920 else if (type == ly_symbol2scm ("scm-music-music"))
922 return MUSIC_FUNCTION_SCM_MUSIC_MUSIC;
924 else if (type == ly_symbol2scm ("scm-scm-music"))
926 return MUSIC_FUNCTION_SCM_SCM_MUSIC;
928 else if (type == ly_symbol2scm ("markup"))
930 return MUSIC_FUNCTION_MARKUP;
932 else if (type == ly_symbol2scm ("markup-music"))
934 return MUSIC_FUNCTION_MARKUP_MUSIC;
936 else if (type == ly_symbol2scm ("markup-markup"))
938 return MUSIC_FUNCTION_MARKUP_MARKUP;
940 else if (type == ly_symbol2scm ("markup-music-music"))
942 return MUSIC_FUNCTION_MARKUP_MUSIC_MUSIC;
944 else if (type == ly_symbol2scm ("markup-markup-music"))
946 return MUSIC_FUNCTION_MARKUP_MARKUP_MUSIC;
948 else if (type == ly_symbol2scm ("noarg"))
950 return MUSIC_FUNCTION;
954 /* TODO: print location */
955 error ("Can not find signature for music function.");
958 return MUSIC_FUNCTION_SCM;