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
89 #define yylval (*lexval_)
91 #define yylloc (*lexloc_)
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
134 NATIONAL [\001-\006\021-\027\031\036]
135 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}|{SPECIAL_CHAR}
136 DASHED_WORD {A}({AN}|-)*
137 DASHED_KEY_WORD \\{DASHED_WORD}
146 REAL ({INT}\.{N}*)|(-?\.{N}+)
148 HORIZONTALWHITE [ \t]
152 MARKUPCOMMAND \\({A}|[-_])+
153 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
157 BOM_UTF8 \357\273\277
163 // swallow and ignore carriage returns
166 <extratoken>{ANY_CHAR} {
167 /* Generate a token without swallowing anything */
169 /* First unswallow the eaten character */
170 add_lexed_char (-YYLeng ());
173 /* produce requested token */
174 int type = scm_to_int (scm_caar (extra_tokens_));
175 yylval.scm = scm_cdar (extra_tokens_);
176 extra_tokens_ = scm_cdr (extra_tokens_);
177 if (scm_is_null (extra_tokens_))
183 <extratoken><<EOF>> {
184 /* Generate a token without swallowing anything */
186 /* produce requested token */
187 int type = scm_to_int (scm_caar (extra_tokens_));
188 yylval.scm = scm_cdar (extra_tokens_);
189 extra_tokens_ = scm_cdr (extra_tokens_);
190 if (scm_is_null (extra_tokens_))
196 /* Use the trailing context feature. Otherwise, the BOM will not be
197 found if the file starts with an identifier definition. */
198 <INITIAL,chords,lyrics,figures,notes>{BOM_UTF8}/.* {
199 if (this->lexloc_->line_number () != 1 || this->lexloc_->column_number () != 0)
201 LexerWarning (_ ("stray UTF-8 BOM encountered").c_str ());
204 debug_output (_ ("Skipping UTF-8 BOM"));
207 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
209 yy_push_state (longcomment);
211 %[^{\n\r][^\n\r]*[\n\r] {
213 %[^{\n\r] { // backup rule
224 <INITIAL,notes,figures,chords,markup>{
230 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
231 yy_push_state (version);
233 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}* {
234 yy_push_state (sourcefilename);
236 <INITIAL,chords,lyrics,notes,figures>\\sourcefileline{WHITE}* {
237 yy_push_state (sourcefileline);
239 <version>\"[^"]*\" { /* got the version number */
240 string s (YYText () + 1);
241 s = s.substr (0, s.rfind ('\"'));
245 SCM top_scope = scm_car (scm_last_pair (scopes_));
246 scm_module_define (top_scope, ly_symbol2scm ("version-seen"), SCM_BOOL_T);
248 if (!is_valid_version (s))
253 <sourcefilename>\"[^"]*\" {
254 string s (YYText () + 1);
255 s = s.substr (0, s.rfind ('\"'));
258 this->here_input().get_source_file ()->name_ = s;
259 message (_f ("Renaming input to: `%s'", s.c_str ()));
260 progress_indication ("\n");
261 scm_module_define (scm_car (scopes_),
262 ly_symbol2scm ("input-file-name"),
267 <sourcefileline>{INT} {
269 sscanf (YYText (), "%d", &i);
272 this->here_input ().get_source_file ()->set_line (here_input ().start (), i);
275 <version>{ANY_CHAR} {
276 LexerError (_ ("quoted string expected after \\version").c_str ());
279 <sourcefilename>{ANY_CHAR} {
280 LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
283 <sourcefileline>{ANY_CHAR} {
284 LexerError (_ ("integer expected after \\sourcefileline").c_str ());
299 <INITIAL,chords,lyrics,notes,figures>\\maininput {
303 is_main_input_ = true;
306 error (_ ("\\maininput not allowed outside init files"));
309 <INITIAL,chords,lyrics,figures,notes>\\include {
310 yy_push_state (incl);
312 <incl>\"[^""]*\" { /* got the include file name */
313 string s (YYText ()+1);
314 s = s.substr (0, s.rfind ('"'));
316 new_input (s, sources_);
319 <incl>\\{BLACK}*{WHITE}? { /* got the include identifier */
320 string s = YYText () + 1;
321 strip_trailing_white (s);
322 if (s.length () && (s[s.length () - 1] == ';'))
323 s = s.substr (0, s.length () - 1);
325 SCM sid = lookup_identifier (s);
326 if (scm_is_string (sid)) {
327 new_input (ly_scm2string (sid), sources_);
330 string msg (_f ("wrong or undefined identifier: `%s'", s ));
332 LexerError (msg.c_str ());
333 SCM err = scm_current_error_port ();
334 scm_puts ("This value was found in the table: ", err);
335 scm_display (sid, err);
338 <incl>(\$|#) { // scm for the filename
340 Input hi = here_input();
342 SCM sval = ly_parse_scm (hi.start (), &n, hi,
343 be_safe_global && is_main_input_, parser_);
344 sval = eval_scm (sval);
346 for (int i = 0; i < n; i++)
350 char_count_stack_.back () += n;
352 if (scm_is_string (sval)) {
353 new_input (ly_scm2string (sval), sources_);
356 LexerError (_ ("string expected after \\include").c_str ());
357 if (sval != SCM_UNDEFINED) {
358 SCM err = scm_current_error_port ();
359 scm_puts ("This value was found instead: ", err);
360 scm_display (sval, err);
365 <incl,version,sourcefilename>\"[^""]* { // backup rule
366 error (_ ("end quote missing"));
369 <chords,notes,figures>{RESTNAME} {
370 char const *s = YYText ();
371 yylval.scm = scm_from_locale_string (s);
374 <chords,notes,figures>R {
375 return MULTI_MEASURE_REST;
377 <INITIAL,chords,figures,lyrics,markup,notes># { //embedded scm
379 Input hi = here_input();
381 SCM sval = ly_parse_scm (hi.start (), &n, hi,
382 be_safe_global && is_main_input_, parser_);
384 if (sval == SCM_UNDEFINED)
387 for (int i = 0; i < n; i++)
391 char_count_stack_.back () += n;
397 <INITIAL,chords,figures,lyrics,markup,notes>\$ { //immediate scm
399 Input hi = here_input();
401 SCM sval = ly_parse_scm (hi.start (), &n, hi,
402 be_safe_global && is_main_input_, parser_);
404 for (int i = 0; i < n; i++)
408 char_count_stack_.back () += n;
410 sval = eval_scm (sval);
412 int token = scan_scm_id (sval);
413 if (!scm_is_eq (yylval.scm, SCM_UNSPECIFIED))
417 <INITIAL,notes,lyrics>{
419 return DOUBLE_ANGLE_OPEN;
422 return DOUBLE_ANGLE_CLOSE;
449 return scan_bare_word (YYText ());
453 return scan_escaped_word (YYText () + 1);
456 yylval.scm = scan_fraction (YYText ());
459 {UNSIGNED}/\/ | // backup rule
461 yylval.scm = scm_c_read_string (YYText ());
465 yylval.i = String_convert::dec2int (string (YYText () +1));
472 *yylval.string += to_string (escaped_char (YYText ()[1]));
475 *yylval.string += YYText ();
481 /* yylval is union. Must remember STRING before setting SCM*/
482 string *sp = yylval.string;
483 yylval.scm = ly_string2scm (*sp);
485 return is_lyric_state () ? LYRICS_STRING : STRING;
488 *yylval.string += YYText ();
494 start_lyric_quote ();
497 yylval.scm = scan_fraction (YYText ());
500 {UNSIGNED}/\/[^0-9] { // backup rule
501 yylval.scm = scm_c_read_string (YYText ());
504 {UNSIGNED}/\/ | // backup rule
506 yylval.scm = scm_c_read_string (YYText ());
510 return scan_escaped_word (YYText () + 1);
514 string s (YYText ());
516 return yylval.i = EXTENDER;
518 return yylval.i = HYPHEN;
521 char c = s[s.length () - 1];
522 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
523 here_input ().warning (
524 _ ("Brace found at end of lyric. Did you forget a space?"));
525 yylval.scm = ly_string2scm (s);
528 return LYRICS_STRING;
536 return scan_bare_word (YYText ());
539 return scan_escaped_word (YYText () + 1);
542 yylval.scm = scan_fraction (YYText ());
545 {UNSIGNED}/\/[^0-9] { // backup rule
546 yylval.scm = scm_c_read_string (YYText ());
549 {UNSIGNED}/\/ | // backup rule
551 yylval.scm = scm_c_read_string (YYText ());
580 string str (YYText () + 1);
582 int token_type = MARKUP_FUNCTION;
583 SCM s = lookup_markup_command (str);
585 // lookup-markup-command returns a pair with the car
586 // being the function to call, and the cdr being the
587 // call signature specified to define-markup-command,
588 // a list of predicates.
590 if (!scm_is_pair (s)) {
591 // If lookup-markup-command was not successful, we
592 // try lookup-markup-list-command instead.
593 // If this fails as well, we just scan and return
595 s = lookup_markup_list_command (str);
597 token_type = MARKUP_LIST_FUNCTION;
599 return scan_escaped_word (str);
602 // If the list of predicates is, say,
603 // (number? number? markup?), then tokens
604 // EXPECT_MARKUP EXPECT_SCM EXPECT_SCM EXPECT_NO_MORE_ARGS
605 // will be generated. Note that we have to push them
606 // in reverse order, so the first token pushed in the
607 // loop will be EXPECT_NO_MORE_ARGS.
609 yylval.scm = scm_car(s);
611 // yylval now contains the function to call as token
612 // value (for token type MARKUP_FUNCTION or
613 // MARKUP_LIST_FUNCTION).
615 push_extra_token(EXPECT_NO_MORE_ARGS);
617 for (; scm_is_pair(s); s = scm_cdr(s)) {
618 SCM predicate = scm_car(s);
620 if (predicate == ly_lily_module_constant ("markup-list?"))
621 push_extra_token(EXPECT_MARKUP_LIST);
622 else if (predicate == ly_lily_module_constant ("markup?"))
623 push_extra_token(EXPECT_MARKUP);
625 push_extra_token(EXPECT_SCM, predicate);
632 [^$#{}\"\\ \t\n\r\f]+ {
633 string s (YYText ());
635 char c = s[s.length () - 1];
636 /* brace open is for not confusing dumb tools. */
637 if (c == '{' || c == '}')
638 here_input ().warning (
639 _ ("Brace found at end of markup. Did you forget a space?"));
640 yylval.scm = ly_string2scm (s);
650 <longcomment><<EOF>> {
651 LexerError (_ ("EOF found inside a comment").c_str ());
652 is_main_input_ = false; // should be safe , can't have \include in --safe.
654 yyterminate (); // can't move this, since it actually rets a YY_NULL
657 <<EOF>> { if (is_main_input_)
659 /* 2 = init.ly + current file.
660 > because we're before closing, but is_main_input_ should
663 is_main_input_ = include_stack_.size () > 2;
665 /* Returns YY_NULL */
668 else if (!close_input ())
669 /* Returns YY_NULL */
675 return scan_bare_word (YYText ());
678 return scan_escaped_word (YYText () + 1);
682 -{UNSIGNED} | // backup rule
684 yylval.scm = scm_c_read_string (YYText ());
688 yylval.scm = scm_from_double (0.0);
693 yylval.scm = scm_c_read_string (YYText ());
703 char c = YYText ()[0];
708 <INITIAL,notes,figures>. {
712 <INITIAL,lyrics,notes,figures>\\. {
713 char c = YYText ()[1];
717 return E_ANGLE_CLOSE;
721 return E_EXCLAMATION;
727 return E_BRACKET_OPEN;
731 return E_BRACKET_CLOSE;
743 string msg = _f ("invalid character: `%c'", YYText ()[0]);
744 LexerError (msg.c_str ());
750 /* Make the lexer generate a token of the given type as the next token.
751 TODO: make it possible to define a value for the token as well */
753 Lily_lexer::push_extra_token (int token_type, SCM scm)
755 if (scm_is_null (extra_tokens_))
757 if (YY_START != extratoken)
758 hidden_state_ = YY_START;
759 yy_push_state (extratoken);
761 extra_tokens_ = scm_acons (scm_from_int (token_type), scm, extra_tokens_);
765 Lily_lexer::push_chord_state (SCM tab)
767 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
768 yy_push_state (chords);
772 Lily_lexer::push_figuredbass_state ()
774 yy_push_state (figures);
778 Lily_lexer::push_initial_state ()
780 yy_push_state (INITIAL);
784 Lily_lexer::push_lyric_state ()
786 yy_push_state (lyrics);
790 Lily_lexer::push_markup_state ()
792 yy_push_state (markup);
796 Lily_lexer::push_note_state (SCM tab)
798 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
799 yy_push_state (notes);
803 Lily_lexer::pop_state ()
805 if (YYSTATE == notes || YYSTATE == chords)
806 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
812 Lily_lexer::identifier_type (SCM sid)
814 int k = try_special_identifiers (&yylval.scm , sid);
815 return k >= 0 ? k : SCM_IDENTIFIER;
820 Lily_lexer::scan_escaped_word (string str)
822 // use more SCM for this.
824 // SCM sym = ly_symbol2scm (str.c_str ());
826 int i = lookup_keyword (str);
827 if (i == MARKUP && is_lyric_state ())
832 SCM sid = lookup_identifier (str);
833 if (sid != SCM_UNDEFINED)
834 return scan_scm_id (sid);
836 string msg (_f ("unknown escaped string: `\\%s'", str));
837 LexerError (msg.c_str ());
839 yylval.scm = ly_string2scm (str);
845 Lily_lexer::scan_scm_id (SCM sid)
847 if (is_music_function (sid))
849 int funtype = SCM_FUNCTION;
851 yylval.scm = get_music_function_transform (sid);
853 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
854 SCM cs = scm_car (s);
856 if (scm_is_pair (cs))
861 if (scm_is_eq (cs, ly_lily_module_constant ("ly:music?")))
862 funtype = MUSIC_FUNCTION;
863 else if (scm_is_eq (cs, ly_lily_module_constant ("ly:event?")))
864 funtype = EVENT_FUNCTION;
865 else if (ly_is_procedure (cs))
866 funtype = SCM_FUNCTION;
867 else programming_error ("Bad syntax function predicate");
869 push_extra_token (EXPECT_NO_MORE_ARGS);
870 for (s = scm_cdr (s); scm_is_pair (s); s = scm_cdr (s))
872 SCM optional = SCM_UNDEFINED;
875 if (scm_is_pair (cs))
877 optional = SCM_CDR (cs);
881 if (cs == Pitch_type_p_proc)
882 push_extra_token (EXPECT_PITCH);
883 else if (cs == Duration_type_p_proc)
884 push_extra_token (EXPECT_DURATION);
885 else if (ly_is_procedure (cs))
886 push_extra_token (EXPECT_SCM, cs);
889 programming_error ("Function parameter without type-checking predicate");
892 if (!scm_is_eq (optional, SCM_UNDEFINED))
893 push_extra_token (EXPECT_OPTIONAL, optional);
898 return identifier_type (sid);
902 Lily_lexer::scan_bare_word (string str)
904 SCM sym = ly_symbol2scm (str.c_str ());
905 if ((YYSTATE == notes) || (YYSTATE == chords)) {
906 SCM handle = SCM_BOOL_F;
907 if (scm_is_pair (pitchname_tab_stack_))
908 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
910 if (scm_is_pair (handle)) {
911 yylval.scm = scm_cdr (handle);
912 if (unsmob_pitch (yylval.scm))
913 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
914 else if (scm_is_symbol (yylval.scm))
917 else if ((YYSTATE == chords)
918 && (handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
920 yylval.scm = scm_cdr (handle);
921 return CHORD_MODIFIER;
923 if ((chord_repetition_.repetition_symbol_ != SCM_EOL)
924 && to_boolean (scm_equal_p (chord_repetition_.repetition_symbol_, sym)))
925 return CHORD_REPETITION;
928 yylval.scm = ly_string2scm (str);
933 Lily_lexer::get_state () const
935 if (YY_START == extratoken)
936 return hidden_state_;
942 Lily_lexer::is_note_state () const
944 return get_state () == notes;
948 Lily_lexer::is_chord_state () const
950 return get_state () == chords;
954 Lily_lexer::is_lyric_state () const
956 return get_state () == lyrics;
960 Lily_lexer::is_figure_state () const
962 return get_state () == figures;
966 Lily_lexer::eval_scm (SCM readerdata)
968 SCM sval = SCM_UNDEFINED;
970 if (!SCM_UNBNDP (readerdata))
972 sval = ly_eval_scm (scm_car (readerdata),
973 *unsmob_input (scm_cdr (readerdata)),
974 be_safe_global && is_main_input_,
978 if (SCM_UNBNDP (sval))
981 return SCM_UNSPECIFIED;
989 urg, belong to string (_convert)
990 and should be generalised
993 strip_leading_white (string&s)
996 for (; i < s.length (); i++)
1004 strip_trailing_white (string&s)
1006 ssize i = s.length ();
1008 if (!isspace (s[i]))
1011 s = s.substr (0, i + 1);
1016 Lilypond_version oldest_version ("2.7.38");
1020 is_valid_version (string s)
1022 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
1023 Lilypond_version ver (s);
1024 if (int (ver) < oldest_version)
1026 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
1027 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
1033 non_fatal_error (_f ("program too old: %s (file requires: %s)", current.to_string (), ver.to_string ()));
1044 lyric_fudge (string s)
1046 char *chars = string_copy (s);
1048 for (char *p = chars; *p ; p++)
1050 if (*p == '_' && (p == chars || *(p-1) != '\\'))
1058 if ((i = s.find ("\\,")) != NPOS) // change "\," to TeX's "\c "
1060 * (((char*)s.c_str ()) + i + 1) = 'c';
1061 s = s.substr (0, i + 2) + " " + s.substr (i - 2);
1068 Convert "NUM/DEN" into a '(NUM . DEN) cons.
1071 scan_fraction (string frac)
1073 ssize i = frac.find ('/');
1074 string left = frac.substr (0, i);
1075 string right = frac.substr (i + 1, (frac.length () - i + 1));
1077 int n = String_convert::dec2int (left);
1078 int d = String_convert::dec2int (right);
1079 return scm_cons (scm_from_int (n), scm_from_int (d));
1083 lookup_markup_command (string s)
1085 SCM proc = ly_lily_module_constant ("lookup-markup-command");
1086 return scm_call_1 (proc, ly_string2scm (s));
1090 lookup_markup_list_command (string s)
1092 SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
1093 return scm_call_1 (proc, ly_string2scm (s));
1096 /* Shut up lexer warnings. */
1116 silence_lexer_warnings ()
1118 (void) yy_start_stack_ptr;
1119 (void) yy_start_stack_depth;
1120 (void) yy_start_stack;
1121 (void) yy_push_state;
1122 (void) yy_pop_state;
1123 (void) yy_top_state;
1124 (void) silence_lexer_warnings;