1 %{ // -*-Fundamental-*-
3 lexer.ll -- 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 :-)
29 /* Flex >= 2.5.29 fix; FlexLexer.h's multiple include bracing breaks
30 when building the actual lexer. */
36 #include "parse-scm.hh"
37 #include "lily-guile.hh"
39 #include "string-convert.hh"
40 #include "my-lily-lexer.hh"
41 #include "input-file-results.hh"
42 #include "interval.hh"
43 #include "lily-guile.hh"
48 #include "lilypond-input-version.hh"
49 #include "translator-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);
62 lookup_markup_command (String s);
65 valid_version_b (String s);
69 #define start_quote() \
70 yy_push_state (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
116 NATIONAL [\001-\006\021-\027\031\036\200-\377]
117 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
125 REAL ({INT}\.{N}*)|(-?\.{N}+)
128 HORIZONTALWHITE [ \t]
132 MARKUPCOMMAND \\({A}|[-_])+
133 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
141 // windows-suck-suck-suck
144 <INITIAL,chords,incl,markup,lyrics,notes,figures>{
146 yy_push_state (longcomment);
150 %[^{\n] { // backup rule
161 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
162 yy_push_state (version);
164 <version>\"[^"]*\" { /* got the version number */
165 String s (YYText ()+1);
166 s = s.left_string (s.index_last ('\"'));
169 if (!valid_version_b (s))
173 LexerError ("No quoted string found after \\version");
186 LexerError (_ ("EOF found inside a comment").to_str0 ());
187 if (! close_input ())
188 yyterminate (); // can't move this, since it actually rets a YY_NULL
193 <INITIAL,chords,lyrics,notes,figures>\\maininput {
197 main_input_b_ = true;
200 error (_ ("\\maininput disallowed outside init files"));
203 <INITIAL,chords,lyrics,figures,notes>\\include {
204 yy_push_state (incl);
206 <incl>\"[^"]*\";? { /* got the include file name */
207 String s (YYText ()+1);
208 s = s.left_string (s.index_last ('"'));
210 new_input (s, &global_input_file->sources_ );
213 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
214 String s = YYText () + 1;
215 strip_trailing_white (s);
216 if (s.length () && (s[s.length () - 1] == ';'))
217 s = s.left_string (s.length () - 1);
219 SCM sid = lookup_identifier (s);
220 if (gh_string_p (sid)) {
221 new_input (ly_scm2string (sid), &global_input_file->sources_);
224 String msg (_f ("wrong or undefined identifier: `%s'", s ));
226 LexerError (msg.to_str0 ());
227 SCM err = scm_current_error_port ();
228 scm_puts ("This value was found in the table: ", err);
229 scm_display (sid, err);
232 <incl>\"[^"]* { // backup rule
233 error (_ ("Missing end quote"));
236 <chords,notes,figures>{RESTNAME} {
237 const char *s = YYText ();
238 yylval.scm = scm_makfrom0str (s);
241 <chords,notes,figures>R {
242 return MULTI_MEASURE_REST;
244 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}*{WHITE} {
245 String s=YYText () + 2;
246 s=s.left_string (s.length () - 1);
247 return scan_escaped_word (s);
249 <INITIAL,chords,lyrics,notes,figures>\${BLACK}*{WHITE} {
250 String s=YYText () + 1;
251 s=s.left_string (s.length () - 1);
252 return scan_bare_word (s);
254 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}* { // backup rule
255 error (_("white expected"));
258 <INITIAL,chords,lyrics,notes,figures>\${BLACK}* { // backup rule
259 error (_("white expected"));
263 <INITIAL,markup,chords,lyrics,notes,figures># { //embedded scm
264 //char const* s = YYText () + 1;
265 char const* s = here_str0 ();
267 if (main_input_b_ && safe_global_b) {
268 error (_ ("Can't evaluate Scheme in safe mode"));
269 yylval.scm = SCM_EOL;
272 SCM sval = ly_parse_scm (s, &n, here_input());
273 if (sval == SCM_UNDEFINED)
275 sval = SCM_UNSPECIFIED;
279 for (int i=0; i < n; i++)
283 char_count_stack_.top () += n;
285 if (unpack_identifier (sval) != SCM_UNDEFINED)
287 yylval.scm = unpack_identifier(sval);
288 return identifier_type (yylval.scm);
294 <INITIAL,notes,markup>{
316 return scan_bare_word (YYText ());
320 return scan_escaped_word (YYText () + 1);
323 yylval.scm = scan_fraction (YYText ());
328 yylval.i = String_convert::dec2int (String (YYText ()));
332 yylval.i = String_convert::dec2int (String (YYText ()));
336 yylval.i = String_convert::dec2int (String (YYText () +1));
350 *yylval.string += to_string (escaped_char (YYText ()[1]));
353 *yylval.string += YYText ();
359 /* yylval is union. Must remember STRING before setting SCM*/
360 String *sp = yylval.string;
361 yylval.scm = scm_makfrom0str (sp->to_str0 ());
366 *yylval.string += YYText ();
375 yylval.scm = scan_fraction (YYText ());
379 yylval.i = String_convert::dec2int (String (YYText ()));
383 return scan_escaped_word (YYText () + 1);
387 String s (YYText ());
389 return yylval.i = EXTENDER;
391 return yylval.i = HYPHEN;
394 char c = s[s.length () - 1];
395 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
396 here_input ().warning (
397 _ ("Brace found at end of lyric. Did you forget a space?"));
398 yylval.scm = scm_makfrom0str (s.to_str0 ());
409 return scan_bare_word (YYText ());
412 return scan_escaped_word (YYText () + 1);
415 yylval.scm = scan_fraction (YYText ());
419 yylval.i = String_convert::dec2int (String (YYText ()));
451 String str (YYText() + 1);
452 SCM s = lookup_markup_command (str);
454 if (gh_pair_p (s) && gh_symbol_p (gh_cdr (s)) ) {
455 yylval.scm = gh_car(s);
457 if (tag == ly_symbol2scm("markup0"))
458 return MARKUP_HEAD_MARKUP0;
459 else if (tag == ly_symbol2scm ("markup0-markup1"))
460 return MARKUP_HEAD_MARKUP0_MARKUP1;
461 else if (tag == ly_symbol2scm ("markup-list0"))
462 return MARKUP_HEAD_LIST0;
463 else if (tag == ly_symbol2scm ("scheme0"))
464 return MARKUP_HEAD_SCM0;
465 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
466 return MARKUP_HEAD_SCM0_SCM1;
467 else if (tag == ly_symbol2scm ("scheme0-markup1"))
468 return MARKUP_HEAD_SCM0_MARKUP1;
469 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
470 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
471 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
472 return MARKUP_HEAD_SCM0_SCM1_SCM2;
474 programming_error ("No parser tag defined for this signature. Abort");
479 return scan_escaped_word (str);
482 String s (YYText ());
484 char c = s[s.length () - 1];
485 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
486 here_input ().warning (
487 _ ("Brace found at end of lyric. Did you forget a space?"));
488 yylval.scm = scm_makfrom0str (s.to_str0 ());
504 if (! close_input ()) {
505 yyterminate (); // can't move this, since it actually rets a YY_NULL
511 return scan_bare_word (YYText ());
514 return scan_escaped_word (YYText () + 1);
518 int cnv=sscanf (YYText (), "%lf", &r);
521 yylval.scm = gh_double2scm (r);
526 yylval.i = String_convert::dec2int (String (YYText ()));
536 char c = YYText ()[0];
541 <INITIAL,notes,figures>. {
545 <INITIAL,lyrics,notes,figures>\\. {
546 char c= YYText ()[1];
554 return E_EXCLAMATION;
562 return E_RIGHTSQUARE;
574 String msg = _f ("invalid character: `%c'", YYText ()[0]);
575 LexerError (msg.to_str0 ());
582 My_lily_lexer::push_note_state ()
584 yy_push_state (notes);
588 My_lily_lexer::push_figuredbass_state()
590 yy_push_state (figures);
593 My_lily_lexer::push_chord_state ()
595 yy_push_state (chords);
599 My_lily_lexer::push_lyric_state ()
601 yy_push_state (lyrics);
605 My_lily_lexer::push_markup_state ()
607 yy_push_state (markup);
611 My_lily_lexer::pop_state ()
617 My_lily_lexer::identifier_type(SCM sid)
619 int k = try_special_identifiers(&yylval.scm , sid);
620 return k >= 0 ? k : SCM_IDENTIFIER;
625 My_lily_lexer::scan_escaped_word (String str)
627 // use more SCM for this.
629 SCM sym = ly_symbol2scm (str.to_str0 ());
631 int l = lookup_keyword (str);
635 SCM sid = lookup_identifier (str);
636 if (sid != SCM_UNDEFINED)
639 return identifier_type (sid);
642 if ((YYSTATE != notes) && (YYSTATE != chords)) {
643 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
645 if (gh_pair_p (pitch))
647 yylval.scm = ly_cdr (pitch);
648 return NOTENAME_PITCH;
651 String msg (_f ("unknown escaped string: `\\%s'", str));
652 LexerError (msg.to_str0 ());
654 yylval.scm = scm_makfrom0str (str.to_str0 ());
660 My_lily_lexer::scan_bare_word (String str)
662 SCM sym = ly_symbol2scm (str.to_str0 ());
663 if ((YYSTATE == notes) || (YYSTATE == chords)) {
664 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
665 if (gh_pair_p (pitch)) {
666 yylval.scm = ly_cdr (pitch);
667 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
668 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
670 yylval.scm = ly_cdr (pitch);
671 return CHORD_MODIFIER;
675 yylval.scm = scm_makfrom0str (str.to_str0 ());
680 My_lily_lexer::note_state_b () const
682 return YY_START == notes;
686 My_lily_lexer::chord_state_b () const
688 return YY_START == chords;
692 My_lily_lexer::lyric_state_b () const
694 return YY_START == lyrics;
698 My_lily_lexer::figure_state_b () const
700 return YY_START == figures;
704 urg, belong to String (_convert)
705 and should be generalised
708 strip_leading_white (String&s)
711 for (; i < s.length (); i++)
715 s = s.nomid_string (0, i);
719 strip_trailing_white (String&s)
726 s = s.left_string (i+1);
731 /* 1.3.146 == removal of ; */
732 LilyPond_version oldest_version ("1.3.146");
736 valid_version_b (String s)
738 LilyPond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
739 LilyPond_version ver (s);
740 if (! ((ver >= oldest_version) && (ver <= current)))
742 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
743 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
754 lyric_fudge (String s)
756 char * chars =s.get_copy_str0 ();
758 for (char * p = chars; *p ; p++)
760 if (*p == '_' && (p == chars || *(p-1) != '\\'))
768 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
770 * (s.get_str0 () + i + 1) = 'c';
771 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
778 Convert "NUM/DEN" into a '(NUM . DEN) cons.
781 scan_fraction (String frac)
783 int i = frac.index ('/');
784 int l = frac.length ();
785 String left = frac.left_string (i);
786 String right = frac.right_string (l - i - 1);
788 int n = String_convert::dec2int (left);
789 int d = String_convert::dec2int (right);
790 return gh_cons (gh_int2scm (n), gh_int2scm (d));
793 // Breaks for flex 2.5.31
795 /* avoid silly flex induced gcc warnings */
796 static void yy_push_state (int) {;}
797 static void yy_pop_state () {;}
798 static int yy_top_state () { return 0; }
801 avoid_silly_flex_induced_gcc_warnings ()
803 (void)yy_start_stack_ptr;
804 (void)yy_start_stack_depth;
805 (void)yy_start_stack;
809 avoid_silly_flex_induced_gcc_warnings ();
814 lookup_markup_command (String s)
818 proc = scm_c_eval_string ("lookup-markup-command");
820 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));