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. */
35 #include "music-function.hh"
36 #include "source-file.hh"
37 #include "parse-scm.hh"
38 #include "lily-guile.hh"
40 #include "string-convert.hh"
41 #include "my-lily-lexer.hh"
42 #include "interval.hh"
43 #include "lily-guile.hh"
48 #include "lilypond-input-version.hh"
49 #include "context-def.hh"
50 #include "identifier-smob.hh"
55 #define isatty HORRIBLEKLUDGE
57 void strip_trailing_white (String&);
58 void strip_leading_white (String&);
59 String lyric_fudge (String s);
60 int music_function_type (SCM);
61 SCM lookup_markup_command (String s);
62 bool is_valid_version (String s);
65 #define start_quote() \
66 yy_push_state (quote);\
67 yylval.string = new String
69 #define start_lyric_quote() \
70 yy_push_state (lyric_quote);\
71 yylval.string = new String
76 #define YY_USER_ACTION add_lexed_char (YYLeng ());
79 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
84 SCM scan_fraction (String);
85 SCM (* scm_parse_error_handler) (void *);
95 %option yyclass="My_lily_lexer"
97 %option never-interactive
119 NATIONAL [\001-\006\021-\027\031\036\200-\377]
120 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
128 REAL ({INT}\.{N}*)|(-?\.{N}+)
131 HORIZONTALWHITE [ \t]
135 MARKUPCOMMAND \\({A}|[-_])+
136 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
144 // windows-suck-suck-suck
147 <INITIAL,chords,incl,markup,lyrics,notes,figures>{
149 yy_push_state (longcomment);
153 %[^{\n\r] { // backup rule
164 <INITIAL,notes>\\encoding{WHITE}* {
165 yy_push_state (encoding);
167 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
168 yy_push_state (version);
170 <INITIAL,chords,lyrics,notes,figures>\\renameinput{WHITE}* {
171 yy_push_state (renameinput);
173 <encoding>\"[^"]*\" {
174 String s (YYText () + 1);
175 s = s.left_string (s.index_last ('\"'));
179 <version>\"[^"]*\" { /* got the version number */
180 String s (YYText () + 1);
181 s = s.left_string (s.index_last ('\"'));
184 if (!is_valid_version (s))
187 <renameinput>\"[^"]*\" {
188 String s (YYText ()+1);
189 s = s.left_string (s.index_last ('\"'));
192 this->here_input().source_file_->name_ = s;
193 progress_indication ("\n");
194 progress_indication (_f ("input renamed to: `%s'", s.to_str0 ()));
195 progress_indication ("\n");
196 scm_module_define (ly_car (scopes_),
197 ly_symbol2scm ("input-file-name"),
198 scm_makfrom0str (s.to_str0()));
202 LexerError (_ ("No quoted string found after \\encoding").to_str0 ());
206 LexerError (_ ("No quoted string found after \\version").to_str0 ());
210 LexerError (_ ("No quoted string found after \\renameinput").to_str0 ());
223 LexerError (_ ("EOF found inside a comment").to_str0 ());
224 main_input_b_ = false;
225 if (! close_input ())
226 yyterminate (); // can't move this, since it actually rets a YY_NULL
231 <INITIAL,chords,lyrics,notes,figures>\\maininput {
235 main_input_b_ = true;
237 This is broken - we should switch to \notes when the pitchnames are loaded.
238 Then we get spanish etc. correct. --hwn.
241 SCM nn = lookup_identifier ("pitchnames");
242 push_note_state (alist_to_hashq (nn));
245 error (_ ("\\maininput not allowed outside init files"));
248 <INITIAL,chords,lyrics,figures,notes>\\include {
249 yy_push_state (incl);
251 <incl>\"[^"]*\";? { /* got the include file name */
252 /* FIXME: semicolon? */
253 String s (YYText ()+1);
254 s = s.left_string (s.index_last ('"'));
256 new_input (s, sources_);
259 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
260 /* FIXME: semicolon? */
261 String s = YYText () + 1;
262 strip_trailing_white (s);
263 if (s.length () && (s[s.length () - 1] == ';'))
264 s = s.left_string (s.length () - 1);
266 SCM sid = lookup_identifier (s);
267 if (ly_c_string_p (sid)) {
268 new_input (ly_scm2string (sid), sources_);
271 String msg (_f ("wrong or undefined identifier: `%s'", s ));
273 LexerError (msg.to_str0 ());
274 SCM err = scm_current_error_port ();
275 scm_puts ("This value was found in the table: ", err);
276 scm_display (sid, err);
279 <incl>\"[^"]* { // backup rule
280 error (_ ("Missing end quote"));
283 <chords,notes,figures>{RESTNAME} {
284 const char *s = YYText ();
285 yylval.scm = scm_makfrom0str (s);
288 <chords,notes,figures>R {
289 return MULTI_MEASURE_REST;
291 <INITIAL,markup,chords,lyrics,notes,figures># { //embedded scm
292 //char const* s = YYText () + 1;
293 char const* s = here_str0 ();
295 SCM sval = ly_parse_scm (s, &n, here_input (),
296 safe_global_b && main_input_b_);
298 if (sval == SCM_UNDEFINED)
300 sval = SCM_UNSPECIFIED;
304 for (int i=0; i < n; i++)
308 char_count_stack_.top () += n;
310 if (unpack_identifier (sval) != SCM_UNDEFINED)
312 yylval.scm = unpack_identifier(sval);
313 return identifier_type (yylval.scm);
319 <INITIAL,notes,lyrics>{
321 return DOUBLE_ANGLE_OPEN;
324 return DOUBLE_ANGLE_CLOSE;
341 return scan_bare_word (YYText ());
345 return scan_escaped_word (YYText () + 1);
348 yylval.scm = scan_fraction (YYText ());
353 yylval.i = String_convert::dec2int (String (YYText ()));
357 yylval.i = String_convert::dec2int (String (YYText ()));
361 yylval.i = String_convert::dec2int (String (YYText () +1));
375 *yylval.string += to_string (escaped_char (YYText ()[1]));
378 *yylval.string += YYText ();
384 /* yylval is union. Must remember STRING before setting SCM*/
385 String *sp = yylval.string;
386 yylval.scm = scm_makfrom0str (sp->to_str0 ());
391 *yylval.string += YYText ();
396 *yylval.string += to_string (escaped_char (YYText ()[1]));
399 *yylval.string += YYText ();
405 /* yylval is union. Must remember STRING before setting SCM*/
406 String *sp = yylval.string;
407 yylval.scm = scm_makfrom0str (sp->to_str0 ());
409 return LYRICS_STRING;
412 *yylval.string += YYText ();
418 start_lyric_quote ();
421 yylval.scm = scan_fraction (YYText ());
425 yylval.i = String_convert::dec2int (String (YYText ()));
429 return scan_escaped_word (YYText () + 1);
433 String s (YYText ());
435 return yylval.i = EXTENDER;
437 return yylval.i = HYPHEN;
440 char c = s[s.length () - 1];
441 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
442 here_input ().warning (
443 _ ("Brace found at end of lyric. Did you forget a space?"));
444 yylval.scm = scm_makfrom0str (s.to_str0 ());
447 return LYRICS_STRING;
455 return scan_bare_word (YYText ());
458 return scan_escaped_word (YYText () + 1);
461 yylval.scm = scan_fraction (YYText ());
465 yylval.i = String_convert::dec2int (String (YYText ()));
506 String str (YYText () + 1);
507 SCM s = lookup_markup_command (str);
509 if (ly_c_pair_p (s) && ly_c_symbol_p (ly_cdr (s)) ) {
510 yylval.scm = ly_car(s);
512 if (tag == ly_symbol2scm("markup0"))
513 return MARKUP_HEAD_MARKUP0;
514 if (tag == ly_symbol2scm("empty"))
515 return MARKUP_HEAD_EMPTY;
516 else if (tag == ly_symbol2scm ("markup0-markup1"))
517 return MARKUP_HEAD_MARKUP0_MARKUP1;
518 else if (tag == ly_symbol2scm ("markup-list0"))
519 return MARKUP_HEAD_LIST0;
520 else if (tag == ly_symbol2scm ("scheme0"))
521 return MARKUP_HEAD_SCM0;
522 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
523 return MARKUP_HEAD_SCM0_SCM1;
524 else if (tag == ly_symbol2scm ("scheme0-markup1"))
525 return MARKUP_HEAD_SCM0_MARKUP1;
526 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
527 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
528 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
529 return MARKUP_HEAD_SCM0_SCM1_SCM2;
531 programming_error ("No parser tag defined for this signature. Abort");
536 return scan_escaped_word (str);
541 [^#{}"\\ \t\n\r\f]+ {
542 String s (YYText ());
544 char c = s[s.length () - 1];
545 /* brace open is for not confusing dumb tools. */
546 if (c == '{' || c == '}')
547 here_input ().warning (
548 _ ("Brace found at end of markup. Did you forget a space?"));
549 yylval.scm = scm_makfrom0str (s.to_str0 ());
562 main_input_b_ = false;
564 /* Returns YY_NULL */
567 else if (!close_input ())
568 /* Returns YY_NULL */
574 return scan_bare_word (YYText ());
577 return scan_escaped_word (YYText () + 1);
581 int cnv=sscanf (YYText (), "%lf", &r);
584 yylval.scm = scm_make_real (r);
589 yylval.i = String_convert::dec2int (String (YYText ()));
599 char c = YYText ()[0];
604 <INITIAL,notes,figures>. {
608 <INITIAL,lyrics,notes,figures>\\. {
609 char c= YYText ()[1];
617 return E_EXCLAMATION;
625 return E_RIGHTSQUARE;
637 String msg = _f ("invalid character: `%c'", YYText ()[0]);
638 LexerError (msg.to_str0 ());
645 My_lily_lexer::push_chord_state (SCM tab)
647 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
648 yy_push_state (chords);
652 My_lily_lexer::push_figuredbass_state ()
654 yy_push_state (figures);
658 My_lily_lexer::push_initial_state ()
660 yy_push_state (INITIAL);
664 My_lily_lexer::push_lyric_state ()
666 yy_push_state (lyrics);
670 My_lily_lexer::push_markup_state ()
672 yy_push_state (markup);
676 My_lily_lexer::push_note_state (SCM tab)
678 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
679 yy_push_state (notes);
683 My_lily_lexer::pop_state ()
685 if (YYSTATE == notes || YYSTATE == chords)
686 pitchname_tab_stack_ = ly_cdr (pitchname_tab_stack_);
691 My_lily_lexer::identifier_type (SCM sid)
693 int k = try_special_identifiers (&yylval.scm , sid);
694 return k >= 0 ? k : SCM_IDENTIFIER;
699 My_lily_lexer::scan_escaped_word (String str)
701 // use more SCM for this.
703 // SCM sym = ly_symbol2scm (str.to_str0 ());
705 int l = lookup_keyword (str);
709 SCM sid = lookup_identifier (str);
710 if (is_music_function (sid))
712 yylval.scm = get_music_function_transform (sid);
714 return music_function_type (yylval.scm);
717 if (sid != SCM_UNDEFINED)
720 return identifier_type (sid);
723 String msg (_f ("unknown escaped string: `\\%s'", str));
724 LexerError (msg.to_str0 ());
726 yylval.scm = scm_makfrom0str (str.to_str0 ());
732 My_lily_lexer::scan_bare_word (String str)
734 SCM sym = ly_symbol2scm (str.to_str0 ());
735 if ((YYSTATE == notes) || (YYSTATE == chords)) {
736 SCM handle = SCM_BOOL_F;
737 if (ly_c_pair_p (pitchname_tab_stack_))
738 handle = scm_hashq_get_handle (ly_car (pitchname_tab_stack_), sym);
740 if (ly_c_pair_p (handle)) {
741 yylval.scm = ly_cdr (handle);
742 if (unsmob_pitch (yylval.scm))
743 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
744 else if (ly_c_symbol_p (yylval.scm))
747 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
749 yylval.scm = ly_cdr (handle);
750 return CHORD_MODIFIER;
754 yylval.scm = scm_makfrom0str (str.to_str0 ());
759 My_lily_lexer::is_note_state () const
761 return YY_START == notes;
765 My_lily_lexer::is_chord_state () const
767 return YY_START == chords;
771 My_lily_lexer::is_lyric_state () const
773 return YY_START == lyrics;
777 My_lily_lexer::is_figure_state () const
779 return YY_START == figures;
783 urg, belong to String (_convert)
784 and should be generalised
787 strip_leading_white (String&s)
790 for (; i < s.length (); i++)
794 s = s.nomid_string (0, i);
798 strip_trailing_white (String&s)
805 s = s.left_string (i+1);
810 /* 2.1.2x something -> \property -> \set. */
811 Lilypond_version oldest_version ("2.2.0");
815 is_valid_version (String s)
817 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
818 Lilypond_version ver (s);
819 if (! ((ver >= oldest_version) && (ver <= current)))
821 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
822 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
833 lyric_fudge (String s)
835 char * chars =s.get_copy_str0 ();
837 for (char * p = chars; *p ; p++)
839 if (*p == '_' && (p == chars || *(p-1) != '\\'))
847 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
849 * (s.get_str0 () + i + 1) = 'c';
850 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
857 Convert "NUM/DEN" into a '(NUM . DEN) cons.
860 scan_fraction (String frac)
862 int i = frac.index ('/');
863 int l = frac.length ();
864 String left = frac.left_string (i);
865 String right = frac.right_string (l - i - 1);
867 int n = String_convert::dec2int (left);
868 int d = String_convert::dec2int (right);
869 return scm_cons (scm_int2num (n), scm_int2num (d));
872 // Breaks for flex 2.5.31
874 /* avoid silly flex induced gcc warnings */
875 static void yy_push_state (int) {;}
876 static void yy_pop_state () {;}
877 static int yy_top_state () { return 0; }
880 avoid_silly_flex_induced_gcc_warnings ()
882 (void)yy_start_stack_ptr;
883 (void)yy_start_stack_depth;
884 (void)yy_start_stack;
888 avoid_silly_flex_induced_gcc_warnings ();
893 lookup_markup_command (String s)
895 SCM proc = ly_scheme_function ("lookup-markup-command");
896 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));
901 music_function_type (SCM func)
903 SCM type= scm_object_property (func, ly_symbol2scm ("music-function-signature-keyword"));
904 if (type == ly_symbol2scm ("scm"))
906 return MUSIC_FUNCTION_SCM;
908 else if (type == ly_symbol2scm ("music"))
910 return MUSIC_FUNCTION_MUSIC;
912 else if (type == ly_symbol2scm ("scm-music"))
914 return MUSIC_FUNCTION_SCM_MUSIC;
916 else if (type == ly_symbol2scm ("scm-scm"))
918 return MUSIC_FUNCTION_SCM_SCM;
920 else if (type == ly_symbol2scm ("music-music"))
922 return MUSIC_FUNCTION_MUSIC_MUSIC;
924 else if (type == ly_symbol2scm ("scm-music-music"))
926 return MUSIC_FUNCTION_SCM_MUSIC_MUSIC;
928 else if (type == ly_symbol2scm ("scm-scm-music"))
930 return MUSIC_FUNCTION_SCM_SCM_MUSIC;
932 else if (type == ly_symbol2scm ("noarg"))
934 return MUSIC_FUNCTION;
938 /* TODO: print location */
939 error ("Can not find signature for music function.");
942 return MUSIC_FUNCTION_SCM;