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);
531 SCM s = lookup_markup_command (str);
532 SCM s2 = lookup_markup_list_command (str);
533 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
534 yylval.scm = scm_car(s);
535 SCM tag = scm_cdr(s);
536 if (tag == ly_symbol2scm("markup0"))
537 return MARKUP_HEAD_MARKUP0;
538 if (tag == ly_symbol2scm("empty"))
539 return MARKUP_HEAD_EMPTY;
540 else if (tag == ly_symbol2scm ("markup0-markup1"))
541 return MARKUP_HEAD_MARKUP0_MARKUP1;
542 else if (tag == ly_symbol2scm ("markup-list0"))
543 return MARKUP_HEAD_LIST0;
544 else if (tag == ly_symbol2scm ("scheme0"))
545 return MARKUP_HEAD_SCM0;
546 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
547 return MARKUP_HEAD_SCM0_SCM1;
548 else if (tag == ly_symbol2scm ("scheme0-markup1"))
549 return MARKUP_HEAD_SCM0_MARKUP1;
550 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
551 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
552 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2-markup3"))
553 return MARKUP_HEAD_SCM0_SCM1_MARKUP2_MARKUP3;
554 else if (tag == ly_symbol2scm ("scheme0-markup1-markup2"))
555 return MARKUP_HEAD_SCM0_MARKUP1_MARKUP2;
556 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
557 return MARKUP_HEAD_SCM0_SCM1_SCM2;
559 programming_error ("no parser tag defined for this markup signature");
563 } else if (scm_is_pair (s2) && scm_is_symbol (scm_cdr (s2))) {
564 yylval.scm = scm_car(s2);
565 SCM tag = scm_cdr(s2);
566 if (tag == ly_symbol2scm("empty"))
567 return MARKUP_LIST_HEAD_EMPTY;
568 else if (tag == ly_symbol2scm ("scheme0"))
569 return MARKUP_LIST_HEAD_SCM0;
570 else if (tag == ly_symbol2scm ("markup-list0"))
571 return MARKUP_LIST_HEAD_LIST0;
572 else if (tag == ly_symbol2scm ("scheme0-markup-list1"))
573 return MARKUP_LIST_HEAD_SCM0_LIST1;
574 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup-list2"))
575 return MARKUP_LIST_HEAD_SCM0_SCM1_LIST2;
577 programming_error ("no parser tag defined for this markup list signature");
582 return scan_escaped_word (str);
587 [^#{}"\\ \t\n\r\f]+ {
588 string s (YYText ());
590 char c = s[s.length () - 1];
591 /* brace open is for not confusing dumb tools. */
592 if (c == '{' || c == '}')
593 here_input ().warning (
594 _ ("Brace found at end of markup. Did you forget a space?"));
595 yylval.scm = ly_string2scm (s);
608 /* 2 = init.ly + current file.
609 > because we're before closing, but is_main_input_ should
612 is_main_input_ = include_stack_.size () > 2;
614 /* Returns YY_NULL */
617 else if (!close_input ())
618 /* Returns YY_NULL */
624 return scan_bare_word (YYText ());
627 return scan_escaped_word (YYText () + 1);
632 return scan_bare_word (YYText ());
635 return scan_escaped_word (YYText () + 1);
639 int cnv = sscanf (YYText (), "%lf", &r);
643 yylval.scm = scm_from_double (r);
648 yylval.i = String_convert::dec2int (string (YYText ()));
658 char c = YYText ()[0];
663 <INITIAL,notes,figures>. {
667 <INITIAL,lyrics,notes,figures>\\. {
668 char c = YYText ()[1];
672 return E_ANGLE_CLOSE;
676 return E_EXCLAMATION;
682 return E_BRACKET_OPEN;
686 return E_BRACKET_CLOSE;
698 string msg = _f ("invalid character: `%c'", YYText ()[0]);
699 LexerError (msg.c_str ());
705 /* Make the lexer generate a token of the given type as the next token.
706 TODO: make it possible to define a value for the token as well */
708 Lily_lexer::push_extra_token (int token_type)
710 if (extra_token_types_.empty ())
712 if (YY_START != extratoken)
713 hidden_state_ = YY_START;
714 yy_push_state (extratoken);
716 extra_token_types_.push_back (token_type);
720 Lily_lexer::push_chord_state (SCM tab)
722 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
723 yy_push_state (chords);
727 Lily_lexer::push_figuredbass_state ()
729 yy_push_state (figures);
733 Lily_lexer::push_initial_state ()
735 yy_push_state (INITIAL);
739 Lily_lexer::push_lyric_state ()
741 yy_push_state (lyrics);
745 Lily_lexer::push_markup_state ()
747 yy_push_state (markup);
751 Lily_lexer::push_note_state (SCM tab)
753 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
754 yy_push_state (notes);
758 Lily_lexer::pop_state ()
760 if (YYSTATE == notes || YYSTATE == chords)
761 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
767 Lily_lexer::identifier_type (SCM sid)
769 int k = try_special_identifiers (&yylval.scm , sid);
770 return k >= 0 ? k : SCM_IDENTIFIER;
775 Lily_lexer::scan_escaped_word (string str)
777 // use more SCM for this.
779 // SCM sym = ly_symbol2scm (str.c_str ());
781 int i = lookup_keyword (str);
782 if (i == MARKUP && is_lyric_state ())
787 SCM sid = lookup_identifier (str);
788 if (is_music_function (sid))
790 yylval.scm = get_music_function_transform (sid);
792 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
793 push_extra_token (EXPECT_NO_MORE_ARGS);
794 for (; scm_is_pair (s); s = scm_cdr (s))
796 if (scm_car (s) == ly_music_p_proc)
797 push_extra_token (EXPECT_MUSIC);
798 else if (scm_car (s) == ly_lily_module_constant ("markup?"))
799 push_extra_token (EXPECT_MARKUP);
800 else if (ly_is_procedure (scm_car (s)))
801 push_extra_token (EXPECT_SCM);
802 else programming_error ("Function parameter without type-checking predicate");
804 return MUSIC_FUNCTION;
807 if (sid != SCM_UNDEFINED)
810 return identifier_type (sid);
813 string msg (_f ("unknown escaped string: `\\%s'", str));
814 LexerError (msg.c_str ());
816 yylval.scm = ly_string2scm (str);
822 Lily_lexer::scan_bare_word (string str)
824 SCM sym = ly_symbol2scm (str.c_str ());
825 if ((YYSTATE == notes) || (YYSTATE == chords)) {
826 SCM handle = SCM_BOOL_F;
827 if (scm_is_pair (pitchname_tab_stack_))
828 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
830 if (scm_is_pair (handle)) {
831 yylval.scm = scm_cdr (handle);
832 if (unsmob_pitch (yylval.scm))
833 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
834 else if (scm_is_symbol (yylval.scm))
837 else if ((YYSTATE == chords)
838 && (handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
840 yylval.scm = scm_cdr (handle);
841 return CHORD_MODIFIER;
843 if ((chord_repetition_.repetition_symbol_ != SCM_EOL)
844 && to_boolean (scm_equal_p (chord_repetition_.repetition_symbol_, sym)))
845 return CHORD_REPETITION;
848 yylval.scm = ly_string2scm (str);
853 Lily_lexer::get_state () const
855 if (YY_START == extratoken)
856 return hidden_state_;
862 Lily_lexer::is_note_state () const
864 return get_state () == notes;
868 Lily_lexer::is_chord_state () const
870 return get_state () == chords;
874 Lily_lexer::is_lyric_state () const
876 return get_state () == lyrics;
880 Lily_lexer::is_figure_state () const
882 return get_state () == figures;
886 urg, belong to string (_convert)
887 and should be generalised
890 strip_leading_white (string&s)
893 for (; i < s.length (); i++)
901 strip_trailing_white (string&s)
903 ssize i = s.length ();
908 s = s.substr (0, i + 1);
913 Lilypond_version oldest_version ("2.7.38");
917 is_valid_version (string s)
919 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
920 Lilypond_version ver (s);
921 if (int (ver) < oldest_version)
923 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
924 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
930 non_fatal_error (_f ("program too old: %s (file requires: %s)", current.to_string (), ver.to_string ()));
941 lyric_fudge (string s)
943 char *chars = string_copy (s);
945 for (char *p = chars; *p ; p++)
947 if (*p == '_' && (p == chars || *(p-1) != '\\'))
955 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
957 * (((char*)s.c_str ()) + i + 1) = 'c';
958 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
965 Convert "NUM/DEN" into a '(NUM . DEN) cons.
968 scan_fraction (string frac)
970 ssize i = frac.find ('/');
971 string left = frac.substr (0, i);
972 string right = frac.substr (i + 1, (frac.length () - i + 1));
974 int n = String_convert::dec2int (left);
975 int d = String_convert::dec2int (right);
976 return scm_cons (scm_from_int (n), scm_from_int (d));
980 lookup_markup_command (string s)
982 SCM proc = ly_lily_module_constant ("lookup-markup-command");
983 return scm_call_1 (proc, ly_string2scm (s));
987 lookup_markup_list_command (string s)
989 SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
990 return scm_call_1 (proc, ly_string2scm (s));
993 /* Shut up lexer warnings. */
1013 silence_lexer_warnings ()
1015 (void) yy_start_stack_ptr;
1016 (void) yy_start_stack_depth;
1017 (void) yy_start_stack;
1018 (void) yy_push_state;
1019 (void) yy_pop_state;
1020 (void) yy_top_state;
1021 (void) silence_lexer_warnings;