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@xs4all.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 :-)
30 /* Flex >= 2.5.29 fix; FlexLexer.h's multiple include bracing breaks
31 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]*
145 BOM_UTF8 \357\273\277
150 // windows-suck-suck-suck
153 <INITIAL,chords,lyrics,figures,notes>{BOM_UTF8} {
154 if (this->lexloc->line_number () != 1 || this->lexloc->column_number () != 0)
156 LexerError (_ ("stray UTF-8 BOM encountered").to_str0 ());
159 if (be_verbose_global)
160 message (_ ("Skipping UTF-8 BOM"));
163 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
165 yy_push_state (longcomment);
169 %[^{\n\r] { // backup rule
180 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
181 yy_push_state (version);
183 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}* {
184 yy_push_state (sourcefilename);
186 <version>\"[^"]*\" { /* got the version number */
187 String s (YYText () + 1);
188 s = s.left_string (s.index_last ('\"'));
191 if (!is_valid_version (s))
194 SCM top_scope = scm_car (scm_last_pair (scopes_));
195 scm_module_define (top_scope, ly_symbol2scm ("version-seen?"), SCM_BOOL_T);
198 <sourcefilename>\"[^"]*\" {
199 String s (YYText () + 1);
200 s = s.left_string (s.index_last ('\"'));
203 this->here_input().get_source_file ()->name_ = s;
204 message (_f ("Renaming input to: `%s'", s.to_str0 ()));
205 progress_indication ("\n");
206 scm_module_define (scm_car (scopes_),
207 ly_symbol2scm ("input-file-name"),
208 scm_makfrom0str (s.to_str0 ()));
212 LexerError (_ ("quoted string expected after \\version").to_str0 ());
216 LexerError (_ ("quoted string expected after \\sourcefilename").to_str0 ());
229 LexerError (_ ("EOF found inside a comment").to_str0 ());
230 is_main_input_ = 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 is_main_input_ = 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 (_ ("end quote missing"));
280 <chords,notes,figures>{RESTNAME} {
281 char const *s = YYText ();
282 yylval.scm = scm_makfrom0str (s);
285 <chords,notes,figures>R {
286 return MULTI_MEASURE_REST;
288 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
290 Input hi = here_input();
292 SCM sval = ly_parse_scm (hi.start (), &n, hi,
293 be_safe_global && is_main_input_);
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 markup signature");
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 is_main_input_ = 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_from_double (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];
603 return E_ANGLE_CLOSE;
607 return E_EXCLAMATION;
613 return E_BRACKET_OPEN;
617 return E_BRACKET_CLOSE;
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 i = lookup_keyword (str);
698 if (i == MARKUP && is_lyric_state ())
703 SCM sid = lookup_identifier (str);
704 if (is_music_function (sid))
706 yylval.scm = get_music_function_transform (sid);
707 return music_function_type (yylval.scm);
710 if (sid != SCM_UNDEFINED)
713 return identifier_type (sid);
716 String msg (_f ("unknown escaped string: `\\%s'", str));
717 LexerError (msg.to_str0 ());
719 yylval.scm = scm_makfrom0str (str.to_str0 ());
725 Lily_lexer::scan_bare_word (String str)
727 SCM sym = ly_symbol2scm (str.to_str0 ());
728 if ((YYSTATE == notes) || (YYSTATE == chords)) {
729 SCM handle = SCM_BOOL_F;
730 if (scm_is_pair (pitchname_tab_stack_))
731 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
733 if (scm_is_pair (handle)) {
734 yylval.scm = scm_cdr (handle);
735 if (unsmob_pitch (yylval.scm))
736 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
737 else if (scm_is_symbol (yylval.scm))
740 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
742 yylval.scm = scm_cdr (handle);
743 return CHORD_MODIFIER;
747 yylval.scm = scm_makfrom0str (str.to_str0 ());
752 Lily_lexer::is_note_state () const
754 return YY_START == notes;
758 Lily_lexer::is_chord_state () const
760 return YY_START == chords;
764 Lily_lexer::is_lyric_state () const
766 return YY_START == lyrics;
770 Lily_lexer::is_figure_state () const
772 return YY_START == figures;
776 urg, belong to String (_convert)
777 and should be generalised
780 strip_leading_white (String&s)
783 for (; i < s.length (); i++)
787 s = s.nomid_string (0, i);
791 strip_trailing_white (String&s)
798 s = s.left_string (i+1);
803 /* 2.1.2x something -> \property -> \set. */
804 Lilypond_version oldest_version ("2.3.22");
808 is_valid_version (String s)
810 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
811 Lilypond_version ver (s);
812 if (! ((ver >= oldest_version) && (ver <= current)))
814 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
815 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
826 lyric_fudge (String s)
828 char * chars = s.get_copy_str0 ();
830 for (char * p = chars; *p ; p++)
832 if (*p == '_' && (p == chars || *(p-1) != '\\'))
840 if ((i = s.index ("\\,")) != -1) // change "\," to TeX's "\c "
842 * (s.get_str0 () + i + 1) = 'c';
843 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
850 Convert "NUM/DEN" into a '(NUM . DEN) cons.
853 scan_fraction (String frac)
855 int i = frac.index ('/');
856 int l = frac.length ();
857 String left = frac.left_string (i);
858 String right = frac.right_string (l - i - 1);
860 int n = String_convert::dec2int (left);
861 int d = String_convert::dec2int (right);
862 return scm_cons (scm_from_int (n), scm_from_int (d));
866 lookup_markup_command (String s)
868 SCM proc = ly_lily_module_constant ("lookup-markup-command");
869 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));
874 music_function_type (SCM func)
876 SCM type = scm_object_property (func, ly_symbol2scm ("music-function-signature-keyword"));
877 if (type == ly_symbol2scm ("scm"))
879 return MUSIC_FUNCTION_SCM;
881 else if (type == ly_symbol2scm ("music"))
883 return MUSIC_FUNCTION_MUSIC;
885 else if (type == ly_symbol2scm ("scm-music"))
887 return MUSIC_FUNCTION_SCM_MUSIC;
889 else if (type == ly_symbol2scm ("scm-scm"))
891 return MUSIC_FUNCTION_SCM_SCM;
893 else if (type == ly_symbol2scm ("music-music"))
895 return MUSIC_FUNCTION_MUSIC_MUSIC;
897 else if (type == ly_symbol2scm ("scm-music-music"))
899 return MUSIC_FUNCTION_SCM_MUSIC_MUSIC;
901 else if (type == ly_symbol2scm ("scm-scm-music"))
903 return MUSIC_FUNCTION_SCM_SCM_MUSIC;
905 else if (type == ly_symbol2scm ("scm-scm-scm"))
907 return MUSIC_FUNCTION_SCM_SCM_SCM;
909 else if (type == ly_symbol2scm ("markup"))
911 return MUSIC_FUNCTION_MARKUP;
913 else if (type == ly_symbol2scm ("markup-music"))
915 return MUSIC_FUNCTION_MARKUP_MUSIC;
917 else if (type == ly_symbol2scm ("markup-markup"))
919 return MUSIC_FUNCTION_MARKUP_MARKUP;
921 else if (type == ly_symbol2scm ("markup-music-music"))
923 return MUSIC_FUNCTION_MARKUP_MUSIC_MUSIC;
925 else if (type == ly_symbol2scm ("markup-markup-music"))
927 return MUSIC_FUNCTION_MARKUP_MARKUP_MUSIC;
929 else if (type == ly_symbol2scm ("noarg"))
931 return MUSIC_FUNCTION;
935 /* TODO: print location */
936 error (_ ("can't find signature for music function"));
939 return MUSIC_FUNCTION_SCM;
942 /* Shut up lexer warnings. */
962 silence_lexer_warnings ()
964 (void) yy_start_stack_ptr;
965 (void) yy_start_stack_depth;
966 (void) yy_start_stack;
967 (void) yy_push_state;
970 (void) silence_lexer_warnings;