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}
118 REAL ({INT}\.{N}*)|(-?\.{N}+)
121 HORIZONTALWHITE [ \t]
125 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
133 // windows-suck-suck-suck
136 <INITIAL,chords,incl,lyrics,notes,figures>{
138 yy_push_state (longcomment);
142 %[^{\n] { // backup rule
153 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
154 yy_push_state (version);
156 <version>\"[^"]*\" { /* got the version number */
157 String s (YYText ()+1);
158 s = s.left_string (s.index_last ('"'));
161 if (!valid_version_b (s))
165 LexerError ("No quoted string found after \\version");
178 LexerError (_ ("EOF found inside a comment").to_str0 ());
179 if (! close_input ())
180 yyterminate (); // can't move this, since it actually rets a YY_NULL
185 <INITIAL,chords,lyrics,notes,figures>\\maininput {
189 main_input_b_ = true;
192 error (_ ("\\maininput disallowed outside init files"));
195 <INITIAL,chords,lyrics,figures,notes>\\include {
196 yy_push_state (incl);
198 <incl>\"[^"]*\";? { /* got the include file name */
199 String s (YYText ()+1);
200 s = s.left_string (s.index_last ('"'));
202 new_input (s, &global_input_file->sources_ );
205 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
206 String s = YYText () + 1;
207 strip_trailing_white (s);
208 if (s.length () && (s[s.length () - 1] == ';'))
209 s = s.left_string (s.length () - 1);
211 SCM sid = lookup_identifier (s);
212 if (gh_string_p (sid)) {
213 new_input (ly_scm2string (sid), &global_input_file->sources_);
216 String msg (_f ("wrong or undefined identifier: `%s'", s ));
218 LexerError (msg.to_str0 ());
219 SCM err = scm_current_error_port ();
220 scm_puts ("This value was found in the table: ", err);
221 scm_display (sid, err);
224 <incl>\"[^"]* { // backup rule
225 error (_ ("Missing end quote"));
228 <chords,notes,figures>{RESTNAME} {
229 const char *s = YYText ();
230 yylval.scm = scm_makfrom0str (s);
233 <chords,notes,figures>R {
234 return MULTI_MEASURE_REST;
236 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}*{WHITE} {
237 String s=YYText () + 2;
238 s=s.left_string (s.length () - 1);
239 return scan_escaped_word (s);
241 <INITIAL,chords,lyrics,notes,figures>\${BLACK}*{WHITE} {
242 String s=YYText () + 1;
243 s=s.left_string (s.length () - 1);
244 return scan_bare_word (s);
246 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}* { // backup rule
247 error (_("white expected"));
250 <INITIAL,chords,lyrics,notes,figures>\${BLACK}* { // backup rule
251 error (_("white expected"));
255 <INITIAL,chords,lyrics,notes,figures># { //embedded scm
256 //char const* s = YYText () + 1;
257 char const* s = here_str0 ();
259 if (main_input_b_ && safe_global_b) {
260 error (_ ("Can't evaluate Scheme in safe mode"));
261 yylval.scm = SCM_EOL;
264 yylval.scm = ly_parse_scm (s, &n, here_input());
266 for (int i=0; i < n; i++)
270 char_count_stack_.top () += n;
287 return scan_bare_word (YYText ());
291 return scan_escaped_word (YYText () + 1);
294 yylval.scm = scan_fraction (YYText ());
299 yylval.i = String_convert::dec2int (String (YYText ()));
303 yylval.i = String_convert::dec2int (String (YYText ()));
317 *yylval.string += to_string (escaped_char (YYText ()[1]));
320 *yylval.string += YYText ();
326 /* yylval is union. Must remember STRING before setting SCM*/
327 String *sp = yylval.string;
328 yylval.scm = scm_makfrom0str (sp->to_str0 ());
333 *yylval.string += YYText ();
342 yylval.scm = scan_fraction (YYText ());
346 yylval.i = String_convert::dec2int (String (YYText ()));
350 return scan_escaped_word (YYText () + 1);
354 String s (YYText ());
356 return yylval.i = EXTENDER;
358 return yylval.i = HYPHEN;
361 char c = s[s.length () - 1];
362 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
363 here_input ().warning (
364 _ ("Brace found at end of lyric. Did you forget a space?"));
365 yylval.scm = scm_makfrom0str (s.to_str0 ());
376 return scan_bare_word (YYText ());
379 return scan_escaped_word (YYText () + 1);
382 yylval.scm = scan_fraction (YYText ());
386 yylval.i = String_convert::dec2int (String (YYText ()));
412 if (! close_input ()) {
413 yyterminate (); // can't move this, since it actually rets a YY_NULL
419 return scan_bare_word (YYText ());
422 return scan_escaped_word (YYText () + 1);
426 int cnv=sscanf (YYText (), "%lf", &r);
429 yylval.scm = gh_double2scm (r);
434 yylval.i = String_convert::dec2int (String (YYText ()));
443 char c = YYText ()[0];
448 <INITIAL,notes,figures>. {
452 <INITIAL,lyrics,notes,figures>\\. {
453 char c= YYText ()[1];
461 return E_EXCLAMATION;
469 return E_RIGHTSQUARE;
480 String msg = _f ("invalid character: `%c'", YYText ()[0]);
481 LexerError (msg.to_str0 ());
488 My_lily_lexer::push_note_state ()
490 yy_push_state (notes);
494 My_lily_lexer::push_figuredbass_state()
496 yy_push_state (figures);
499 My_lily_lexer::push_chord_state ()
501 yy_push_state (chords);
505 My_lily_lexer::push_lyric_state ()
507 yy_push_state (lyrics);
511 My_lily_lexer::pop_state ()
517 My_lily_lexer::scan_escaped_word (String str)
519 // use more SCM for this.
521 SCM sym = ly_symbol2scm (str.to_str0 ());
523 int l = lookup_keyword (str);
527 SCM sid = lookup_identifier (str);
528 if (gh_string_p (sid)) {
530 return STRING_IDENTIFIER;
531 } else if (gh_number_p (sid)) {
533 return NUMBER_IDENTIFIER;
534 } else if (unsmob_translator_def (sid)) {
536 return TRANSLATOR_IDENTIFIER;
537 } else if (unsmob_score (sid)) {
539 return SCORE_IDENTIFIER;
540 } else if (Music * mus =unsmob_music (sid)) {
543 return dynamic_cast<Request*> (mus) ? REQUEST_IDENTIFIER : MUSIC_IDENTIFIER;
544 } else if (unsmob_duration (sid)) {
546 return DURATION_IDENTIFIER;
547 } else if (unsmob_music_output_def (sid)) {
549 return MUSIC_OUTPUT_DEF_IDENTIFIER;
552 if (sid != SCM_UNDEFINED) {
554 return SCM_IDENTIFIER;
557 if ((YYSTATE != notes) && (YYSTATE != chords)) {
558 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
560 if (gh_pair_p (pitch))
562 yylval.scm = ly_cdr (pitch);
563 return NOTENAME_PITCH;
566 String msg (_f ("unknown escaped string: `\\%s'", str));
567 LexerError (msg.to_str0 ());
569 yylval.scm = scm_makfrom0str (str.to_str0 ());
575 My_lily_lexer::scan_bare_word (String str)
577 SCM sym = ly_symbol2scm (str.to_str0 ());
578 if ((YYSTATE == notes) || (YYSTATE == chords)) {
579 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
580 if (gh_pair_p (pitch)) {
581 yylval.scm = ly_cdr (pitch);
582 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
583 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
585 yylval.scm = ly_cdr (pitch);
586 return CHORDMODIFIER_PITCH;
590 yylval.scm = scm_makfrom0str (str.to_str0 ());
595 My_lily_lexer::note_state_b () const
597 return YY_START == notes;
601 My_lily_lexer::chord_state_b () const
603 return YY_START == chords;
607 My_lily_lexer::lyric_state_b () const
609 return YY_START == lyrics;
613 My_lily_lexer::figure_state_b () const
615 return YY_START == figures;
619 urg, belong to String (_convert)
620 and should be generalised
623 strip_leading_white (String&s)
626 for (; i < s.length (); i++)
630 s = s.nomid_string (0, i);
634 strip_trailing_white (String&s)
641 s = s.left_string (i+1);
646 /* 1.3.146 == removal of ; */
647 Lilypond_version oldest_version ("1.3.146");
651 valid_version_b (String s)
653 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
654 Lilypond_version ver (s);
655 if (! ((ver >= oldest_version) && (ver <= current)))
657 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.string (), oldest_version.string (), current.string ()));
658 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
666 lyric_fudge (String s)
668 char * chars =s.get_copy_str0 ();
670 for (char * p = chars; *p ; p++)
672 if (*p == '_' && (p == chars || *(p-1) != '\\'))
680 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
682 * (s.get_str0 () + i + 1) = 'c';
683 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
690 Convert "NUM/DEN" into a '(NUM . DEN) cons.
693 scan_fraction (String frac)
695 int i = frac.index ('/');
696 int l = frac.length ();
697 String left = frac.left_string (i);
698 String right = frac.right_string (l - i - 1);
700 int n = String_convert::dec2int (left);
701 int d = String_convert::dec2int (right);
702 return gh_cons (gh_int2scm (n), gh_int2scm (d));