1 %{ // -*-Fundamental-*-
3 lexer.l -- implement the Flex lexer
5 source file of the LilyPond music typesetter
7 (c) 1996--2003 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 :-)
32 #include "parse-scm.hh"
33 #include "lily-guile.hh"
35 #include "string-convert.hh"
36 #include "my-lily-lexer.hh"
37 #include "input-file-results.hh"
38 #include "interval.hh"
39 #include "lily-guile.hh"
44 #include "lilypond-input-version.hh"
45 #include "translator-def.hh"
46 #include "identifier-smob.hh"
51 #define isatty HORRIBLEKLUDGE
53 void strip_trailing_white (String&);
54 void strip_leading_white (String&);
55 String lyric_fudge (String s);
58 lookup_markup_command (String s);
61 valid_version_b (String s);
65 #define start_quote() \
66 yy_push_state (quote);\
67 yylval.string = new String
72 #define YY_USER_ACTION add_lexed_char (YYLeng ());
75 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
80 SCM scan_fraction (String);
81 SCM (* scm_parse_error_handler) (void *);
91 %option yyclass="My_lily_lexer"
93 %option never-interactive
112 NATIONAL [\001-\006\021-\027\031\036\200-\377]
113 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
121 REAL ({INT}\.{N}*)|(-?\.{N}+)
124 HORIZONTALWHITE [ \t]
128 MARKUPCOMMAND \\({A}|[-_])+
129 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
137 // windows-suck-suck-suck
140 <INITIAL,chords,incl,markup,lyrics,notes,figures>{
142 yy_push_state (longcomment);
146 %[^{\n] { // backup rule
157 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
158 yy_push_state (version);
160 <version>\"[^"]*\" { /* got the version number */
161 String s (YYText ()+1);
162 s = s.left_string (s.index_last ('\"'));
165 if (!valid_version_b (s))
169 LexerError ("No quoted string found after \\version");
182 LexerError (_ ("EOF found inside a comment").to_str0 ());
183 if (! close_input ())
184 yyterminate (); // can't move this, since it actually rets a YY_NULL
189 <INITIAL,chords,lyrics,notes,figures>\\maininput {
193 main_input_b_ = true;
196 error (_ ("\\maininput disallowed outside init files"));
199 <INITIAL,chords,lyrics,figures,notes>\\include {
200 yy_push_state (incl);
202 <incl>\"[^"]*\";? { /* got the include file name */
203 String s (YYText ()+1);
204 s = s.left_string (s.index_last ('"'));
206 new_input (s, &global_input_file->sources_ );
209 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
210 String s = YYText () + 1;
211 strip_trailing_white (s);
212 if (s.length () && (s[s.length () - 1] == ';'))
213 s = s.left_string (s.length () - 1);
215 SCM sid = lookup_identifier (s);
216 if (gh_string_p (sid)) {
217 new_input (ly_scm2string (sid), &global_input_file->sources_);
220 String msg (_f ("wrong or undefined identifier: `%s'", s ));
222 LexerError (msg.to_str0 ());
223 SCM err = scm_current_error_port ();
224 scm_puts ("This value was found in the table: ", err);
225 scm_display (sid, err);
228 <incl>\"[^"]* { // backup rule
229 error (_ ("Missing end quote"));
232 <chords,notes,figures>{RESTNAME} {
233 const char *s = YYText ();
234 yylval.scm = scm_makfrom0str (s);
237 <chords,notes,figures>R {
238 return MULTI_MEASURE_REST;
240 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}*{WHITE} {
241 String s=YYText () + 2;
242 s=s.left_string (s.length () - 1);
243 return scan_escaped_word (s);
245 <INITIAL,chords,lyrics,notes,figures>\${BLACK}*{WHITE} {
246 String s=YYText () + 1;
247 s=s.left_string (s.length () - 1);
248 return scan_bare_word (s);
250 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}* { // backup rule
251 error (_("white expected"));
254 <INITIAL,chords,lyrics,notes,figures>\${BLACK}* { // backup rule
255 error (_("white expected"));
259 <INITIAL,markup,chords,lyrics,notes,figures># { //embedded scm
260 //char const* s = YYText () + 1;
261 char const* s = here_str0 ();
263 if (main_input_b_ && safe_global_b) {
264 error (_ ("Can't evaluate Scheme in safe mode"));
265 yylval.scm = SCM_EOL;
268 SCM sval = ly_parse_scm (s, &n, here_input());
269 if (sval == SCM_UNDEFINED)
271 sval = SCM_UNSPECIFIED;
275 for (int i=0; i < n; i++)
279 char_count_stack_.top () += n;
281 if (unpack_identifier (sval) != SCM_UNDEFINED)
283 yylval.scm = unpack_identifier(sval);
284 return identifier_type (yylval.scm);
290 <INITIAL,notes,markup>{
312 return scan_bare_word (YYText ());
316 return scan_escaped_word (YYText () + 1);
319 yylval.scm = scan_fraction (YYText ());
324 yylval.i = String_convert::dec2int (String (YYText ()));
328 yylval.i = String_convert::dec2int (String (YYText ()));
332 yylval.i = String_convert::dec2int (String (YYText () +1));
346 *yylval.string += to_string (escaped_char (YYText ()[1]));
349 *yylval.string += YYText ();
355 /* yylval is union. Must remember STRING before setting SCM*/
356 String *sp = yylval.string;
357 yylval.scm = scm_makfrom0str (sp->to_str0 ());
362 *yylval.string += YYText ();
371 yylval.scm = scan_fraction (YYText ());
375 yylval.i = String_convert::dec2int (String (YYText ()));
379 return scan_escaped_word (YYText () + 1);
383 String s (YYText ());
385 return yylval.i = EXTENDER;
387 return yylval.i = HYPHEN;
390 char c = s[s.length () - 1];
391 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
392 here_input ().warning (
393 _ ("Brace found at end of lyric. Did you forget a space?"));
394 yylval.scm = scm_makfrom0str (s.to_str0 ());
405 return scan_bare_word (YYText ());
408 return scan_escaped_word (YYText () + 1);
411 yylval.scm = scan_fraction (YYText ());
415 yylval.i = String_convert::dec2int (String (YYText ()));
447 String str (YYText() + 1);
448 SCM s = lookup_markup_command (str);
450 if (gh_pair_p (s) && gh_symbol_p (gh_cdr (s)) ) {
451 yylval.scm = gh_car(s);
453 if (tag == ly_symbol2scm("markup0"))
454 return MARKUP_HEAD_MARKUP0;
455 else if (tag == ly_symbol2scm ("markup0-markup1"))
456 return MARKUP_HEAD_MARKUP0_MARKUP1;
457 else if (tag == ly_symbol2scm ("markup-list0"))
458 return MARKUP_HEAD_LIST0;
459 else if (tag == ly_symbol2scm ("scheme0"))
460 return MARKUP_HEAD_SCM0;
461 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
462 return MARKUP_HEAD_SCM0_SCM1;
463 else if (tag == ly_symbol2scm ("scheme0-markup1"))
464 return MARKUP_HEAD_SCM0_MARKUP1;
465 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
466 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
472 return scan_escaped_word (str);
475 String s (YYText ());
477 char c = s[s.length () - 1];
478 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
479 here_input ().warning (
480 _ ("Brace found at end of lyric. Did you forget a space?"));
481 yylval.scm = scm_makfrom0str (s.to_str0 ());
497 if (! close_input ()) {
498 yyterminate (); // can't move this, since it actually rets a YY_NULL
504 return scan_bare_word (YYText ());
507 return scan_escaped_word (YYText () + 1);
511 int cnv=sscanf (YYText (), "%lf", &r);
514 yylval.scm = gh_double2scm (r);
519 yylval.i = String_convert::dec2int (String (YYText ()));
529 char c = YYText ()[0];
534 <INITIAL,notes,figures>. {
538 <INITIAL,lyrics,notes,figures>\\. {
539 char c= YYText ()[1];
547 return E_EXCLAMATION;
555 return E_RIGHTSQUARE;
567 String msg = _f ("invalid character: `%c'", YYText ()[0]);
568 LexerError (msg.to_str0 ());
575 My_lily_lexer::push_note_state ()
577 yy_push_state (notes);
581 My_lily_lexer::push_figuredbass_state()
583 yy_push_state (figures);
586 My_lily_lexer::push_chord_state ()
588 yy_push_state (chords);
592 My_lily_lexer::push_lyric_state ()
594 yy_push_state (lyrics);
598 My_lily_lexer::push_markup_state ()
600 yy_push_state (markup);
604 My_lily_lexer::pop_state ()
610 My_lily_lexer::identifier_type(SCM sid)
612 int k = try_special_identifiers(&yylval.scm , sid);
613 return k >= 0 ? k : SCM_IDENTIFIER;
618 My_lily_lexer::scan_escaped_word (String str)
620 // use more SCM for this.
622 SCM sym = ly_symbol2scm (str.to_str0 ());
624 int l = lookup_keyword (str);
628 SCM sid = lookup_identifier (str);
629 if (sid != SCM_UNDEFINED)
632 return identifier_type (sid);
635 if ((YYSTATE != notes) && (YYSTATE != chords)) {
636 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
638 if (gh_pair_p (pitch))
640 yylval.scm = ly_cdr (pitch);
641 return NOTENAME_PITCH;
644 String msg (_f ("unknown escaped string: `\\%s'", str));
645 LexerError (msg.to_str0 ());
647 yylval.scm = scm_makfrom0str (str.to_str0 ());
653 My_lily_lexer::scan_bare_word (String str)
655 SCM sym = ly_symbol2scm (str.to_str0 ());
656 if ((YYSTATE == notes) || (YYSTATE == chords)) {
657 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
658 if (gh_pair_p (pitch)) {
659 yylval.scm = ly_cdr (pitch);
660 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
661 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
663 yylval.scm = ly_cdr (pitch);
664 return CHORD_MODIFIER;
668 yylval.scm = scm_makfrom0str (str.to_str0 ());
673 My_lily_lexer::note_state_b () const
675 return YY_START == notes;
679 My_lily_lexer::chord_state_b () const
681 return YY_START == chords;
685 My_lily_lexer::lyric_state_b () const
687 return YY_START == lyrics;
691 My_lily_lexer::figure_state_b () const
693 return YY_START == figures;
697 urg, belong to String (_convert)
698 and should be generalised
701 strip_leading_white (String&s)
704 for (; i < s.length (); i++)
708 s = s.nomid_string (0, i);
712 strip_trailing_white (String&s)
719 s = s.left_string (i+1);
724 /* 1.3.146 == removal of ; */
725 Lilypond_version oldest_version ("1.3.146");
729 valid_version_b (String s)
731 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
732 Lilypond_version ver (s);
733 if (! ((ver >= oldest_version) && (ver <= current)))
735 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
736 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
747 lyric_fudge (String s)
749 char * chars =s.get_copy_str0 ();
751 for (char * p = chars; *p ; p++)
753 if (*p == '_' && (p == chars || *(p-1) != '\\'))
761 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
763 * (s.get_str0 () + i + 1) = 'c';
764 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
771 Convert "NUM/DEN" into a '(NUM . DEN) cons.
774 scan_fraction (String frac)
776 int i = frac.index ('/');
777 int l = frac.length ();
778 String left = frac.left_string (i);
779 String right = frac.right_string (l - i - 1);
781 int n = String_convert::dec2int (left);
782 int d = String_convert::dec2int (right);
783 return gh_cons (gh_int2scm (n), gh_int2scm (d));
786 /* avoid silly flex induced gcc warnings */
787 static void yy_push_state (int) {;}
788 static void yy_pop_state () {;}
789 static int yy_top_state () { return 0; }
792 avoid_silly_flex_induced_gcc_warnings ()
794 (void)yy_start_stack_ptr;
795 (void)yy_start_stack_depth;
796 (void)yy_start_stack;
800 avoid_silly_flex_induced_gcc_warnings ();
804 lookup_markup_command (String s)
808 proc = scm_c_eval_string ("lookup-markup-command");
810 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));