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. */
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);
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>\\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;
236 SCM nn = lookup_identifier ("pitchnames");
237 push_note_state (alist_to_hashq (nn));
240 error (_ ("\\maininput not allowed outside init files"));
243 <INITIAL,chords,lyrics,figures,notes>\\include {
244 yy_push_state (incl);
246 <incl>\"[^"]*\";? { /* got the include file name */
247 /* FIXME: semicolon? */
248 String s (YYText ()+1);
249 s = s.left_string (s.index_last ('"'));
251 new_input (s, sources_);
254 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
255 /* FIXME: semicolon? */
256 String s = YYText () + 1;
257 strip_trailing_white (s);
258 if (s.length () && (s[s.length () - 1] == ';'))
259 s = s.left_string (s.length () - 1);
261 SCM sid = lookup_identifier (s);
262 if (ly_c_string_p (sid)) {
263 new_input (ly_scm2string (sid), sources_);
266 String msg (_f ("wrong or undefined identifier: `%s'", s ));
268 LexerError (msg.to_str0 ());
269 SCM err = scm_current_error_port ();
270 scm_puts ("This value was found in the table: ", err);
271 scm_display (sid, err);
274 <incl>\"[^"]* { // backup rule
275 error (_ ("Missing end quote"));
278 <chords,notes,figures>{RESTNAME} {
279 const char *s = YYText ();
280 yylval.scm = scm_makfrom0str (s);
283 <chords,notes,figures>R {
284 return MULTI_MEASURE_REST;
286 <INITIAL,markup,chords,lyrics,notes,figures># { //embedded scm
287 //char const* s = YYText () + 1;
288 char const* s = here_str0 ();
290 SCM sval = ly_parse_scm (s, &n, here_input (),
291 safe_global_b && main_input_b_);
293 if (sval == SCM_UNDEFINED)
295 sval = SCM_UNSPECIFIED;
299 for (int i=0; i < n; i++)
303 char_count_stack_.top () += n;
305 if (unpack_identifier (sval) != SCM_UNDEFINED)
307 yylval.scm = unpack_identifier(sval);
308 return identifier_type (yylval.scm);
314 <INITIAL,notes,lyrics>{
336 return scan_bare_word (YYText ());
340 return scan_escaped_word (YYText () + 1);
343 yylval.scm = scan_fraction (YYText ());
348 yylval.i = String_convert::dec2int (String (YYText ()));
352 yylval.i = String_convert::dec2int (String (YYText ()));
356 yylval.i = String_convert::dec2int (String (YYText () +1));
370 *yylval.string += to_string (escaped_char (YYText ()[1]));
373 *yylval.string += YYText ();
379 /* yylval is union. Must remember STRING before setting SCM*/
380 String *sp = yylval.string;
381 yylval.scm = scm_makfrom0str (sp->to_str0 ());
386 *yylval.string += YYText ();
391 *yylval.string += to_string (escaped_char (YYText ()[1]));
394 *yylval.string += YYText ();
400 /* yylval is union. Must remember STRING before setting SCM*/
401 String *sp = yylval.string;
402 yylval.scm = scm_makfrom0str (sp->to_str0 ());
404 return LYRICS_STRING;
407 *yylval.string += YYText ();
413 start_lyric_quote ();
416 yylval.scm = scan_fraction (YYText ());
420 yylval.i = String_convert::dec2int (String (YYText ()));
424 return scan_escaped_word (YYText () + 1);
428 String s (YYText ());
430 return yylval.i = EXTENDER;
432 return yylval.i = HYPHEN;
435 char c = s[s.length () - 1];
436 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
437 here_input ().warning (
438 _ ("Brace found at end of lyric. Did you forget a space?"));
439 yylval.scm = scm_makfrom0str (s.to_str0 ());
442 return LYRICS_STRING;
450 return scan_bare_word (YYText ());
453 return scan_escaped_word (YYText () + 1);
456 yylval.scm = scan_fraction (YYText ());
460 yylval.i = String_convert::dec2int (String (YYText ()));
498 String str (YYText () + 1);
499 SCM s = lookup_markup_command (str);
501 if (ly_c_pair_p (s) && ly_c_symbol_p (ly_cdr (s)) ) {
502 yylval.scm = ly_car(s);
504 if (tag == ly_symbol2scm("markup0"))
505 return MARKUP_HEAD_MARKUP0;
506 if (tag == ly_symbol2scm("empty"))
507 return MARKUP_HEAD_EMPTY;
508 else if (tag == ly_symbol2scm ("markup0-markup1"))
509 return MARKUP_HEAD_MARKUP0_MARKUP1;
510 else if (tag == ly_symbol2scm ("markup-list0"))
511 return MARKUP_HEAD_LIST0;
512 else if (tag == ly_symbol2scm ("scheme0"))
513 return MARKUP_HEAD_SCM0;
514 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
515 return MARKUP_HEAD_SCM0_SCM1;
516 else if (tag == ly_symbol2scm ("scheme0-markup1"))
517 return MARKUP_HEAD_SCM0_MARKUP1;
518 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
519 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
520 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
521 return MARKUP_HEAD_SCM0_SCM1_SCM2;
523 programming_error ("No parser tag defined for this signature. Abort");
528 return scan_escaped_word (str);
533 [^#{}"\\ \t\n\r\f]+ {
534 String s (YYText ());
536 char c = s[s.length () - 1];
537 /* brace open is for not confusing dumb tools. */
538 if (c == '{' || c == '}')
539 here_input ().warning (
540 _ ("Brace found at end of markup. Did you forget a space?"));
541 yylval.scm = scm_makfrom0str (s.to_str0 ());
554 main_input_b_ = false;
556 /* Returns YY_NULL */
560 else if (!close_input ())
561 /* Returns YY_NULL */
567 return scan_bare_word (YYText ());
570 return scan_escaped_word (YYText () + 1);
574 int cnv=sscanf (YYText (), "%lf", &r);
577 yylval.scm = scm_make_real (r);
582 yylval.i = String_convert::dec2int (String (YYText ()));
592 char c = YYText ()[0];
597 <INITIAL,notes,figures>. {
601 <INITIAL,lyrics,notes,figures>\\. {
602 char c= YYText ()[1];
610 return E_EXCLAMATION;
618 return E_RIGHTSQUARE;
630 String msg = _f ("invalid character: `%c'", YYText ()[0]);
631 LexerError (msg.to_str0 ());
638 My_lily_lexer::push_chord_state (SCM tab)
640 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
641 yy_push_state (chords);
645 My_lily_lexer::push_figuredbass_state ()
647 yy_push_state (figures);
651 My_lily_lexer::push_initial_state ()
653 yy_push_state (INITIAL);
657 My_lily_lexer::push_lyric_state ()
659 yy_push_state (lyrics);
663 My_lily_lexer::push_markup_state ()
665 yy_push_state (markup);
669 My_lily_lexer::push_note_state (SCM tab)
671 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
672 yy_push_state (notes);
676 My_lily_lexer::pop_state ()
678 if (YYSTATE == notes || YYSTATE == chords)
679 pitchname_tab_stack_ = ly_cdr (pitchname_tab_stack_);
684 My_lily_lexer::identifier_type (SCM sid)
686 int k = try_special_identifiers (&yylval.scm , sid);
687 return k >= 0 ? k : SCM_IDENTIFIER;
692 My_lily_lexer::scan_escaped_word (String str)
694 // use more SCM for this.
696 // SCM sym = ly_symbol2scm (str.to_str0 ());
698 int l = lookup_keyword (str);
702 SCM sid = lookup_identifier (str);
703 if (sid != SCM_UNDEFINED)
706 return identifier_type (sid);
709 String msg (_f ("unknown escaped string: `\\%s'", str));
710 LexerError (msg.to_str0 ());
712 yylval.scm = scm_makfrom0str (str.to_str0 ());
718 My_lily_lexer::scan_bare_word (String str)
720 SCM sym = ly_symbol2scm (str.to_str0 ());
721 if ((YYSTATE == notes) || (YYSTATE == chords)) {
722 SCM handle = SCM_BOOL_F;
723 if (ly_c_pair_p (pitchname_tab_stack_))
724 handle = scm_hashq_get_handle (ly_car (pitchname_tab_stack_), sym);
726 if (ly_c_pair_p (handle)) {
727 yylval.scm = ly_cdr (handle);
728 if (unsmob_pitch (yylval.scm))
729 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
730 else if (ly_c_symbol_p (yylval.scm))
733 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
735 yylval.scm = ly_cdr (handle);
736 return CHORD_MODIFIER;
740 yylval.scm = scm_makfrom0str (str.to_str0 ());
745 My_lily_lexer::is_note_state () const
747 return YY_START == notes;
751 My_lily_lexer::is_chord_state () const
753 return YY_START == chords;
757 My_lily_lexer::is_lyric_state () const
759 return YY_START == lyrics;
763 My_lily_lexer::is_figure_state () const
765 return YY_START == figures;
769 urg, belong to String (_convert)
770 and should be generalised
773 strip_leading_white (String&s)
776 for (; i < s.length (); i++)
780 s = s.nomid_string (0, i);
784 strip_trailing_white (String&s)
791 s = s.left_string (i+1);
796 /* 2.1.2x something -> \property -> \set. */
797 Lilypond_version oldest_version ("2.2.0");
801 is_valid_version (String s)
803 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
804 Lilypond_version ver (s);
805 if (! ((ver >= oldest_version) && (ver <= current)))
807 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
808 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
819 lyric_fudge (String s)
821 char * chars =s.get_copy_str0 ();
823 for (char * p = chars; *p ; p++)
825 if (*p == '_' && (p == chars || *(p-1) != '\\'))
833 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
835 * (s.get_str0 () + i + 1) = 'c';
836 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
843 Convert "NUM/DEN" into a '(NUM . DEN) cons.
846 scan_fraction (String frac)
848 int i = frac.index ('/');
849 int l = frac.length ();
850 String left = frac.left_string (i);
851 String right = frac.right_string (l - i - 1);
853 int n = String_convert::dec2int (left);
854 int d = String_convert::dec2int (right);
855 return scm_cons (scm_int2num (n), scm_int2num (d));
858 // Breaks for flex 2.5.31
860 /* avoid silly flex induced gcc warnings */
861 static void yy_push_state (int) {;}
862 static void yy_pop_state () {;}
863 static int yy_top_state () { return 0; }
866 avoid_silly_flex_induced_gcc_warnings ()
868 (void)yy_start_stack_ptr;
869 (void)yy_start_stack_depth;
870 (void)yy_start_stack;
874 avoid_silly_flex_induced_gcc_warnings ();
879 lookup_markup_command (String s)
881 SCM proc = ly_scheme_function ("lookup-markup-command");
882 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));