1 %{ // -*-Fundamental-*-
3 This file is part of LilyPond, the GNU music typesetter.
5 Copyright (C) 1996--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 Jan Nieuwenhuizen <janneke@gnu.org>
8 LilyPond is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 LilyPond is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
25 after making a change to the lexer rules, run
26 flex -b <this lexer file>
29 contains no backup states, but only the reminder
30 Compressed tables always back up.
31 (don-t forget to rm lex.yy.cc :-)
40 /* Flex >= 2.5.29 fix; FlexLexer.h's multiple include bracing breaks
41 when building the actual lexer. */
48 #include "context-def.hh"
49 #include "duration.hh"
50 #include "identifier-smob.hh"
51 #include "international.hh"
52 #include "interval.hh"
53 #include "lily-guile.hh"
54 #include "lily-lexer.hh"
55 #include "lily-parser.hh"
56 #include "lilypond-version.hh"
59 #include "music-function.hh"
60 #include "parse-scm.hh"
63 #include "source-file.hh"
64 #include "std-string.hh"
65 #include "string-convert.hh"
72 #define isatty HORRIBLEKLUDGE
74 void strip_trailing_white (string&);
75 void strip_leading_white (string&);
76 string lyric_fudge (string s);
77 SCM lookup_markup_command (string s);
78 SCM lookup_markup_list_command (string s);
79 bool is_valid_version (string s);
82 #define start_quote() \
83 yy_push_state (quote);\
84 yylval.string = new string
86 #define start_lyric_quote() \
87 yy_push_state (lyric_quote);\
88 yylval.string = new string
96 #define YY_USER_ACTION add_lexed_char (YYLeng ());
99 SCM scan_fraction (string);
100 SCM (* scm_parse_error_handler) (void *);
110 %option yyclass="Lily_lexer"
112 %option never-interactive
137 NATIONAL [\001-\006\021-\027\031\036]
138 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}|{SPECIAL_CHAR}
139 DASHED_WORD {A}({AN}|-)*
140 DASHED_KEY_WORD \\{DASHED_WORD}
149 REAL ({INT}\.{N}*)|(-?\.{N}+)
151 HORIZONTALWHITE [ \t]
155 MARKUPCOMMAND \\({A}|[-_])+
156 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
160 BOM_UTF8 \357\273\277
166 // swallow and ignore carriage returns
169 <extratoken>{ANY_CHAR} {
170 /* Generate a token without swallowing anything */
172 /* First unswallow the eaten character */
173 add_lexed_char (-YYLeng ());
176 /* produce requested token */
177 int type = scm_to_int (scm_caar (extra_tokens_));
178 yylval.scm = scm_cdar (extra_tokens_);
179 extra_tokens_ = scm_cdr (extra_tokens_);
180 if (scm_is_null (extra_tokens_))
186 <extratoken><<EOF>> {
187 /* Generate a token without swallowing anything */
189 /* produce requested token */
190 int type = scm_to_int (scm_caar (extra_tokens_));
191 yylval.scm = scm_cdar (extra_tokens_);
192 extra_tokens_ = scm_cdr (extra_tokens_);
193 if (scm_is_null (extra_tokens_))
199 /* Use the trailing context feature. Otherwise, the BOM will not be
200 found if the file starts with an identifier definition. */
201 <INITIAL,chords,lyrics,figures,notes>{BOM_UTF8}/.* {
202 if (this->lexloc_->line_number () != 1 || this->lexloc_->column_number () != 0)
204 LexerWarning (_ ("stray UTF-8 BOM encountered").c_str ());
207 debug_output (_ ("Skipping UTF-8 BOM"));
210 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
212 yy_push_state (longcomment);
214 %[^{\n\r][^\n\r]*[\n\r] {
216 %[^{\n\r] { // backup rule
227 <INITIAL,notes,figures,chords,markup>{
233 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
234 yy_push_state (version);
236 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}* {
237 yy_push_state (sourcefilename);
239 <INITIAL,chords,lyrics,notes,figures>\\sourcefileline{WHITE}* {
240 yy_push_state (sourcefileline);
242 <version>\"[^"]*\" { /* got the version number */
243 string s (YYText () + 1);
244 s = s.substr (0, s.rfind ('\"'));
248 SCM top_scope = scm_car (scm_last_pair (scopes_));
249 scm_module_define (top_scope, ly_symbol2scm ("version-seen"), SCM_BOOL_T);
251 if (!is_valid_version (s))
256 <sourcefilename>\"[^"]*\" {
257 string s (YYText () + 1);
258 s = s.substr (0, s.rfind ('\"'));
261 this->here_input().get_source_file ()->name_ = s;
262 message (_f ("Renaming input to: `%s'", s.c_str ()));
263 progress_indication ("\n");
264 scm_module_define (scm_car (scopes_),
265 ly_symbol2scm ("input-file-name"),
270 <sourcefileline>{INT} {
272 sscanf (YYText (), "%d", &i);
275 this->here_input ().get_source_file ()->set_line (here_input ().start (), i);
278 <version>{ANY_CHAR} {
279 LexerError (_ ("quoted string expected after \\version").c_str ());
282 <sourcefilename>{ANY_CHAR} {
283 LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
286 <sourcefileline>{ANY_CHAR} {
287 LexerError (_ ("integer expected after \\sourcefileline").c_str ());
302 <INITIAL,chords,lyrics,notes,figures>\\maininput {
306 is_main_input_ = true;
309 error (_ ("\\maininput not allowed outside init files"));
312 <INITIAL,chords,lyrics,figures,notes>\\include {
313 yy_push_state (incl);
315 <incl>\"[^"]*\" { /* got the include file name */
316 string s (YYText ()+1);
317 s = s.substr (0, s.rfind ('"'));
319 new_input (s, sources_);
322 <incl>\\{BLACK}*{WHITE}? { /* got the include identifier */
323 string s = YYText () + 1;
324 strip_trailing_white (s);
325 if (s.length () && (s[s.length () - 1] == ';'))
326 s = s.substr (0, s.length () - 1);
328 SCM sid = lookup_identifier (s);
329 if (scm_is_string (sid)) {
330 new_input (ly_scm2string (sid), sources_);
333 string msg (_f ("wrong or undefined identifier: `%s'", s ));
335 LexerError (msg.c_str ());
336 SCM err = scm_current_error_port ();
337 scm_puts ("This value was found in the table: ", err);
338 scm_display (sid, err);
341 <incl,version,sourcefilename>\"[^"]* { // backup rule
342 error (_ ("end quote missing"));
345 <chords,notes,figures>{RESTNAME} {
346 char const *s = YYText ();
347 yylval.scm = scm_from_locale_string (s);
350 <chords,notes,figures>R {
351 return MULTI_MEASURE_REST;
353 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
355 Input hi = here_input();
357 SCM sval = ly_parse_scm (hi.start (), &n, hi,
358 be_safe_global && is_main_input_, parser_);
360 if (sval == SCM_UNDEFINED)
362 sval = SCM_UNSPECIFIED;
366 for (int i = 0; i < n; i++)
370 char_count_stack_.back () += n;
372 if (unpack_identifier (sval) != SCM_UNDEFINED)
374 yylval.scm = unpack_identifier(sval);
375 return identifier_type (yylval.scm);
378 for (size_t i = 0; i < pending_string_includes_.size (); i++)
379 new_input ("<included string>", pending_string_includes_[i],
381 pending_string_includes_.clear ();
387 <INITIAL,chords,figures,lyrics,markup,notes>\$ { //immediate scm
389 Input hi = here_input();
391 SCM sval = ly_parse_scm (hi.start (), &n, hi,
392 be_safe_global && is_main_input_, parser_);
394 for (int i = 0; i < n; i++)
398 char_count_stack_.back () += n;
400 if (sval == SCM_UNDEFINED)
402 yylval.scm = SCM_UNSPECIFIED;
405 LexerError (_ ("bad Scheme expression").c_str ());
406 return SCM_IDENTIFIER;
409 return scan_scm_id (sval);
412 <INITIAL,notes,lyrics>{
414 return DOUBLE_ANGLE_OPEN;
417 return DOUBLE_ANGLE_CLOSE;
444 return scan_bare_word (YYText ());
448 return scan_escaped_word (YYText () + 1);
451 yylval.scm = scan_fraction (YYText ());
454 {UNSIGNED}/\/ | // backup rule
456 yylval.scm = scm_c_read_string (YYText ());
460 yylval.i = String_convert::dec2int (string (YYText () +1));
467 *yylval.string += to_string (escaped_char (YYText ()[1]));
470 *yylval.string += YYText ();
476 /* yylval is union. Must remember STRING before setting SCM*/
477 string *sp = yylval.string;
478 yylval.scm = ly_string2scm (*sp);
480 return is_lyric_state () ? LYRICS_STRING : STRING;
483 *yylval.string += YYText ();
489 start_lyric_quote ();
492 yylval.scm = scan_fraction (YYText ());
495 {UNSIGNED}/\/[^0-9] { // backup rule
496 yylval.scm = scm_c_read_string (YYText ());
499 {UNSIGNED}/\/ | // backup rule
501 yylval.scm = scm_c_read_string (YYText ());
505 return scan_escaped_word (YYText () + 1);
509 string s (YYText ());
511 return yylval.i = EXTENDER;
513 return yylval.i = HYPHEN;
516 char c = s[s.length () - 1];
517 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
518 here_input ().warning (
519 _ ("Brace found at end of lyric. Did you forget a space?"));
520 yylval.scm = ly_string2scm (s);
523 return LYRICS_STRING;
531 return scan_bare_word (YYText ());
534 return scan_escaped_word (YYText () + 1);
537 yylval.scm = scan_fraction (YYText ());
540 {UNSIGNED}/\/[^0-9] { // backup rule
541 yylval.scm = scm_c_read_string (YYText ());
544 {UNSIGNED}/\/ | // backup rule
546 yylval.scm = scm_c_read_string (YYText ());
575 string str (YYText () + 1);
577 int token_type = MARKUP_FUNCTION;
578 SCM s = lookup_markup_command (str);
580 // lookup-markup-command returns a pair with the car
581 // being the function to call, and the cdr being the
582 // call signature specified to define-markup-command,
583 // a list of predicates.
585 if (!scm_is_pair (s)) {
586 // If lookup-markup-command was not successful, we
587 // try lookup-markup-list-command instead.
588 // If this fails as well, we just scan and return
590 s = lookup_markup_list_command (str);
592 token_type = MARKUP_LIST_FUNCTION;
594 return scan_escaped_word (str);
597 // If the list of predicates is, say,
598 // (number? number? markup?), then tokens
599 // EXPECT_MARKUP EXPECT_SCM EXPECT_SCM EXPECT_NO_MORE_ARGS
600 // will be generated. Note that we have to push them
601 // in reverse order, so the first token pushed in the
602 // loop will be EXPECT_NO_MORE_ARGS.
604 yylval.scm = scm_car(s);
606 // yylval now contains the function to call as token
607 // value (for token type MARKUP_FUNCTION or
608 // MARKUP_LIST_FUNCTION).
610 push_extra_token(EXPECT_NO_MORE_ARGS);
612 for (; scm_is_pair(s); s = scm_cdr(s)) {
613 SCM predicate = scm_car(s);
615 if (predicate == ly_lily_module_constant ("markup-list?"))
616 push_extra_token(EXPECT_MARKUP_LIST);
617 else if (predicate == ly_lily_module_constant ("markup?"))
618 push_extra_token(EXPECT_MARKUP);
620 push_extra_token(EXPECT_SCM, predicate);
627 [^$#{}\"\\ \t\n\r\f]+ {
628 string s (YYText ());
630 char c = s[s.length () - 1];
631 /* brace open is for not confusing dumb tools. */
632 if (c == '{' || c == '}')
633 here_input ().warning (
634 _ ("Brace found at end of markup. Did you forget a space?"));
635 yylval.scm = ly_string2scm (s);
645 <longcomment><<EOF>> {
646 LexerError (_ ("EOF found inside a comment").c_str ());
647 is_main_input_ = false; // should be safe , can't have \include in --safe.
649 yyterminate (); // can't move this, since it actually rets a YY_NULL
652 <<EOF>> { if (is_main_input_)
654 /* 2 = init.ly + current file.
655 > because we're before closing, but is_main_input_ should
658 is_main_input_ = include_stack_.size () > 2;
660 /* Returns YY_NULL */
663 else if (!close_input ())
664 /* Returns YY_NULL */
670 return scan_bare_word (YYText ());
673 return scan_escaped_word (YYText () + 1);
677 -{UNSIGNED} | // backup rule
679 yylval.scm = scm_c_read_string (YYText ());
683 yylval.scm = scm_from_double (0.0);
688 yylval.scm = scm_c_read_string (YYText ());
698 char c = YYText ()[0];
703 <INITIAL,notes,figures>. {
707 <INITIAL,lyrics,notes,figures>\\. {
708 char c = YYText ()[1];
712 return E_ANGLE_CLOSE;
716 return E_EXCLAMATION;
722 return E_BRACKET_OPEN;
726 return E_BRACKET_CLOSE;
738 string msg = _f ("invalid character: `%c'", YYText ()[0]);
739 LexerError (msg.c_str ());
745 /* Make the lexer generate a token of the given type as the next token.
746 TODO: make it possible to define a value for the token as well */
748 Lily_lexer::push_extra_token (int token_type, SCM scm)
750 if (scm_is_null (extra_tokens_))
752 if (YY_START != extratoken)
753 hidden_state_ = YY_START;
754 yy_push_state (extratoken);
756 extra_tokens_ = scm_acons (scm_from_int (token_type), scm, extra_tokens_);
760 Lily_lexer::push_chord_state (SCM tab)
762 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
763 yy_push_state (chords);
767 Lily_lexer::push_figuredbass_state ()
769 yy_push_state (figures);
773 Lily_lexer::push_initial_state ()
775 yy_push_state (INITIAL);
779 Lily_lexer::push_lyric_state ()
781 yy_push_state (lyrics);
785 Lily_lexer::push_markup_state ()
787 yy_push_state (markup);
791 Lily_lexer::push_note_state (SCM tab)
793 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
794 yy_push_state (notes);
798 Lily_lexer::pop_state ()
800 if (YYSTATE == notes || YYSTATE == chords)
801 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
807 Lily_lexer::identifier_type (SCM sid)
809 int k = try_special_identifiers (&yylval.scm , sid);
810 return k >= 0 ? k : SCM_IDENTIFIER;
815 Lily_lexer::scan_escaped_word (string str)
817 // use more SCM for this.
819 // SCM sym = ly_symbol2scm (str.c_str ());
821 int i = lookup_keyword (str);
822 if (i == MARKUP && is_lyric_state ())
827 SCM sid = lookup_identifier (str);
828 if (sid != SCM_UNDEFINED)
829 return scan_scm_id (sid);
831 string msg (_f ("unknown escaped string: `\\%s'", str));
832 LexerError (msg.c_str ());
834 yylval.scm = ly_string2scm (str);
840 Lily_lexer::scan_scm_id (SCM sid)
842 if (is_music_function (sid))
844 int funtype = SCM_FUNCTION;
846 yylval.scm = get_music_function_transform (sid);
848 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
849 SCM cs = scm_car (s);
851 if (scm_is_pair (cs))
856 if (scm_is_eq (cs, ly_lily_module_constant ("ly:music?")))
857 funtype = MUSIC_FUNCTION;
858 else if (scm_is_eq (cs, ly_lily_module_constant ("ly:event?")))
859 funtype = EVENT_FUNCTION;
860 else if (ly_is_procedure (cs))
861 funtype = SCM_FUNCTION;
862 else programming_error ("Bad syntax function predicate");
864 push_extra_token (EXPECT_NO_MORE_ARGS);
865 for (s = scm_cdr (s); scm_is_pair (s); s = scm_cdr (s))
867 SCM optional = SCM_UNDEFINED;
870 if (scm_is_pair (cs))
872 optional = SCM_CDR (cs);
876 if (cs == Pitch_type_p_proc)
877 push_extra_token (EXPECT_PITCH);
878 else if (cs == Duration_type_p_proc)
879 push_extra_token (EXPECT_DURATION);
880 else if (ly_is_procedure (cs))
881 push_extra_token (EXPECT_SCM, cs);
884 programming_error ("Function parameter without type-checking predicate");
887 if (!scm_is_eq (optional, SCM_UNDEFINED))
888 push_extra_token (EXPECT_OPTIONAL, optional);
893 return identifier_type (sid);
897 Lily_lexer::scan_bare_word (string str)
899 SCM sym = ly_symbol2scm (str.c_str ());
900 if ((YYSTATE == notes) || (YYSTATE == chords)) {
901 SCM handle = SCM_BOOL_F;
902 if (scm_is_pair (pitchname_tab_stack_))
903 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
905 if (scm_is_pair (handle)) {
906 yylval.scm = scm_cdr (handle);
907 if (unsmob_pitch (yylval.scm))
908 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
909 else if (scm_is_symbol (yylval.scm))
912 else if ((YYSTATE == chords)
913 && (handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
915 yylval.scm = scm_cdr (handle);
916 return CHORD_MODIFIER;
918 if ((chord_repetition_.repetition_symbol_ != SCM_EOL)
919 && to_boolean (scm_equal_p (chord_repetition_.repetition_symbol_, sym)))
920 return CHORD_REPETITION;
923 yylval.scm = ly_string2scm (str);
928 Lily_lexer::get_state () const
930 if (YY_START == extratoken)
931 return hidden_state_;
937 Lily_lexer::is_note_state () const
939 return get_state () == notes;
943 Lily_lexer::is_chord_state () const
945 return get_state () == chords;
949 Lily_lexer::is_lyric_state () const
951 return get_state () == lyrics;
955 Lily_lexer::is_figure_state () const
957 return get_state () == figures;
961 urg, belong to string (_convert)
962 and should be generalised
965 strip_leading_white (string&s)
968 for (; i < s.length (); i++)
976 strip_trailing_white (string&s)
978 ssize i = s.length ();
983 s = s.substr (0, i + 1);
988 Lilypond_version oldest_version ("2.7.38");
992 is_valid_version (string s)
994 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
995 Lilypond_version ver (s);
996 if (int (ver) < oldest_version)
998 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
999 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
1005 non_fatal_error (_f ("program too old: %s (file requires: %s)", current.to_string (), ver.to_string ()));
1016 lyric_fudge (string s)
1018 char *chars = string_copy (s);
1020 for (char *p = chars; *p ; p++)
1022 if (*p == '_' && (p == chars || *(p-1) != '\\'))
1030 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
1032 * (((char*)s.c_str ()) + i + 1) = 'c';
1033 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
1040 Convert "NUM/DEN" into a '(NUM . DEN) cons.
1043 scan_fraction (string frac)
1045 ssize i = frac.find ('/');
1046 string left = frac.substr (0, i);
1047 string right = frac.substr (i + 1, (frac.length () - i + 1));
1049 int n = String_convert::dec2int (left);
1050 int d = String_convert::dec2int (right);
1051 return scm_cons (scm_from_int (n), scm_from_int (d));
1055 lookup_markup_command (string s)
1057 SCM proc = ly_lily_module_constant ("lookup-markup-command");
1058 return scm_call_1 (proc, ly_string2scm (s));
1062 lookup_markup_list_command (string s)
1064 SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
1065 return scm_call_1 (proc, ly_string2scm (s));
1068 /* Shut up lexer warnings. */
1088 silence_lexer_warnings ()
1090 (void) yy_start_stack_ptr;
1091 (void) yy_start_stack_depth;
1092 (void) yy_start_stack;
1093 (void) yy_push_state;
1094 (void) yy_pop_state;
1095 (void) yy_top_state;
1096 (void) silence_lexer_warnings;