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}
139 DASHED_WORD {A}({AN}|-)*
140 DASHED_KEY_WORD \\{DASHED_WORD}
150 REAL ({INT}\.{N}*)|(-?\.{N}+)
153 HORIZONTALWHITE [ \t]
157 MARKUPCOMMAND \\({A}|[-_])+
158 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
162 BOM_UTF8 \357\273\277
168 // windows-suck-suck-suck
171 <extratoken>{ANY_CHAR} {
172 /* Generate a token without swallowing anything */
174 /* First unswallow the eaten character */
175 add_lexed_char (-YYLeng ());
178 /* produce requested token */
179 int type = extra_token_types_.back ();
180 extra_token_types_.pop_back ();
181 if (extra_token_types_.empty ())
187 /* Use the trailing context feature. Otherwise, the BOM will not be
188 found if the file starts with an identifier definition. */
189 <INITIAL,chords,lyrics,figures,notes>{BOM_UTF8}/.* {
190 if (this->lexloc_->line_number () != 1 || this->lexloc_->column_number () != 0)
192 LexerError (_ ("stray UTF-8 BOM encountered").c_str ());
195 if (be_verbose_global)
196 message (_ ("Skipping UTF-8 BOM"));
199 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
201 yy_push_state (longcomment);
203 %[^{\n\r][^\n\r]*[\n\r] {
205 %[^{\n\r] { // backup rule
216 <INITIAL,notes,figures,chords,markup>{
222 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
223 yy_push_state (version);
225 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}* {
226 yy_push_state (sourcefilename);
228 <INITIAL,chords,lyrics,notes,figures>\\sourcefileline{WHITE}* {
229 yy_push_state (sourcefileline);
231 <version>\"[^"]*\" { /* got the version number */
232 string s (YYText () + 1);
233 s = s.substr (0, s.rfind ('\"'));
237 SCM top_scope = scm_car (scm_last_pair (scopes_));
238 scm_module_define (top_scope, ly_symbol2scm ("version-seen"), SCM_BOOL_T);
240 if (!is_valid_version (s))
245 <sourcefilename>\"[^"]*\" {
246 string s (YYText () + 1);
247 s = s.substr (0, s.rfind ('\"'));
250 this->here_input().get_source_file ()->name_ = s;
251 message (_f ("Renaming input to: `%s'", s.c_str ()));
252 progress_indication ("\n");
253 scm_module_define (scm_car (scopes_),
254 ly_symbol2scm ("input-file-name"),
259 <sourcefileline>{INT} {
261 sscanf (YYText (), "%d", &i);
264 this->here_input ().get_source_file ()->set_line (here_input ().start (), i);
268 LexerError (_ ("quoted string expected after \\version").c_str ());
272 LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
276 LexerError (_ ("integer expected after \\sourcefileline").c_str ());
289 LexerError (_ ("EOF found inside a comment").c_str ());
290 is_main_input_ = false; // should be safe , can't have \include in --safe.
291 if (! close_input ())
292 yyterminate (); // can't move this, since it actually rets a YY_NULL
297 <INITIAL,chords,lyrics,notes,figures>\\maininput {
301 is_main_input_ = true;
304 error (_ ("\\maininput not allowed outside init files"));
307 <INITIAL,chords,lyrics,figures,notes>\\include {
308 yy_push_state (incl);
310 <incl>\"[^"]*\" { /* got the include file name */
311 string s (YYText ()+1);
312 s = s.substr (0, s.rfind ('"'));
314 new_input (s, sources_);
317 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
318 string s = YYText () + 1;
319 strip_trailing_white (s);
320 if (s.length () && (s[s.length () - 1] == ';'))
321 s = s.substr (0, s.length () - 1);
323 SCM sid = lookup_identifier (s);
324 if (scm_is_string (sid)) {
325 new_input (ly_scm2string (sid), sources_);
328 string msg (_f ("wrong or undefined identifier: `%s'", s ));
330 LexerError (msg.c_str ());
331 SCM err = scm_current_error_port ();
332 scm_puts ("This value was found in the table: ", err);
333 scm_display (sid, err);
336 <incl>\"[^"]* { // backup rule
337 error (_ ("end quote missing"));
340 <chords,notes,figures>{RESTNAME} {
341 char const *s = YYText ();
342 yylval.scm = scm_from_locale_string (s);
345 <chords,notes,figures>R {
346 return MULTI_MEASURE_REST;
348 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
350 Input hi = here_input();
352 SCM sval = ly_parse_scm (hi.start (), &n, hi,
353 be_safe_global && is_main_input_, parser_);
355 if (sval == SCM_UNDEFINED)
357 sval = SCM_UNSPECIFIED;
361 for (int i = 0; i < n; i++)
365 char_count_stack_.back () += n;
367 if (unpack_identifier (sval) != SCM_UNDEFINED)
369 yylval.scm = unpack_identifier(sval);
370 return identifier_type (yylval.scm);
373 for (size_t i = 0; i < pending_string_includes_.size (); i++)
374 new_input ("<included string>", pending_string_includes_[i],
376 pending_string_includes_.clear ();
381 <INITIAL,notes,lyrics>{
383 return DOUBLE_ANGLE_OPEN;
386 return DOUBLE_ANGLE_CLOSE;
413 return scan_bare_word (YYText ());
417 return scan_escaped_word (YYText () + 1);
420 yylval.scm = scan_fraction (YYText ());
425 yylval.i = String_convert::dec2int (string (YYText ()));
429 yylval.i = String_convert::dec2int (string (YYText ()));
433 yylval.i = String_convert::dec2int (string (YYText () +1));
440 *yylval.string += to_string (escaped_char (YYText ()[1]));
443 *yylval.string += YYText ();
449 /* yylval is union. Must remember STRING before setting SCM*/
450 string *sp = yylval.string;
451 yylval.scm = ly_string2scm (*sp);
453 return is_lyric_state () ? LYRICS_STRING : STRING;
456 *yylval.string += YYText ();
462 start_lyric_quote ();
465 yylval.scm = scan_fraction (YYText ());
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 ());
509 yylval.i = String_convert::dec2int (string (YYText ()));
538 string str (YYText () + 1);
540 int token_type = MARKUP_FUNCTION;
541 SCM s = lookup_markup_command (str);
543 // lookup-markup-command returns a pair with the car
544 // being the function to call, and the cdr being the
545 // call signature specified to define-markup-command,
546 // a list of predicates.
548 if (!scm_is_pair (s)) {
549 // If lookup-markup-command was not successful, we
550 // try lookup-markup-list-command instead.
551 // If this fails as well, we just scan and return
553 s = lookup_markup_list_command (str);
555 token_type = MARKUP_LIST_FUNCTION;
557 return scan_escaped_word (str);
560 // If the list of predicates is, say,
561 // (number? number? markup?), then tokens
562 // EXPECT_MARKUP EXPECT_SCM EXPECT_SCM EXPECT_NO_MORE_ARGS
563 // will be generated. Note that we have to push them
564 // in reverse order, so the first token pushed in the
565 // loop will be EXPECT_NO_MORE_ARGS.
567 yylval.scm = scm_car(s);
569 // yylval now contains the function to call as token
570 // value (for token type MARKUP_FUNCTION or
571 // MARKUP_LIST_FUNCTION).
573 push_extra_token(EXPECT_NO_MORE_ARGS);
575 for (; scm_is_pair(s); s = scm_cdr(s)) {
576 SCM predicate = scm_car(s);
578 if (predicate == ly_lily_module_constant ("markup-list?"))
579 push_extra_token(EXPECT_MARKUP_LIST);
580 else if (predicate == ly_lily_module_constant ("markup?"))
581 push_extra_token(EXPECT_MARKUP);
583 push_extra_token(EXPECT_SCM);
590 [^#{}"\\ \t\n\r\f]+ {
591 string s (YYText ());
593 char c = s[s.length () - 1];
594 /* brace open is for not confusing dumb tools. */
595 if (c == '{' || c == '}')
596 here_input ().warning (
597 _ ("Brace found at end of markup. Did you forget a space?"));
598 yylval.scm = ly_string2scm (s);
611 /* 2 = init.ly + current file.
612 > because we're before closing, but is_main_input_ should
615 is_main_input_ = include_stack_.size () > 2;
617 /* Returns YY_NULL */
620 else if (!close_input ())
621 /* Returns YY_NULL */
627 return scan_bare_word (YYText ());
630 return scan_escaped_word (YYText () + 1);
635 return scan_bare_word (YYText ());
638 return scan_escaped_word (YYText () + 1);
642 int cnv = sscanf (YYText (), "%lf", &r);
646 yylval.scm = scm_from_double (r);
651 yylval.i = String_convert::dec2int (string (YYText ()));
661 char c = YYText ()[0];
666 <INITIAL,notes,figures>. {
670 <INITIAL,lyrics,notes,figures>\\. {
671 char c = YYText ()[1];
675 return E_ANGLE_CLOSE;
679 return E_EXCLAMATION;
685 return E_BRACKET_OPEN;
689 return E_BRACKET_CLOSE;
701 string msg = _f ("invalid character: `%c'", YYText ()[0]);
702 LexerError (msg.c_str ());
708 /* Make the lexer generate a token of the given type as the next token.
709 TODO: make it possible to define a value for the token as well */
711 Lily_lexer::push_extra_token (int token_type)
713 if (extra_token_types_.empty ())
715 if (YY_START != extratoken)
716 hidden_state_ = YY_START;
717 yy_push_state (extratoken);
719 extra_token_types_.push_back (token_type);
723 Lily_lexer::push_chord_state (SCM tab)
725 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
726 yy_push_state (chords);
730 Lily_lexer::push_figuredbass_state ()
732 yy_push_state (figures);
736 Lily_lexer::push_initial_state ()
738 yy_push_state (INITIAL);
742 Lily_lexer::push_lyric_state ()
744 yy_push_state (lyrics);
748 Lily_lexer::push_markup_state ()
750 yy_push_state (markup);
754 Lily_lexer::push_note_state (SCM tab)
756 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
757 yy_push_state (notes);
761 Lily_lexer::pop_state ()
763 if (YYSTATE == notes || YYSTATE == chords)
764 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
770 Lily_lexer::identifier_type (SCM sid)
772 int k = try_special_identifiers (&yylval.scm , sid);
773 return k >= 0 ? k : SCM_IDENTIFIER;
778 Lily_lexer::scan_escaped_word (string str)
780 // use more SCM for this.
782 // SCM sym = ly_symbol2scm (str.c_str ());
784 int i = lookup_keyword (str);
785 if (i == MARKUP && is_lyric_state ())
790 SCM sid = lookup_identifier (str);
791 if (is_music_function (sid))
793 yylval.scm = get_music_function_transform (sid);
795 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
796 push_extra_token (EXPECT_NO_MORE_ARGS);
797 for (; scm_is_pair (s); s = scm_cdr (s))
799 SCM cs = scm_car (s);
801 if (cs == ly_music_p_proc)
802 push_extra_token (EXPECT_MUSIC);
803 else if (cs == Pitch_type_p_proc)
804 push_extra_token (EXPECT_PITCH);
805 else if (cs == Duration_type_p_proc)
806 push_extra_token (EXPECT_DURATION);
807 else if (cs == ly_lily_module_constant ("markup?"))
808 push_extra_token (EXPECT_MARKUP);
809 else if (ly_is_procedure (cs))
810 push_extra_token (EXPECT_SCM);
811 else programming_error ("Function parameter without type-checking predicate");
813 return MUSIC_FUNCTION;
816 if (sid != SCM_UNDEFINED)
819 return identifier_type (sid);
822 string msg (_f ("unknown escaped string: `\\%s'", str));
823 LexerError (msg.c_str ());
825 yylval.scm = ly_string2scm (str);
831 Lily_lexer::scan_bare_word (string str)
833 SCM sym = ly_symbol2scm (str.c_str ());
834 if ((YYSTATE == notes) || (YYSTATE == chords)) {
835 SCM handle = SCM_BOOL_F;
836 if (scm_is_pair (pitchname_tab_stack_))
837 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
839 if (scm_is_pair (handle)) {
840 yylval.scm = scm_cdr (handle);
841 if (unsmob_pitch (yylval.scm))
842 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
843 else if (scm_is_symbol (yylval.scm))
846 else if ((YYSTATE == chords)
847 && (handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
849 yylval.scm = scm_cdr (handle);
850 return CHORD_MODIFIER;
852 if ((chord_repetition_.repetition_symbol_ != SCM_EOL)
853 && to_boolean (scm_equal_p (chord_repetition_.repetition_symbol_, sym)))
854 return CHORD_REPETITION;
857 yylval.scm = ly_string2scm (str);
862 Lily_lexer::get_state () const
864 if (YY_START == extratoken)
865 return hidden_state_;
871 Lily_lexer::is_note_state () const
873 return get_state () == notes;
877 Lily_lexer::is_chord_state () const
879 return get_state () == chords;
883 Lily_lexer::is_lyric_state () const
885 return get_state () == lyrics;
889 Lily_lexer::is_figure_state () const
891 return get_state () == figures;
895 urg, belong to string (_convert)
896 and should be generalised
899 strip_leading_white (string&s)
902 for (; i < s.length (); i++)
910 strip_trailing_white (string&s)
912 ssize i = s.length ();
917 s = s.substr (0, i + 1);
922 Lilypond_version oldest_version ("2.7.38");
926 is_valid_version (string s)
928 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
929 Lilypond_version ver (s);
930 if (int (ver) < oldest_version)
932 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
933 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
939 non_fatal_error (_f ("program too old: %s (file requires: %s)", current.to_string (), ver.to_string ()));
950 lyric_fudge (string s)
952 char *chars = string_copy (s);
954 for (char *p = chars; *p ; p++)
956 if (*p == '_' && (p == chars || *(p-1) != '\\'))
964 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
966 * (((char*)s.c_str ()) + i + 1) = 'c';
967 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
974 Convert "NUM/DEN" into a '(NUM . DEN) cons.
977 scan_fraction (string frac)
979 ssize i = frac.find ('/');
980 string left = frac.substr (0, i);
981 string right = frac.substr (i + 1, (frac.length () - i + 1));
983 int n = String_convert::dec2int (left);
984 int d = String_convert::dec2int (right);
985 return scm_cons (scm_from_int (n), scm_from_int (d));
989 lookup_markup_command (string s)
991 SCM proc = ly_lily_module_constant ("lookup-markup-command");
992 return scm_call_1 (proc, ly_string2scm (s));
996 lookup_markup_list_command (string s)
998 SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
999 return scm_call_1 (proc, ly_string2scm (s));
1002 /* Shut up lexer warnings. */
1022 silence_lexer_warnings ()
1024 (void) yy_start_stack_ptr;
1025 (void) yy_start_stack_depth;
1026 (void) yy_start_stack;
1027 (void) yy_push_state;
1028 (void) yy_pop_state;
1029 (void) yy_top_state;
1030 (void) silence_lexer_warnings;