1 %{ // -*-Fundamental-*-
3 lexer.ll -- implement the Flex lexer
5 source file of the LilyPond music typesetter
7 (c) 1996--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
15 after making a change to the lexer rules, run
16 flex -b <this lexer file>
19 contains no backup states, but only the reminder
20 Compressed tables always back up.
21 (don-t forget to rm lex.yy.cc :-)
29 /* Flex >= 2.5.29 fix; FlexLexer.h's multiple include bracing breaks
30 when building the actual lexer. */
38 #include "music-function.hh"
39 #include "source-file.hh"
40 #include "parse-scm.hh"
41 #include "lily-guile.hh"
43 #include "string-convert.hh"
44 #include "lily-lexer.hh"
45 #include "interval.hh"
46 #include "lily-guile.hh"
51 #include "lilypond-input-version.hh"
52 #include "context-def.hh"
53 #include "identifier-smob.hh"
58 #define isatty HORRIBLEKLUDGE
60 void strip_trailing_white (String&);
61 void strip_leading_white (String&);
62 String lyric_fudge (String s);
63 int music_function_type (SCM);
64 SCM lookup_markup_command (String s);
65 bool is_valid_version (String s);
68 #define start_quote() \
69 yy_push_state (quote);\
70 yylval.string = new String
72 #define start_lyric_quote() \
73 yy_push_state (lyric_quote);\
74 yylval.string = new String
79 #define YY_USER_ACTION add_lexed_char (YYLeng ());
82 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
87 SCM scan_fraction (String);
88 SCM (* scm_parse_error_handler) (void *);
98 %option yyclass="Lily_lexer"
100 %option never-interactive
121 NATIONAL [\001-\006\021-\027\031\036\200-\377]
122 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
130 REAL ({INT}\.{N}*)|(-?\.{N}+)
133 HORIZONTALWHITE [ \t]
137 MARKUPCOMMAND \\({A}|[-_])+
138 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
146 // windows-suck-suck-suck
149 <INITIAL,chords,incl,markup,lyrics,notes,figures>{
151 yy_push_state (longcomment);
155 %[^{\n\r] { // backup rule
166 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
167 yy_push_state (version);
169 <INITIAL,chords,lyrics,notes,figures>\\renameinput{WHITE}* {
170 yy_push_state (renameinput);
172 <version>\"[^"]*\" { /* got the version number */
173 String s (YYText () + 1);
174 s = s.left_string (s.index_last ('\"'));
177 if (!is_valid_version (s))
180 SCM top_scope = scm_car (scm_last_pair (scopes_));
181 scm_module_define (top_scope, ly_symbol2scm ("version-seen?"), SCM_BOOL_T);
184 <renameinput>\"[^"]*\" {
185 String s (YYText () + 1);
186 s = s.left_string (s.index_last ('\"'));
189 this->here_input().source_file_->name_ = s;
190 progress_indication (_f ("Renaming input to: `%s'", s.to_str0 ()));
191 progress_indication ("\n");
192 scm_module_define (scm_car (scopes_),
193 ly_symbol2scm ("input-file-name"),
194 scm_makfrom0str (s.to_str0 ()));
198 LexerError (_ ("No quoted string found after \\version").to_str0 ());
202 LexerError (_ ("No quoted string found after \\renameinput").to_str0 ());
215 LexerError (_ ("EOF found inside a comment").to_str0 ());
216 main_input_b_ = false;
217 if (! close_input ())
218 yyterminate (); // can't move this, since it actually rets a YY_NULL
223 <INITIAL,chords,lyrics,notes,figures>\\maininput {
227 main_input_b_ = true;
230 error (_ ("\\maininput not allowed outside init files"));
233 <INITIAL,chords,lyrics,figures,notes>\\include {
234 yy_push_state (incl);
236 <incl>\"[^"]*\" { /* got the include file name */
237 String s (YYText ()+1);
238 s = s.left_string (s.index_last ('"'));
240 new_input (s, sources_);
243 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
244 String s = YYText () + 1;
245 strip_trailing_white (s);
246 if (s.length () && (s[s.length () - 1] == ';'))
247 s = s.left_string (s.length () - 1);
249 SCM sid = lookup_identifier (s);
250 if (scm_is_string (sid)) {
251 new_input (ly_scm2string (sid), sources_);
254 String msg (_f ("wrong or undefined identifier: `%s'", s ));
256 LexerError (msg.to_str0 ());
257 SCM err = scm_current_error_port ();
258 scm_puts ("This value was found in the table: ", err);
259 scm_display (sid, err);
262 <incl>\"[^"]* { // backup rule
263 error (_ ("Missing end quote"));
266 <chords,notes,figures>{RESTNAME} {
267 const char *s = YYText ();
268 yylval.scm = scm_makfrom0str (s);
271 <chords,notes,figures>R {
272 return MULTI_MEASURE_REST;
274 <INITIAL,markup,chords,lyrics,notes,figures># { //embedded scm
275 //char const* s = YYText () + 1;
276 char const* s = here_str0 ();
278 SCM sval = ly_parse_scm (s, &n, here_input (),
279 safe_global_b && main_input_b_);
281 if (sval == SCM_UNDEFINED)
283 sval = SCM_UNSPECIFIED;
287 for (int i = 0; i < n; i++)
291 char_count_stack_.top () += n;
293 if (unpack_identifier (sval) != SCM_UNDEFINED)
295 yylval.scm = unpack_identifier(sval);
296 return identifier_type (yylval.scm);
302 <INITIAL,notes,lyrics>{
304 return DOUBLE_ANGLE_OPEN;
307 return DOUBLE_ANGLE_CLOSE;
324 return scan_bare_word (YYText ());
328 return scan_escaped_word (YYText () + 1);
331 yylval.scm = scan_fraction (YYText ());
336 yylval.i = String_convert::dec2int (String (YYText ()));
340 yylval.i = String_convert::dec2int (String (YYText ()));
344 yylval.i = String_convert::dec2int (String (YYText () +1));
357 *yylval.string += to_string (escaped_char (YYText ()[1]));
360 *yylval.string += YYText ();
366 /* yylval is union. Must remember STRING before setting SCM*/
367 String *sp = yylval.string;
368 yylval.scm = scm_makfrom0str (sp->to_str0 ());
373 *yylval.string += YYText ();
378 *yylval.string += to_string (escaped_char (YYText ()[1]));
381 *yylval.string += YYText ();
387 /* yylval is union. Must remember STRING before setting SCM*/
388 String *sp = yylval.string;
389 yylval.scm = scm_makfrom0str (sp->to_str0 ());
391 return LYRICS_STRING;
394 *yylval.string += YYText ();
400 start_lyric_quote ();
403 yylval.scm = scan_fraction (YYText ());
407 yylval.i = String_convert::dec2int (String (YYText ()));
411 return scan_escaped_word (YYText () + 1);
415 String s (YYText ());
417 return yylval.i = EXTENDER;
419 return yylval.i = HYPHEN;
422 char c = s[s.length () - 1];
423 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
424 here_input ().warning (
425 _ ("Brace found at end of lyric. Did you forget a space?"));
426 yylval.scm = scm_makfrom0str (s.to_str0 ());
429 return LYRICS_STRING;
437 return scan_bare_word (YYText ());
440 return scan_escaped_word (YYText () + 1);
443 yylval.scm = scan_fraction (YYText ());
447 yylval.i = String_convert::dec2int (String (YYText ()));
482 String str (YYText () + 1);
483 SCM s = lookup_markup_command (str);
485 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
486 yylval.scm = scm_car(s);
487 SCM tag = scm_cdr(s);
488 if (tag == ly_symbol2scm("markup0"))
489 return MARKUP_HEAD_MARKUP0;
490 if (tag == ly_symbol2scm("empty"))
491 return MARKUP_HEAD_EMPTY;
492 else if (tag == ly_symbol2scm ("markup0-markup1"))
493 return MARKUP_HEAD_MARKUP0_MARKUP1;
494 else if (tag == ly_symbol2scm ("markup-list0"))
495 return MARKUP_HEAD_LIST0;
496 else if (tag == ly_symbol2scm ("scheme0"))
497 return MARKUP_HEAD_SCM0;
498 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
499 return MARKUP_HEAD_SCM0_SCM1;
500 else if (tag == ly_symbol2scm ("scheme0-markup1"))
501 return MARKUP_HEAD_SCM0_MARKUP1;
502 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
503 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
504 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
505 return MARKUP_HEAD_SCM0_SCM1_SCM2;
507 programming_error ("No parser tag defined for this signature. Abort");
512 return scan_escaped_word (str);
517 [^#{}"\\ \t\n\r\f]+ {
518 String s (YYText ());
520 char c = s[s.length () - 1];
521 /* brace open is for not confusing dumb tools. */
522 if (c == '{' || c == '}')
523 here_input ().warning (
524 _ ("Brace found at end of markup. Did you forget a space?"));
525 yylval.scm = scm_makfrom0str (s.to_str0 ());
538 main_input_b_ = false;
540 /* Returns YY_NULL */
543 else if (!close_input ())
544 /* Returns YY_NULL */
550 return scan_bare_word (YYText ());
553 return scan_escaped_word (YYText () + 1);
557 int cnv = sscanf (YYText (), "%lf", &r);
560 yylval.scm = scm_make_real (r);
565 yylval.i = String_convert::dec2int (String (YYText ()));
575 char c = YYText ()[0];
580 <INITIAL,notes,figures>. {
584 <INITIAL,lyrics,notes,figures>\\. {
585 char c = YYText ()[1];
593 return E_EXCLAMATION;
601 return E_RIGHTSQUARE;
613 String msg = _f ("invalid character: `%c'", YYText ()[0]);
614 LexerError (msg.to_str0 ());
621 Lily_lexer::push_chord_state (SCM tab)
623 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
624 yy_push_state (chords);
628 Lily_lexer::push_figuredbass_state ()
630 yy_push_state (figures);
634 Lily_lexer::push_initial_state ()
636 yy_push_state (INITIAL);
640 Lily_lexer::push_lyric_state ()
642 yy_push_state (lyrics);
646 Lily_lexer::push_markup_state ()
648 yy_push_state (markup);
652 Lily_lexer::push_note_state (SCM tab)
654 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
655 yy_push_state (notes);
659 Lily_lexer::pop_state ()
661 if (YYSTATE == notes || YYSTATE == chords)
662 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
667 Lily_lexer::identifier_type (SCM sid)
669 int k = try_special_identifiers (&yylval.scm , sid);
670 return k >= 0 ? k : SCM_IDENTIFIER;
675 Lily_lexer::scan_escaped_word (String str)
677 // use more SCM for this.
679 // SCM sym = ly_symbol2scm (str.to_str0 ());
681 int l = lookup_keyword (str);
685 SCM sid = lookup_identifier (str);
686 if (is_music_function (sid))
688 yylval.scm = get_music_function_transform (sid);
690 return music_function_type (yylval.scm);
693 if (sid != SCM_UNDEFINED)
696 return identifier_type (sid);
699 String msg (_f ("unknown escaped string: `\\%s'", str));
700 LexerError (msg.to_str0 ());
702 yylval.scm = scm_makfrom0str (str.to_str0 ());
708 Lily_lexer::scan_bare_word (String str)
710 SCM sym = ly_symbol2scm (str.to_str0 ());
711 if ((YYSTATE == notes) || (YYSTATE == chords)) {
712 SCM handle = SCM_BOOL_F;
713 if (scm_is_pair (pitchname_tab_stack_))
714 handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
716 if (scm_is_pair (handle)) {
717 yylval.scm = scm_cdr (handle);
718 if (unsmob_pitch (yylval.scm))
719 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
720 else if (scm_is_symbol (yylval.scm))
723 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
725 yylval.scm = scm_cdr (handle);
726 return CHORD_MODIFIER;
730 yylval.scm = scm_makfrom0str (str.to_str0 ());
735 Lily_lexer::is_note_state () const
737 return YY_START == notes;
741 Lily_lexer::is_chord_state () const
743 return YY_START == chords;
747 Lily_lexer::is_lyric_state () const
749 return YY_START == lyrics;
753 Lily_lexer::is_figure_state () const
755 return YY_START == figures;
759 urg, belong to String (_convert)
760 and should be generalised
763 strip_leading_white (String&s)
766 for (; i < s.length (); i++)
770 s = s.nomid_string (0, i);
774 strip_trailing_white (String&s)
781 s = s.left_string (i+1);
786 /* 2.1.2x something -> \property -> \set. */
787 Lilypond_version oldest_version ("2.3.22");
791 is_valid_version (String s)
793 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
794 Lilypond_version ver (s);
795 if (! ((ver >= oldest_version) && (ver <= current)))
797 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
798 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
809 lyric_fudge (String s)
811 char * chars = s.get_copy_str0 ();
813 for (char * p = chars; *p ; p++)
815 if (*p == '_' && (p == chars || *(p-1) != '\\'))
823 if ((i = s.index ("\\,")) != -1) // change "\," to TeX's "\c "
825 * (s.get_str0 () + i + 1) = 'c';
826 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
833 Convert "NUM/DEN" into a '(NUM . DEN) cons.
836 scan_fraction (String frac)
838 int i = frac.index ('/');
839 int l = frac.length ();
840 String left = frac.left_string (i);
841 String right = frac.right_string (l - i - 1);
843 int n = String_convert::dec2int (left);
844 int d = String_convert::dec2int (right);
845 return scm_cons (scm_int2num (n), scm_int2num (d));
848 // Breaks for flex 2.5.31
850 /* avoid silly flex induced gcc warnings */
851 static void yy_push_state (int) {;}
852 static void yy_pop_state () {;}
853 static int yy_top_state () { return 0; }
856 avoid_silly_flex_induced_gcc_warnings ()
858 (void)yy_start_stack_ptr;
859 (void)yy_start_stack_depth;
860 (void)yy_start_stack;
864 avoid_silly_flex_induced_gcc_warnings ();
869 lookup_markup_command (String s)
871 SCM proc = ly_lily_module_constant ("lookup-markup-command");
872 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));
877 music_function_type (SCM func)
879 SCM type = scm_object_property (func, ly_symbol2scm ("music-function-signature-keyword"));
880 if (type == ly_symbol2scm ("scm"))
882 return MUSIC_FUNCTION_SCM;
884 else if (type == ly_symbol2scm ("music"))
886 return MUSIC_FUNCTION_MUSIC;
888 else if (type == ly_symbol2scm ("scm-music"))
890 return MUSIC_FUNCTION_SCM_MUSIC;
892 else if (type == ly_symbol2scm ("scm-scm"))
894 return MUSIC_FUNCTION_SCM_SCM;
896 else if (type == ly_symbol2scm ("music-music"))
898 return MUSIC_FUNCTION_MUSIC_MUSIC;
900 else if (type == ly_symbol2scm ("scm-music-music"))
902 return MUSIC_FUNCTION_SCM_MUSIC_MUSIC;
904 else if (type == ly_symbol2scm ("scm-scm-music"))
906 return MUSIC_FUNCTION_SCM_SCM_MUSIC;
908 else if (type == ly_symbol2scm ("markup"))
910 return MUSIC_FUNCTION_MARKUP;
912 else if (type == ly_symbol2scm ("markup-music"))
914 return MUSIC_FUNCTION_MARKUP_MUSIC;
916 else if (type == ly_symbol2scm ("markup-markup"))
918 return MUSIC_FUNCTION_MARKUP_MARKUP;
920 else if (type == ly_symbol2scm ("markup-music-music"))
922 return MUSIC_FUNCTION_MARKUP_MUSIC_MUSIC;
924 else if (type == ly_symbol2scm ("markup-markup-music"))
926 return MUSIC_FUNCTION_MARKUP_MARKUP_MUSIC;
928 else if (type == ly_symbol2scm ("noarg"))
930 return MUSIC_FUNCTION;
934 /* TODO: print location */
935 error ("Can not find signature for music function.");
938 return MUSIC_FUNCTION_SCM;