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\r\f]*
143 // windows-suck-suck-suck
146 <INITIAL,chords,incl,markup,lyrics,notes,figures>{
148 yy_push_state (longcomment);
152 %[^{\n\r] { // 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;
183 scm_module_define (gh_car (scopes_),
184 ly_symbol2scm ("input-file-name"),
185 scm_makfrom0str (s.to_str0()));
190 LexerError ("No quoted string found after \\version");
194 LexerError ("No quoted string found after \\renameinput");
207 LexerError (_ ("EOF found inside a comment").to_str0 ());
208 if (! close_input ())
209 yyterminate (); // can't move this, since it actually rets a YY_NULL
214 <INITIAL,chords,lyrics,notes,figures>\\maininput {
218 main_input_b_ = true;
221 error (_ ("\\maininput disallowed outside init files"));
224 <INITIAL,chords,lyrics,figures,notes>\\include {
225 yy_push_state (incl);
227 <incl>\"[^"]*\";? { /* got the include file name */
228 String s (YYText ()+1);
229 s = s.left_string (s.index_last ('"'));
231 new_input (s, &global_input_file->sources_ );
234 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
235 String s = YYText () + 1;
236 strip_trailing_white (s);
237 if (s.length () && (s[s.length () - 1] == ';'))
238 s = s.left_string (s.length () - 1);
240 SCM sid = lookup_identifier (s);
241 if (gh_string_p (sid)) {
242 new_input (ly_scm2string (sid), &global_input_file->sources_);
245 String msg (_f ("wrong or undefined identifier: `%s'", s ));
247 LexerError (msg.to_str0 ());
248 SCM err = scm_current_error_port ();
249 scm_puts ("This value was found in the table: ", err);
250 scm_display (sid, err);
253 <incl>\"[^"]* { // backup rule
254 error (_ ("Missing end quote"));
257 <chords,notes,figures>{RESTNAME} {
258 const char *s = YYText ();
259 yylval.scm = scm_makfrom0str (s);
262 <chords,notes,figures>R {
263 return MULTI_MEASURE_REST;
265 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}*{WHITE} {
266 String s=YYText () + 2;
267 s=s.left_string (s.length () - 1);
268 return scan_escaped_word (s);
270 <INITIAL,chords,lyrics,notes,figures>\${BLACK}*{WHITE} {
271 String s=YYText () + 1;
272 s=s.left_string (s.length () - 1);
273 return scan_bare_word (s);
275 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}* { // backup rule
276 error (_("white expected"));
279 <INITIAL,chords,lyrics,notes,figures>\${BLACK}* { // backup rule
280 error (_("white expected"));
284 <INITIAL,markup,chords,lyrics,notes,figures># { //embedded scm
285 //char const* s = YYText () + 1;
286 char const* s = here_str0 ();
288 if (main_input_b_ && safe_global_b) {
289 error (_ ("Can't evaluate Scheme in safe mode"));
290 yylval.scm = SCM_EOL;
293 SCM sval = ly_parse_scm (s, &n, here_input());
294 if (sval == SCM_UNDEFINED)
296 sval = SCM_UNSPECIFIED;
300 for (int i=0; i < n; i++)
304 char_count_stack_.top () += n;
306 if (unpack_identifier (sval) != SCM_UNDEFINED)
308 yylval.scm = unpack_identifier(sval);
309 return identifier_type (yylval.scm);
315 <INITIAL,notes,lyrics>{
337 return scan_bare_word (YYText ());
341 return scan_escaped_word (YYText () + 1);
344 yylval.scm = scan_fraction (YYText ());
349 yylval.i = String_convert::dec2int (String (YYText ()));
353 yylval.i = String_convert::dec2int (String (YYText ()));
357 yylval.i = String_convert::dec2int (String (YYText () +1));
371 *yylval.string += to_string (escaped_char (YYText ()[1]));
374 *yylval.string += YYText ();
380 /* yylval is union. Must remember STRING before setting SCM*/
381 String *sp = yylval.string;
382 yylval.scm = scm_makfrom0str (sp->to_str0 ());
387 *yylval.string += YYText ();
396 yylval.scm = scan_fraction (YYText ());
400 yylval.i = String_convert::dec2int (String (YYText ()));
404 return scan_escaped_word (YYText () + 1);
408 String s (YYText ());
410 return yylval.i = EXTENDER;
412 return yylval.i = HYPHEN;
415 char c = s[s.length () - 1];
416 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
417 here_input ().warning (
418 _ ("Brace found at end of lyric. Did you forget a space?"));
419 yylval.scm = scm_makfrom0str (s.to_str0 ());
430 return scan_bare_word (YYText ());
433 return scan_escaped_word (YYText () + 1);
436 yylval.scm = scan_fraction (YYText ());
440 yylval.i = String_convert::dec2int (String (YYText ()));
478 String str (YYText() + 1);
479 SCM s = lookup_markup_command (str);
481 if (gh_pair_p (s) && gh_symbol_p (gh_cdr (s)) ) {
482 yylval.scm = gh_car(s);
484 if (tag == ly_symbol2scm("markup0"))
485 return MARKUP_HEAD_MARKUP0;
486 else if (tag == ly_symbol2scm ("markup0-markup1"))
487 return MARKUP_HEAD_MARKUP0_MARKUP1;
488 else if (tag == ly_symbol2scm ("markup-list0"))
489 return MARKUP_HEAD_LIST0;
490 else if (tag == ly_symbol2scm ("scheme0"))
491 return MARKUP_HEAD_SCM0;
492 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
493 return MARKUP_HEAD_SCM0_SCM1;
494 else if (tag == ly_symbol2scm ("scheme0-markup1"))
495 return MARKUP_HEAD_SCM0_MARKUP1;
496 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
497 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
498 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
499 return MARKUP_HEAD_SCM0_SCM1_SCM2;
501 programming_error ("No parser tag defined for this signature. Abort");
506 return scan_escaped_word (str);
511 [^#{}"\\ \t\n\r\f]+ {
512 String s (YYText ());
514 char c = s[s.length () - 1];
515 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
516 here_input ().warning (
517 _ ("Brace found at end of markup. Did you forget a space?"));
518 yylval.scm = scm_makfrom0str (s.to_str0 ());
531 if (! close_input ()) {
532 yyterminate (); // can't move this, since it actually rets a YY_NULL
538 return scan_bare_word (YYText ());
541 return scan_escaped_word (YYText () + 1);
545 int cnv=sscanf (YYText (), "%lf", &r);
548 yylval.scm = gh_double2scm (r);
553 yylval.i = String_convert::dec2int (String (YYText ()));
563 char c = YYText ()[0];
568 <INITIAL,notes,figures>. {
572 <INITIAL,lyrics,notes,figures>\\. {
573 char c= YYText ()[1];
581 return E_EXCLAMATION;
589 return E_RIGHTSQUARE;
601 String msg = _f ("invalid character: `%c'", YYText ()[0]);
602 LexerError (msg.to_str0 ());
609 My_lily_lexer::push_note_state ()
611 yy_push_state (notes);
615 My_lily_lexer::push_figuredbass_state()
617 yy_push_state (figures);
620 My_lily_lexer::push_chord_state ()
622 yy_push_state (chords);
626 My_lily_lexer::push_lyric_state ()
628 yy_push_state (lyrics);
632 My_lily_lexer::push_markup_state ()
634 yy_push_state (markup);
638 My_lily_lexer::pop_state ()
644 My_lily_lexer::identifier_type(SCM sid)
646 int k = try_special_identifiers(&yylval.scm , sid);
647 return k >= 0 ? k : SCM_IDENTIFIER;
652 My_lily_lexer::scan_escaped_word (String str)
654 // use more SCM for this.
656 SCM sym = ly_symbol2scm (str.to_str0 ());
658 int l = lookup_keyword (str);
662 SCM sid = lookup_identifier (str);
663 if (sid != SCM_UNDEFINED)
666 return identifier_type (sid);
669 if ((YYSTATE != notes) && (YYSTATE != chords)) {
670 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
672 if (gh_pair_p (pitch))
674 yylval.scm = ly_cdr (pitch);
675 return NOTENAME_PITCH;
678 String msg (_f ("unknown escaped string: `\\%s'", str));
679 LexerError (msg.to_str0 ());
681 yylval.scm = scm_makfrom0str (str.to_str0 ());
687 My_lily_lexer::scan_bare_word (String str)
689 SCM sym = ly_symbol2scm (str.to_str0 ());
690 if ((YYSTATE == notes) || (YYSTATE == chords)) {
691 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
692 if (gh_pair_p (pitch)) {
693 yylval.scm = ly_cdr (pitch);
694 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
695 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
697 yylval.scm = ly_cdr (pitch);
698 return CHORD_MODIFIER;
702 yylval.scm = scm_makfrom0str (str.to_str0 ());
707 My_lily_lexer::note_state_b () const
709 return YY_START == notes;
713 My_lily_lexer::chord_state_b () const
715 return YY_START == chords;
719 My_lily_lexer::lyric_state_b () const
721 return YY_START == lyrics;
725 My_lily_lexer::figure_state_b () const
727 return YY_START == figures;
731 urg, belong to String (_convert)
732 and should be generalised
735 strip_leading_white (String&s)
738 for (; i < s.length (); i++)
742 s = s.nomid_string (0, i);
746 strip_trailing_white (String&s)
753 s = s.left_string (i+1);
758 /* 1.9.0 == postfix articulations */
759 Lilypond_version oldest_version ("1.9.0");
763 valid_version_b (String s)
765 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
766 Lilypond_version ver (s);
767 if (! ((ver >= oldest_version) && (ver <= current)))
769 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
770 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
781 lyric_fudge (String s)
783 char * chars =s.get_copy_str0 ();
785 for (char * p = chars; *p ; p++)
787 if (*p == '_' && (p == chars || *(p-1) != '\\'))
795 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
797 * (s.get_str0 () + i + 1) = 'c';
798 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
805 Convert "NUM/DEN" into a '(NUM . DEN) cons.
808 scan_fraction (String frac)
810 int i = frac.index ('/');
811 int l = frac.length ();
812 String left = frac.left_string (i);
813 String right = frac.right_string (l - i - 1);
815 int n = String_convert::dec2int (left);
816 int d = String_convert::dec2int (right);
817 return gh_cons (gh_int2scm (n), gh_int2scm (d));
820 // Breaks for flex 2.5.31
822 /* avoid silly flex induced gcc warnings */
823 static void yy_push_state (int) {;}
824 static void yy_pop_state () {;}
825 static int yy_top_state () { return 0; }
828 avoid_silly_flex_induced_gcc_warnings ()
830 (void)yy_start_stack_ptr;
831 (void)yy_start_stack_depth;
832 (void)yy_start_stack;
836 avoid_silly_flex_induced_gcc_warnings ();
841 lookup_markup_command (String s)
845 proc = scm_c_eval_string ("lookup-markup-command");
847 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));