1 %{ // -*-Fundamental-*-
3 lexer.ll -- implement the Flex lexer
5 source file of the LilyPond music typesetter
7 (c) 1996--2006 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 "context-def.hh"
39 #include "identifier-smob.hh"
40 #include "international.hh"
41 #include "interval.hh"
42 #include "lily-guile.hh"
43 #include "lily-lexer.hh"
44 #include "lilypond-input-version.hh"
46 #include "music-function.hh"
47 #include "parse-scm.hh"
49 #include "source-file.hh"
50 #include "std-string.hh"
51 #include "string-convert.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}
127 DASHED_WORD {A}({AN}|-)*
128 DASHED_KEY_WORD \\{DASHED_WORD}
135 REAL ({INT}\.{N}*)|(-?\.{N}+)
138 HORIZONTALWHITE [ \t]
142 MARKUPCOMMAND \\({A}|[-_])+
143 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
147 BOM_UTF8 \357\273\277
152 // windows-suck-suck-suck
155 <INITIAL,chords,lyrics,figures,notes>{BOM_UTF8} {
156 if (this->lexloc->line_number () != 1 || this->lexloc->column_number () != 0)
158 LexerError (_ ("stray UTF-8 BOM encountered").c_str ());
161 if (be_verbose_global)
162 message (_ ("Skipping UTF-8 BOM"));
165 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
167 yy_push_state (longcomment);
171 %[^{\n\r] { // backup rule
182 <INITIAL,notes,figures,chords,markup>{
188 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
189 yy_push_state (version);
191 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}* {
192 yy_push_state (sourcefilename);
194 <version>\"[^"]*\" { /* got the version number */
195 string s (YYText () + 1);
196 s = s.substr (0, s.rfind ('\"'));
200 SCM top_scope = scm_car (scm_last_pair (scopes_));
201 scm_module_define (top_scope, ly_symbol2scm ("version-seen"), SCM_BOOL_T);
203 if (!is_valid_version (s))
208 <sourcefilename>\"[^"]*\" {
209 string s (YYText () + 1);
210 s = s.substr (0, s.rfind ('\"'));
213 this->here_input().get_source_file ()->name_ = s;
214 message (_f ("Renaming input to: `%s'", s.c_str ()));
215 progress_indication ("\n");
216 scm_module_define (scm_car (scopes_),
217 ly_symbol2scm ("input-file-name"),
218 scm_makfrom0str (s.c_str ()));
222 LexerError (_ ("quoted string expected after \\version").c_str ());
226 LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
239 LexerError (_ ("EOF found inside a comment").c_str ());
240 is_main_input_ = false;
241 if (! close_input ())
242 yyterminate (); // can't move this, since it actually rets a YY_NULL
247 <INITIAL,chords,lyrics,notes,figures>\\maininput {
251 is_main_input_ = true;
254 error (_ ("\\maininput not allowed outside init files"));
257 <INITIAL,chords,lyrics,figures,notes>\\include {
258 yy_push_state (incl);
260 <incl>\"[^"]*\" { /* got the include file name */
261 string s (YYText ()+1);
262 s = s.substr (0, s.rfind ('"'));
264 new_input (s, sources_);
267 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
268 string s = YYText () + 1;
269 strip_trailing_white (s);
270 if (s.length () && (s[s.length () - 1] == ';'))
271 s = s.substr (0, s.length () - 1);
273 SCM sid = lookup_identifier (s);
274 if (scm_is_string (sid)) {
275 new_input (ly_scm2string (sid), sources_);
278 string msg (_f ("wrong or undefined identifier: `%s'", s ));
280 LexerError (msg.c_str ());
281 SCM err = scm_current_error_port ();
282 scm_puts ("This value was found in the table: ", err);
283 scm_display (sid, err);
286 <incl>\"[^"]* { // backup rule
287 error (_ ("end quote missing"));
290 <chords,notes,figures>{RESTNAME} {
291 char const *s = YYText ();
292 yylval.scm = scm_makfrom0str (s);
295 <chords,notes,figures>R {
296 return MULTI_MEASURE_REST;
298 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
300 Input hi = here_input();
302 SCM sval = ly_parse_scm (hi.start (), &n, hi,
303 be_safe_global && is_main_input_);
305 if (sval == SCM_UNDEFINED)
307 sval = SCM_UNSPECIFIED;
311 for (int i = 0; i < n; i++)
315 char_count_stack_.back () += n;
317 if (unpack_identifier (sval) != SCM_UNDEFINED)
319 yylval.scm = unpack_identifier(sval);
320 return identifier_type (yylval.scm);
326 <INITIAL,notes,lyrics>{
328 return DOUBLE_ANGLE_OPEN;
331 return DOUBLE_ANGLE_CLOSE;
358 return scan_bare_word (YYText ());
362 return scan_escaped_word (YYText () + 1);
365 yylval.scm = scan_fraction (YYText ());
370 yylval.i = String_convert::dec2int (string (YYText ()));
374 yylval.i = String_convert::dec2int (string (YYText ()));
378 yylval.i = String_convert::dec2int (string (YYText () +1));
385 *yylval.string += to_string (escaped_char (YYText ()[1]));
388 *yylval.string += YYText ();
394 /* yylval is union. Must remember STRING before setting SCM*/
395 string *sp = yylval.string;
396 yylval.scm = scm_makfrom0str (sp->c_str ());
398 return is_lyric_state () ? LYRICS_STRING : STRING;
401 *yylval.string += YYText ();
407 start_lyric_quote ();
410 yylval.scm = scan_fraction (YYText ());
414 yylval.i = String_convert::dec2int (string (YYText ()));
418 return scan_escaped_word (YYText () + 1);
422 string s (YYText ());
424 return yylval.i = EXTENDER;
426 return yylval.i = HYPHEN;
429 char c = s[s.length () - 1];
430 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
431 here_input ().warning (
432 _ ("Brace found at end of lyric. Did you forget a space?"));
433 yylval.scm = scm_makfrom0str (s.c_str ());
436 return LYRICS_STRING;
444 return scan_bare_word (YYText ());
447 return scan_escaped_word (YYText () + 1);
450 yylval.scm = scan_fraction (YYText ());
454 yylval.i = String_convert::dec2int (string (YYText ()));
483 string str (YYText () + 1);
484 SCM s = lookup_markup_command (str);
486 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
487 yylval.scm = scm_car(s);
488 SCM tag = scm_cdr(s);
489 if (tag == ly_symbol2scm("markup0"))
490 return MARKUP_HEAD_MARKUP0;
491 if (tag == ly_symbol2scm("empty"))
492 return MARKUP_HEAD_EMPTY;
493 else if (tag == ly_symbol2scm ("markup0-markup1"))
494 return MARKUP_HEAD_MARKUP0_MARKUP1;
495 else if (tag == ly_symbol2scm ("markup-list0"))
496 return MARKUP_HEAD_LIST0;
497 else if (tag == ly_symbol2scm ("scheme0"))
498 return MARKUP_HEAD_SCM0;
499 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
500 return MARKUP_HEAD_SCM0_SCM1;
501 else if (tag == ly_symbol2scm ("scheme0-markup1"))
502 return MARKUP_HEAD_SCM0_MARKUP1;
503 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
504 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
505 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
506 return MARKUP_HEAD_SCM0_SCM1_SCM2;
508 programming_error ("no parser tag defined for this markup signature");
513 return scan_escaped_word (str);
518 [^#{}"\\ \t\n\r\f]+ {
519 string s (YYText ());
521 char c = s[s.length () - 1];
522 /* brace open is for not confusing dumb tools. */
523 if (c == '{' || c == '}')
524 here_input ().warning (
525 _ ("Brace found at end of markup. Did you forget a space?"));
526 yylval.scm = scm_makfrom0str (s.c_str ());
539 is_main_input_ = false;
541 /* Returns YY_NULL */
544 else if (!close_input ())
545 /* Returns YY_NULL */
551 return scan_bare_word (YYText ());
554 return scan_escaped_word (YYText () + 1);
559 return scan_bare_word (YYText ());
562 return scan_escaped_word (YYText () + 1);
566 int cnv = sscanf (YYText (), "%lf", &r);
570 yylval.scm = scm_from_double (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];
599 return E_ANGLE_CLOSE;
603 return E_EXCLAMATION;
609 return E_BRACKET_OPEN;
613 return E_BRACKET_CLOSE;
625 string msg = _f ("invalid character: `%c'", YYText ()[0]);
626 LexerError (msg.c_str ());
633 Lily_lexer::push_chord_state (SCM tab)
635 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
636 yy_push_state (chords);
640 Lily_lexer::push_figuredbass_state ()
642 yy_push_state (figures);
646 Lily_lexer::push_initial_state ()
648 yy_push_state (INITIAL);
652 Lily_lexer::push_lyric_state ()
654 yy_push_state (lyrics);
658 Lily_lexer::push_markup_state ()
660 yy_push_state (markup);
664 Lily_lexer::push_note_state (SCM tab)
666 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
667 yy_push_state (notes);
671 Lily_lexer::pop_state ()
673 if (YYSTATE == notes || YYSTATE == chords)
674 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
679 Lily_lexer::identifier_type (SCM sid)
681 int k = try_special_identifiers (&yylval.scm , sid);
682 return k >= 0 ? k : SCM_IDENTIFIER;
687 Lily_lexer::scan_escaped_word (string str)
689 // use more SCM for this.
691 // SCM sym = ly_symbol2scm (str.c_str ());
693 int i = lookup_keyword (str);
694 if (i == MARKUP && is_lyric_state ())
699 SCM sid = lookup_identifier (str);
700 if (is_music_function (sid))
702 yylval.scm = get_music_function_transform (sid);
703 return music_function_type (yylval.scm);
706 if (sid != SCM_UNDEFINED)
709 return identifier_type (sid);
712 string msg (_f ("unknown escaped string: `\\%s'", str));
713 LexerError (msg.c_str ());
715 yylval.scm = scm_makfrom0str (str.c_str ());
721 Lily_lexer::scan_bare_word (string str)
723 SCM sym = ly_symbol2scm (str.c_str ());
724 if ((YYSTATE == notes) || (YYSTATE == chords)) {
725 SCM handle = SCM_BOOL_F;
726 if (scm_is_pair (pitchname_tab_stack_))
727 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
729 if (scm_is_pair (handle)) {
730 yylval.scm = scm_cdr (handle);
731 if (unsmob_pitch (yylval.scm))
732 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
733 else if (scm_is_symbol (yylval.scm))
736 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
738 yylval.scm = scm_cdr (handle);
739 return CHORD_MODIFIER;
743 yylval.scm = scm_makfrom0str (str.c_str ());
748 Lily_lexer::is_note_state () const
750 return YY_START == notes;
754 Lily_lexer::is_chord_state () const
756 return YY_START == chords;
760 Lily_lexer::is_lyric_state () const
762 return YY_START == lyrics;
766 Lily_lexer::is_figure_state () const
768 return YY_START == figures;
772 urg, belong to string (_convert)
773 and should be generalised
776 strip_leading_white (string&s)
779 for (; i < s.length (); i++)
787 strip_trailing_white (string&s)
789 ssize i = s.length ();
794 s = s.substr (0, i + 1);
799 Lilypond_version oldest_version ("2.7.38");
803 is_valid_version (string s)
805 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
806 Lilypond_version ver (s);
807 if (! ((ver >= oldest_version) && (ver <= current)))
809 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
810 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
821 lyric_fudge (string s)
823 char *chars = string_copy (s);
825 for (char *p = chars; *p ; p++)
827 if (*p == '_' && (p == chars || *(p-1) != '\\'))
835 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
837 * (((char*)s.c_str ()) + i + 1) = 'c';
838 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
845 Convert "NUM/DEN" into a '(NUM . DEN) cons.
848 scan_fraction (string frac)
850 ssize i = frac.find ('/');
851 string left = frac.substr (0, i);
852 string right = frac.substr (i + 1, (frac.length () - i + 1));
854 int n = String_convert::dec2int (left);
855 int d = String_convert::dec2int (right);
856 return scm_cons (scm_from_int (n), scm_from_int (d));
860 lookup_markup_command (string s)
862 SCM proc = ly_lily_module_constant ("lookup-markup-command");
863 return scm_call_1 (proc, scm_makfrom0str (s.c_str ()));
866 struct Parser_signature
871 static SCM signature_hash_table;
873 static void init_signature_hash_table ()
875 signature_hash_table = scm_gc_protect_object (scm_c_make_hash_table (31));
876 Parser_signature sigs[] = {
877 {"scm", MUSIC_FUNCTION_SCM},
878 {"music", MUSIC_FUNCTION_MUSIC},
879 {"scm-music", MUSIC_FUNCTION_SCM_MUSIC},
880 {"scm-scm", MUSIC_FUNCTION_SCM_SCM},
881 {"music-music", MUSIC_FUNCTION_MUSIC_MUSIC},
882 {"scm-music-music", MUSIC_FUNCTION_SCM_MUSIC_MUSIC},
883 {"scm-scm-music-music", MUSIC_FUNCTION_SCM_SCM_MUSIC_MUSIC},
884 {"scm-scm-music", MUSIC_FUNCTION_SCM_SCM_MUSIC},
885 {"scm-scm-scm-music", MUSIC_FUNCTION_SCM_SCM_SCM_SCM_MUSIC},
886 {"scm-scm-scm-scm-music", MUSIC_FUNCTION_SCM_SCM_SCM_MUSIC},
887 {"scm-scm-scm", MUSIC_FUNCTION_SCM_SCM_SCM},
888 {"markup", MUSIC_FUNCTION_MARKUP},
889 {"markup-music", MUSIC_FUNCTION_MARKUP_MUSIC},
890 {"markup-markup", MUSIC_FUNCTION_MARKUP_MARKUP},
891 {"markup-music-music", MUSIC_FUNCTION_MARKUP_MUSIC_MUSIC},
892 {"markup-markup-music", MUSIC_FUNCTION_MARKUP_MARKUP_MUSIC},
893 {"noarg", MUSIC_FUNCTION},
897 for (int i = 0; sigs[i].symbol; i++)
898 scm_hashq_set_x (signature_hash_table, scm_gc_protect_object (ly_symbol2scm (sigs[i].symbol)),
899 scm_from_int (sigs[i].token_type));
903 music_function_type (SCM func)
905 if (!signature_hash_table)
906 init_signature_hash_table ();
908 SCM type = scm_object_property (func, ly_symbol2scm ("music-function-signature-keyword"));
909 SCM token_type = scm_hashq_ref (signature_hash_table, type, SCM_BOOL_F);
910 if (!scm_is_number (token_type))
912 programming_error (_ ("can't find signature for music function"));
913 return MUSIC_FUNCTION_SCM;
916 return scm_to_int (token_type);
919 /* Shut up lexer warnings. */
939 silence_lexer_warnings ()
941 (void) yy_start_stack_ptr;
942 (void) yy_start_stack_depth;
943 (void) yy_start_stack;
944 (void) yy_push_state;
947 (void) silence_lexer_warnings;