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
136 NATIONAL [\001-\006\021-\027\031\036]
137 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
138 DASHED_WORD {A}({AN}|-)*
139 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 = extra_token_types_.back ();
178 extra_token_types_.pop_back ();
179 if (extra_token_types_.empty ())
185 /* Use the trailing context feature. Otherwise, the BOM will not be
186 found if the file starts with an identifier definition. */
187 <INITIAL,chords,lyrics,figures,notes>{BOM_UTF8}/.* {
188 if (this->lexloc_->line_number () != 1 || this->lexloc_->column_number () != 0)
190 LexerError (_ ("stray UTF-8 BOM encountered").c_str ());
193 debug_output (_ ("Skipping UTF-8 BOM"));
196 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
198 yy_push_state (longcomment);
200 %[^{\n\r][^\n\r]*[\n\r] {
202 %[^{\n\r] { // backup rule
213 <INITIAL,notes,figures,chords,markup>{
219 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
220 yy_push_state (version);
222 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}* {
223 yy_push_state (sourcefilename);
225 <INITIAL,chords,lyrics,notes,figures>\\sourcefileline{WHITE}* {
226 yy_push_state (sourcefileline);
228 <version>\"[^"]*\" { /* got the version number */
229 string s (YYText () + 1);
230 s = s.substr (0, s.rfind ('\"'));
234 SCM top_scope = scm_car (scm_last_pair (scopes_));
235 scm_module_define (top_scope, ly_symbol2scm ("version-seen"), SCM_BOOL_T);
237 if (!is_valid_version (s))
242 <sourcefilename>\"[^"]*\" {
243 string s (YYText () + 1);
244 s = s.substr (0, s.rfind ('\"'));
247 this->here_input().get_source_file ()->name_ = s;
248 message (_f ("Renaming input to: `%s'", s.c_str ()));
249 progress_indication ("\n");
250 scm_module_define (scm_car (scopes_),
251 ly_symbol2scm ("input-file-name"),
256 <sourcefileline>{INT} {
258 sscanf (YYText (), "%d", &i);
261 this->here_input ().get_source_file ()->set_line (here_input ().start (), i);
264 <version>{ANY_CHAR} {
265 LexerError (_ ("quoted string expected after \\version").c_str ());
268 <sourcefilename>{ANY_CHAR} {
269 LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
272 <sourcefileline>{ANY_CHAR} {
273 LexerError (_ ("integer expected after \\sourcefileline").c_str ());
288 <INITIAL,chords,lyrics,notes,figures>\\maininput {
292 is_main_input_ = true;
295 error (_ ("\\maininput not allowed outside init files"));
298 <INITIAL,chords,lyrics,figures,notes>\\include {
299 yy_push_state (incl);
301 <incl>\"[^"]*\" { /* got the include file name */
302 string s (YYText ()+1);
303 s = s.substr (0, s.rfind ('"'));
305 new_input (s, sources_);
308 <incl>\\{BLACK}*{WHITE}? { /* got the include identifier */
309 string s = YYText () + 1;
310 strip_trailing_white (s);
311 if (s.length () && (s[s.length () - 1] == ';'))
312 s = s.substr (0, s.length () - 1);
314 SCM sid = lookup_identifier (s);
315 if (scm_is_string (sid)) {
316 new_input (ly_scm2string (sid), sources_);
319 string msg (_f ("wrong or undefined identifier: `%s'", s ));
321 LexerError (msg.c_str ());
322 SCM err = scm_current_error_port ();
323 scm_puts ("This value was found in the table: ", err);
324 scm_display (sid, err);
327 <incl,version,sourcefilename>\"[^"]* { // backup rule
328 error (_ ("end quote missing"));
331 <chords,notes,figures>{RESTNAME} {
332 char const *s = YYText ();
333 yylval.scm = scm_from_locale_string (s);
336 <chords,notes,figures>R {
337 return MULTI_MEASURE_REST;
339 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
341 Input hi = here_input();
343 SCM sval = ly_parse_scm (hi.start (), &n, hi,
344 be_safe_global && is_main_input_, parser_);
346 if (sval == SCM_UNDEFINED)
348 sval = SCM_UNSPECIFIED;
352 for (int i = 0; i < n; i++)
356 char_count_stack_.back () += n;
358 if (unpack_identifier (sval) != SCM_UNDEFINED)
360 yylval.scm = unpack_identifier(sval);
361 return identifier_type (yylval.scm);
364 for (size_t i = 0; i < pending_string_includes_.size (); i++)
365 new_input ("<included string>", pending_string_includes_[i],
367 pending_string_includes_.clear ();
372 <INITIAL,notes,lyrics>{
374 return DOUBLE_ANGLE_OPEN;
377 return DOUBLE_ANGLE_CLOSE;
404 return scan_bare_word (YYText ());
408 return scan_escaped_word (YYText () + 1);
411 yylval.scm = scan_fraction (YYText ());
415 yylval.i = String_convert::dec2int (string (YYText ()));
418 {UNSIGNED}/\/[^0-9] { // backup rule
419 yylval.i = String_convert::dec2int (string (YYText ()));
422 {UNSIGNED}/\/ | // backup rule
424 yylval.i = String_convert::dec2int (string (YYText ()));
428 yylval.i = String_convert::dec2int (string (YYText () +1));
435 *yylval.string += to_string (escaped_char (YYText ()[1]));
438 *yylval.string += YYText ();
444 /* yylval is union. Must remember STRING before setting SCM*/
445 string *sp = yylval.string;
446 yylval.scm = ly_string2scm (*sp);
448 return is_lyric_state () ? LYRICS_STRING : STRING;
451 *yylval.string += YYText ();
457 start_lyric_quote ();
460 yylval.scm = scan_fraction (YYText ());
463 {UNSIGNED}/\/[^0-9] { // backup rule
464 yylval.i = String_convert::dec2int (string (YYText ()));
467 {UNSIGNED}/\/ | // backup rule
469 yylval.i = String_convert::dec2int (string (YYText ()));
473 return scan_escaped_word (YYText () + 1);
477 string s (YYText ());
479 return yylval.i = EXTENDER;
481 return yylval.i = HYPHEN;
484 char c = s[s.length () - 1];
485 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
486 here_input ().warning (
487 _ ("Brace found at end of lyric. Did you forget a space?"));
488 yylval.scm = ly_string2scm (s);
491 return LYRICS_STRING;
499 return scan_bare_word (YYText ());
502 return scan_escaped_word (YYText () + 1);
505 yylval.scm = scan_fraction (YYText ());
508 {UNSIGNED}/\/[^0-9] { // backup rule
509 yylval.i = String_convert::dec2int (string (YYText ()));
512 {UNSIGNED}/\/ | // backup rule
514 yylval.i = String_convert::dec2int (string (YYText ()));
543 string str (YYText () + 1);
545 int token_type = MARKUP_FUNCTION;
546 SCM s = lookup_markup_command (str);
548 // lookup-markup-command returns a pair with the car
549 // being the function to call, and the cdr being the
550 // call signature specified to define-markup-command,
551 // a list of predicates.
553 if (!scm_is_pair (s)) {
554 // If lookup-markup-command was not successful, we
555 // try lookup-markup-list-command instead.
556 // If this fails as well, we just scan and return
558 s = lookup_markup_list_command (str);
560 token_type = MARKUP_LIST_FUNCTION;
562 return scan_escaped_word (str);
565 // If the list of predicates is, say,
566 // (number? number? markup?), then tokens
567 // EXPECT_MARKUP EXPECT_SCM EXPECT_SCM EXPECT_NO_MORE_ARGS
568 // will be generated. Note that we have to push them
569 // in reverse order, so the first token pushed in the
570 // loop will be EXPECT_NO_MORE_ARGS.
572 yylval.scm = scm_car(s);
574 // yylval now contains the function to call as token
575 // value (for token type MARKUP_FUNCTION or
576 // MARKUP_LIST_FUNCTION).
578 push_extra_token(EXPECT_NO_MORE_ARGS);
580 for (; scm_is_pair(s); s = scm_cdr(s)) {
581 SCM predicate = scm_car(s);
583 if (predicate == ly_lily_module_constant ("markup-list?"))
584 push_extra_token(EXPECT_MARKUP_LIST);
585 else if (predicate == ly_lily_module_constant ("markup?"))
586 push_extra_token(EXPECT_MARKUP);
588 push_extra_token(EXPECT_SCM);
595 [^#{}"\\ \t\n\r\f]+ {
596 string s (YYText ());
598 char c = s[s.length () - 1];
599 /* brace open is for not confusing dumb tools. */
600 if (c == '{' || c == '}')
601 here_input ().warning (
602 _ ("Brace found at end of markup. Did you forget a space?"));
603 yylval.scm = ly_string2scm (s);
614 if (YY_START == longcomment)
616 LexerError (_ ("EOF found inside a comment").c_str ());
617 is_main_input_ = false; // should be safe , can't have \include in --safe.
619 yyterminate (); // can't move this, since it actually rets a YY_NULL
621 else if (is_main_input_)
623 /* 2 = init.ly + current file.
624 > because we're before closing, but is_main_input_ should
627 is_main_input_ = include_stack_.size () > 2;
629 /* Returns YY_NULL */
632 else if (!close_input ())
633 /* Returns YY_NULL */
639 return scan_bare_word (YYText ());
642 return scan_escaped_word (YYText () + 1);
646 -{UNSIGNED} | // backup rule
649 int cnv = sscanf (YYText (), "%lf", &r);
653 yylval.scm = scm_from_double (r);
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)
724 if (extra_token_types_.empty ())
726 if (YY_START != extratoken)
727 hidden_state_ = YY_START;
728 yy_push_state (extratoken);
730 extra_token_types_.push_back (token_type);
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 yylval.scm = get_music_function_transform (sid);
806 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
807 push_extra_token (EXPECT_NO_MORE_ARGS);
808 for (; scm_is_pair (s); s = scm_cdr (s))
810 SCM cs = scm_car (s);
812 if (cs == ly_music_p_proc)
813 push_extra_token (EXPECT_MUSIC);
814 else if (cs == Pitch_type_p_proc)
815 push_extra_token (EXPECT_PITCH);
816 else if (cs == Duration_type_p_proc)
817 push_extra_token (EXPECT_DURATION);
818 else if (cs == ly_lily_module_constant ("markup?"))
819 push_extra_token (EXPECT_MARKUP);
820 else if (ly_is_procedure (cs))
821 push_extra_token (EXPECT_SCM);
822 else programming_error ("Function parameter without type-checking predicate");
824 return MUSIC_FUNCTION;
827 if (sid != SCM_UNDEFINED)
830 return identifier_type (sid);
833 string msg (_f ("unknown escaped string: `\\%s'", str));
834 LexerError (msg.c_str ());
836 yylval.scm = ly_string2scm (str);
842 Lily_lexer::scan_bare_word (string str)
844 SCM sym = ly_symbol2scm (str.c_str ());
845 if ((YYSTATE == notes) || (YYSTATE == chords)) {
846 SCM handle = SCM_BOOL_F;
847 if (scm_is_pair (pitchname_tab_stack_))
848 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
850 if (scm_is_pair (handle)) {
851 yylval.scm = scm_cdr (handle);
852 if (unsmob_pitch (yylval.scm))
853 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
854 else if (scm_is_symbol (yylval.scm))
857 else if ((YYSTATE == chords)
858 && (handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
860 yylval.scm = scm_cdr (handle);
861 return CHORD_MODIFIER;
863 if ((chord_repetition_.repetition_symbol_ != SCM_EOL)
864 && to_boolean (scm_equal_p (chord_repetition_.repetition_symbol_, sym)))
865 return CHORD_REPETITION;
868 yylval.scm = ly_string2scm (str);
873 Lily_lexer::get_state () const
875 if (YY_START == extratoken)
876 return hidden_state_;
882 Lily_lexer::is_note_state () const
884 return get_state () == notes;
888 Lily_lexer::is_chord_state () const
890 return get_state () == chords;
894 Lily_lexer::is_lyric_state () const
896 return get_state () == lyrics;
900 Lily_lexer::is_figure_state () const
902 return get_state () == figures;
906 urg, belong to string (_convert)
907 and should be generalised
910 strip_leading_white (string&s)
913 for (; i < s.length (); i++)
921 strip_trailing_white (string&s)
923 ssize i = s.length ();
928 s = s.substr (0, i + 1);
933 Lilypond_version oldest_version ("2.7.38");
937 is_valid_version (string s)
939 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
940 Lilypond_version ver (s);
941 if (int (ver) < oldest_version)
943 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
944 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
950 non_fatal_error (_f ("program too old: %s (file requires: %s)", current.to_string (), ver.to_string ()));
961 lyric_fudge (string s)
963 char *chars = string_copy (s);
965 for (char *p = chars; *p ; p++)
967 if (*p == '_' && (p == chars || *(p-1) != '\\'))
975 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
977 * (((char*)s.c_str ()) + i + 1) = 'c';
978 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
985 Convert "NUM/DEN" into a '(NUM . DEN) cons.
988 scan_fraction (string frac)
990 ssize i = frac.find ('/');
991 string left = frac.substr (0, i);
992 string right = frac.substr (i + 1, (frac.length () - i + 1));
994 int n = String_convert::dec2int (left);
995 int d = String_convert::dec2int (right);
996 return scm_cons (scm_from_int (n), scm_from_int (d));
1000 lookup_markup_command (string s)
1002 SCM proc = ly_lily_module_constant ("lookup-markup-command");
1003 return scm_call_1 (proc, ly_string2scm (s));
1007 lookup_markup_list_command (string s)
1009 SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
1010 return scm_call_1 (proc, ly_string2scm (s));
1013 /* Shut up lexer warnings. */
1033 silence_lexer_warnings ()
1035 (void) yy_start_stack_ptr;
1036 (void) yy_start_stack_depth;
1037 (void) yy_start_stack;
1038 (void) yy_push_state;
1039 (void) yy_pop_state;
1040 (void) yy_top_state;
1041 (void) silence_lexer_warnings;