1 %{ // -*-Fundamental-*-
3 This file is part of LilyPond, the GNU music typesetter.
5 Copyright (C) 1996--2010 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 "identifier-smob.hh"
50 #include "international.hh"
51 #include "interval.hh"
52 #include "lily-guile.hh"
53 #include "lily-lexer.hh"
54 #include "lilypond-version.hh"
57 #include "music-function.hh"
58 #include "parse-scm.hh"
60 #include "source-file.hh"
61 #include "std-string.hh"
62 #include "string-convert.hh"
69 #define isatty HORRIBLEKLUDGE
71 void strip_trailing_white (string&);
72 void strip_leading_white (string&);
73 string lyric_fudge (string s);
74 SCM lookup_markup_command (string s);
75 SCM lookup_markup_list_command (string s);
76 bool is_valid_version (string s);
79 #define start_quote() \
80 yy_push_state (quote);\
81 yylval.string = new string
83 #define start_lyric_quote() \
84 yy_push_state (lyric_quote);\
85 yylval.string = new string
93 #define YY_USER_ACTION add_lexed_char (YYLeng ());
96 SCM scan_fraction (string);
97 SCM (* scm_parse_error_handler) (void *);
107 %option yyclass="Lily_lexer"
109 %option never-interactive
133 NATIONAL [\001-\006\021-\027\031\036]
134 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
136 DASHED_WORD {A}({AN}|-)*
137 DASHED_KEY_WORD \\{DASHED_WORD}
147 REAL ({INT}\.{N}*)|(-?\.{N}+)
150 HORIZONTALWHITE [ \t]
154 MARKUPCOMMAND \\({A}|[-_])+
155 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
159 BOM_UTF8 \357\273\277
165 // windows-suck-suck-suck
168 <extratoken>{ANY_CHAR} {
169 /* Generate a token without swallowing anything */
171 /* First unswallow the eaten character */
172 add_lexed_char (-YYLeng ());
175 /* produce requested token */
176 int type = extra_token_types_.back ();
177 extra_token_types_.pop_back ();
178 if (extra_token_types_.empty ())
184 /* Use the trailing context feature. Otherwise, the BOM will not be
185 found if the file starts with an identifier definition. */
186 <INITIAL,chords,lyrics,figures,notes>{BOM_UTF8}/.* {
187 if (this->lexloc_->line_number () != 1 || this->lexloc_->column_number () != 0)
189 LexerError (_ ("stray UTF-8 BOM encountered").c_str ());
192 if (be_verbose_global)
193 message (_ ("Skipping UTF-8 BOM"));
196 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
198 yy_push_state (longcomment);
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);
265 LexerError (_ ("quoted string expected after \\version").c_str ());
269 LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
273 LexerError (_ ("integer expected after \\sourcefileline").c_str ());
286 LexerError (_ ("EOF found inside a comment").c_str ());
287 is_main_input_ = false; // should be safe , can't have \include in --safe.
288 if (! close_input ())
289 yyterminate (); // can't move this, since it actually rets a YY_NULL
294 <INITIAL,chords,lyrics,notes,figures>\\maininput {
298 is_main_input_ = true;
301 error (_ ("\\maininput not allowed outside init files"));
304 <INITIAL,chords,lyrics,figures,notes>\\include {
305 yy_push_state (incl);
307 <incl>\"[^"]*\" { /* got the include file name */
308 string s (YYText ()+1);
309 s = s.substr (0, s.rfind ('"'));
311 new_input (s, sources_);
314 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
315 string s = YYText () + 1;
316 strip_trailing_white (s);
317 if (s.length () && (s[s.length () - 1] == ';'))
318 s = s.substr (0, s.length () - 1);
320 SCM sid = lookup_identifier (s);
321 if (scm_is_string (sid)) {
322 new_input (ly_scm2string (sid), sources_);
325 string msg (_f ("wrong or undefined identifier: `%s'", s ));
327 LexerError (msg.c_str ());
328 SCM err = scm_current_error_port ();
329 scm_puts ("This value was found in the table: ", err);
330 scm_display (sid, err);
333 <incl>\"[^"]* { // backup rule
334 error (_ ("end quote missing"));
337 <chords,notes,figures>{RESTNAME} {
338 char const *s = YYText ();
339 yylval.scm = scm_from_locale_string (s);
342 <chords,notes,figures>R {
343 return MULTI_MEASURE_REST;
345 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
347 Input hi = here_input();
349 SCM sval = ly_parse_scm (hi.start (), &n, hi,
350 be_safe_global && is_main_input_, parser_);
352 if (sval == SCM_UNDEFINED)
354 sval = SCM_UNSPECIFIED;
358 for (int i = 0; i < n; i++)
362 char_count_stack_.back () += n;
364 if (unpack_identifier (sval) != SCM_UNDEFINED)
366 yylval.scm = unpack_identifier(sval);
367 return identifier_type (yylval.scm);
373 <INITIAL,notes,lyrics>{
375 return DOUBLE_ANGLE_OPEN;
378 return DOUBLE_ANGLE_CLOSE;
405 return scan_bare_word (YYText ());
409 return scan_escaped_word (YYText () + 1);
412 yylval.scm = scan_fraction (YYText ());
417 yylval.i = String_convert::dec2int (string (YYText ()));
421 yylval.i = String_convert::dec2int (string (YYText ()));
425 yylval.i = String_convert::dec2int (string (YYText () +1));
432 *yylval.string += to_string (escaped_char (YYText ()[1]));
435 *yylval.string += YYText ();
441 /* yylval is union. Must remember STRING before setting SCM*/
442 string *sp = yylval.string;
443 yylval.scm = ly_string2scm (*sp);
445 return is_lyric_state () ? LYRICS_STRING : STRING;
448 *yylval.string += YYText ();
454 start_lyric_quote ();
457 yylval.scm = scan_fraction (YYText ());
461 yylval.i = String_convert::dec2int (string (YYText ()));
465 return scan_escaped_word (YYText () + 1);
469 string s (YYText ());
471 return yylval.i = EXTENDER;
473 return yylval.i = HYPHEN;
476 char c = s[s.length () - 1];
477 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
478 here_input ().warning (
479 _ ("Brace found at end of lyric. Did you forget a space?"));
480 yylval.scm = ly_string2scm (s);
483 return LYRICS_STRING;
491 return scan_bare_word (YYText ());
494 return scan_escaped_word (YYText () + 1);
497 yylval.scm = scan_fraction (YYText ());
501 yylval.i = String_convert::dec2int (string (YYText ()));
530 string str (YYText () + 1);
532 int token_type = MARKUP_FUNCTION;
533 SCM s = lookup_markup_command (str);
535 // lookup-markup-command returns a pair with the car
536 // being the function to call, and the cdr being the
537 // call signature specified to define-markup-command,
538 // a list of predicates.
540 if (!scm_is_pair (s)) {
541 // If lookup-markup-command was not successful, we
542 // try lookup-markup-list-command instead.
543 // If this fails as well, we just scan and return
545 s = lookup_markup_list_command (str);
547 token_type = MARKUP_LIST_FUNCTION;
549 return scan_escaped_word (str);
552 // If the list of predicates is, say,
553 // (number? number? markup?), then tokens
554 // EXPECT_MARKUP EXPECT_SCM EXPECT_SCM EXPECT_NO_MORE_ARGS
555 // will be generated. Note that we have to push them
556 // in reverse order, so the first token pushed in the
557 // loop will be EXPECT_NO_MORE_ARGS.
559 yylval.scm = scm_car(s);
561 // yylval now contains the function to call as token
562 // value (for token type MARKUP_FUNCTION or
563 // MARKUP_LIST_FUNCTION).
565 push_extra_token(EXPECT_NO_MORE_ARGS);
567 for (; scm_is_pair(s); s = scm_cdr(s)) {
568 SCM predicate = scm_car(s);
570 if (predicate == ly_lily_module_constant ("markup-list?"))
571 push_extra_token(EXPECT_MARKUP_LIST);
572 else if (predicate == ly_lily_module_constant ("markup?"))
573 push_extra_token(EXPECT_MARKUP);
575 push_extra_token(EXPECT_SCM);
582 [^#{}"\\ \t\n\r\f]+ {
583 string s (YYText ());
585 char c = s[s.length () - 1];
586 /* brace open is for not confusing dumb tools. */
587 if (c == '{' || c == '}')
588 here_input ().warning (
589 _ ("Brace found at end of markup. Did you forget a space?"));
590 yylval.scm = ly_string2scm (s);
603 /* 2 = init.ly + current file.
604 > because we're before closing, but is_main_input_ should
607 is_main_input_ = include_stack_.size () > 2;
609 /* Returns YY_NULL */
612 else if (!close_input ())
613 /* Returns YY_NULL */
619 return scan_bare_word (YYText ());
622 return scan_escaped_word (YYText () + 1);
627 return scan_bare_word (YYText ());
630 return scan_escaped_word (YYText () + 1);
634 int cnv = sscanf (YYText (), "%lf", &r);
638 yylval.scm = scm_from_double (r);
643 yylval.i = String_convert::dec2int (string (YYText ()));
653 char c = YYText ()[0];
658 <INITIAL,notes,figures>. {
662 <INITIAL,lyrics,notes,figures>\\. {
663 char c = YYText ()[1];
667 return E_ANGLE_CLOSE;
671 return E_EXCLAMATION;
677 return E_BRACKET_OPEN;
681 return E_BRACKET_CLOSE;
693 string msg = _f ("invalid character: `%c'", YYText ()[0]);
694 LexerError (msg.c_str ());
700 /* Make the lexer generate a token of the given type as the next token.
701 TODO: make it possible to define a value for the token as well */
703 Lily_lexer::push_extra_token (int token_type)
705 if (extra_token_types_.empty ())
707 if (YY_START != extratoken)
708 hidden_state_ = YY_START;
709 yy_push_state (extratoken);
711 extra_token_types_.push_back (token_type);
715 Lily_lexer::push_chord_state (SCM tab)
717 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
718 yy_push_state (chords);
722 Lily_lexer::push_figuredbass_state ()
724 yy_push_state (figures);
728 Lily_lexer::push_initial_state ()
730 yy_push_state (INITIAL);
734 Lily_lexer::push_lyric_state ()
736 yy_push_state (lyrics);
740 Lily_lexer::push_markup_state ()
742 yy_push_state (markup);
746 Lily_lexer::push_note_state (SCM tab)
748 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
749 yy_push_state (notes);
753 Lily_lexer::pop_state ()
755 if (YYSTATE == notes || YYSTATE == chords)
756 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
762 Lily_lexer::identifier_type (SCM sid)
764 int k = try_special_identifiers (&yylval.scm , sid);
765 return k >= 0 ? k : SCM_IDENTIFIER;
770 Lily_lexer::scan_escaped_word (string str)
772 // use more SCM for this.
774 // SCM sym = ly_symbol2scm (str.c_str ());
776 int i = lookup_keyword (str);
777 if (i == MARKUP && is_lyric_state ())
782 SCM sid = lookup_identifier (str);
783 if (is_music_function (sid))
785 yylval.scm = get_music_function_transform (sid);
787 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
788 push_extra_token (EXPECT_NO_MORE_ARGS);
789 for (; scm_is_pair (s); s = scm_cdr (s))
791 if (scm_car (s) == ly_music_p_proc)
792 push_extra_token (EXPECT_MUSIC);
793 else if (scm_car (s) == ly_lily_module_constant ("markup?"))
794 push_extra_token (EXPECT_MARKUP);
795 else if (ly_is_procedure (scm_car (s)))
796 push_extra_token (EXPECT_SCM);
797 else programming_error ("Function parameter without type-checking predicate");
799 return MUSIC_FUNCTION;
802 if (sid != SCM_UNDEFINED)
805 return identifier_type (sid);
808 string msg (_f ("unknown escaped string: `\\%s'", str));
809 LexerError (msg.c_str ());
811 yylval.scm = ly_string2scm (str);
817 Lily_lexer::scan_bare_word (string str)
819 SCM sym = ly_symbol2scm (str.c_str ());
820 if ((YYSTATE == notes) || (YYSTATE == chords)) {
821 SCM handle = SCM_BOOL_F;
822 if (scm_is_pair (pitchname_tab_stack_))
823 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
825 if (scm_is_pair (handle)) {
826 yylval.scm = scm_cdr (handle);
827 if (unsmob_pitch (yylval.scm))
828 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
829 else if (scm_is_symbol (yylval.scm))
832 else if ((YYSTATE == chords)
833 && (handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
835 yylval.scm = scm_cdr (handle);
836 return CHORD_MODIFIER;
838 if ((chord_repetition_.repetition_symbol_ != SCM_EOL)
839 && to_boolean (scm_equal_p (chord_repetition_.repetition_symbol_, sym)))
840 return CHORD_REPETITION;
843 yylval.scm = ly_string2scm (str);
848 Lily_lexer::get_state () const
850 if (YY_START == extratoken)
851 return hidden_state_;
857 Lily_lexer::is_note_state () const
859 return get_state () == notes;
863 Lily_lexer::is_chord_state () const
865 return get_state () == chords;
869 Lily_lexer::is_lyric_state () const
871 return get_state () == lyrics;
875 Lily_lexer::is_figure_state () const
877 return get_state () == figures;
881 urg, belong to string (_convert)
882 and should be generalised
885 strip_leading_white (string&s)
888 for (; i < s.length (); i++)
896 strip_trailing_white (string&s)
898 ssize i = s.length ();
903 s = s.substr (0, i + 1);
908 Lilypond_version oldest_version ("2.7.38");
912 is_valid_version (string s)
914 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
915 Lilypond_version ver (s);
916 if (int (ver) < oldest_version)
918 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
919 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
925 non_fatal_error (_f ("program too old: %s (file requires: %s)", current.to_string (), ver.to_string ()));
936 lyric_fudge (string s)
938 char *chars = string_copy (s);
940 for (char *p = chars; *p ; p++)
942 if (*p == '_' && (p == chars || *(p-1) != '\\'))
950 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
952 * (((char*)s.c_str ()) + i + 1) = 'c';
953 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
960 Convert "NUM/DEN" into a '(NUM . DEN) cons.
963 scan_fraction (string frac)
965 ssize i = frac.find ('/');
966 string left = frac.substr (0, i);
967 string right = frac.substr (i + 1, (frac.length () - i + 1));
969 int n = String_convert::dec2int (left);
970 int d = String_convert::dec2int (right);
971 return scm_cons (scm_from_int (n), scm_from_int (d));
975 lookup_markup_command (string s)
977 SCM proc = ly_lily_module_constant ("lookup-markup-command");
978 return scm_call_1 (proc, ly_string2scm (s));
982 lookup_markup_list_command (string s)
984 SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
985 return scm_call_1 (proc, ly_string2scm (s));
988 /* Shut up lexer warnings. */
1008 silence_lexer_warnings ()
1010 (void) yy_start_stack_ptr;
1011 (void) yy_start_stack_depth;
1012 (void) yy_start_stack;
1013 (void) yy_push_state;
1014 (void) yy_pop_state;
1015 (void) yy_top_state;
1016 (void) silence_lexer_warnings;