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 :-)
29 #include "lily-guile.hh"
31 #include "string-convert.hh"
32 #include "my-lily-lexer.hh"
34 #include "interval.hh"
35 #include "lily-guile.hh"
39 #include "musical-request.hh"
41 #include "lilypond-input-version.hh"
42 #include "translator-def.hh"
43 #include "music-output-def.hh"
48 #define isatty HORRIBLEKLUDGE
50 void strip_trailing_white (String&);
51 void strip_leading_white (String&);
52 String lyric_fudge (String s);
56 valid_version_b (String s);
60 #define start_quote() \
61 yy_push_state (quote);\
62 yylval.string = new String
67 #define YY_USER_ACTION add_lexed_char (YYLeng ());
70 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
80 %option yyclass="My_lily_lexer"
82 %option never-interactive
100 NATIONAL [\001-\006\021-\027\031\036\200-\377]
101 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
107 REAL ({INT}\.{N}*)|(-?\.{N}+)
110 HORIZONTALWHITE [ \t]
114 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
122 // windows-suck-suck-suck
125 <INITIAL,chords,incl,lyrics,notes>{
127 yy_push_state (longcomment);
131 %[^{\n] { // backup rule
142 <INITIAL,chords,lyrics,notes>\\version{WHITE}* {
143 yy_push_state (version);
145 <version>\"[^"]*\" { /* got the version number */
146 String s (YYText ()+1);
147 s = s.left_str (s.index_last_i ('"'));
150 if (!valid_version_b (s))
154 LexerError ("No quoted string found after \\version");
167 LexerError (_ ("EOF found inside a comment").ch_C ());
168 if (! close_input ())
169 yyterminate (); // can't move this, since it actually rets a YY_NULL
174 <INITIAL,chords,lyrics,notes>\\maininput {
178 main_input_b_ = true;
181 error (_ ("\\maininput disallowed outside init files"));
184 <INITIAL,chords,lyrics,notes>\\include {
185 yy_push_state (incl);
187 <incl>\"[^"]*\";? { /* got the include file name */
188 String s (YYText ()+1);
189 s = s.left_str (s.index_last_i ('"'));
191 new_input (s,source_global_l);
194 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
195 String s = YYText () + 1;
196 strip_trailing_white (s);
197 if (s.length_i () && (s[s.length_i () - 1] == ';'))
198 s = s.left_str (s.length_i () - 1);
200 SCM sid = lookup_identifier (s);
201 if (gh_string_p (sid)) {
202 new_input (ly_scm2string (sid), source_global_l);
205 String msg (_f ("wrong or undefined identifier: `%s'", s ));
207 LexerError (msg.ch_C ());
208 SCM err = scm_current_error_port ();
209 scm_puts ("This value was found in the table: ", err);
210 scm_display (sid, err);
213 <incl>\"[^"]* { // backup rule
214 cerr << _ ("Missing end quote") << endl;
217 <chords,notes>{RESTNAME} {
218 const char *s = YYText ();
219 yylval.scm = ly_str02scm (s);
223 return MULTI_MEASURE_REST;
225 <INITIAL,chords,lyrics,notes>\\\${BLACK}*{WHITE} {
226 String s=YYText () + 2;
227 s=s.left_str (s.length_i () - 1);
228 return scan_escaped_word (s);
230 <INITIAL,chords,lyrics,notes>\${BLACK}*{WHITE} {
231 String s=YYText () + 1;
232 s=s.left_str (s.length_i () - 1);
233 return scan_bare_word (s);
235 <INITIAL,chords,lyrics,notes>\\\${BLACK}* { // backup rule
236 cerr << _ ("white expected") << endl;
239 <INITIAL,chords,lyrics,notes>\${BLACK}* { // backup rule
240 cerr << _ ("white expected") << endl;
244 <INITIAL,chords,lyrics,notes># { //embedded scm
245 //char const* s = YYText () + 1;
246 char const* s = here_ch_C ();
248 if (main_input_b_ && safe_global_b) {
249 error (_ ("Can't evaluate Scheme in safe mode"));
250 yylval.scm = SCM_EOL;
253 yylval.scm = ly_parse_scm (s, &n);
255 for (int i=0; i < n; i++)
259 char_count_stack_.top () += n;
265 return scan_bare_word (YYText ());
269 return scan_escaped_word (YYText () + 1);
273 yylval.i = String_convert::dec2_i (String (YYText ()));
277 yylval.i = String_convert::dec2_i (String (YYText ()));
291 *yylval.string += to_str (escaped_char (YYText ()[1]));
294 *yylval.string += YYText ();
300 /* yylval is union. Must remember STRING before setting SCM*/
301 String *sp = yylval.string;
302 yylval.scm = ly_str02scm (sp->ch_C ());
307 *yylval.string += YYText ();
316 yylval.i = String_convert::dec2_i (String (YYText ()));
320 return scan_escaped_word (YYText () + 1);
324 String s (YYText ());
326 return yylval.i = EXTENDER;
328 return yylval.i = HYPHEN;
331 char c = s[s.length_i () - 1];
332 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
333 here_input ().warning (
334 _ ("Brace found at end of lyric. Did you forget a space?"));
335 yylval.scm = ly_str02scm (s.ch_C ());
346 return scan_bare_word (YYText ());
349 return scan_escaped_word (YYText () + 1);
352 yylval.i = String_convert::dec2_i (String (YYText ()));
378 if (! close_input ()) {
379 yyterminate (); // can't move this, since it actually rets a YY_NULL
385 return scan_bare_word (YYText ());
388 return scan_escaped_word (YYText () + 1);
392 int cnv=sscanf (YYText (), "%lf", &r);
395 yylval.scm = gh_double2scm (r);
400 yylval.i = String_convert::dec2_i (String (YYText ()));
409 char c = YYText ()[0];
418 <INITIAL,lyrics,notes>\\. {
419 char c= YYText ()[1];
427 return E_EXCLAMATION;
438 String msg = _f ("invalid character: `%c'", YYText ()[0]);
439 LexerError (msg.ch_C ());
446 My_lily_lexer::push_note_state ()
448 yy_push_state (notes);
452 My_lily_lexer::push_chord_state ()
454 yy_push_state (chords);
458 My_lily_lexer::push_lyric_state ()
460 yy_push_state (lyrics);
464 My_lily_lexer::pop_state ()
470 My_lily_lexer::scan_escaped_word (String str)
472 // use more SCM for this.
474 SCM sym = ly_symbol2scm (str.ch_C ());
476 int l = lookup_keyword (str);
480 SCM sid = lookup_identifier (str);
481 if (gh_string_p (sid)) {
483 return STRING_IDENTIFIER;
484 } else if (gh_number_p (sid)) {
486 return NUMBER_IDENTIFIER;
487 } else if (unsmob_translator_def (sid)) {
489 return TRANSLATOR_IDENTIFIER;
490 } else if (unsmob_score (sid)) {
492 return SCORE_IDENTIFIER;
493 } else if (Music * mus =unsmob_music (sid)) {
496 return dynamic_cast<Request*> (mus) ? REQUEST_IDENTIFIER : MUSIC_IDENTIFIER;
497 } else if (unsmob_duration (sid)) {
499 return DURATION_IDENTIFIER;
500 } else if (unsmob_music_output_def (sid)) {
502 return MUSIC_OUTPUT_DEF_IDENTIFIER;
505 if (sid != SCM_UNDEFINED) {
507 return SCM_IDENTIFIER;
510 if ((YYSTATE != notes) && (YYSTATE != chords)) {
511 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
513 if (gh_pair_p (pitch))
515 yylval.scm = gh_cdr (pitch);
516 return NOTENAME_PITCH;
519 String msg (_f ("unknown escaped string: `\\%s'", str));
520 LexerError (msg.ch_C ());
522 yylval.scm = ly_str02scm (str.ch_C ());
528 My_lily_lexer::scan_bare_word (String str)
530 SCM sym = ly_symbol2scm (str.ch_C ());
531 if ((YYSTATE == notes) || (YYSTATE == chords)) {
532 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
533 if (gh_pair_p (pitch)) {
534 yylval.scm = gh_cdr (pitch);
535 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
536 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
538 yylval.scm = gh_cdr (pitch);
539 return CHORDMODIFIER_PITCH;
543 yylval.scm = ly_str02scm (str.ch_C ());
548 My_lily_lexer::note_state_b () const
550 return YY_START == notes;
554 My_lily_lexer::chord_state_b () const
556 return YY_START == chords;
560 My_lily_lexer::lyric_state_b () const
562 return YY_START == lyrics;
566 urg, belong to String (_convert)
567 and should be generalised
570 strip_leading_white (String&s)
573 for (; i < s.length_i (); i++)
577 s = s.nomid_str (0, i);
581 strip_trailing_white (String&s)
588 s = s.left_str (i+1);
595 valid_version_b (String s)
597 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
598 Lilypond_version ver (s);
599 if (! ((ver >= oldest_version) && (ver <= current)))
601 non_fatal_error (_f ("incorrect lilypond version: %s (%s, %s)", ver.str (), oldest_version.str (), current.str ()));
602 non_fatal_error (_ ("Consider converting the input with the convert-ly script"));
610 lyric_fudge (String s)
612 char * chars =s.copy_ch_p ();
614 for (char * p = chars; *p ; p++)
616 if (*p == '_' && (p == chars || *(p-1) != '\\'))
624 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
626 * (s.ch_l () + i + 1) = 'c';
627 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);