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 "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"
62 #include "source-file.hh"
63 #include "std-string.hh"
64 #include "string-convert.hh"
71 #define isatty HORRIBLEKLUDGE
73 void strip_trailing_white (string&);
74 void strip_leading_white (string&);
75 string lyric_fudge (string s);
76 SCM lookup_markup_command (string s);
77 SCM lookup_markup_list_command (string s);
78 bool is_valid_version (string s);
81 #define start_quote() \
82 yy_push_state (quote);\
83 yylval.string = new string
85 #define start_lyric_quote() \
86 yy_push_state (lyric_quote);\
87 yylval.string = new string
95 #define YY_USER_ACTION add_lexed_char (YYLeng ());
98 SCM scan_fraction (string);
99 SCM (* scm_parse_error_handler) (void *);
109 %option yyclass="Lily_lexer"
111 %option never-interactive
136 NATIONAL [\001-\006\021-\027\031\036]
137 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}|{SPECIAL_CHAR}
138 DASHED_WORD {A}({AN}|-)*
139 DASHED_KEY_WORD \\{DASHED_WORD}
148 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 // swallow and ignore carriage returns
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 = scm_to_int (scm_caar (extra_tokens_));
177 yylval.scm = scm_cdar (extra_tokens_);
178 extra_tokens_ = scm_cdr (extra_tokens_);
179 if (scm_is_null (extra_tokens_))
185 <extratoken><<EOF>> {
186 /* Generate a token without swallowing anything */
188 /* produce requested token */
189 int type = scm_to_int (scm_caar (extra_tokens_));
190 yylval.scm = scm_cdar (extra_tokens_);
191 extra_tokens_ = scm_cdr (extra_tokens_);
192 if (scm_is_null (extra_tokens_))
198 /* Use the trailing context feature. Otherwise, the BOM will not be
199 found if the file starts with an identifier definition. */
200 <INITIAL,chords,lyrics,figures,notes>{BOM_UTF8}/.* {
201 if (this->lexloc_->line_number () != 1 || this->lexloc_->column_number () != 0)
203 LexerWarning (_ ("stray UTF-8 BOM encountered").c_str ());
206 debug_output (_ ("Skipping UTF-8 BOM"));
209 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
211 yy_push_state (longcomment);
213 %[^{\n\r][^\n\r]*[\n\r] {
215 %[^{\n\r] { // backup rule
226 <INITIAL,notes,figures,chords,markup>{
232 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
233 yy_push_state (version);
235 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}* {
236 yy_push_state (sourcefilename);
238 <INITIAL,chords,lyrics,notes,figures>\\sourcefileline{WHITE}* {
239 yy_push_state (sourcefileline);
241 <version>\"[^"]*\" { /* got the version number */
242 string s (YYText () + 1);
243 s = s.substr (0, s.rfind ('\"'));
247 SCM top_scope = scm_car (scm_last_pair (scopes_));
248 scm_module_define (top_scope, ly_symbol2scm ("version-seen"), SCM_BOOL_T);
250 if (!is_valid_version (s))
255 <sourcefilename>\"[^"]*\" {
256 string s (YYText () + 1);
257 s = s.substr (0, s.rfind ('\"'));
260 this->here_input().get_source_file ()->name_ = s;
261 message (_f ("Renaming input to: `%s'", s.c_str ()));
262 progress_indication ("\n");
263 scm_module_define (scm_car (scopes_),
264 ly_symbol2scm ("input-file-name"),
269 <sourcefileline>{INT} {
271 sscanf (YYText (), "%d", &i);
274 this->here_input ().get_source_file ()->set_line (here_input ().start (), i);
277 <version>{ANY_CHAR} {
278 LexerError (_ ("quoted string expected after \\version").c_str ());
281 <sourcefilename>{ANY_CHAR} {
282 LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
285 <sourcefileline>{ANY_CHAR} {
286 LexerError (_ ("integer expected after \\sourcefileline").c_str ());
301 <INITIAL,chords,lyrics,notes,figures>\\maininput {
305 is_main_input_ = true;
308 error (_ ("\\maininput not allowed outside init files"));
311 <INITIAL,chords,lyrics,figures,notes>\\include {
312 yy_push_state (incl);
314 <incl>\"[^""]*\" { /* got the include file name */
315 string s (YYText ()+1);
316 s = s.substr (0, s.rfind ('"'));
318 new_input (s, sources_);
321 <incl>\\{BLACK}*{WHITE}? { /* got the include identifier */
322 string s = YYText () + 1;
323 strip_trailing_white (s);
324 if (s.length () && (s[s.length () - 1] == ';'))
325 s = s.substr (0, s.length () - 1);
327 SCM sid = lookup_identifier (s);
328 if (scm_is_string (sid)) {
329 new_input (ly_scm2string (sid), sources_);
332 string msg (_f ("wrong or undefined identifier: `%s'", s ));
334 LexerError (msg.c_str ());
335 SCM err = scm_current_error_port ();
336 scm_puts ("This value was found in the table: ", err);
337 scm_display (sid, err);
340 <incl>(\$|#) { // scm for the filename
342 Input hi = here_input();
344 SCM sval = ly_parse_scm (hi.start (), &n, hi,
345 be_safe_global && is_main_input_, parser_);
346 sval = eval_scm (sval);
348 for (int i = 0; i < n; i++)
352 char_count_stack_.back () += n;
354 if (scm_is_string (sval)) {
355 new_input (ly_scm2string (sval), sources_);
358 LexerError (_ ("string expected after \\include").c_str ());
359 if (sval != SCM_UNDEFINED) {
360 SCM err = scm_current_error_port ();
361 scm_puts ("This value was found instead: ", err);
362 scm_display (sval, err);
367 <incl,version,sourcefilename>\"[^""]* { // backup rule
368 error (_ ("end quote missing"));
371 <chords,notes,figures>{RESTNAME} {
372 char const *s = YYText ();
373 yylval.scm = scm_from_locale_string (s);
376 <chords,notes,figures>R {
377 return MULTI_MEASURE_REST;
379 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
381 Input hi = here_input();
383 SCM sval = ly_parse_scm (hi.start (), &n, hi,
384 be_safe_global && is_main_input_, parser_);
386 if (sval == SCM_UNDEFINED)
389 for (int i = 0; i < n; i++)
393 char_count_stack_.back () += n;
395 for (size_t i = 0; i < pending_string_includes_.size (); i++)
396 new_input ("<included string>", pending_string_includes_[i],
398 pending_string_includes_.clear ();
404 <INITIAL,chords,figures,lyrics,markup,notes>\$ { //immediate scm
406 Input hi = here_input();
408 SCM sval = ly_parse_scm (hi.start (), &n, hi,
409 be_safe_global && is_main_input_, parser_);
410 sval = eval_scm (sval);
412 for (int i = 0; i < n; i++)
416 char_count_stack_.back () += n;
418 return scan_scm_id (sval);
421 <INITIAL,notes,lyrics>{
423 return DOUBLE_ANGLE_OPEN;
426 return DOUBLE_ANGLE_CLOSE;
453 return scan_bare_word (YYText ());
457 return scan_escaped_word (YYText () + 1);
460 yylval.scm = scan_fraction (YYText ());
463 {UNSIGNED}/\/ | // backup rule
465 yylval.scm = scm_c_read_string (YYText ());
469 yylval.i = String_convert::dec2int (string (YYText () +1));
476 *yylval.string += to_string (escaped_char (YYText ()[1]));
479 *yylval.string += YYText ();
485 /* yylval is union. Must remember STRING before setting SCM*/
486 string *sp = yylval.string;
487 yylval.scm = ly_string2scm (*sp);
489 return is_lyric_state () ? LYRICS_STRING : STRING;
492 *yylval.string += YYText ();
498 start_lyric_quote ();
501 yylval.scm = scan_fraction (YYText ());
504 {UNSIGNED}/\/[^0-9] { // backup rule
505 yylval.scm = scm_c_read_string (YYText ());
508 {UNSIGNED}/\/ | // backup rule
510 yylval.scm = scm_c_read_string (YYText ());
514 return scan_escaped_word (YYText () + 1);
518 string s (YYText ());
520 return yylval.i = EXTENDER;
522 return yylval.i = HYPHEN;
525 char c = s[s.length () - 1];
526 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
527 here_input ().warning (
528 _ ("Brace found at end of lyric. Did you forget a space?"));
529 yylval.scm = ly_string2scm (s);
532 return LYRICS_STRING;
540 return scan_bare_word (YYText ());
543 return scan_escaped_word (YYText () + 1);
546 yylval.scm = scan_fraction (YYText ());
549 {UNSIGNED}/\/[^0-9] { // backup rule
550 yylval.scm = scm_c_read_string (YYText ());
553 {UNSIGNED}/\/ | // backup rule
555 yylval.scm = scm_c_read_string (YYText ());
584 string str (YYText () + 1);
586 int token_type = MARKUP_FUNCTION;
587 SCM s = lookup_markup_command (str);
589 // lookup-markup-command returns a pair with the car
590 // being the function to call, and the cdr being the
591 // call signature specified to define-markup-command,
592 // a list of predicates.
594 if (!scm_is_pair (s)) {
595 // If lookup-markup-command was not successful, we
596 // try lookup-markup-list-command instead.
597 // If this fails as well, we just scan and return
599 s = lookup_markup_list_command (str);
601 token_type = MARKUP_LIST_FUNCTION;
603 return scan_escaped_word (str);
606 // If the list of predicates is, say,
607 // (number? number? markup?), then tokens
608 // EXPECT_MARKUP EXPECT_SCM EXPECT_SCM EXPECT_NO_MORE_ARGS
609 // will be generated. Note that we have to push them
610 // in reverse order, so the first token pushed in the
611 // loop will be EXPECT_NO_MORE_ARGS.
613 yylval.scm = scm_car(s);
615 // yylval now contains the function to call as token
616 // value (for token type MARKUP_FUNCTION or
617 // MARKUP_LIST_FUNCTION).
619 push_extra_token(EXPECT_NO_MORE_ARGS);
621 for (; scm_is_pair(s); s = scm_cdr(s)) {
622 SCM predicate = scm_car(s);
624 if (predicate == ly_lily_module_constant ("markup-list?"))
625 push_extra_token(EXPECT_MARKUP_LIST);
626 else if (predicate == ly_lily_module_constant ("markup?"))
627 push_extra_token(EXPECT_MARKUP);
629 push_extra_token(EXPECT_SCM, predicate);
636 [^$#{}\"\\ \t\n\r\f]+ {
637 string s (YYText ());
639 char c = s[s.length () - 1];
640 /* brace open is for not confusing dumb tools. */
641 if (c == '{' || c == '}')
642 here_input ().warning (
643 _ ("Brace found at end of markup. Did you forget a space?"));
644 yylval.scm = ly_string2scm (s);
654 <longcomment><<EOF>> {
655 LexerError (_ ("EOF found inside a comment").c_str ());
656 is_main_input_ = false; // should be safe , can't have \include in --safe.
658 yyterminate (); // can't move this, since it actually rets a YY_NULL
661 <<EOF>> { if (is_main_input_)
663 /* 2 = init.ly + current file.
664 > because we're before closing, but is_main_input_ should
667 is_main_input_ = include_stack_.size () > 2;
669 /* Returns YY_NULL */
672 else if (!close_input ())
673 /* Returns YY_NULL */
679 return scan_bare_word (YYText ());
682 return scan_escaped_word (YYText () + 1);
686 -{UNSIGNED} | // backup rule
688 yylval.scm = scm_c_read_string (YYText ());
692 yylval.scm = scm_from_double (0.0);
697 yylval.scm = scm_c_read_string (YYText ());
707 char c = YYText ()[0];
712 <INITIAL,notes,figures>. {
716 <INITIAL,lyrics,notes,figures>\\. {
717 char c = YYText ()[1];
721 return E_ANGLE_CLOSE;
725 return E_EXCLAMATION;
731 return E_BRACKET_OPEN;
735 return E_BRACKET_CLOSE;
747 string msg = _f ("invalid character: `%c'", YYText ()[0]);
748 LexerError (msg.c_str ());
754 /* Make the lexer generate a token of the given type as the next token.
755 TODO: make it possible to define a value for the token as well */
757 Lily_lexer::push_extra_token (int token_type, SCM scm)
759 if (scm_is_null (extra_tokens_))
761 if (YY_START != extratoken)
762 hidden_state_ = YY_START;
763 yy_push_state (extratoken);
765 extra_tokens_ = scm_acons (scm_from_int (token_type), scm, extra_tokens_);
769 Lily_lexer::push_chord_state (SCM tab)
771 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
772 yy_push_state (chords);
776 Lily_lexer::push_figuredbass_state ()
778 yy_push_state (figures);
782 Lily_lexer::push_initial_state ()
784 yy_push_state (INITIAL);
788 Lily_lexer::push_lyric_state ()
790 yy_push_state (lyrics);
794 Lily_lexer::push_markup_state ()
796 yy_push_state (markup);
800 Lily_lexer::push_note_state (SCM tab)
802 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
803 yy_push_state (notes);
807 Lily_lexer::pop_state ()
809 if (YYSTATE == notes || YYSTATE == chords)
810 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
816 Lily_lexer::identifier_type (SCM sid)
818 int k = try_special_identifiers (&yylval.scm , sid);
819 return k >= 0 ? k : SCM_IDENTIFIER;
824 Lily_lexer::scan_escaped_word (string str)
826 // use more SCM for this.
828 // SCM sym = ly_symbol2scm (str.c_str ());
830 int i = lookup_keyword (str);
831 if (i == MARKUP && is_lyric_state ())
836 SCM sid = lookup_identifier (str);
837 if (sid != SCM_UNDEFINED)
838 return scan_scm_id (sid);
840 string msg (_f ("unknown escaped string: `\\%s'", str));
841 LexerError (msg.c_str ());
843 yylval.scm = ly_string2scm (str);
849 Lily_lexer::scan_scm_id (SCM sid)
851 if (is_music_function (sid))
853 int funtype = SCM_FUNCTION;
855 yylval.scm = get_music_function_transform (sid);
857 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
858 SCM cs = scm_car (s);
860 if (scm_is_pair (cs))
865 if (scm_is_eq (cs, ly_lily_module_constant ("ly:music?")))
866 funtype = MUSIC_FUNCTION;
867 else if (scm_is_eq (cs, ly_lily_module_constant ("ly:event?")))
868 funtype = EVENT_FUNCTION;
869 else if (ly_is_procedure (cs))
870 funtype = SCM_FUNCTION;
871 else programming_error ("Bad syntax function predicate");
873 push_extra_token (EXPECT_NO_MORE_ARGS);
874 for (s = scm_cdr (s); scm_is_pair (s); s = scm_cdr (s))
876 SCM optional = SCM_UNDEFINED;
879 if (scm_is_pair (cs))
881 optional = SCM_CDR (cs);
885 if (cs == Pitch_type_p_proc)
886 push_extra_token (EXPECT_PITCH);
887 else if (cs == Duration_type_p_proc)
888 push_extra_token (EXPECT_DURATION);
889 else if (ly_is_procedure (cs))
890 push_extra_token (EXPECT_SCM, cs);
893 programming_error ("Function parameter without type-checking predicate");
896 if (!scm_is_eq (optional, SCM_UNDEFINED))
897 push_extra_token (EXPECT_OPTIONAL, optional);
902 return identifier_type (sid);
906 Lily_lexer::scan_bare_word (string str)
908 SCM sym = ly_symbol2scm (str.c_str ());
909 if ((YYSTATE == notes) || (YYSTATE == chords)) {
910 SCM handle = SCM_BOOL_F;
911 if (scm_is_pair (pitchname_tab_stack_))
912 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
914 if (scm_is_pair (handle)) {
915 yylval.scm = scm_cdr (handle);
916 if (unsmob_pitch (yylval.scm))
917 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
918 else if (scm_is_symbol (yylval.scm))
921 else if ((YYSTATE == chords)
922 && (handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
924 yylval.scm = scm_cdr (handle);
925 return CHORD_MODIFIER;
927 if ((chord_repetition_.repetition_symbol_ != SCM_EOL)
928 && to_boolean (scm_equal_p (chord_repetition_.repetition_symbol_, sym)))
929 return CHORD_REPETITION;
932 yylval.scm = ly_string2scm (str);
937 Lily_lexer::get_state () const
939 if (YY_START == extratoken)
940 return hidden_state_;
946 Lily_lexer::is_note_state () const
948 return get_state () == notes;
952 Lily_lexer::is_chord_state () const
954 return get_state () == chords;
958 Lily_lexer::is_lyric_state () const
960 return get_state () == lyrics;
964 Lily_lexer::is_figure_state () const
966 return get_state () == figures;
970 Lily_lexer::eval_scm (SCM readerdata)
972 SCM sval = SCM_UNDEFINED;
974 if (!SCM_UNBNDP (readerdata))
976 sval = ly_eval_scm (scm_car (readerdata),
977 *unsmob_input (scm_cdr (readerdata)),
978 be_safe_global && is_main_input_,
982 if (SCM_UNBNDP (sval))
985 return SCM_UNSPECIFIED;
993 urg, belong to string (_convert)
994 and should be generalised
997 strip_leading_white (string&s)
1000 for (; i < s.length (); i++)
1001 if (!isspace (s[i]))
1008 strip_trailing_white (string&s)
1010 ssize i = s.length ();
1012 if (!isspace (s[i]))
1015 s = s.substr (0, i + 1);
1020 Lilypond_version oldest_version ("2.7.38");
1024 is_valid_version (string s)
1026 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
1027 Lilypond_version ver (s);
1028 if (int (ver) < oldest_version)
1030 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
1031 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
1037 non_fatal_error (_f ("program too old: %s (file requires: %s)", current.to_string (), ver.to_string ()));
1048 lyric_fudge (string s)
1050 char *chars = string_copy (s);
1052 for (char *p = chars; *p ; p++)
1054 if (*p == '_' && (p == chars || *(p-1) != '\\'))
1062 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
1064 * (((char*)s.c_str ()) + i + 1) = 'c';
1065 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
1072 Convert "NUM/DEN" into a '(NUM . DEN) cons.
1075 scan_fraction (string frac)
1077 ssize i = frac.find ('/');
1078 string left = frac.substr (0, i);
1079 string right = frac.substr (i + 1, (frac.length () - i + 1));
1081 int n = String_convert::dec2int (left);
1082 int d = String_convert::dec2int (right);
1083 return scm_cons (scm_from_int (n), scm_from_int (d));
1087 lookup_markup_command (string s)
1089 SCM proc = ly_lily_module_constant ("lookup-markup-command");
1090 return scm_call_1 (proc, ly_string2scm (s));
1094 lookup_markup_list_command (string s)
1096 SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
1097 return scm_call_1 (proc, ly_string2scm (s));
1100 /* Shut up lexer warnings. */
1120 silence_lexer_warnings ()
1122 (void) yy_start_stack_ptr;
1123 (void) yy_start_stack_depth;
1124 (void) yy_start_stack;
1125 (void) yy_push_state;
1126 (void) yy_pop_state;
1127 (void) yy_top_state;
1128 (void) silence_lexer_warnings;