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 ();
386 <INITIAL,notes,lyrics>{
388 return DOUBLE_ANGLE_OPEN;
391 return DOUBLE_ANGLE_CLOSE;
418 return scan_bare_word (YYText ());
422 return scan_escaped_word (YYText () + 1);
425 yylval.scm = scan_fraction (YYText ());
428 {UNSIGNED}/\/ | // backup rule
430 yylval.i = String_convert::dec2int (string (YYText ()));
434 yylval.i = String_convert::dec2int (string (YYText () +1));
441 *yylval.string += to_string (escaped_char (YYText ()[1]));
444 *yylval.string += YYText ();
450 /* yylval is union. Must remember STRING before setting SCM*/
451 string *sp = yylval.string;
452 yylval.scm = ly_string2scm (*sp);
454 return is_lyric_state () ? LYRICS_STRING : STRING;
457 *yylval.string += YYText ();
463 start_lyric_quote ();
466 yylval.scm = scan_fraction (YYText ());
469 {UNSIGNED}/\/[^0-9] { // backup rule
470 yylval.i = String_convert::dec2int (string (YYText ()));
473 {UNSIGNED}/\/ | // backup rule
475 yylval.i = String_convert::dec2int (string (YYText ()));
479 return scan_escaped_word (YYText () + 1);
483 string s (YYText ());
485 return yylval.i = EXTENDER;
487 return yylval.i = HYPHEN;
490 char c = s[s.length () - 1];
491 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
492 here_input ().warning (
493 _ ("Brace found at end of lyric. Did you forget a space?"));
494 yylval.scm = ly_string2scm (s);
497 return LYRICS_STRING;
505 return scan_bare_word (YYText ());
508 return scan_escaped_word (YYText () + 1);
511 yylval.scm = scan_fraction (YYText ());
514 {UNSIGNED}/\/[^0-9] { // backup rule
515 yylval.i = String_convert::dec2int (string (YYText ()));
518 {UNSIGNED}/\/ | // backup rule
520 yylval.i = String_convert::dec2int (string (YYText ()));
549 string str (YYText () + 1);
551 int token_type = MARKUP_FUNCTION;
552 SCM s = lookup_markup_command (str);
554 // lookup-markup-command returns a pair with the car
555 // being the function to call, and the cdr being the
556 // call signature specified to define-markup-command,
557 // a list of predicates.
559 if (!scm_is_pair (s)) {
560 // If lookup-markup-command was not successful, we
561 // try lookup-markup-list-command instead.
562 // If this fails as well, we just scan and return
564 s = lookup_markup_list_command (str);
566 token_type = MARKUP_LIST_FUNCTION;
568 return scan_escaped_word (str);
571 // If the list of predicates is, say,
572 // (number? number? markup?), then tokens
573 // EXPECT_MARKUP EXPECT_SCM EXPECT_SCM EXPECT_NO_MORE_ARGS
574 // will be generated. Note that we have to push them
575 // in reverse order, so the first token pushed in the
576 // loop will be EXPECT_NO_MORE_ARGS.
578 yylval.scm = scm_car(s);
580 // yylval now contains the function to call as token
581 // value (for token type MARKUP_FUNCTION or
582 // MARKUP_LIST_FUNCTION).
584 push_extra_token(EXPECT_NO_MORE_ARGS);
586 for (; scm_is_pair(s); s = scm_cdr(s)) {
587 SCM predicate = scm_car(s);
589 if (predicate == ly_lily_module_constant ("markup-list?"))
590 push_extra_token(EXPECT_MARKUP_LIST);
591 else if (predicate == ly_lily_module_constant ("markup?"))
592 push_extra_token(EXPECT_MARKUP);
594 push_extra_token(EXPECT_SCM, predicate);
601 [^#{}\"\\ \t\n\r\f]+ {
602 string s (YYText ());
604 char c = s[s.length () - 1];
605 /* brace open is for not confusing dumb tools. */
606 if (c == '{' || c == '}')
607 here_input ().warning (
608 _ ("Brace found at end of markup. Did you forget a space?"));
609 yylval.scm = ly_string2scm (s);
619 <longcomment><<EOF>> {
620 LexerError (_ ("EOF found inside a comment").c_str ());
621 is_main_input_ = false; // should be safe , can't have \include in --safe.
623 yyterminate (); // can't move this, since it actually rets a YY_NULL
626 <<EOF>> { if (is_main_input_)
628 /* 2 = init.ly + current file.
629 > because we're before closing, but is_main_input_ should
632 is_main_input_ = include_stack_.size () > 2;
634 /* Returns YY_NULL */
637 else if (!close_input ())
638 /* Returns YY_NULL */
644 return scan_bare_word (YYText ());
647 return scan_escaped_word (YYText () + 1);
651 -{UNSIGNED} | // backup rule
653 yylval.scm = scm_c_read_string (YYText ());
657 yylval.scm = scm_from_double (0.0);
662 yylval.i = String_convert::dec2int (string (YYText ()));
672 char c = YYText ()[0];
677 <INITIAL,notes,figures>. {
681 <INITIAL,lyrics,notes,figures>\\. {
682 char c = YYText ()[1];
686 return E_ANGLE_CLOSE;
690 return E_EXCLAMATION;
696 return E_BRACKET_OPEN;
700 return E_BRACKET_CLOSE;
712 string msg = _f ("invalid character: `%c'", YYText ()[0]);
713 LexerError (msg.c_str ());
719 /* Make the lexer generate a token of the given type as the next token.
720 TODO: make it possible to define a value for the token as well */
722 Lily_lexer::push_extra_token (int token_type, SCM scm)
724 if (scm_is_null (extra_tokens_))
726 if (YY_START != extratoken)
727 hidden_state_ = YY_START;
728 yy_push_state (extratoken);
730 extra_tokens_ = scm_acons (scm_from_int (token_type), scm, extra_tokens_);
734 Lily_lexer::push_chord_state (SCM tab)
736 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
737 yy_push_state (chords);
741 Lily_lexer::push_figuredbass_state ()
743 yy_push_state (figures);
747 Lily_lexer::push_initial_state ()
749 yy_push_state (INITIAL);
753 Lily_lexer::push_lyric_state ()
755 yy_push_state (lyrics);
759 Lily_lexer::push_markup_state ()
761 yy_push_state (markup);
765 Lily_lexer::push_note_state (SCM tab)
767 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
768 yy_push_state (notes);
772 Lily_lexer::pop_state ()
774 if (YYSTATE == notes || YYSTATE == chords)
775 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
781 Lily_lexer::identifier_type (SCM sid)
783 int k = try_special_identifiers (&yylval.scm , sid);
784 return k >= 0 ? k : SCM_IDENTIFIER;
789 Lily_lexer::scan_escaped_word (string str)
791 // use more SCM for this.
793 // SCM sym = ly_symbol2scm (str.c_str ());
795 int i = lookup_keyword (str);
796 if (i == MARKUP && is_lyric_state ())
801 SCM sid = lookup_identifier (str);
802 if (is_music_function (sid))
804 int funtype = SCM_FUNCTION;
806 yylval.scm = get_music_function_transform (sid);
808 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
809 SCM cs = scm_car (s);
811 if (scm_is_pair (cs))
816 if (scm_is_eq (cs, ly_lily_module_constant ("ly:music?")))
817 funtype = MUSIC_FUNCTION;
818 else if (scm_is_eq (cs, ly_lily_module_constant ("ly:event?")))
819 funtype = EVENT_FUNCTION;
820 else if (ly_is_procedure (cs))
821 funtype = SCM_FUNCTION;
822 else programming_error ("Bad syntax function predicate");
824 push_extra_token (EXPECT_NO_MORE_ARGS);
825 for (s = scm_cdr (s); scm_is_pair (s); s = scm_cdr (s))
827 SCM optional = SCM_UNDEFINED;
830 if (scm_is_pair (cs))
832 optional = SCM_CDR (cs);
836 if (cs == Pitch_type_p_proc)
837 push_extra_token (EXPECT_PITCH);
838 else if (cs == Duration_type_p_proc)
839 push_extra_token (EXPECT_DURATION);
840 else if (ly_is_procedure (cs))
841 push_extra_token (EXPECT_SCM, cs);
844 programming_error ("Function parameter without type-checking predicate");
847 if (!scm_is_eq (optional, SCM_UNDEFINED))
848 push_extra_token (EXPECT_OPTIONAL, optional);
853 if (sid != SCM_UNDEFINED)
856 return identifier_type (sid);
859 string msg (_f ("unknown escaped string: `\\%s'", str));
860 LexerError (msg.c_str ());
862 yylval.scm = ly_string2scm (str);
868 Lily_lexer::scan_bare_word (string str)
870 SCM sym = ly_symbol2scm (str.c_str ());
871 if ((YYSTATE == notes) || (YYSTATE == chords)) {
872 SCM handle = SCM_BOOL_F;
873 if (scm_is_pair (pitchname_tab_stack_))
874 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
876 if (scm_is_pair (handle)) {
877 yylval.scm = scm_cdr (handle);
878 if (unsmob_pitch (yylval.scm))
879 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
880 else if (scm_is_symbol (yylval.scm))
883 else if ((YYSTATE == chords)
884 && (handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
886 yylval.scm = scm_cdr (handle);
887 return CHORD_MODIFIER;
889 if ((chord_repetition_.repetition_symbol_ != SCM_EOL)
890 && to_boolean (scm_equal_p (chord_repetition_.repetition_symbol_, sym)))
891 return CHORD_REPETITION;
894 yylval.scm = ly_string2scm (str);
899 Lily_lexer::get_state () const
901 if (YY_START == extratoken)
902 return hidden_state_;
908 Lily_lexer::is_note_state () const
910 return get_state () == notes;
914 Lily_lexer::is_chord_state () const
916 return get_state () == chords;
920 Lily_lexer::is_lyric_state () const
922 return get_state () == lyrics;
926 Lily_lexer::is_figure_state () const
928 return get_state () == figures;
932 urg, belong to string (_convert)
933 and should be generalised
936 strip_leading_white (string&s)
939 for (; i < s.length (); i++)
947 strip_trailing_white (string&s)
949 ssize i = s.length ();
954 s = s.substr (0, i + 1);
959 Lilypond_version oldest_version ("2.7.38");
963 is_valid_version (string s)
965 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
966 Lilypond_version ver (s);
967 if (int (ver) < oldest_version)
969 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
970 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
976 non_fatal_error (_f ("program too old: %s (file requires: %s)", current.to_string (), ver.to_string ()));
987 lyric_fudge (string s)
989 char *chars = string_copy (s);
991 for (char *p = chars; *p ; p++)
993 if (*p == '_' && (p == chars || *(p-1) != '\\'))
1001 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
1003 * (((char*)s.c_str ()) + i + 1) = 'c';
1004 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
1011 Convert "NUM/DEN" into a '(NUM . DEN) cons.
1014 scan_fraction (string frac)
1016 ssize i = frac.find ('/');
1017 string left = frac.substr (0, i);
1018 string right = frac.substr (i + 1, (frac.length () - i + 1));
1020 int n = String_convert::dec2int (left);
1021 int d = String_convert::dec2int (right);
1022 return scm_cons (scm_from_int (n), scm_from_int (d));
1026 lookup_markup_command (string s)
1028 SCM proc = ly_lily_module_constant ("lookup-markup-command");
1029 return scm_call_1 (proc, ly_string2scm (s));
1033 lookup_markup_list_command (string s)
1035 SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
1036 return scm_call_1 (proc, ly_string2scm (s));
1039 /* Shut up lexer warnings. */
1059 silence_lexer_warnings ()
1061 (void) yy_start_stack_ptr;
1062 (void) yy_start_stack_depth;
1063 (void) yy_start_stack;
1064 (void) yy_push_state;
1065 (void) yy_pop_state;
1066 (void) yy_top_state;
1067 (void) silence_lexer_warnings;