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 "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 "input-file-results.hh"
43 #include "interval.hh"
44 #include "lily-guile.hh"
49 #include "lilypond-input-version.hh"
50 #include "translator-def.hh"
51 #include "identifier-smob.hh"
56 #define isatty HORRIBLEKLUDGE
58 void strip_trailing_white (String&);
59 void strip_leading_white (String&);
60 String lyric_fudge (String s);
63 lookup_markup_command (String s);
66 valid_version_b (String s);
70 #define start_quote() \
71 yy_push_state (quote);\
72 yylval.string = new String
77 #define YY_USER_ACTION add_lexed_char (YYLeng ());
80 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
85 SCM scan_fraction (String);
86 SCM (* scm_parse_error_handler) (void *);
96 %option yyclass="My_lily_lexer"
98 %option never-interactive
118 NATIONAL [\001-\006\021-\027\031\036\200-\377]
119 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
127 REAL ({INT}\.{N}*)|(-?\.{N}+)
130 HORIZONTALWHITE [ \t]
134 MARKUPCOMMAND \\({A}|[-_])+
135 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
143 // windows-suck-suck-suck
146 <INITIAL,chords,incl,markup,lyrics,notes,figures>{
148 yy_push_state (longcomment);
152 %[^{\n] { // backup rule
163 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
164 yy_push_state (version);
166 <INITIAL,chords,lyrics,notes,figures>\\renameinput{WHITE}* {
167 yy_push_state (renameinput);
169 <version>\"[^"]*\" { /* got the version number */
170 String s (YYText ()+1);
171 s = s.left_string (s.index_last ('\"'));
174 if (!valid_version_b (s))
177 <renameinput>\"[^"]*\" { /* got the version number */
178 String s (YYText ()+1);
179 s = s.left_string (s.index_last ('\"'));
182 this->here_input().source_file_->name_ = s;
186 LexerError ("No quoted string found after \\version");
190 LexerError ("No quoted string found after \\renameinput");
203 LexerError (_ ("EOF found inside a comment").to_str0 ());
204 if (! close_input ())
205 yyterminate (); // can't move this, since it actually rets a YY_NULL
210 <INITIAL,chords,lyrics,notes,figures>\\maininput {
214 main_input_b_ = true;
217 error (_ ("\\maininput disallowed outside init files"));
220 <INITIAL,chords,lyrics,figures,notes>\\include {
221 yy_push_state (incl);
223 <incl>\"[^"]*\";? { /* got the include file name */
224 String s (YYText ()+1);
225 s = s.left_string (s.index_last ('"'));
227 new_input (s, &global_input_file->sources_ );
230 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
231 String s = YYText () + 1;
232 strip_trailing_white (s);
233 if (s.length () && (s[s.length () - 1] == ';'))
234 s = s.left_string (s.length () - 1);
236 SCM sid = lookup_identifier (s);
237 if (gh_string_p (sid)) {
238 new_input (ly_scm2string (sid), &global_input_file->sources_);
241 String msg (_f ("wrong or undefined identifier: `%s'", s ));
243 LexerError (msg.to_str0 ());
244 SCM err = scm_current_error_port ();
245 scm_puts ("This value was found in the table: ", err);
246 scm_display (sid, err);
249 <incl>\"[^"]* { // backup rule
250 error (_ ("Missing end quote"));
253 <chords,notes,figures>{RESTNAME} {
254 const char *s = YYText ();
255 yylval.scm = scm_makfrom0str (s);
258 <chords,notes,figures>R {
259 return MULTI_MEASURE_REST;
261 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}*{WHITE} {
262 String s=YYText () + 2;
263 s=s.left_string (s.length () - 1);
264 return scan_escaped_word (s);
266 <INITIAL,chords,lyrics,notes,figures>\${BLACK}*{WHITE} {
267 String s=YYText () + 1;
268 s=s.left_string (s.length () - 1);
269 return scan_bare_word (s);
271 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}* { // backup rule
272 error (_("white expected"));
275 <INITIAL,chords,lyrics,notes,figures>\${BLACK}* { // backup rule
276 error (_("white expected"));
280 <INITIAL,markup,chords,lyrics,notes,figures># { //embedded scm
281 //char const* s = YYText () + 1;
282 char const* s = here_str0 ();
284 if (main_input_b_ && safe_global_b) {
285 error (_ ("Can't evaluate Scheme in safe mode"));
286 yylval.scm = SCM_EOL;
289 SCM sval = ly_parse_scm (s, &n, here_input());
290 if (sval == SCM_UNDEFINED)
292 sval = SCM_UNSPECIFIED;
296 for (int i=0; i < n; i++)
300 char_count_stack_.top () += n;
302 if (unpack_identifier (sval) != SCM_UNDEFINED)
304 yylval.scm = unpack_identifier(sval);
305 return identifier_type (yylval.scm);
311 <INITIAL,notes,markup>{
333 return scan_bare_word (YYText ());
337 return scan_escaped_word (YYText () + 1);
340 yylval.scm = scan_fraction (YYText ());
345 yylval.i = String_convert::dec2int (String (YYText ()));
349 yylval.i = String_convert::dec2int (String (YYText ()));
353 yylval.i = String_convert::dec2int (String (YYText () +1));
367 *yylval.string += to_string (escaped_char (YYText ()[1]));
370 *yylval.string += YYText ();
376 /* yylval is union. Must remember STRING before setting SCM*/
377 String *sp = yylval.string;
378 yylval.scm = scm_makfrom0str (sp->to_str0 ());
383 *yylval.string += YYText ();
392 yylval.scm = scan_fraction (YYText ());
396 yylval.i = String_convert::dec2int (String (YYText ()));
400 return scan_escaped_word (YYText () + 1);
404 String s (YYText ());
406 return yylval.i = EXTENDER;
408 return yylval.i = HYPHEN;
411 char c = s[s.length () - 1];
412 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
413 here_input ().warning (
414 _ ("Brace found at end of lyric. Did you forget a space?"));
415 yylval.scm = scm_makfrom0str (s.to_str0 ());
426 return scan_bare_word (YYText ());
429 return scan_escaped_word (YYText () + 1);
432 yylval.scm = scan_fraction (YYText ());
436 yylval.i = String_convert::dec2int (String (YYText ()));
468 String str (YYText() + 1);
469 SCM s = lookup_markup_command (str);
471 if (gh_pair_p (s) && gh_symbol_p (gh_cdr (s)) ) {
472 yylval.scm = gh_car(s);
474 if (tag == ly_symbol2scm("markup0"))
475 return MARKUP_HEAD_MARKUP0;
476 else if (tag == ly_symbol2scm ("markup0-markup1"))
477 return MARKUP_HEAD_MARKUP0_MARKUP1;
478 else if (tag == ly_symbol2scm ("markup-list0"))
479 return MARKUP_HEAD_LIST0;
480 else if (tag == ly_symbol2scm ("scheme0"))
481 return MARKUP_HEAD_SCM0;
482 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
483 return MARKUP_HEAD_SCM0_SCM1;
484 else if (tag == ly_symbol2scm ("scheme0-markup1"))
485 return MARKUP_HEAD_SCM0_MARKUP1;
486 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
487 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
488 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
489 return MARKUP_HEAD_SCM0_SCM1_SCM2;
491 programming_error ("No parser tag defined for this signature. Abort");
496 return scan_escaped_word (str);
499 String s (YYText ());
501 char c = s[s.length () - 1];
502 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
503 here_input ().warning (
504 _ ("Brace found at end of lyric. Did you forget a space?"));
505 yylval.scm = scm_makfrom0str (s.to_str0 ());
521 if (! close_input ()) {
522 yyterminate (); // can't move this, since it actually rets a YY_NULL
528 return scan_bare_word (YYText ());
531 return scan_escaped_word (YYText () + 1);
535 int cnv=sscanf (YYText (), "%lf", &r);
538 yylval.scm = gh_double2scm (r);
543 yylval.i = String_convert::dec2int (String (YYText ()));
553 char c = YYText ()[0];
558 <INITIAL,notes,figures>. {
562 <INITIAL,lyrics,notes,figures>\\. {
563 char c= YYText ()[1];
571 return E_EXCLAMATION;
579 return E_RIGHTSQUARE;
591 String msg = _f ("invalid character: `%c'", YYText ()[0]);
592 LexerError (msg.to_str0 ());
599 My_lily_lexer::push_note_state ()
601 yy_push_state (notes);
605 My_lily_lexer::push_figuredbass_state()
607 yy_push_state (figures);
610 My_lily_lexer::push_chord_state ()
612 yy_push_state (chords);
616 My_lily_lexer::push_lyric_state ()
618 yy_push_state (lyrics);
622 My_lily_lexer::push_markup_state ()
624 yy_push_state (markup);
628 My_lily_lexer::pop_state ()
634 My_lily_lexer::identifier_type(SCM sid)
636 int k = try_special_identifiers(&yylval.scm , sid);
637 return k >= 0 ? k : SCM_IDENTIFIER;
642 My_lily_lexer::scan_escaped_word (String str)
644 // use more SCM for this.
646 SCM sym = ly_symbol2scm (str.to_str0 ());
648 int l = lookup_keyword (str);
652 SCM sid = lookup_identifier (str);
653 if (sid != SCM_UNDEFINED)
656 return identifier_type (sid);
659 if ((YYSTATE != notes) && (YYSTATE != chords)) {
660 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
662 if (gh_pair_p (pitch))
664 yylval.scm = ly_cdr (pitch);
665 return NOTENAME_PITCH;
668 String msg (_f ("unknown escaped string: `\\%s'", str));
669 LexerError (msg.to_str0 ());
671 yylval.scm = scm_makfrom0str (str.to_str0 ());
677 My_lily_lexer::scan_bare_word (String str)
679 SCM sym = ly_symbol2scm (str.to_str0 ());
680 if ((YYSTATE == notes) || (YYSTATE == chords)) {
681 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
682 if (gh_pair_p (pitch)) {
683 yylval.scm = ly_cdr (pitch);
684 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
685 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
687 yylval.scm = ly_cdr (pitch);
688 return CHORD_MODIFIER;
692 yylval.scm = scm_makfrom0str (str.to_str0 ());
697 My_lily_lexer::note_state_b () const
699 return YY_START == notes;
703 My_lily_lexer::chord_state_b () const
705 return YY_START == chords;
709 My_lily_lexer::lyric_state_b () const
711 return YY_START == lyrics;
715 My_lily_lexer::figure_state_b () const
717 return YY_START == figures;
721 urg, belong to String (_convert)
722 and should be generalised
725 strip_leading_white (String&s)
728 for (; i < s.length (); i++)
732 s = s.nomid_string (0, i);
736 strip_trailing_white (String&s)
743 s = s.left_string (i+1);
748 /* 1.9.0 == postfix articulations */
749 Lilypond_version oldest_version ("1.9.0");
753 valid_version_b (String s)
755 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
756 Lilypond_version ver (s);
757 if (! ((ver >= oldest_version) && (ver <= current)))
759 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
760 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
771 lyric_fudge (String s)
773 char * chars =s.get_copy_str0 ();
775 for (char * p = chars; *p ; p++)
777 if (*p == '_' && (p == chars || *(p-1) != '\\'))
785 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
787 * (s.get_str0 () + i + 1) = 'c';
788 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
795 Convert "NUM/DEN" into a '(NUM . DEN) cons.
798 scan_fraction (String frac)
800 int i = frac.index ('/');
801 int l = frac.length ();
802 String left = frac.left_string (i);
803 String right = frac.right_string (l - i - 1);
805 int n = String_convert::dec2int (left);
806 int d = String_convert::dec2int (right);
807 return gh_cons (gh_int2scm (n), gh_int2scm (d));
810 // Breaks for flex 2.5.31
812 /* avoid silly flex induced gcc warnings */
813 static void yy_push_state (int) {;}
814 static void yy_pop_state () {;}
815 static int yy_top_state () { return 0; }
818 avoid_silly_flex_induced_gcc_warnings ()
820 (void)yy_start_stack_ptr;
821 (void)yy_start_stack_depth;
822 (void)yy_start_stack;
826 avoid_silly_flex_induced_gcc_warnings ();
831 lookup_markup_command (String s)
835 proc = scm_c_eval_string ("lookup-markup-command");
837 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));