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"
43 #include "musical-request.hh"
45 #include "lilypond-input-version.hh"
46 #include "translator-def.hh"
47 #include "music-output-def.hh"
52 #define isatty HORRIBLEKLUDGE
54 void strip_trailing_white (String&);
55 void strip_leading_white (String&);
56 String lyric_fudge (String s);
60 valid_version_b (String s);
64 #define start_quote() \
65 yy_push_state (quote);\
66 yylval.string = new String
71 #define YY_USER_ACTION add_lexed_char (YYLeng ());
74 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
79 SCM scan_fraction (String);
80 SCM (* scm_parse_error_handler) (void *);
89 %option yyclass="My_lily_lexer"
91 %option never-interactive
110 NATIONAL [\001-\006\021-\027\031\036\200-\377]
111 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
119 REAL ({INT}\.{N}*)|(-?\.{N}+)
122 HORIZONTALWHITE [ \t]
126 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
134 // windows-suck-suck-suck
137 <INITIAL,chords,incl,lyrics,notes,figures>{
139 yy_push_state (longcomment);
143 %[^{\n] { // backup rule
154 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
155 yy_push_state (version);
157 <version>\"[^"]*\" { /* got the version number */
158 String s (YYText ()+1);
159 s = s.left_string (s.index_last ('"'));
162 if (!valid_version_b (s))
166 LexerError ("No quoted string found after \\version");
179 LexerError (_ ("EOF found inside a comment").to_str0 ());
180 if (! close_input ())
181 yyterminate (); // can't move this, since it actually rets a YY_NULL
186 <INITIAL,chords,lyrics,notes,figures>\\maininput {
190 main_input_b_ = true;
193 error (_ ("\\maininput disallowed outside init files"));
196 <INITIAL,chords,lyrics,figures,notes>\\include {
197 yy_push_state (incl);
199 <incl>\"[^"]*\";? { /* got the include file name */
200 String s (YYText ()+1);
201 s = s.left_string (s.index_last ('"'));
203 new_input (s, &global_input_file->sources_ );
206 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
207 String s = YYText () + 1;
208 strip_trailing_white (s);
209 if (s.length () && (s[s.length () - 1] == ';'))
210 s = s.left_string (s.length () - 1);
212 SCM sid = lookup_identifier (s);
213 if (gh_string_p (sid)) {
214 new_input (ly_scm2string (sid), &global_input_file->sources_);
217 String msg (_f ("wrong or undefined identifier: `%s'", s ));
219 LexerError (msg.to_str0 ());
220 SCM err = scm_current_error_port ();
221 scm_puts ("This value was found in the table: ", err);
222 scm_display (sid, err);
225 <incl>\"[^"]* { // backup rule
226 error (_ ("Missing end quote"));
229 <chords,notes,figures>{RESTNAME} {
230 const char *s = YYText ();
231 yylval.scm = scm_makfrom0str (s);
234 <chords,notes,figures>R {
235 return MULTI_MEASURE_REST;
237 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}*{WHITE} {
238 String s=YYText () + 2;
239 s=s.left_string (s.length () - 1);
240 return scan_escaped_word (s);
242 <INITIAL,chords,lyrics,notes,figures>\${BLACK}*{WHITE} {
243 String s=YYText () + 1;
244 s=s.left_string (s.length () - 1);
245 return scan_bare_word (s);
247 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}* { // backup rule
248 error (_("white expected"));
251 <INITIAL,chords,lyrics,notes,figures>\${BLACK}* { // backup rule
252 error (_("white expected"));
256 <INITIAL,chords,lyrics,notes,figures># { //embedded scm
257 //char const* s = YYText () + 1;
258 char const* s = here_str0 ();
260 if (main_input_b_ && safe_global_b) {
261 error (_ ("Can't evaluate Scheme in safe mode"));
262 yylval.scm = SCM_EOL;
265 yylval.scm = ly_parse_scm (s, &n, here_input());
267 for (int i=0; i < n; i++)
271 char_count_stack_.top () += n;
288 return scan_bare_word (YYText ());
292 return scan_escaped_word (YYText () + 1);
295 yylval.scm = scan_fraction (YYText ());
300 yylval.i = String_convert::dec2int (String (YYText ()));
304 yylval.i = String_convert::dec2int (String (YYText ()));
308 yylval.i = String_convert::dec2int (String (YYText () +1));
322 *yylval.string += to_string (escaped_char (YYText ()[1]));
325 *yylval.string += YYText ();
331 /* yylval is union. Must remember STRING before setting SCM*/
332 String *sp = yylval.string;
333 yylval.scm = scm_makfrom0str (sp->to_str0 ());
338 *yylval.string += YYText ();
347 yylval.scm = scan_fraction (YYText ());
351 yylval.i = String_convert::dec2int (String (YYText ()));
355 return scan_escaped_word (YYText () + 1);
359 String s (YYText ());
361 return yylval.i = EXTENDER;
363 return yylval.i = HYPHEN;
366 char c = s[s.length () - 1];
367 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
368 here_input ().warning (
369 _ ("Brace found at end of lyric. Did you forget a space?"));
370 yylval.scm = scm_makfrom0str (s.to_str0 ());
381 return scan_bare_word (YYText ());
384 return scan_escaped_word (YYText () + 1);
387 yylval.scm = scan_fraction (YYText ());
391 yylval.i = String_convert::dec2int (String (YYText ()));
420 if (! close_input ()) {
421 yyterminate (); // can't move this, since it actually rets a YY_NULL
427 return scan_bare_word (YYText ());
430 return scan_escaped_word (YYText () + 1);
434 int cnv=sscanf (YYText (), "%lf", &r);
437 yylval.scm = gh_double2scm (r);
442 yylval.i = String_convert::dec2int (String (YYText ()));
452 char c = YYText ()[0];
457 <INITIAL,notes,figures>. {
461 <INITIAL,lyrics,notes,figures>\\. {
462 char c= YYText ()[1];
470 return E_EXCLAMATION;
478 return E_RIGHTSQUARE;
490 String msg = _f ("invalid character: `%c'", YYText ()[0]);
491 LexerError (msg.to_str0 ());
498 My_lily_lexer::push_note_state ()
500 yy_push_state (notes);
504 My_lily_lexer::push_figuredbass_state()
506 yy_push_state (figures);
509 My_lily_lexer::push_chord_state ()
511 yy_push_state (chords);
515 My_lily_lexer::push_lyric_state ()
517 yy_push_state (lyrics);
521 My_lily_lexer::pop_state ()
527 My_lily_lexer::scan_escaped_word (String str)
529 // use more SCM for this.
531 SCM sym = ly_symbol2scm (str.to_str0 ());
533 int l = lookup_keyword (str);
537 SCM sid = lookup_identifier (str);
538 if (gh_string_p (sid)) {
540 return STRING_IDENTIFIER;
541 } else if (gh_number_p (sid)) {
543 return NUMBER_IDENTIFIER;
544 } else if (unsmob_translator_def (sid)) {
546 return TRANSLATOR_IDENTIFIER;
547 } else if (unsmob_score (sid)) {
549 return SCORE_IDENTIFIER;
550 } else if (Music * mus =unsmob_music (sid)) {
553 return dynamic_cast<Request*> (mus) ? REQUEST_IDENTIFIER : MUSIC_IDENTIFIER;
554 } else if (unsmob_duration (sid)) {
556 return DURATION_IDENTIFIER;
557 } else if (unsmob_music_output_def (sid)) {
559 return MUSIC_OUTPUT_DEF_IDENTIFIER;
562 if (sid != SCM_UNDEFINED) {
564 return SCM_IDENTIFIER;
567 if ((YYSTATE != notes) && (YYSTATE != chords)) {
568 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
570 if (gh_pair_p (pitch))
572 yylval.scm = ly_cdr (pitch);
573 return NOTENAME_PITCH;
576 String msg (_f ("unknown escaped string: `\\%s'", str));
577 LexerError (msg.to_str0 ());
579 yylval.scm = scm_makfrom0str (str.to_str0 ());
585 My_lily_lexer::scan_bare_word (String str)
587 SCM sym = ly_symbol2scm (str.to_str0 ());
588 if ((YYSTATE == notes) || (YYSTATE == chords)) {
589 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
590 if (gh_pair_p (pitch)) {
591 yylval.scm = ly_cdr (pitch);
592 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
593 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
595 yylval.scm = ly_cdr (pitch);
596 return CHORDMODIFIER_PITCH;
600 yylval.scm = scm_makfrom0str (str.to_str0 ());
605 My_lily_lexer::note_state_b () const
607 return YY_START == notes;
611 My_lily_lexer::chord_state_b () const
613 return YY_START == chords;
617 My_lily_lexer::lyric_state_b () const
619 return YY_START == lyrics;
623 My_lily_lexer::figure_state_b () const
625 return YY_START == figures;
629 urg, belong to String (_convert)
630 and should be generalised
633 strip_leading_white (String&s)
636 for (; i < s.length (); i++)
640 s = s.nomid_string (0, i);
644 strip_trailing_white (String&s)
651 s = s.left_string (i+1);
656 /* 1.3.146 == removal of ; */
657 Lilypond_version oldest_version ("1.3.146");
661 valid_version_b (String s)
663 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
664 Lilypond_version ver (s);
665 if (! ((ver >= oldest_version) && (ver <= current)))
667 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.string (), oldest_version.string (), current.string ()));
668 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
676 lyric_fudge (String s)
678 char * chars =s.get_copy_str0 ();
680 for (char * p = chars; *p ; p++)
682 if (*p == '_' && (p == chars || *(p-1) != '\\'))
690 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
692 * (s.get_str0 () + i + 1) = 'c';
693 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
700 Convert "NUM/DEN" into a '(NUM . DEN) cons.
703 scan_fraction (String frac)
705 int i = frac.index ('/');
706 int l = frac.length ();
707 String left = frac.left_string (i);
708 String right = frac.right_string (l - i - 1);
710 int n = String_convert::dec2int (left);
711 int d = String_convert::dec2int (right);
712 return gh_cons (gh_int2scm (n), gh_int2scm (d));