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]*
81 %option yyclass="My_lily_lexer"
83 %option never-interactive
101 NATIONAL [\001-\006\021-\027\031\036\200-\377]
102 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
108 REAL ({INT}\.{N}*)|(-?\.{N}+)
111 HORIZONTALWHITE [ \t]
115 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
123 // windows-suck-suck-suck
126 <INITIAL,chords,incl,lyrics,notes>{
128 yy_push_state (longcomment);
132 %[^{\n] { // backup rule
143 <INITIAL,chords,lyrics,notes>\\version{WHITE}* {
144 yy_push_state (version);
146 <version>\"[^"]*\" { /* got the version number */
147 String s (YYText ()+1);
148 s = s.left_str (s.index_last_i ('"'));
151 if (!valid_version_b (s))
155 LexerError ("No quoted string found after \\version");
168 LexerError (_ ("EOF found inside a comment").ch_C ());
169 if (! close_input ())
170 yyterminate (); // can't move this, since it actually rets a YY_NULL
175 <INITIAL,chords,lyrics,notes>\\maininput {
179 main_input_b_ = true;
182 error (_ ("\\maininput disallowed outside init files"));
185 <INITIAL,chords,lyrics,notes>\\include {
186 yy_push_state (incl);
188 <incl>\"[^"]*\";? { /* got the include file name */
189 String s (YYText ()+1);
190 s = s.left_str (s.index_last_i ('"'));
192 new_input (s,source_global_l);
195 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
196 String s = YYText () + 1;
197 strip_trailing_white (s);
198 if (s.length_i () && (s[s.length_i () - 1] == ';'))
199 s = s.left_str (s.length_i () - 1);
201 SCM sid = lookup_identifier (s);
202 if (gh_string_p (sid)) {
203 new_input (ly_scm2string (sid), source_global_l);
206 String msg (_f ("wrong or undefined identifier: `%s'", s ));
208 LexerError (msg.ch_C ());
209 SCM err = scm_current_error_port ();
210 scm_puts ("This value was found in the table: ", err);
211 scm_display (sid, err);
214 <incl>\"[^"]* { // backup rule
215 cerr << _ ("Missing end quote") << endl;
218 <chords,notes>{RESTNAME} {
219 const char *s = YYText ();
220 yylval.scm = ly_str02scm (s);
224 return MULTI_MEASURE_REST;
226 <INITIAL,chords,lyrics,notes>\\\${BLACK}*{WHITE} {
227 String s=YYText () + 2;
228 s=s.left_str (s.length_i () - 1);
229 return scan_escaped_word (s);
231 <INITIAL,chords,lyrics,notes>\${BLACK}*{WHITE} {
232 String s=YYText () + 1;
233 s=s.left_str (s.length_i () - 1);
234 return scan_bare_word (s);
236 <INITIAL,chords,lyrics,notes>\\\${BLACK}* { // backup rule
237 cerr << _ ("white expected") << endl;
240 <INITIAL,chords,lyrics,notes>\${BLACK}* { // backup rule
241 cerr << _ ("white expected") << endl;
245 <INITIAL,chords,lyrics,notes># { //embedded scm
246 //char const* s = YYText () + 1;
247 char const* s = here_ch_C ();
249 if (main_input_b_ && safe_global_b) {
250 error (_ ("Can't evaluate Scheme in safe mode"));
251 yylval.scm = SCM_EOL;
254 yylval.scm = ly_parse_scm (s, &n);
256 for (int i=0; i < n; i++)
260 char_count_stack_.top () += n;
266 return scan_bare_word (YYText ());
270 return scan_escaped_word (YYText () + 1);
274 yylval.i = String_convert::dec2_i (String (YYText ()));
278 yylval.i = String_convert::dec2_i (String (YYText ()));
292 *yylval.string += to_str (escaped_char (YYText ()[1]));
295 *yylval.string += YYText ();
301 /* yylval is union. Must remember STRING before setting SCM*/
302 String *sp = yylval.string;
303 yylval.scm = ly_str02scm (sp->ch_C ());
308 *yylval.string += YYText ();
317 yylval.i = String_convert::dec2_i (String (YYText ()));
321 return scan_escaped_word (YYText () + 1);
325 String s (YYText ());
327 return yylval.i = EXTENDER;
329 return yylval.i = HYPHEN;
332 char c = s[s.length_i () - 1];
333 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
334 here_input ().warning (
335 _ ("Brace found at end of lyric. Did you forget a space?"));
336 yylval.scm = ly_str02scm (s.ch_C ());
347 return scan_bare_word (YYText ());
350 return scan_escaped_word (YYText () + 1);
353 yylval.i = String_convert::dec2_i (String (YYText ()));
379 if (! close_input ()) {
380 yyterminate (); // can't move this, since it actually rets a YY_NULL
386 return scan_bare_word (YYText ());
389 return scan_escaped_word (YYText () + 1);
393 int cnv=sscanf (YYText (), "%lf", &r);
396 yylval.scm = gh_double2scm (r);
401 yylval.i = String_convert::dec2_i (String (YYText ()));
410 char c = YYText ()[0];
419 <INITIAL,lyrics,notes>\\. {
420 char c= YYText ()[1];
428 return E_EXCLAMATION;
439 String msg = _f ("invalid character: `%c'", YYText ()[0]);
440 LexerError (msg.ch_C ());
447 My_lily_lexer::push_note_state ()
449 yy_push_state (notes);
453 My_lily_lexer::push_chord_state ()
455 yy_push_state (chords);
459 My_lily_lexer::push_lyric_state ()
461 yy_push_state (lyrics);
465 My_lily_lexer::pop_state ()
471 My_lily_lexer::scan_escaped_word (String str)
473 // use more SCM for this.
475 SCM sym = ly_symbol2scm (str.ch_C ());
477 int l = lookup_keyword (str);
481 SCM sid = lookup_identifier (str);
482 if (gh_string_p (sid)) {
484 return STRING_IDENTIFIER;
485 } else if (gh_number_p (sid)) {
487 return NUMBER_IDENTIFIER;
488 } else if (unsmob_translator_def (sid)) {
490 return TRANSLATOR_IDENTIFIER;
491 } else if (unsmob_score (sid)) {
493 return SCORE_IDENTIFIER;
494 } else if (Music * mus =unsmob_music (sid)) {
497 return dynamic_cast<Request*> (mus) ? REQUEST_IDENTIFIER : MUSIC_IDENTIFIER;
498 } else if (unsmob_duration (sid)) {
500 return DURATION_IDENTIFIER;
501 } else if (unsmob_music_output_def (sid)) {
503 return MUSIC_OUTPUT_DEF_IDENTIFIER;
506 if (sid != SCM_UNDEFINED) {
508 return SCM_IDENTIFIER;
511 if ((YYSTATE != notes) && (YYSTATE != chords)) {
512 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
514 if (gh_pair_p (pitch))
516 yylval.scm = gh_cdr (pitch);
517 return NOTENAME_PITCH;
520 String msg (_f ("unknown escaped string: `\\%s'", str));
521 LexerError (msg.ch_C ());
523 yylval.scm = ly_str02scm (str.ch_C ());
529 My_lily_lexer::scan_bare_word (String str)
531 SCM sym = ly_symbol2scm (str.ch_C ());
532 if ((YYSTATE == notes) || (YYSTATE == chords)) {
533 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
534 if (gh_pair_p (pitch)) {
535 yylval.scm = gh_cdr (pitch);
536 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
537 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
539 yylval.scm = gh_cdr (pitch);
540 return CHORDMODIFIER_PITCH;
544 yylval.scm = ly_str02scm (str.ch_C ());
549 My_lily_lexer::note_state_b () const
551 return YY_START == notes;
555 My_lily_lexer::chord_state_b () const
557 return YY_START == chords;
561 My_lily_lexer::lyric_state_b () const
563 return YY_START == lyrics;
567 urg, belong to String (_convert)
568 and should be generalised
571 strip_leading_white (String&s)
574 for (; i < s.length_i (); i++)
578 s = s.nomid_str (0, i);
582 strip_trailing_white (String&s)
589 s = s.left_str (i+1);
596 valid_version_b (String s)
598 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
599 Lilypond_version ver (s);
600 if (! ((ver >= oldest_version) && (ver <= current)))
602 non_fatal_error (_f ("incorrect lilypond version: %s (%s, %s)", ver.str (), oldest_version.str (), current.str ()));
603 non_fatal_error (_ ("Consider converting the input with the convert-ly script"));
611 lyric_fudge (String s)
613 char * chars =s.copy_ch_p ();
615 for (char * p = chars; *p ; p++)
617 if (*p == '_' && (p == chars || *(p-1) != '\\'))
625 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
627 * (s.ch_l () + i + 1) = 'c';
628 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);