1 %{ // -*-Fundamental-*-
3 lexer.l -- implement the Flex lexer
5 source file of the LilyPond music typesetter
7 (c) 1996--2000 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"
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"
45 #include "lilypond-input-version.hh"
46 #include "translator-def.hh"
47 #include "music-output-def.hh"
48 #include "identifier-smob.hh"
53 #define isatty HORRIBLEKLUDGE
55 void strip_trailing_white (String&);
56 void strip_leading_white (String&);
57 String lyric_fudge (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 *);
90 %option yyclass="My_lily_lexer"
92 %option never-interactive
111 NATIONAL [\001-\006\021-\027\031\036\200-\377]
112 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
120 REAL ({INT}\.{N}*)|(-?\.{N}+)
123 HORIZONTALWHITE [ \t]
127 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
135 // windows-suck-suck-suck
138 <INITIAL,chords,incl,lyrics,notes,figures>{
140 yy_push_state (longcomment);
144 %[^{\n] { // backup rule
155 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
156 yy_push_state (version);
158 <version>\"[^"]*\" { /* got the version number */
159 String s (YYText ()+1);
160 s = s.left_string (s.index_last ('"'));
163 if (!valid_version_b (s))
167 LexerError ("No quoted string found after \\version");
180 LexerError (_ ("EOF found inside a comment").to_str0 ());
181 if (! close_input ())
182 yyterminate (); // can't move this, since it actually rets a YY_NULL
187 <INITIAL,chords,lyrics,notes,figures>\\maininput {
191 main_input_b_ = true;
194 error (_ ("\\maininput disallowed outside init files"));
197 <INITIAL,chords,lyrics,figures,notes>\\include {
198 yy_push_state (incl);
200 <incl>\"[^"]*\";? { /* got the include file name */
201 String s (YYText ()+1);
202 s = s.left_string (s.index_last ('"'));
204 new_input (s, &global_input_file->sources_ );
207 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
208 String s = YYText () + 1;
209 strip_trailing_white (s);
210 if (s.length () && (s[s.length () - 1] == ';'))
211 s = s.left_string (s.length () - 1);
213 SCM sid = lookup_identifier (s);
214 if (gh_string_p (sid)) {
215 new_input (ly_scm2string (sid), &global_input_file->sources_);
218 String msg (_f ("wrong or undefined identifier: `%s'", s ));
220 LexerError (msg.to_str0 ());
221 SCM err = scm_current_error_port ();
222 scm_puts ("This value was found in the table: ", err);
223 scm_display (sid, err);
226 <incl>\"[^"]* { // backup rule
227 error (_ ("Missing end quote"));
230 <chords,notes,figures>{RESTNAME} {
231 const char *s = YYText ();
232 yylval.scm = scm_makfrom0str (s);
235 <chords,notes,figures>R {
236 return MULTI_MEASURE_REST;
238 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}*{WHITE} {
239 String s=YYText () + 2;
240 s=s.left_string (s.length () - 1);
241 return scan_escaped_word (s);
243 <INITIAL,chords,lyrics,notes,figures>\${BLACK}*{WHITE} {
244 String s=YYText () + 1;
245 s=s.left_string (s.length () - 1);
246 return scan_bare_word (s);
248 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}* { // backup rule
249 error (_("white expected"));
252 <INITIAL,chords,lyrics,notes,figures>\${BLACK}* { // backup rule
253 error (_("white expected"));
257 <INITIAL,chords,lyrics,notes,figures># { //embedded scm
258 //char const* s = YYText () + 1;
259 char const* s = here_str0 ();
261 if (main_input_b_ && safe_global_b) {
262 error (_ ("Can't evaluate Scheme in safe mode"));
263 yylval.scm = SCM_EOL;
266 SCM sval = ly_parse_scm (s, &n, here_input());
268 for (int i=0; i < n; i++)
272 char_count_stack_.top () += n;
274 if (unpack_identifier (sval) != SCM_UNDEFINED)
276 yylval.scm = unpack_identifier(sval);
277 return identifier_type (yylval.scm);
296 return scan_bare_word (YYText ());
300 return scan_escaped_word (YYText () + 1);
303 yylval.scm = scan_fraction (YYText ());
308 yylval.i = String_convert::dec2int (String (YYText ()));
312 yylval.i = String_convert::dec2int (String (YYText ()));
316 yylval.i = String_convert::dec2int (String (YYText () +1));
330 *yylval.string += to_string (escaped_char (YYText ()[1]));
333 *yylval.string += YYText ();
339 /* yylval is union. Must remember STRING before setting SCM*/
340 String *sp = yylval.string;
341 yylval.scm = scm_makfrom0str (sp->to_str0 ());
346 *yylval.string += YYText ();
355 yylval.scm = scan_fraction (YYText ());
359 yylval.i = String_convert::dec2int (String (YYText ()));
363 return scan_escaped_word (YYText () + 1);
367 String s (YYText ());
369 return yylval.i = EXTENDER;
371 return yylval.i = HYPHEN;
374 char c = s[s.length () - 1];
375 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
376 here_input ().warning (
377 _ ("Brace found at end of lyric. Did you forget a space?"));
378 yylval.scm = scm_makfrom0str (s.to_str0 ());
389 return scan_bare_word (YYText ());
392 return scan_escaped_word (YYText () + 1);
395 yylval.scm = scan_fraction (YYText ());
399 yylval.i = String_convert::dec2int (String (YYText ()));
428 if (! close_input ()) {
429 yyterminate (); // can't move this, since it actually rets a YY_NULL
435 return scan_bare_word (YYText ());
438 return scan_escaped_word (YYText () + 1);
442 int cnv=sscanf (YYText (), "%lf", &r);
445 yylval.scm = gh_double2scm (r);
450 yylval.i = String_convert::dec2int (String (YYText ()));
460 char c = YYText ()[0];
465 <INITIAL,notes,figures>. {
469 <INITIAL,lyrics,notes,figures>\\. {
470 char c= YYText ()[1];
478 return E_EXCLAMATION;
486 return E_RIGHTSQUARE;
498 String msg = _f ("invalid character: `%c'", YYText ()[0]);
499 LexerError (msg.to_str0 ());
506 My_lily_lexer::push_note_state ()
508 yy_push_state (notes);
512 My_lily_lexer::push_figuredbass_state()
514 yy_push_state (figures);
517 My_lily_lexer::push_chord_state ()
519 yy_push_state (chords);
523 My_lily_lexer::push_lyric_state ()
525 yy_push_state (lyrics);
529 My_lily_lexer::pop_state ()
535 My_lily_lexer::identifier_type(SCM sid)
537 if (gh_string_p (sid)) {
539 return STRING_IDENTIFIER;
540 } else if (gh_number_p (sid)) {
542 return NUMBER_IDENTIFIER;
543 } else if (unsmob_translator_def (sid)) {
545 return TRANSLATOR_IDENTIFIER;
546 } else if (unsmob_score (sid)) {
548 return SCORE_IDENTIFIER;
549 } else if (Music * mus =unsmob_music (sid)) {
552 return dynamic_cast<Event*> (mus) ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
553 } else if (unsmob_duration (sid)) {
555 return DURATION_IDENTIFIER;
556 } else if (unsmob_music_output_def (sid)) {
558 return MUSIC_OUTPUT_DEF_IDENTIFIER;
560 return SCM_IDENTIFIER;
565 My_lily_lexer::scan_escaped_word (String str)
567 // use more SCM for this.
569 SCM sym = ly_symbol2scm (str.to_str0 ());
571 int l = lookup_keyword (str);
575 SCM sid = lookup_identifier (str);
576 if (sid != SCM_UNDEFINED)
579 return identifier_type (sid);
582 if ((YYSTATE != notes) && (YYSTATE != chords)) {
583 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
585 if (gh_pair_p (pitch))
587 yylval.scm = ly_cdr (pitch);
588 return NOTENAME_PITCH;
591 String msg (_f ("unknown escaped string: `\\%s'", str));
592 LexerError (msg.to_str0 ());
594 yylval.scm = scm_makfrom0str (str.to_str0 ());
600 My_lily_lexer::scan_bare_word (String str)
602 SCM sym = ly_symbol2scm (str.to_str0 ());
603 if ((YYSTATE == notes) || (YYSTATE == chords)) {
604 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
605 if (gh_pair_p (pitch)) {
606 yylval.scm = ly_cdr (pitch);
607 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
608 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
610 yylval.scm = ly_cdr (pitch);
611 return CHORDMODIFIER_PITCH;
615 yylval.scm = scm_makfrom0str (str.to_str0 ());
620 My_lily_lexer::note_state_b () const
622 return YY_START == notes;
626 My_lily_lexer::chord_state_b () const
628 return YY_START == chords;
632 My_lily_lexer::lyric_state_b () const
634 return YY_START == lyrics;
638 My_lily_lexer::figure_state_b () const
640 return YY_START == figures;
644 urg, belong to String (_convert)
645 and should be generalised
648 strip_leading_white (String&s)
651 for (; i < s.length (); i++)
655 s = s.nomid_string (0, i);
659 strip_trailing_white (String&s)
666 s = s.left_string (i+1);
671 /* 1.3.146 == removal of ; */
672 Lilypond_version oldest_version ("1.3.146");
676 valid_version_b (String s)
678 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
679 Lilypond_version ver (s);
680 if (! ((ver >= oldest_version) && (ver <= current)))
682 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.string (), oldest_version.string (), current.string ()));
683 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
691 lyric_fudge (String s)
693 char * chars =s.get_copy_str0 ();
695 for (char * p = chars; *p ; p++)
697 if (*p == '_' && (p == chars || *(p-1) != '\\'))
705 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
707 * (s.get_str0 () + i + 1) = 'c';
708 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
715 Convert "NUM/DEN" into a '(NUM . DEN) cons.
718 scan_fraction (String frac)
720 int i = frac.index ('/');
721 int l = frac.length ();
722 String left = frac.left_string (i);
723 String right = frac.right_string (l - i - 1);
725 int n = String_convert::dec2int (left);
726 int d = String_convert::dec2int (right);
727 return gh_cons (gh_int2scm (n), gh_int2scm (d));