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 debug_output (_ ("Skipping UTF-8 BOM"));
198 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
200 yy_push_state (longcomment);
202 %[^{\n\r][^\n\r]*[\n\r] {
204 %[^{\n\r] { // backup rule
215 <INITIAL,notes,figures,chords,markup>{
221 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
222 yy_push_state (version);
224 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}* {
225 yy_push_state (sourcefilename);
227 <INITIAL,chords,lyrics,notes,figures>\\sourcefileline{WHITE}* {
228 yy_push_state (sourcefileline);
230 <version>\"[^"]*\" { /* got the version number */
231 string s (YYText () + 1);
232 s = s.substr (0, s.rfind ('\"'));
236 SCM top_scope = scm_car (scm_last_pair (scopes_));
237 scm_module_define (top_scope, ly_symbol2scm ("version-seen"), SCM_BOOL_T);
239 if (!is_valid_version (s))
244 <sourcefilename>\"[^"]*\" {
245 string s (YYText () + 1);
246 s = s.substr (0, s.rfind ('\"'));
249 this->here_input().get_source_file ()->name_ = s;
250 message (_f ("Renaming input to: `%s'", s.c_str ()));
251 progress_indication ("\n");
252 scm_module_define (scm_car (scopes_),
253 ly_symbol2scm ("input-file-name"),
258 <sourcefileline>{INT} {
260 sscanf (YYText (), "%d", &i);
263 this->here_input ().get_source_file ()->set_line (here_input ().start (), i);
267 LexerError (_ ("quoted string expected after \\version").c_str ());
271 LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
275 LexerError (_ ("integer expected after \\sourcefileline").c_str ());
288 LexerError (_ ("EOF found inside a comment").c_str ());
289 is_main_input_ = false; // should be safe , can't have \include in --safe.
290 if (! close_input ())
291 yyterminate (); // can't move this, since it actually rets a YY_NULL
296 <INITIAL,chords,lyrics,notes,figures>\\maininput {
300 is_main_input_ = true;
303 error (_ ("\\maininput not allowed outside init files"));
306 <INITIAL,chords,lyrics,figures,notes>\\include {
307 yy_push_state (incl);
309 <incl>\"[^"]*\" { /* got the include file name */
310 string s (YYText ()+1);
311 s = s.substr (0, s.rfind ('"'));
313 new_input (s, sources_);
316 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
317 string s = YYText () + 1;
318 strip_trailing_white (s);
319 if (s.length () && (s[s.length () - 1] == ';'))
320 s = s.substr (0, s.length () - 1);
322 SCM sid = lookup_identifier (s);
323 if (scm_is_string (sid)) {
324 new_input (ly_scm2string (sid), sources_);
327 string msg (_f ("wrong or undefined identifier: `%s'", s ));
329 LexerError (msg.c_str ());
330 SCM err = scm_current_error_port ();
331 scm_puts ("This value was found in the table: ", err);
332 scm_display (sid, err);
335 <incl>\"[^"]* { // backup rule
336 error (_ ("end quote missing"));
339 <chords,notes,figures>{RESTNAME} {
340 char const *s = YYText ();
341 yylval.scm = scm_from_locale_string (s);
344 <chords,notes,figures>R {
345 return MULTI_MEASURE_REST;
347 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
349 Input hi = here_input();
351 SCM sval = ly_parse_scm (hi.start (), &n, hi,
352 be_safe_global && is_main_input_, parser_);
354 if (sval == SCM_UNDEFINED)
356 sval = SCM_UNSPECIFIED;
360 for (int i = 0; i < n; i++)
364 char_count_stack_.back () += n;
366 if (unpack_identifier (sval) != SCM_UNDEFINED)
368 yylval.scm = unpack_identifier(sval);
369 return identifier_type (yylval.scm);
372 for (size_t i = 0; i < pending_string_includes_.size (); i++)
373 new_input ("<included string>", pending_string_includes_[i],
375 pending_string_includes_.clear ();
380 <INITIAL,notes,lyrics>{
382 return DOUBLE_ANGLE_OPEN;
385 return DOUBLE_ANGLE_CLOSE;
412 return scan_bare_word (YYText ());
416 return scan_escaped_word (YYText () + 1);
419 yylval.scm = scan_fraction (YYText ());
424 yylval.i = String_convert::dec2int (string (YYText ()));
428 yylval.i = String_convert::dec2int (string (YYText ()));
432 yylval.i = String_convert::dec2int (string (YYText () +1));
439 *yylval.string += to_string (escaped_char (YYText ()[1]));
442 *yylval.string += YYText ();
448 /* yylval is union. Must remember STRING before setting SCM*/
449 string *sp = yylval.string;
450 yylval.scm = ly_string2scm (*sp);
452 return is_lyric_state () ? LYRICS_STRING : STRING;
455 *yylval.string += YYText ();
461 start_lyric_quote ();
464 yylval.scm = scan_fraction (YYText ());
468 yylval.i = String_convert::dec2int (string (YYText ()));
472 return scan_escaped_word (YYText () + 1);
476 string s (YYText ());
478 return yylval.i = EXTENDER;
480 return yylval.i = HYPHEN;
483 char c = s[s.length () - 1];
484 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
485 here_input ().warning (
486 _ ("Brace found at end of lyric. Did you forget a space?"));
487 yylval.scm = ly_string2scm (s);
490 return LYRICS_STRING;
498 return scan_bare_word (YYText ());
501 return scan_escaped_word (YYText () + 1);
504 yylval.scm = scan_fraction (YYText ());
508 yylval.i = String_convert::dec2int (string (YYText ()));
537 string str (YYText () + 1);
539 int token_type = MARKUP_FUNCTION;
540 SCM s = lookup_markup_command (str);
542 // lookup-markup-command returns a pair with the car
543 // being the function to call, and the cdr being the
544 // call signature specified to define-markup-command,
545 // a list of predicates.
547 if (!scm_is_pair (s)) {
548 // If lookup-markup-command was not successful, we
549 // try lookup-markup-list-command instead.
550 // If this fails as well, we just scan and return
552 s = lookup_markup_list_command (str);
554 token_type = MARKUP_LIST_FUNCTION;
556 return scan_escaped_word (str);
559 // If the list of predicates is, say,
560 // (number? number? markup?), then tokens
561 // EXPECT_MARKUP EXPECT_SCM EXPECT_SCM EXPECT_NO_MORE_ARGS
562 // will be generated. Note that we have to push them
563 // in reverse order, so the first token pushed in the
564 // loop will be EXPECT_NO_MORE_ARGS.
566 yylval.scm = scm_car(s);
568 // yylval now contains the function to call as token
569 // value (for token type MARKUP_FUNCTION or
570 // MARKUP_LIST_FUNCTION).
572 push_extra_token(EXPECT_NO_MORE_ARGS);
574 for (; scm_is_pair(s); s = scm_cdr(s)) {
575 SCM predicate = scm_car(s);
577 if (predicate == ly_lily_module_constant ("markup-list?"))
578 push_extra_token(EXPECT_MARKUP_LIST);
579 else if (predicate == ly_lily_module_constant ("markup?"))
580 push_extra_token(EXPECT_MARKUP);
582 push_extra_token(EXPECT_SCM);
589 [^#{}"\\ \t\n\r\f]+ {
590 string s (YYText ());
592 char c = s[s.length () - 1];
593 /* brace open is for not confusing dumb tools. */
594 if (c == '{' || c == '}')
595 here_input ().warning (
596 _ ("Brace found at end of markup. Did you forget a space?"));
597 yylval.scm = ly_string2scm (s);
610 /* 2 = init.ly + current file.
611 > because we're before closing, but is_main_input_ should
614 is_main_input_ = include_stack_.size () > 2;
616 /* Returns YY_NULL */
619 else if (!close_input ())
620 /* Returns YY_NULL */
626 return scan_bare_word (YYText ());
629 return scan_escaped_word (YYText () + 1);
634 return scan_bare_word (YYText ());
637 return scan_escaped_word (YYText () + 1);
641 int cnv = sscanf (YYText (), "%lf", &r);
645 yylval.scm = scm_from_double (r);
650 yylval.i = String_convert::dec2int (string (YYText ()));
660 char c = YYText ()[0];
665 <INITIAL,notes,figures>. {
669 <INITIAL,lyrics,notes,figures>\\. {
670 char c = YYText ()[1];
674 return E_ANGLE_CLOSE;
678 return E_EXCLAMATION;
684 return E_BRACKET_OPEN;
688 return E_BRACKET_CLOSE;
700 string msg = _f ("invalid character: `%c'", YYText ()[0]);
701 LexerError (msg.c_str ());
707 /* Make the lexer generate a token of the given type as the next token.
708 TODO: make it possible to define a value for the token as well */
710 Lily_lexer::push_extra_token (int token_type)
712 if (extra_token_types_.empty ())
714 if (YY_START != extratoken)
715 hidden_state_ = YY_START;
716 yy_push_state (extratoken);
718 extra_token_types_.push_back (token_type);
722 Lily_lexer::push_chord_state (SCM tab)
724 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
725 yy_push_state (chords);
729 Lily_lexer::push_figuredbass_state ()
731 yy_push_state (figures);
735 Lily_lexer::push_initial_state ()
737 yy_push_state (INITIAL);
741 Lily_lexer::push_lyric_state ()
743 yy_push_state (lyrics);
747 Lily_lexer::push_markup_state ()
749 yy_push_state (markup);
753 Lily_lexer::push_note_state (SCM tab)
755 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
756 yy_push_state (notes);
760 Lily_lexer::pop_state ()
762 if (YYSTATE == notes || YYSTATE == chords)
763 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
769 Lily_lexer::identifier_type (SCM sid)
771 int k = try_special_identifiers (&yylval.scm , sid);
772 return k >= 0 ? k : SCM_IDENTIFIER;
777 Lily_lexer::scan_escaped_word (string str)
779 // use more SCM for this.
781 // SCM sym = ly_symbol2scm (str.c_str ());
783 int i = lookup_keyword (str);
784 if (i == MARKUP && is_lyric_state ())
789 SCM sid = lookup_identifier (str);
790 if (is_music_function (sid))
792 yylval.scm = get_music_function_transform (sid);
794 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
795 push_extra_token (EXPECT_NO_MORE_ARGS);
796 for (; scm_is_pair (s); s = scm_cdr (s))
798 SCM cs = scm_car (s);
800 if (cs == ly_music_p_proc)
801 push_extra_token (EXPECT_MUSIC);
802 else if (cs == Pitch_type_p_proc)
803 push_extra_token (EXPECT_PITCH);
804 else if (cs == Duration_type_p_proc)
805 push_extra_token (EXPECT_DURATION);
806 else if (cs == ly_lily_module_constant ("markup?"))
807 push_extra_token (EXPECT_MARKUP);
808 else if (ly_is_procedure (cs))
809 push_extra_token (EXPECT_SCM);
810 else programming_error ("Function parameter without type-checking predicate");
812 return MUSIC_FUNCTION;
815 if (sid != SCM_UNDEFINED)
818 return identifier_type (sid);
821 string msg (_f ("unknown escaped string: `\\%s'", str));
822 LexerError (msg.c_str ());
824 yylval.scm = ly_string2scm (str);
830 Lily_lexer::scan_bare_word (string str)
832 SCM sym = ly_symbol2scm (str.c_str ());
833 if ((YYSTATE == notes) || (YYSTATE == chords)) {
834 SCM handle = SCM_BOOL_F;
835 if (scm_is_pair (pitchname_tab_stack_))
836 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
838 if (scm_is_pair (handle)) {
839 yylval.scm = scm_cdr (handle);
840 if (unsmob_pitch (yylval.scm))
841 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
842 else if (scm_is_symbol (yylval.scm))
845 else if ((YYSTATE == chords)
846 && (handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
848 yylval.scm = scm_cdr (handle);
849 return CHORD_MODIFIER;
851 if ((chord_repetition_.repetition_symbol_ != SCM_EOL)
852 && to_boolean (scm_equal_p (chord_repetition_.repetition_symbol_, sym)))
853 return CHORD_REPETITION;
856 yylval.scm = ly_string2scm (str);
861 Lily_lexer::get_state () const
863 if (YY_START == extratoken)
864 return hidden_state_;
870 Lily_lexer::is_note_state () const
872 return get_state () == notes;
876 Lily_lexer::is_chord_state () const
878 return get_state () == chords;
882 Lily_lexer::is_lyric_state () const
884 return get_state () == lyrics;
888 Lily_lexer::is_figure_state () const
890 return get_state () == figures;
894 urg, belong to string (_convert)
895 and should be generalised
898 strip_leading_white (string&s)
901 for (; i < s.length (); i++)
909 strip_trailing_white (string&s)
911 ssize i = s.length ();
916 s = s.substr (0, i + 1);
921 Lilypond_version oldest_version ("2.7.38");
925 is_valid_version (string s)
927 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
928 Lilypond_version ver (s);
929 if (int (ver) < oldest_version)
931 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
932 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
938 non_fatal_error (_f ("program too old: %s (file requires: %s)", current.to_string (), ver.to_string ()));
949 lyric_fudge (string s)
951 char *chars = string_copy (s);
953 for (char *p = chars; *p ; p++)
955 if (*p == '_' && (p == chars || *(p-1) != '\\'))
963 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
965 * (((char*)s.c_str ()) + i + 1) = 'c';
966 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
973 Convert "NUM/DEN" into a '(NUM . DEN) cons.
976 scan_fraction (string frac)
978 ssize i = frac.find ('/');
979 string left = frac.substr (0, i);
980 string right = frac.substr (i + 1, (frac.length () - i + 1));
982 int n = String_convert::dec2int (left);
983 int d = String_convert::dec2int (right);
984 return scm_cons (scm_from_int (n), scm_from_int (d));
988 lookup_markup_command (string s)
990 SCM proc = ly_lily_module_constant ("lookup-markup-command");
991 return scm_call_1 (proc, ly_string2scm (s));
995 lookup_markup_list_command (string s)
997 SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
998 return scm_call_1 (proc, ly_string2scm (s));
1001 /* Shut up lexer warnings. */
1021 silence_lexer_warnings ()
1023 (void) yy_start_stack_ptr;
1024 (void) yy_start_stack_depth;
1025 (void) yy_start_stack;
1026 (void) yy_push_state;
1027 (void) yy_pop_state;
1028 (void) yy_top_state;
1029 (void) silence_lexer_warnings;