1 %{ // -*-Fundamental-*-
3 lexer.l -- implement the Flex lexer
5 source file of the LilyPond music typesetter
7 (c) 1996--2002 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 :-)
31 #include "parse-scm.hh"
32 #include "lily-guile.hh"
34 #include "string-convert.hh"
35 #include "my-lily-lexer.hh"
36 #include "input-file-results.hh"
37 #include "interval.hh"
38 #include "lily-guile.hh"
43 #include "lilypond-input-version.hh"
44 #include "translator-def.hh"
45 #include "identifier-smob.hh"
50 #define isatty HORRIBLEKLUDGE
52 void strip_trailing_white (String&);
53 void strip_leading_white (String&);
54 String lyric_fudge (String s);
58 valid_version_b (String s);
62 #define start_quote() \
63 yy_push_state (quote);\
64 yylval.string = new String
69 #define YY_USER_ACTION add_lexed_char (YYLeng ());
72 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
77 SCM scan_fraction (String);
78 SCM (* scm_parse_error_handler) (void *);
87 %option yyclass="My_lily_lexer"
89 %option never-interactive
108 NATIONAL [\001-\006\021-\027\031\036\200-\377]
109 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
117 REAL ({INT}\.{N}*)|(-?\.{N}+)
120 HORIZONTALWHITE [ \t]
124 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
132 // windows-suck-suck-suck
135 <INITIAL,chords,incl,lyrics,notes,figures>{
137 yy_push_state (longcomment);
141 %[^{\n] { // backup rule
152 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
153 yy_push_state (version);
155 <version>\"[^"]*\" { /* got the version number */
156 String s (YYText ()+1);
157 s = s.left_string (s.index_last ('\"'));
160 if (!valid_version_b (s))
164 LexerError ("No quoted string found after \\version");
177 LexerError (_ ("EOF found inside a comment").to_str0 ());
178 if (! close_input ())
179 yyterminate (); // can't move this, since it actually rets a YY_NULL
184 <INITIAL,chords,lyrics,notes,figures>\\maininput {
188 main_input_b_ = true;
191 error (_ ("\\maininput disallowed outside init files"));
194 <INITIAL,chords,lyrics,figures,notes>\\include {
195 yy_push_state (incl);
197 <incl>\"[^"]*\";? { /* got the include file name */
198 String s (YYText ()+1);
199 s = s.left_string (s.index_last ('"'));
201 new_input (s, &global_input_file->sources_ );
204 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
205 String s = YYText () + 1;
206 strip_trailing_white (s);
207 if (s.length () && (s[s.length () - 1] == ';'))
208 s = s.left_string (s.length () - 1);
210 SCM sid = lookup_identifier (s);
211 if (gh_string_p (sid)) {
212 new_input (ly_scm2string (sid), &global_input_file->sources_);
215 String msg (_f ("wrong or undefined identifier: `%s'", s ));
217 LexerError (msg.to_str0 ());
218 SCM err = scm_current_error_port ();
219 scm_puts ("This value was found in the table: ", err);
220 scm_display (sid, err);
223 <incl>\"[^"]* { // backup rule
224 error (_ ("Missing end quote"));
227 <chords,notes,figures>{RESTNAME} {
228 const char *s = YYText ();
229 yylval.scm = scm_makfrom0str (s);
232 <chords,notes,figures>R {
233 return MULTI_MEASURE_REST;
235 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}*{WHITE} {
236 String s=YYText () + 2;
237 s=s.left_string (s.length () - 1);
238 return scan_escaped_word (s);
240 <INITIAL,chords,lyrics,notes,figures>\${BLACK}*{WHITE} {
241 String s=YYText () + 1;
242 s=s.left_string (s.length () - 1);
243 return scan_bare_word (s);
245 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}* { // backup rule
246 error (_("white expected"));
249 <INITIAL,chords,lyrics,notes,figures>\${BLACK}* { // backup rule
250 error (_("white expected"));
254 <INITIAL,chords,lyrics,notes,figures># { //embedded scm
255 //char const* s = YYText () + 1;
256 char const* s = here_str0 ();
258 if (main_input_b_ && safe_global_b) {
259 error (_ ("Can't evaluate Scheme in safe mode"));
260 yylval.scm = SCM_EOL;
263 SCM sval = ly_parse_scm (s, &n, here_input());
265 for (int i=0; i < n; i++)
269 char_count_stack_.top () += n;
271 if (unpack_identifier (sval) != SCM_UNDEFINED)
273 yylval.scm = unpack_identifier(sval);
274 return identifier_type (yylval.scm);
302 return scan_bare_word (YYText ());
306 return scan_escaped_word (YYText () + 1);
309 yylval.scm = scan_fraction (YYText ());
314 yylval.i = String_convert::dec2int (String (YYText ()));
318 yylval.i = String_convert::dec2int (String (YYText ()));
322 yylval.i = String_convert::dec2int (String (YYText () +1));
336 *yylval.string += to_string (escaped_char (YYText ()[1]));
339 *yylval.string += YYText ();
345 /* yylval is union. Must remember STRING before setting SCM*/
346 String *sp = yylval.string;
347 yylval.scm = scm_makfrom0str (sp->to_str0 ());
352 *yylval.string += YYText ();
361 yylval.scm = scan_fraction (YYText ());
365 yylval.i = String_convert::dec2int (String (YYText ()));
369 return scan_escaped_word (YYText () + 1);
373 String s (YYText ());
375 return yylval.i = EXTENDER;
377 return yylval.i = HYPHEN;
380 char c = s[s.length () - 1];
381 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
382 here_input ().warning (
383 _ ("Brace found at end of lyric. Did you forget a space?"));
384 yylval.scm = scm_makfrom0str (s.to_str0 ());
395 return scan_bare_word (YYText ());
398 return scan_escaped_word (YYText () + 1);
401 yylval.scm = scan_fraction (YYText ());
405 yylval.i = String_convert::dec2int (String (YYText ()));
437 if (! close_input ()) {
438 yyterminate (); // can't move this, since it actually rets a YY_NULL
444 return scan_bare_word (YYText ());
447 return scan_escaped_word (YYText () + 1);
451 int cnv=sscanf (YYText (), "%lf", &r);
454 yylval.scm = gh_double2scm (r);
459 yylval.i = String_convert::dec2int (String (YYText ()));
469 char c = YYText ()[0];
474 <INITIAL,notes,figures>. {
478 <INITIAL,lyrics,notes,figures>\\. {
479 char c= YYText ()[1];
487 return E_EXCLAMATION;
495 return E_RIGHTSQUARE;
507 String msg = _f ("invalid character: `%c'", YYText ()[0]);
508 LexerError (msg.to_str0 ());
515 My_lily_lexer::push_note_state ()
517 yy_push_state (notes);
521 My_lily_lexer::push_figuredbass_state()
523 yy_push_state (figures);
526 My_lily_lexer::push_chord_state ()
528 yy_push_state (chords);
532 My_lily_lexer::push_lyric_state ()
534 yy_push_state (lyrics);
538 My_lily_lexer::pop_state ()
544 My_lily_lexer::identifier_type(SCM sid)
546 int k = try_special_identifiers(&yylval.scm , sid);
547 return k >= 0 ? k : SCM_IDENTIFIER;
552 My_lily_lexer::scan_escaped_word (String str)
554 // use more SCM for this.
556 SCM sym = ly_symbol2scm (str.to_str0 ());
558 int l = lookup_keyword (str);
562 SCM sid = lookup_identifier (str);
563 if (sid != SCM_UNDEFINED)
566 return identifier_type (sid);
569 if ((YYSTATE != notes) && (YYSTATE != chords)) {
570 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
572 if (gh_pair_p (pitch))
574 yylval.scm = ly_cdr (pitch);
575 return NOTENAME_PITCH;
578 String msg (_f ("unknown escaped string: `\\%s'", str));
579 LexerError (msg.to_str0 ());
581 yylval.scm = scm_makfrom0str (str.to_str0 ());
587 My_lily_lexer::scan_bare_word (String str)
589 SCM sym = ly_symbol2scm (str.to_str0 ());
590 if ((YYSTATE == notes) || (YYSTATE == chords)) {
591 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
592 if (gh_pair_p (pitch)) {
593 yylval.scm = ly_cdr (pitch);
594 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
595 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
597 yylval.scm = ly_cdr (pitch);
598 return CHORDMODIFIER_PITCH;
602 yylval.scm = scm_makfrom0str (str.to_str0 ());
607 My_lily_lexer::note_state_b () const
609 return YY_START == notes;
613 My_lily_lexer::chord_state_b () const
615 return YY_START == chords;
619 My_lily_lexer::lyric_state_b () const
621 return YY_START == lyrics;
625 My_lily_lexer::figure_state_b () const
627 return YY_START == figures;
631 urg, belong to String (_convert)
632 and should be generalised
635 strip_leading_white (String&s)
638 for (; i < s.length (); i++)
642 s = s.nomid_string (0, i);
646 strip_trailing_white (String&s)
653 s = s.left_string (i+1);
658 /* 1.3.146 == removal of ; */
659 Lilypond_version oldest_version ("1.3.146");
663 valid_version_b (String s)
665 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
666 Lilypond_version ver (s);
667 if (! ((ver >= oldest_version) && (ver <= current)))
669 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.string (), oldest_version.string (), current.string ()));
670 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
678 lyric_fudge (String s)
680 char * chars =s.get_copy_str0 ();
682 for (char * p = chars; *p ; p++)
684 if (*p == '_' && (p == chars || *(p-1) != '\\'))
692 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
694 * (s.get_str0 () + i + 1) = 'c';
695 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
702 Convert "NUM/DEN" into a '(NUM . DEN) cons.
705 scan_fraction (String frac)
707 int i = frac.index ('/');
708 int l = frac.length ();
709 String left = frac.left_string (i);
710 String right = frac.right_string (l - i - 1);
712 int n = String_convert::dec2int (left);
713 int d = String_convert::dec2int (right);
714 return gh_cons (gh_int2scm (n), gh_int2scm (d));
717 /* avoid silly flex induced gcc warnings */
718 static void yy_push_state (int) {;}
719 static void yy_pop_state () {;}
720 static int yy_top_state () { return 0; }
723 avoid_silly_flex_induced_gcc_warnings ()
725 (void)yy_start_stack_ptr;
726 (void)yy_start_stack_depth;
727 (void)yy_start_stack;
731 avoid_silly_flex_induced_gcc_warnings ();