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&);
55 valid_version_b (String s);
59 #define start_quote() \
60 yy_push_state (quote);\
61 yylval.string = new String
63 #define yylval (*(YYSTYPE*)lexval_l)
65 #define YY_USER_ACTION add_lexed_char (YYLeng ());
68 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
78 %option yyclass="My_lily_lexer"
80 %option never-interactive
98 NATIONAL [\001-\006\021-\027\031\036\200-\377]
99 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
105 REAL ({INT}\.{N}*)|(-?\.{N}+)
108 HORIZONTALWHITE [ \t]
112 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
120 // windows-suck-suck-suck
123 <INITIAL,chords,incl,lyrics,notes>{
125 yy_push_state (longcomment);
129 %[^{\n] { // backup rule
140 <INITIAL,chords,lyrics,notes>\\version{WHITE}* {
141 yy_push_state (version);
143 <version>\"[^"]*\";? { /* got the include file name */
144 String s (YYText ()+1);
145 s = s.left_str (s.index_last_i ('"'));
146 DEBUG_OUT << "#version `" << s << "'\n";
148 if (!valid_version_b (s))
152 LexerError ("No quoted string found after \\version");
165 LexerError (_ ("EOF found inside a comment").ch_C ());
166 if (! close_input ())
167 yyterminate (); // can't move this, since it actually rets a YY_NULL
172 <INITIAL,chords,lyrics,notes>\\maininput {
176 main_input_b_ = true;
179 error (_ ("\\maininput disallowed outside init files"));
182 <INITIAL,chords,lyrics,notes>\\include {
183 yy_push_state (incl);
185 <incl>\"[^"]*\";? { /* got the include file name */
186 String s (YYText ()+1);
187 s = s.left_str (s.index_last_i ('"'));
188 DEBUG_OUT << "#include `" << s << "'\n";
189 new_input (s,source_global_l);
192 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
193 String s = YYText () + 1;
194 strip_trailing_white (s);
195 if (s.length_i () && (s[s.length_i () - 1] == ';'))
196 s = s.left_str (s.length_i () - 1);
197 DEBUG_OUT << "#include `\\" << s << "'\n";
198 SCM sid = lookup_identifier (s);
199 if (gh_string_p (sid)) {
200 new_input (ly_scm2string (sid), source_global_l);
203 String msg (_f ("wrong or undefined identifier: `%s'", s ));
204 LexerError (msg.ch_C ());
207 <incl>\"[^"]* { // backup rule
208 cerr << _ ("Missing end quote") << endl;
211 <chords,notes>{RESTNAME} {
212 const char *s = YYText ();
213 yylval.scm = ly_str02scm (s);
219 <INITIAL,chords,lyrics,notes>\\\${BLACK}*{WHITE} {
220 String s=YYText () + 2;
221 s=s.left_str (s.length_i () - 1);
222 return scan_escaped_word (s);
224 <INITIAL,chords,lyrics,notes>\${BLACK}*{WHITE} {
225 String s=YYText () + 1;
226 s=s.left_str (s.length_i () - 1);
227 return scan_bare_word (s);
229 <INITIAL,chords,lyrics,notes>\\\${BLACK}* { // backup rule
230 cerr << _ ("white expected") << endl;
233 <INITIAL,chords,lyrics,notes>\${BLACK}* { // backup rule
234 cerr << _ ("white expected") << endl;
238 <INITIAL,chords,lyrics,notes># { //embedded scm
239 //char const* s = YYText () + 1;
240 char const* s = here_ch_C ();
242 if (main_input_b_ && safe_global_b) {
243 error (_ ("Can't evaluate Scheme in safe mode"));
244 yylval.scm = SCM_EOL;
247 yylval.scm = ly_parse_scm (s, &n);
249 for (int i=0; i < n; i++)
253 char_count_stack_.top () += n;
259 return scan_bare_word (YYText ());
263 return scan_escaped_word (YYText () + 1);
267 yylval.i = String_convert::dec2_i (String (YYText ()));
271 yylval.i = String_convert::dec2_i (String (YYText ()));
285 *yylval.string += to_str (escaped_char(YYText()[1]));
288 *yylval.string += YYText ();
291 DEBUG_OUT << "quoted string: `" << *yylval.string << "'\n";
294 /* yylval is union. Must remember STRING before setting SCM*/
295 String *sp = yylval.string;
296 yylval.scm = ly_str02scm (sp->ch_C ());
301 *yylval.string += YYText ();
310 yylval.i = String_convert::dec2_i (String (YYText ()));
314 return scan_escaped_word (YYText () + 1);
318 String s (YYText ());
320 return yylval.i = EXTENDER;
322 return yylval.i = HYPHEN;
324 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
325 *(s.ch_l () + i) = ' ';
326 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
328 *(s.ch_l () + i + 1) = 'c';
329 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
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 ());
338 DEBUG_OUT << "lyric : `" << s << "'\n";
347 return scan_bare_word (YYText ());
350 return scan_escaped_word (YYText () + 1);
353 yylval.i = String_convert::dec2_i (String (YYText ()));
377 DEBUG_OUT << "<<eof>>";
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);
395 DEBUG_OUT << "REAL" << r<<'\n';
401 yylval.i = String_convert::dec2_i (String (YYText ()));
406 DEBUG_OUT << "parens\n";
410 char c = YYText ()[0];
411 DEBUG_OUT << "misc char" <<c<<"\n";
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"));