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 <extratoken><<EOF>> {
186 /* Generate a token without swallowing anything */
188 /* produce requested token */
189 int type = extra_token_types_.back ();
190 extra_token_types_.pop_back ();
191 if (extra_token_types_.empty ())
197 /* Use the trailing context feature. Otherwise, the BOM will not be
198 found if the file starts with an identifier definition. */
199 <INITIAL,chords,lyrics,figures,notes>{BOM_UTF8}/.* {
200 if (this->lexloc_->line_number () != 1 || this->lexloc_->column_number () != 0)
202 LexerError (_ ("stray UTF-8 BOM encountered").c_str ());
205 debug_output (_ ("Skipping UTF-8 BOM"));
208 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
210 yy_push_state (longcomment);
212 %[^{\n\r][^\n\r]*[\n\r] {
214 %[^{\n\r] { // backup rule
225 <INITIAL,notes,figures,chords,markup>{
231 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
232 yy_push_state (version);
234 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}* {
235 yy_push_state (sourcefilename);
237 <INITIAL,chords,lyrics,notes,figures>\\sourcefileline{WHITE}* {
238 yy_push_state (sourcefileline);
240 <version>\"[^"]*\" { /* got the version number */
241 string s (YYText () + 1);
242 s = s.substr (0, s.rfind ('\"'));
246 SCM top_scope = scm_car (scm_last_pair (scopes_));
247 scm_module_define (top_scope, ly_symbol2scm ("version-seen"), SCM_BOOL_T);
249 if (!is_valid_version (s))
254 <sourcefilename>\"[^"]*\" {
255 string s (YYText () + 1);
256 s = s.substr (0, s.rfind ('\"'));
259 this->here_input().get_source_file ()->name_ = s;
260 message (_f ("Renaming input to: `%s'", s.c_str ()));
261 progress_indication ("\n");
262 scm_module_define (scm_car (scopes_),
263 ly_symbol2scm ("input-file-name"),
268 <sourcefileline>{INT} {
270 sscanf (YYText (), "%d", &i);
273 this->here_input ().get_source_file ()->set_line (here_input ().start (), i);
276 <version>{ANY_CHAR} {
277 LexerError (_ ("quoted string expected after \\version").c_str ());
280 <sourcefilename>{ANY_CHAR} {
281 LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
284 <sourcefileline>{ANY_CHAR} {
285 LexerError (_ ("integer expected after \\sourcefileline").c_str ());
300 <INITIAL,chords,lyrics,notes,figures>\\maininput {
304 is_main_input_ = true;
307 error (_ ("\\maininput not allowed outside init files"));
310 <INITIAL,chords,lyrics,figures,notes>\\include {
311 yy_push_state (incl);
313 <incl>\"[^"]*\" { /* got the include file name */
314 string s (YYText ()+1);
315 s = s.substr (0, s.rfind ('"'));
317 new_input (s, sources_);
320 <incl>\\{BLACK}*{WHITE}? { /* got the include identifier */
321 string s = YYText () + 1;
322 strip_trailing_white (s);
323 if (s.length () && (s[s.length () - 1] == ';'))
324 s = s.substr (0, s.length () - 1);
326 SCM sid = lookup_identifier (s);
327 if (scm_is_string (sid)) {
328 new_input (ly_scm2string (sid), sources_);
331 string msg (_f ("wrong or undefined identifier: `%s'", s ));
333 LexerError (msg.c_str ());
334 SCM err = scm_current_error_port ();
335 scm_puts ("This value was found in the table: ", err);
336 scm_display (sid, err);
339 <incl,version,sourcefilename>\"[^"]* { // backup rule
340 error (_ ("end quote missing"));
343 <chords,notes,figures>{RESTNAME} {
344 char const *s = YYText ();
345 yylval.scm = scm_from_locale_string (s);
348 <chords,notes,figures>R {
349 return MULTI_MEASURE_REST;
351 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
353 Input hi = here_input();
355 SCM sval = ly_parse_scm (hi.start (), &n, hi,
356 be_safe_global && is_main_input_, parser_);
358 if (sval == SCM_UNDEFINED)
360 sval = SCM_UNSPECIFIED;
364 for (int i = 0; i < n; i++)
368 char_count_stack_.back () += n;
370 if (unpack_identifier (sval) != SCM_UNDEFINED)
372 yylval.scm = unpack_identifier(sval);
373 return identifier_type (yylval.scm);
376 for (size_t i = 0; i < pending_string_includes_.size (); i++)
377 new_input ("<included string>", pending_string_includes_[i],
379 pending_string_includes_.clear ();
384 <INITIAL,notes,lyrics>{
386 return DOUBLE_ANGLE_OPEN;
389 return DOUBLE_ANGLE_CLOSE;
416 return scan_bare_word (YYText ());
420 return scan_escaped_word (YYText () + 1);
423 yylval.scm = scan_fraction (YYText ());
427 yylval.i = String_convert::dec2int (string (YYText ()));
430 {UNSIGNED}/\/[^0-9] { // backup rule
431 yylval.i = String_convert::dec2int (string (YYText ()));
434 {UNSIGNED}/\/ | // backup rule
436 yylval.i = String_convert::dec2int (string (YYText ()));
440 yylval.i = String_convert::dec2int (string (YYText () +1));
447 *yylval.string += to_string (escaped_char (YYText ()[1]));
450 *yylval.string += YYText ();
456 /* yylval is union. Must remember STRING before setting SCM*/
457 string *sp = yylval.string;
458 yylval.scm = ly_string2scm (*sp);
460 return is_lyric_state () ? LYRICS_STRING : STRING;
463 *yylval.string += YYText ();
469 start_lyric_quote ();
472 yylval.scm = scan_fraction (YYText ());
475 {UNSIGNED}/\/[^0-9] { // backup rule
476 yylval.i = String_convert::dec2int (string (YYText ()));
479 {UNSIGNED}/\/ | // backup rule
481 yylval.i = String_convert::dec2int (string (YYText ()));
485 return scan_escaped_word (YYText () + 1);
489 string s (YYText ());
491 return yylval.i = EXTENDER;
493 return yylval.i = HYPHEN;
496 char c = s[s.length () - 1];
497 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
498 here_input ().warning (
499 _ ("Brace found at end of lyric. Did you forget a space?"));
500 yylval.scm = ly_string2scm (s);
503 return LYRICS_STRING;
511 return scan_bare_word (YYText ());
514 return scan_escaped_word (YYText () + 1);
517 yylval.scm = scan_fraction (YYText ());
520 {UNSIGNED}/\/[^0-9] { // backup rule
521 yylval.i = String_convert::dec2int (string (YYText ()));
524 {UNSIGNED}/\/ | // backup rule
526 yylval.i = String_convert::dec2int (string (YYText ()));
555 string str (YYText () + 1);
557 int token_type = MARKUP_FUNCTION;
558 SCM s = lookup_markup_command (str);
560 // lookup-markup-command returns a pair with the car
561 // being the function to call, and the cdr being the
562 // call signature specified to define-markup-command,
563 // a list of predicates.
565 if (!scm_is_pair (s)) {
566 // If lookup-markup-command was not successful, we
567 // try lookup-markup-list-command instead.
568 // If this fails as well, we just scan and return
570 s = lookup_markup_list_command (str);
572 token_type = MARKUP_LIST_FUNCTION;
574 return scan_escaped_word (str);
577 // If the list of predicates is, say,
578 // (number? number? markup?), then tokens
579 // EXPECT_MARKUP EXPECT_SCM EXPECT_SCM EXPECT_NO_MORE_ARGS
580 // will be generated. Note that we have to push them
581 // in reverse order, so the first token pushed in the
582 // loop will be EXPECT_NO_MORE_ARGS.
584 yylval.scm = scm_car(s);
586 // yylval now contains the function to call as token
587 // value (for token type MARKUP_FUNCTION or
588 // MARKUP_LIST_FUNCTION).
590 push_extra_token(EXPECT_NO_MORE_ARGS);
592 for (; scm_is_pair(s); s = scm_cdr(s)) {
593 SCM predicate = scm_car(s);
595 if (predicate == ly_lily_module_constant ("markup-list?"))
596 push_extra_token(EXPECT_MARKUP_LIST);
597 else if (predicate == ly_lily_module_constant ("markup?"))
598 push_extra_token(EXPECT_MARKUP);
600 push_extra_token(EXPECT_SCM);
607 [^#{}"\\ \t\n\r\f]+ {
608 string s (YYText ());
610 char c = s[s.length () - 1];
611 /* brace open is for not confusing dumb tools. */
612 if (c == '{' || c == '}')
613 here_input ().warning (
614 _ ("Brace found at end of markup. Did you forget a space?"));
615 yylval.scm = ly_string2scm (s);
625 <longcomment><<EOF>> {
626 LexerError (_ ("EOF found inside a comment").c_str ());
627 is_main_input_ = false; // should be safe , can't have \include in --safe.
629 yyterminate (); // can't move this, since it actually rets a YY_NULL
632 <<EOF>> { if (is_main_input_)
634 /* 2 = init.ly + current file.
635 > because we're before closing, but is_main_input_ should
638 is_main_input_ = include_stack_.size () > 2;
640 /* Returns YY_NULL */
643 else if (!close_input ())
644 /* Returns YY_NULL */
650 return scan_bare_word (YYText ());
653 return scan_escaped_word (YYText () + 1);
657 -{UNSIGNED} | // backup rule
660 int cnv = sscanf (YYText (), "%lf", &r);
664 yylval.scm = scm_from_double (r);
668 yylval.scm = scm_from_double (0.0);
673 yylval.i = String_convert::dec2int (string (YYText ()));
683 char c = YYText ()[0];
688 <INITIAL,notes,figures>. {
692 <INITIAL,lyrics,notes,figures>\\. {
693 char c = YYText ()[1];
697 return E_ANGLE_CLOSE;
701 return E_EXCLAMATION;
707 return E_BRACKET_OPEN;
711 return E_BRACKET_CLOSE;
723 string msg = _f ("invalid character: `%c'", YYText ()[0]);
724 LexerError (msg.c_str ());
730 /* Make the lexer generate a token of the given type as the next token.
731 TODO: make it possible to define a value for the token as well */
733 Lily_lexer::push_extra_token (int token_type)
735 if (extra_token_types_.empty ())
737 if (YY_START != extratoken)
738 hidden_state_ = YY_START;
739 yy_push_state (extratoken);
741 extra_token_types_.push_back (token_type);
745 Lily_lexer::push_embedded_token ()
747 push_extra_token (EMBEDDED_LILY);
751 Lily_lexer::push_chord_state (SCM tab)
753 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
754 yy_push_state (chords);
758 Lily_lexer::push_figuredbass_state ()
760 yy_push_state (figures);
764 Lily_lexer::push_initial_state ()
766 yy_push_state (INITIAL);
770 Lily_lexer::push_lyric_state ()
772 yy_push_state (lyrics);
776 Lily_lexer::push_markup_state ()
778 yy_push_state (markup);
782 Lily_lexer::push_note_state (SCM tab)
784 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
785 yy_push_state (notes);
789 Lily_lexer::pop_state ()
791 if (YYSTATE == notes || YYSTATE == chords)
792 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
798 Lily_lexer::identifier_type (SCM sid)
800 int k = try_special_identifiers (&yylval.scm , sid);
801 return k >= 0 ? k : SCM_IDENTIFIER;
806 Lily_lexer::scan_escaped_word (string str)
808 // use more SCM for this.
810 // SCM sym = ly_symbol2scm (str.c_str ());
812 int i = lookup_keyword (str);
813 if (i == MARKUP && is_lyric_state ())
818 SCM sid = lookup_identifier (str);
819 if (is_music_function (sid))
821 yylval.scm = get_music_function_transform (sid);
823 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
824 push_extra_token (EXPECT_NO_MORE_ARGS);
825 for (; scm_is_pair (s); s = scm_cdr (s))
827 SCM cs = scm_car (s);
829 if (cs == ly_music_p_proc)
830 push_extra_token (EXPECT_MUSIC);
831 else if (cs == Pitch_type_p_proc)
832 push_extra_token (EXPECT_PITCH);
833 else if (cs == Duration_type_p_proc)
834 push_extra_token (EXPECT_DURATION);
835 else if (cs == ly_lily_module_constant ("markup?"))
836 push_extra_token (EXPECT_MARKUP);
837 else if (ly_is_procedure (cs))
838 push_extra_token (EXPECT_SCM);
839 else programming_error ("Function parameter without type-checking predicate");
841 return MUSIC_FUNCTION;
844 if (sid != SCM_UNDEFINED)
847 return identifier_type (sid);
850 string msg (_f ("unknown escaped string: `\\%s'", str));
851 LexerError (msg.c_str ());
853 yylval.scm = ly_string2scm (str);
859 Lily_lexer::scan_bare_word (string str)
861 SCM sym = ly_symbol2scm (str.c_str ());
862 if ((YYSTATE == notes) || (YYSTATE == chords)) {
863 SCM handle = SCM_BOOL_F;
864 if (scm_is_pair (pitchname_tab_stack_))
865 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
867 if (scm_is_pair (handle)) {
868 yylval.scm = scm_cdr (handle);
869 if (unsmob_pitch (yylval.scm))
870 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
871 else if (scm_is_symbol (yylval.scm))
874 else if ((YYSTATE == chords)
875 && (handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
877 yylval.scm = scm_cdr (handle);
878 return CHORD_MODIFIER;
880 if ((chord_repetition_.repetition_symbol_ != SCM_EOL)
881 && to_boolean (scm_equal_p (chord_repetition_.repetition_symbol_, sym)))
882 return CHORD_REPETITION;
885 yylval.scm = ly_string2scm (str);
890 Lily_lexer::get_state () const
892 if (YY_START == extratoken)
893 return hidden_state_;
899 Lily_lexer::is_note_state () const
901 return get_state () == notes;
905 Lily_lexer::is_chord_state () const
907 return get_state () == chords;
911 Lily_lexer::is_lyric_state () const
913 return get_state () == lyrics;
917 Lily_lexer::is_figure_state () const
919 return get_state () == figures;
923 urg, belong to string (_convert)
924 and should be generalised
927 strip_leading_white (string&s)
930 for (; i < s.length (); i++)
938 strip_trailing_white (string&s)
940 ssize i = s.length ();
945 s = s.substr (0, i + 1);
950 Lilypond_version oldest_version ("2.7.38");
954 is_valid_version (string s)
956 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
957 Lilypond_version ver (s);
958 if (int (ver) < oldest_version)
960 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
961 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
967 non_fatal_error (_f ("program too old: %s (file requires: %s)", current.to_string (), ver.to_string ()));
978 lyric_fudge (string s)
980 char *chars = string_copy (s);
982 for (char *p = chars; *p ; p++)
984 if (*p == '_' && (p == chars || *(p-1) != '\\'))
992 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
994 * (((char*)s.c_str ()) + i + 1) = 'c';
995 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
1002 Convert "NUM/DEN" into a '(NUM . DEN) cons.
1005 scan_fraction (string frac)
1007 ssize i = frac.find ('/');
1008 string left = frac.substr (0, i);
1009 string right = frac.substr (i + 1, (frac.length () - i + 1));
1011 int n = String_convert::dec2int (left);
1012 int d = String_convert::dec2int (right);
1013 return scm_cons (scm_from_int (n), scm_from_int (d));
1017 lookup_markup_command (string s)
1019 SCM proc = ly_lily_module_constant ("lookup-markup-command");
1020 return scm_call_1 (proc, ly_string2scm (s));
1024 lookup_markup_list_command (string s)
1026 SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
1027 return scm_call_1 (proc, ly_string2scm (s));
1030 /* Shut up lexer warnings. */
1050 silence_lexer_warnings ()
1052 (void) yy_start_stack_ptr;
1053 (void) yy_start_stack_depth;
1054 (void) yy_start_stack;
1055 (void) yy_push_state;
1056 (void) yy_pop_state;
1057 (void) yy_top_state;
1058 (void) silence_lexer_warnings;