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 */
32 #include "lily-guile.hh"
34 #include "string-convert.hh"
35 #include "my-lily-lexer.hh"
37 #include "interval.hh"
38 #include "lily-guile.hh"
42 #include "musical-request.hh"
44 #include "lilypond-input-version.hh"
45 #include "translator-def.hh"
46 #include "music-output-def.hh"
51 #define isatty HORRIBLEKLUDGE
53 void strip_trailing_white (String&);
54 void strip_leading_white (String&);
55 String lyric_fudge (String s);
59 valid_version_b (String s);
63 #define start_quote() \
64 yy_push_state (quote);\
65 yylval.string = new String
70 #define YY_USER_ACTION add_lexed_char (YYLeng ());
73 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
78 SCM scan_fraction (String);
86 %option yyclass="My_lily_lexer"
88 %option never-interactive
106 NATIONAL [\001-\006\021-\027\031\036\200-\377]
107 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
114 REAL ({INT}\.{N}*)|(-?\.{N}+)
117 HORIZONTALWHITE [ \t]
121 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
129 // windows-suck-suck-suck
132 <INITIAL,chords,incl,lyrics,notes>{
134 yy_push_state (longcomment);
138 %[^{\n] { // backup rule
149 <INITIAL,chords,lyrics,notes>\\version{WHITE}* {
150 yy_push_state (version);
152 <version>\"[^"]*\" { /* got the version number */
153 String s (YYText ()+1);
154 s = s.left_str (s.index_last_i ('"'));
157 if (!valid_version_b (s))
161 LexerError ("No quoted string found after \\version");
174 LexerError (_ ("EOF found inside a comment").ch_C ());
175 if (! close_input ())
176 yyterminate (); // can't move this, since it actually rets a YY_NULL
181 <INITIAL,chords,lyrics,notes>\\maininput {
185 main_input_b_ = true;
188 error (_ ("\\maininput disallowed outside init files"));
191 <INITIAL,chords,lyrics,notes>\\include {
192 yy_push_state (incl);
194 <incl>\"[^"]*\";? { /* got the include file name */
195 String s (YYText ()+1);
196 s = s.left_str (s.index_last_i ('"'));
198 new_input (s,source_global_l);
201 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
202 String s = YYText () + 1;
203 strip_trailing_white (s);
204 if (s.length_i () && (s[s.length_i () - 1] == ';'))
205 s = s.left_str (s.length_i () - 1);
207 SCM sid = lookup_identifier (s);
208 if (gh_string_p (sid)) {
209 new_input (ly_scm2string (sid), source_global_l);
212 String msg (_f ("wrong or undefined identifier: `%s'", s ));
214 LexerError (msg.ch_C ());
215 SCM err = scm_current_error_port ();
216 scm_puts ("This value was found in the table: ", err);
217 scm_display (sid, err);
220 <incl>\"[^"]* { // backup rule
221 cerr << _ ("Missing end quote") << endl;
224 <chords,notes>{RESTNAME} {
225 const char *s = YYText ();
226 yylval.scm = ly_str02scm (s);
230 return MULTI_MEASURE_REST;
232 <INITIAL,chords,lyrics,notes>\\\${BLACK}*{WHITE} {
233 String s=YYText () + 2;
234 s=s.left_str (s.length_i () - 1);
235 return scan_escaped_word (s);
237 <INITIAL,chords,lyrics,notes>\${BLACK}*{WHITE} {
238 String s=YYText () + 1;
239 s=s.left_str (s.length_i () - 1);
240 return scan_bare_word (s);
242 <INITIAL,chords,lyrics,notes>\\\${BLACK}* { // backup rule
243 cerr << _ ("white expected") << endl;
246 <INITIAL,chords,lyrics,notes>\${BLACK}* { // backup rule
247 cerr << _ ("white expected") << endl;
251 <INITIAL,chords,lyrics,notes># { //embedded scm
252 //char const* s = YYText () + 1;
253 char const* s = here_ch_C ();
255 if (main_input_b_ && safe_global_b) {
256 error (_ ("Can't evaluate Scheme in safe mode"));
257 yylval.scm = SCM_EOL;
260 yylval.scm = ly_parse_scm (s, &n);
262 for (int i=0; i < n; i++)
266 char_count_stack_.top () += n;
272 return scan_bare_word (YYText ());
276 return scan_escaped_word (YYText () + 1);
279 yylval.scm = scan_fraction (YYText ());
284 yylval.i = String_convert::dec2_i (String (YYText ()));
288 yylval.i = String_convert::dec2_i (String (YYText ()));
302 *yylval.string += to_str (escaped_char (YYText ()[1]));
305 *yylval.string += YYText ();
311 /* yylval is union. Must remember STRING before setting SCM*/
312 String *sp = yylval.string;
313 yylval.scm = ly_str02scm (sp->ch_C ());
318 *yylval.string += YYText ();
327 yylval.scm = scan_fraction (YYText ());
331 yylval.i = String_convert::dec2_i (String (YYText ()));
335 return scan_escaped_word (YYText () + 1);
339 String s (YYText ());
341 return yylval.i = EXTENDER;
343 return yylval.i = HYPHEN;
346 char c = s[s.length_i () - 1];
347 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
348 here_input ().warning (
349 _ ("Brace found at end of lyric. Did you forget a space?"));
350 yylval.scm = ly_str02scm (s.ch_C ());
361 return scan_bare_word (YYText ());
364 return scan_escaped_word (YYText () + 1);
367 yylval.scm = scan_fraction (YYText ());
371 yylval.i = String_convert::dec2_i (String (YYText ()));
397 if (! close_input ()) {
398 yyterminate (); // can't move this, since it actually rets a YY_NULL
404 return scan_bare_word (YYText ());
407 return scan_escaped_word (YYText () + 1);
411 int cnv=sscanf (YYText (), "%lf", &r);
414 yylval.scm = gh_double2scm (r);
419 yylval.i = String_convert::dec2_i (String (YYText ()));
428 char c = YYText ()[0];
437 <INITIAL,lyrics,notes>\\. {
438 char c= YYText ()[1];
446 return E_EXCLAMATION;
457 String msg = _f ("invalid character: `%c'", YYText ()[0]);
458 LexerError (msg.ch_C ());
465 My_lily_lexer::push_note_state ()
467 yy_push_state (notes);
471 My_lily_lexer::push_chord_state ()
473 yy_push_state (chords);
477 My_lily_lexer::push_lyric_state ()
479 yy_push_state (lyrics);
483 My_lily_lexer::pop_state ()
489 My_lily_lexer::scan_escaped_word (String str)
491 // use more SCM for this.
493 SCM sym = ly_symbol2scm (str.ch_C ());
495 int l = lookup_keyword (str);
499 SCM sid = lookup_identifier (str);
500 if (gh_string_p (sid)) {
502 return STRING_IDENTIFIER;
503 } else if (gh_number_p (sid)) {
505 return NUMBER_IDENTIFIER;
506 } else if (unsmob_translator_def (sid)) {
508 return TRANSLATOR_IDENTIFIER;
509 } else if (unsmob_score (sid)) {
511 return SCORE_IDENTIFIER;
512 } else if (Music * mus =unsmob_music (sid)) {
515 return dynamic_cast<Request*> (mus) ? REQUEST_IDENTIFIER : MUSIC_IDENTIFIER;
516 } else if (unsmob_duration (sid)) {
518 return DURATION_IDENTIFIER;
519 } else if (unsmob_music_output_def (sid)) {
521 return MUSIC_OUTPUT_DEF_IDENTIFIER;
524 if (sid != SCM_UNDEFINED) {
526 return SCM_IDENTIFIER;
529 if ((YYSTATE != notes) && (YYSTATE != chords)) {
530 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
532 if (gh_pair_p (pitch))
534 yylval.scm = gh_cdr (pitch);
535 return NOTENAME_PITCH;
538 String msg (_f ("unknown escaped string: `\\%s'", str));
539 LexerError (msg.ch_C ());
541 yylval.scm = ly_str02scm (str.ch_C ());
547 My_lily_lexer::scan_bare_word (String str)
549 SCM sym = ly_symbol2scm (str.ch_C ());
550 if ((YYSTATE == notes) || (YYSTATE == chords)) {
551 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
552 if (gh_pair_p (pitch)) {
553 yylval.scm = gh_cdr (pitch);
554 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
555 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
557 yylval.scm = gh_cdr (pitch);
558 return CHORDMODIFIER_PITCH;
562 yylval.scm = ly_str02scm (str.ch_C ());
567 My_lily_lexer::note_state_b () const
569 return YY_START == notes;
573 My_lily_lexer::chord_state_b () const
575 return YY_START == chords;
579 My_lily_lexer::lyric_state_b () const
581 return YY_START == lyrics;
585 urg, belong to String (_convert)
586 and should be generalised
589 strip_leading_white (String&s)
592 for (; i < s.length_i (); i++)
596 s = s.nomid_str (0, i);
600 strip_trailing_white (String&s)
607 s = s.left_str (i+1);
612 Lilypond_version oldest_version ("1.3.59");
615 print_lilypond_versions (ostream &os)
617 os << _f ("Oldest supported input version: %s", oldest_version.str ())
623 valid_version_b (String s)
625 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
626 Lilypond_version ver (s);
627 if (! ((ver >= oldest_version) && (ver <= current)))
629 non_fatal_error (_f ("incorrect lilypond version: %s (%s, %s)", ver.str (), oldest_version.str (), current.str ()));
630 non_fatal_error (_ ("Consider converting the input with the convert-ly script"));
638 lyric_fudge (String s)
640 char * chars =s.copy_ch_p ();
642 for (char * p = chars; *p ; p++)
644 if (*p == '_' && (p == chars || *(p-1) != '\\'))
652 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
654 * (s.ch_l () + i + 1) = 'c';
655 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
662 Convert "NUM/DEN" into a '(NUM . DEN) cons.
665 scan_fraction (String frac)
667 int i = frac.index_i ('/');
668 int l = frac.length_i ();
669 String left = frac.left_str (i);
670 String right = frac.right_str (l - i - 1);
672 int n = String_convert::dec2_i (left);
673 int d = String_convert::dec2_i (right);
674 return gh_cons (gh_int2scm (n), gh_int2scm (d));