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 "identifier-smob.hh"
50 #include "international.hh"
51 #include "interval.hh"
52 #include "lily-guile.hh"
53 #include "lily-lexer.hh"
54 #include "lily-parser.hh"
55 #include "lilypond-version.hh"
58 #include "music-function.hh"
59 #include "parse-scm.hh"
61 #include "source-file.hh"
62 #include "std-string.hh"
63 #include "string-convert.hh"
70 #define isatty HORRIBLEKLUDGE
72 void strip_trailing_white (string&);
73 void strip_leading_white (string&);
74 string lyric_fudge (string s);
75 SCM lookup_markup_command (string s);
76 SCM lookup_markup_list_command (string s);
77 bool is_valid_version (string s);
80 #define start_quote() \
81 yy_push_state (quote);\
82 yylval.string = new string
84 #define start_lyric_quote() \
85 yy_push_state (lyric_quote);\
86 yylval.string = new string
94 #define YY_USER_ACTION add_lexed_char (YYLeng ());
97 SCM scan_fraction (string);
98 SCM (* scm_parse_error_handler) (void *);
108 %option yyclass="Lily_lexer"
110 %option never-interactive
134 NATIONAL [\001-\006\021-\027\031\036]
135 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
137 DASHED_WORD {A}({AN}|-)*
138 DASHED_KEY_WORD \\{DASHED_WORD}
148 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 // windows-suck-suck-suck
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 if (be_verbose_global)
194 message (_ ("Skipping UTF-8 BOM"));
197 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
199 yy_push_state (longcomment);
203 %[^{\n\r] { // backup rule
214 <INITIAL,notes,figures,chords,markup>{
220 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
221 yy_push_state (version);
223 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}* {
224 yy_push_state (sourcefilename);
226 <INITIAL,chords,lyrics,notes,figures>\\sourcefileline{WHITE}* {
227 yy_push_state (sourcefileline);
229 <version>\"[^"]*\" { /* got the version number */
230 string s (YYText () + 1);
231 s = s.substr (0, s.rfind ('\"'));
235 SCM top_scope = scm_car (scm_last_pair (scopes_));
236 scm_module_define (top_scope, ly_symbol2scm ("version-seen"), SCM_BOOL_T);
238 if (!is_valid_version (s))
243 <sourcefilename>\"[^"]*\" {
244 string s (YYText () + 1);
245 s = s.substr (0, s.rfind ('\"'));
248 this->here_input().get_source_file ()->name_ = s;
249 message (_f ("Renaming input to: `%s'", s.c_str ()));
250 progress_indication ("\n");
251 scm_module_define (scm_car (scopes_),
252 ly_symbol2scm ("input-file-name"),
257 <sourcefileline>{INT} {
259 sscanf (YYText (), "%d", &i);
262 this->here_input ().get_source_file ()->set_line (here_input ().start (), i);
266 LexerError (_ ("quoted string expected after \\version").c_str ());
270 LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
274 LexerError (_ ("integer expected after \\sourcefileline").c_str ());
287 LexerError (_ ("EOF found inside a comment").c_str ());
288 is_main_input_ = false; // should be safe , can't have \include in --safe.
289 if (! close_input ())
290 yyterminate (); // can't move this, since it actually rets a YY_NULL
295 <INITIAL,chords,lyrics,notes,figures>\\maininput {
299 is_main_input_ = true;
302 error (_ ("\\maininput not allowed outside init files"));
305 <INITIAL,chords,lyrics,figures,notes>\\include {
306 yy_push_state (incl);
308 <incl>\"[^"]*\" { /* got the include file name */
309 string s (YYText ()+1);
310 s = s.substr (0, s.rfind ('"'));
312 new_input (s, sources_);
315 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
316 string s = YYText () + 1;
317 strip_trailing_white (s);
318 if (s.length () && (s[s.length () - 1] == ';'))
319 s = s.substr (0, s.length () - 1);
321 SCM sid = lookup_identifier (s);
322 if (scm_is_string (sid)) {
323 new_input (ly_scm2string (sid), sources_);
326 string msg (_f ("wrong or undefined identifier: `%s'", s ));
328 LexerError (msg.c_str ());
329 SCM err = scm_current_error_port ();
330 scm_puts ("This value was found in the table: ", err);
331 scm_display (sid, err);
334 <incl>\"[^"]* { // backup rule
335 error (_ ("end quote missing"));
338 <chords,notes,figures>{RESTNAME} {
339 char const *s = YYText ();
340 yylval.scm = scm_from_locale_string (s);
343 <chords,notes,figures>R {
344 return MULTI_MEASURE_REST;
346 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
348 Input hi = here_input();
350 SCM sval = ly_parse_scm (hi.start (), &n, hi,
351 be_safe_global && is_main_input_, parser_);
353 if (sval == SCM_UNDEFINED)
355 sval = SCM_UNSPECIFIED;
359 for (int i = 0; i < n; i++)
363 char_count_stack_.back () += n;
365 if (unpack_identifier (sval) != SCM_UNDEFINED)
367 yylval.scm = unpack_identifier(sval);
368 return identifier_type (yylval.scm);
371 for (size_t i = 0; i < pending_string_includes_.size (); i++)
372 new_input ("<included string>", pending_string_includes_[i],
374 pending_string_includes_.clear ();
379 <INITIAL,notes,lyrics>{
381 return DOUBLE_ANGLE_OPEN;
384 return DOUBLE_ANGLE_CLOSE;
411 return scan_bare_word (YYText ());
415 return scan_escaped_word (YYText () + 1);
418 yylval.scm = scan_fraction (YYText ());
423 yylval.i = String_convert::dec2int (string (YYText ()));
427 yylval.i = String_convert::dec2int (string (YYText ()));
431 yylval.i = String_convert::dec2int (string (YYText () +1));
438 *yylval.string += to_string (escaped_char (YYText ()[1]));
441 *yylval.string += YYText ();
447 /* yylval is union. Must remember STRING before setting SCM*/
448 string *sp = yylval.string;
449 yylval.scm = ly_string2scm (*sp);
451 return is_lyric_state () ? LYRICS_STRING : STRING;
454 *yylval.string += YYText ();
460 start_lyric_quote ();
463 yylval.scm = scan_fraction (YYText ());
467 yylval.i = String_convert::dec2int (string (YYText ()));
471 return scan_escaped_word (YYText () + 1);
475 string s (YYText ());
477 return yylval.i = EXTENDER;
479 return yylval.i = HYPHEN;
482 char c = s[s.length () - 1];
483 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
484 here_input ().warning (
485 _ ("Brace found at end of lyric. Did you forget a space?"));
486 yylval.scm = ly_string2scm (s);
489 return LYRICS_STRING;
497 return scan_bare_word (YYText ());
500 return scan_escaped_word (YYText () + 1);
503 yylval.scm = scan_fraction (YYText ());
507 yylval.i = String_convert::dec2int (string (YYText ()));
536 string str (YYText () + 1);
538 int token_type = MARKUP_FUNCTION;
539 SCM s = lookup_markup_command (str);
541 // lookup-markup-command returns a pair with the car
542 // being the function to call, and the cdr being the
543 // call signature specified to define-markup-command,
544 // a list of predicates.
546 if (!scm_is_pair (s)) {
547 // If lookup-markup-command was not successful, we
548 // try lookup-markup-list-command instead.
549 // If this fails as well, we just scan and return
551 s = lookup_markup_list_command (str);
553 token_type = MARKUP_LIST_FUNCTION;
555 return scan_escaped_word (str);
558 // If the list of predicates is, say,
559 // (number? number? markup?), then tokens
560 // EXPECT_MARKUP EXPECT_SCM EXPECT_SCM EXPECT_NO_MORE_ARGS
561 // will be generated. Note that we have to push them
562 // in reverse order, so the first token pushed in the
563 // loop will be EXPECT_NO_MORE_ARGS.
565 yylval.scm = scm_car(s);
567 // yylval now contains the function to call as token
568 // value (for token type MARKUP_FUNCTION or
569 // MARKUP_LIST_FUNCTION).
571 push_extra_token(EXPECT_NO_MORE_ARGS);
573 for (; scm_is_pair(s); s = scm_cdr(s)) {
574 SCM predicate = scm_car(s);
576 if (predicate == ly_lily_module_constant ("markup-list?"))
577 push_extra_token(EXPECT_MARKUP_LIST);
578 else if (predicate == ly_lily_module_constant ("markup?"))
579 push_extra_token(EXPECT_MARKUP);
581 push_extra_token(EXPECT_SCM);
588 [^#{}"\\ \t\n\r\f]+ {
589 string s (YYText ());
591 char c = s[s.length () - 1];
592 /* brace open is for not confusing dumb tools. */
593 if (c == '{' || c == '}')
594 here_input ().warning (
595 _ ("Brace found at end of markup. Did you forget a space?"));
596 yylval.scm = ly_string2scm (s);
609 /* 2 = init.ly + current file.
610 > because we're before closing, but is_main_input_ should
613 is_main_input_ = include_stack_.size () > 2;
615 /* Returns YY_NULL */
618 else if (!close_input ())
619 /* Returns YY_NULL */
625 return scan_bare_word (YYText ());
628 return scan_escaped_word (YYText () + 1);
633 return scan_bare_word (YYText ());
636 return scan_escaped_word (YYText () + 1);
640 int cnv = sscanf (YYText (), "%lf", &r);
644 yylval.scm = scm_from_double (r);
649 yylval.i = String_convert::dec2int (string (YYText ()));
659 char c = YYText ()[0];
664 <INITIAL,notes,figures>. {
668 <INITIAL,lyrics,notes,figures>\\. {
669 char c = YYText ()[1];
673 return E_ANGLE_CLOSE;
677 return E_EXCLAMATION;
683 return E_BRACKET_OPEN;
687 return E_BRACKET_CLOSE;
699 string msg = _f ("invalid character: `%c'", YYText ()[0]);
700 LexerError (msg.c_str ());
706 /* Make the lexer generate a token of the given type as the next token.
707 TODO: make it possible to define a value for the token as well */
709 Lily_lexer::push_extra_token (int token_type)
711 if (extra_token_types_.empty ())
713 if (YY_START != extratoken)
714 hidden_state_ = YY_START;
715 yy_push_state (extratoken);
717 extra_token_types_.push_back (token_type);
721 Lily_lexer::push_chord_state (SCM tab)
723 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
724 yy_push_state (chords);
728 Lily_lexer::push_figuredbass_state ()
730 yy_push_state (figures);
734 Lily_lexer::push_initial_state ()
736 yy_push_state (INITIAL);
740 Lily_lexer::push_lyric_state ()
742 yy_push_state (lyrics);
746 Lily_lexer::push_markup_state ()
748 yy_push_state (markup);
752 Lily_lexer::push_note_state (SCM tab)
754 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
755 yy_push_state (notes);
759 Lily_lexer::pop_state ()
761 if (YYSTATE == notes || YYSTATE == chords)
762 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
768 Lily_lexer::identifier_type (SCM sid)
770 int k = try_special_identifiers (&yylval.scm , sid);
771 return k >= 0 ? k : SCM_IDENTIFIER;
776 Lily_lexer::scan_escaped_word (string str)
778 // use more SCM for this.
780 // SCM sym = ly_symbol2scm (str.c_str ());
782 int i = lookup_keyword (str);
783 if (i == MARKUP && is_lyric_state ())
788 SCM sid = lookup_identifier (str);
789 if (is_music_function (sid))
791 yylval.scm = get_music_function_transform (sid);
793 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
794 push_extra_token (EXPECT_NO_MORE_ARGS);
795 for (; scm_is_pair (s); s = scm_cdr (s))
797 if (scm_car (s) == ly_music_p_proc)
798 push_extra_token (EXPECT_MUSIC);
799 else if (scm_car (s) == ly_lily_module_constant ("markup?"))
800 push_extra_token (EXPECT_MARKUP);
801 else if (ly_is_procedure (scm_car (s)))
802 push_extra_token (EXPECT_SCM);
803 else programming_error ("Function parameter without type-checking predicate");
805 return MUSIC_FUNCTION;
808 if (sid != SCM_UNDEFINED)
811 return identifier_type (sid);
814 string msg (_f ("unknown escaped string: `\\%s'", str));
815 LexerError (msg.c_str ());
817 yylval.scm = ly_string2scm (str);
823 Lily_lexer::scan_bare_word (string str)
825 SCM sym = ly_symbol2scm (str.c_str ());
826 if ((YYSTATE == notes) || (YYSTATE == chords)) {
827 SCM handle = SCM_BOOL_F;
828 if (scm_is_pair (pitchname_tab_stack_))
829 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
831 if (scm_is_pair (handle)) {
832 yylval.scm = scm_cdr (handle);
833 if (unsmob_pitch (yylval.scm))
834 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
835 else if (scm_is_symbol (yylval.scm))
838 else if ((YYSTATE == chords)
839 && (handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
841 yylval.scm = scm_cdr (handle);
842 return CHORD_MODIFIER;
844 if ((chord_repetition_.repetition_symbol_ != SCM_EOL)
845 && to_boolean (scm_equal_p (chord_repetition_.repetition_symbol_, sym)))
846 return CHORD_REPETITION;
849 yylval.scm = ly_string2scm (str);
854 Lily_lexer::get_state () const
856 if (YY_START == extratoken)
857 return hidden_state_;
863 Lily_lexer::is_note_state () const
865 return get_state () == notes;
869 Lily_lexer::is_chord_state () const
871 return get_state () == chords;
875 Lily_lexer::is_lyric_state () const
877 return get_state () == lyrics;
881 Lily_lexer::is_figure_state () const
883 return get_state () == figures;
887 urg, belong to string (_convert)
888 and should be generalised
891 strip_leading_white (string&s)
894 for (; i < s.length (); i++)
902 strip_trailing_white (string&s)
904 ssize i = s.length ();
909 s = s.substr (0, i + 1);
914 Lilypond_version oldest_version ("2.7.38");
918 is_valid_version (string s)
920 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
921 Lilypond_version ver (s);
922 if (int (ver) < oldest_version)
924 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
925 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
931 non_fatal_error (_f ("program too old: %s (file requires: %s)", current.to_string (), ver.to_string ()));
942 lyric_fudge (string s)
944 char *chars = string_copy (s);
946 for (char *p = chars; *p ; p++)
948 if (*p == '_' && (p == chars || *(p-1) != '\\'))
956 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
958 * (((char*)s.c_str ()) + i + 1) = 'c';
959 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
966 Convert "NUM/DEN" into a '(NUM . DEN) cons.
969 scan_fraction (string frac)
971 ssize i = frac.find ('/');
972 string left = frac.substr (0, i);
973 string right = frac.substr (i + 1, (frac.length () - i + 1));
975 int n = String_convert::dec2int (left);
976 int d = String_convert::dec2int (right);
977 return scm_cons (scm_from_int (n), scm_from_int (d));
981 lookup_markup_command (string s)
983 SCM proc = ly_lily_module_constant ("lookup-markup-command");
984 return scm_call_1 (proc, ly_string2scm (s));
988 lookup_markup_list_command (string s)
990 SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
991 return scm_call_1 (proc, ly_string2scm (s));
994 /* Shut up lexer warnings. */
1014 silence_lexer_warnings ()
1016 (void) yy_start_stack_ptr;
1017 (void) yy_start_stack_depth;
1018 (void) yy_start_stack;
1019 (void) yy_push_state;
1020 (void) yy_pop_state;
1021 (void) yy_top_state;
1022 (void) silence_lexer_warnings;