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 :-)
27 #include <iostream.h> /* gcc 3.0 */
30 #include "lily-guile.hh"
32 #include "string-convert.hh"
33 #include "my-lily-lexer.hh"
35 #include "interval.hh"
36 #include "lily-guile.hh"
40 #include "musical-request.hh"
42 #include "lilypond-input-version.hh"
43 #include "translator-def.hh"
44 #include "music-output-def.hh"
49 #define isatty HORRIBLEKLUDGE
51 void strip_trailing_white (String&);
52 void strip_leading_white (String&);
53 String lyric_fudge (String s);
57 valid_version_b (String s);
61 #define start_quote() \
62 yy_push_state (quote);\
63 yylval.string = new String
68 #define YY_USER_ACTION add_lexed_char (YYLeng ());
71 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
76 SCM scan_fraction (String);
84 %option yyclass="My_lily_lexer"
86 %option never-interactive
104 NATIONAL [\001-\006\021-\027\031\036\200-\377]
105 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
112 REAL ({INT}\.{N}*)|(-?\.{N}+)
115 HORIZONTALWHITE [ \t]
119 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
127 // windows-suck-suck-suck
130 <INITIAL,chords,incl,lyrics,notes>{
132 yy_push_state (longcomment);
136 %[^{\n] { // backup rule
147 <INITIAL,chords,lyrics,notes>\\version{WHITE}* {
148 yy_push_state (version);
150 <version>\"[^"]*\" { /* got the version number */
151 String s (YYText ()+1);
152 s = s.left_str (s.index_last_i ('"'));
155 if (!valid_version_b (s))
159 LexerError ("No quoted string found after \\version");
172 LexerError (_ ("EOF found inside a comment").ch_C ());
173 if (! close_input ())
174 yyterminate (); // can't move this, since it actually rets a YY_NULL
179 <INITIAL,chords,lyrics,notes>\\maininput {
183 main_input_b_ = true;
186 error (_ ("\\maininput disallowed outside init files"));
189 <INITIAL,chords,lyrics,notes>\\include {
190 yy_push_state (incl);
192 <incl>\"[^"]*\";? { /* got the include file name */
193 String s (YYText ()+1);
194 s = s.left_str (s.index_last_i ('"'));
196 new_input (s,source_global_l);
199 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
200 String s = YYText () + 1;
201 strip_trailing_white (s);
202 if (s.length_i () && (s[s.length_i () - 1] == ';'))
203 s = s.left_str (s.length_i () - 1);
205 SCM sid = lookup_identifier (s);
206 if (gh_string_p (sid)) {
207 new_input (ly_scm2string (sid), source_global_l);
210 String msg (_f ("wrong or undefined identifier: `%s'", s ));
212 LexerError (msg.ch_C ());
213 SCM err = scm_current_error_port ();
214 scm_puts ("This value was found in the table: ", err);
215 scm_display (sid, err);
218 <incl>\"[^"]* { // backup rule
219 cerr << _ ("Missing end quote") << endl;
222 <chords,notes>{RESTNAME} {
223 const char *s = YYText ();
224 yylval.scm = ly_str02scm (s);
228 return MULTI_MEASURE_REST;
230 <INITIAL,chords,lyrics,notes>\\\${BLACK}*{WHITE} {
231 String s=YYText () + 2;
232 s=s.left_str (s.length_i () - 1);
233 return scan_escaped_word (s);
235 <INITIAL,chords,lyrics,notes>\${BLACK}*{WHITE} {
236 String s=YYText () + 1;
237 s=s.left_str (s.length_i () - 1);
238 return scan_bare_word (s);
240 <INITIAL,chords,lyrics,notes>\\\${BLACK}* { // backup rule
241 cerr << _ ("white expected") << endl;
244 <INITIAL,chords,lyrics,notes>\${BLACK}* { // backup rule
245 cerr << _ ("white expected") << endl;
249 <INITIAL,chords,lyrics,notes># { //embedded scm
250 //char const* s = YYText () + 1;
251 char const* s = here_ch_C ();
253 if (main_input_b_ && safe_global_b) {
254 error (_ ("Can't evaluate Scheme in safe mode"));
255 yylval.scm = SCM_EOL;
258 yylval.scm = ly_parse_scm (s, &n);
260 for (int i=0; i < n; i++)
264 char_count_stack_.top () += n;
270 return scan_bare_word (YYText ());
274 return scan_escaped_word (YYText () + 1);
277 yylval.scm = scan_fraction (YYText ());
282 yylval.i = String_convert::dec2_i (String (YYText ()));
286 yylval.i = String_convert::dec2_i (String (YYText ()));
300 *yylval.string += to_str (escaped_char (YYText ()[1]));
303 *yylval.string += YYText ();
309 /* yylval is union. Must remember STRING before setting SCM*/
310 String *sp = yylval.string;
311 yylval.scm = ly_str02scm (sp->ch_C ());
316 *yylval.string += YYText ();
325 yylval.scm = scan_fraction (YYText ());
329 yylval.i = String_convert::dec2_i (String (YYText ()));
333 return scan_escaped_word (YYText () + 1);
337 String s (YYText ());
339 return yylval.i = EXTENDER;
341 return yylval.i = HYPHEN;
344 char c = s[s.length_i () - 1];
345 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
346 here_input ().warning (
347 _ ("Brace found at end of lyric. Did you forget a space?"));
348 yylval.scm = ly_str02scm (s.ch_C ());
359 return scan_bare_word (YYText ());
362 return scan_escaped_word (YYText () + 1);
365 yylval.scm = scan_fraction (YYText ());
369 yylval.i = String_convert::dec2_i (String (YYText ()));
395 if (! close_input ()) {
396 yyterminate (); // can't move this, since it actually rets a YY_NULL
402 return scan_bare_word (YYText ());
405 return scan_escaped_word (YYText () + 1);
409 int cnv=sscanf (YYText (), "%lf", &r);
412 yylval.scm = gh_double2scm (r);
417 yylval.i = String_convert::dec2_i (String (YYText ()));
426 char c = YYText ()[0];
435 <INITIAL,lyrics,notes>\\. {
436 char c= YYText ()[1];
444 return E_EXCLAMATION;
455 String msg = _f ("invalid character: `%c'", YYText ()[0]);
456 LexerError (msg.ch_C ());
463 My_lily_lexer::push_note_state ()
465 yy_push_state (notes);
469 My_lily_lexer::push_chord_state ()
471 yy_push_state (chords);
475 My_lily_lexer::push_lyric_state ()
477 yy_push_state (lyrics);
481 My_lily_lexer::pop_state ()
487 My_lily_lexer::scan_escaped_word (String str)
489 // use more SCM for this.
491 SCM sym = ly_symbol2scm (str.ch_C ());
493 int l = lookup_keyword (str);
497 SCM sid = lookup_identifier (str);
498 if (gh_string_p (sid)) {
500 return STRING_IDENTIFIER;
501 } else if (gh_number_p (sid)) {
503 return NUMBER_IDENTIFIER;
504 } else if (unsmob_translator_def (sid)) {
506 return TRANSLATOR_IDENTIFIER;
507 } else if (unsmob_score (sid)) {
509 return SCORE_IDENTIFIER;
510 } else if (Music * mus =unsmob_music (sid)) {
513 return dynamic_cast<Request*> (mus) ? REQUEST_IDENTIFIER : MUSIC_IDENTIFIER;
514 } else if (unsmob_duration (sid)) {
516 return DURATION_IDENTIFIER;
517 } else if (unsmob_music_output_def (sid)) {
519 return MUSIC_OUTPUT_DEF_IDENTIFIER;
522 if (sid != SCM_UNDEFINED) {
524 return SCM_IDENTIFIER;
527 if ((YYSTATE != notes) && (YYSTATE != chords)) {
528 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
530 if (gh_pair_p (pitch))
532 yylval.scm = gh_cdr (pitch);
533 return NOTENAME_PITCH;
536 String msg (_f ("unknown escaped string: `\\%s'", str));
537 LexerError (msg.ch_C ());
539 yylval.scm = ly_str02scm (str.ch_C ());
545 My_lily_lexer::scan_bare_word (String str)
547 SCM sym = ly_symbol2scm (str.ch_C ());
548 if ((YYSTATE == notes) || (YYSTATE == chords)) {
549 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
550 if (gh_pair_p (pitch)) {
551 yylval.scm = gh_cdr (pitch);
552 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
553 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
555 yylval.scm = gh_cdr (pitch);
556 return CHORDMODIFIER_PITCH;
560 yylval.scm = ly_str02scm (str.ch_C ());
565 My_lily_lexer::note_state_b () const
567 return YY_START == notes;
571 My_lily_lexer::chord_state_b () const
573 return YY_START == chords;
577 My_lily_lexer::lyric_state_b () const
579 return YY_START == lyrics;
583 urg, belong to String (_convert)
584 and should be generalised
587 strip_leading_white (String&s)
590 for (; i < s.length_i (); i++)
594 s = s.nomid_str (0, i);
598 strip_trailing_white (String&s)
605 s = s.left_str (i+1);
610 Lilypond_version oldest_version ("1.3.59");
613 print_lilypond_versions (ostream &os)
615 os << _f ("Oldest supported input version: %s", oldest_version.str ())
621 valid_version_b (String s)
623 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
624 Lilypond_version ver (s);
625 if (! ((ver >= oldest_version) && (ver <= current)))
627 non_fatal_error (_f ("incorrect lilypond version: %s (%s, %s)", ver.str (), oldest_version.str (), current.str ()));
628 non_fatal_error (_ ("Consider converting the input with the convert-ly script"));
636 lyric_fudge (String s)
638 char * chars =s.copy_ch_p ();
640 for (char * p = chars; *p ; p++)
642 if (*p == '_' && (p == chars || *(p-1) != '\\'))
650 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
652 * (s.ch_l () + i + 1) = 'c';
653 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
660 Convert "NUM/DEN" into a '(NUM . DEN) cons.
663 scan_fraction (String frac)
665 int i = frac.index_i ('/');
666 int l = frac.length_i ();
667 String left = frac.left_str (i);
668 String right = frac.right_str (l - i - 1);
670 int n = String_convert::dec2_i (left);
671 int d = String_convert::dec2_i (right);
672 return gh_cons (gh_int2scm (n), gh_int2scm (d));