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 :-)
28 #include "lily-guile.hh"
30 #include "string-convert.hh"
31 #include "my-lily-lexer.hh"
33 #include "interval.hh"
34 #include "lily-guile.hh"
38 #include "musical-request.hh"
39 #include "identifier.hh"
41 #include "mudela-version.hh"
42 #include "translator-def.hh"
44 #define isatty HORRIBLEKLUDGE
46 void strip_trailing_white (String&);
47 void strip_leading_white (String&);
51 valid_version_b (String s);
55 #define start_quote() \
56 yy_push_state (quote);\
57 yylval.string = new String
59 #define yylval (*(YYSTYPE*)lexval_l)
61 #define YY_USER_ACTION add_lexed_char (YYLeng ());
64 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
74 %option yyclass="My_lily_lexer"
76 %option never-interactive
94 NATIONAL [\001-\006\021-\027\031\036\200-\377]
95 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
101 REAL ({INT}\.{N}*)|(-?\.{N}+)
104 HORIZONTALWHITE [ \t]
108 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
116 // windows-suck-suck-suck
119 <INITIAL,chords,incl,lyrics,notes>{
121 yy_push_state (longcomment);
125 %[^{\n] { // backup rule
136 <INITIAL,chords,lyrics,notes>\\version{WHITE}* {
137 yy_push_state (version);
139 <version>\"[^"]*\";? { /* got the include file name */
140 String s (YYText ()+1);
141 s = s.left_str (s.index_last_i ('"'));
142 DEBUG_OUT << "#version `" << s << "'\n";
144 if (!valid_version_b (s))
148 LexerError ("No quoted string found after \\version");
161 LexerError (_ ("EOF found inside a comment").ch_C ());
162 if (! close_input ())
163 yyterminate (); // can't move this, since it actually rets a YY_NULL
168 <INITIAL,chords,lyrics,notes>\\maininput {
172 main_input_b_ = true;
175 error (_ ("\\maininput disallowed outside init files"));
178 <INITIAL,chords,lyrics,notes>\\include {
179 yy_push_state (incl);
181 <incl>\"[^"]*\";? { /* got the include file name */
182 String s (YYText ()+1);
183 s = s.left_str (s.index_last_i ('"'));
184 DEBUG_OUT << "#include `" << s << "'\n";
185 new_input (s,source_global_l);
188 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
189 String s = YYText () + 1;
190 strip_trailing_white (s);
191 if (s.length_i () && (s[s.length_i () - 1] == ';'))
192 s = s.left_str (s.length_i () - 1);
193 DEBUG_OUT << "#include `\\" << s << "'\n";
194 SCM sid = lookup_identifier (s);
195 if (gh_string_p (sid)) {
196 new_input (ly_scm2string (sid), source_global_l);
199 String msg (_f ("wrong or undefined identifier: `%s'", s ));
200 LexerError (msg.ch_C ());
203 <incl>\"[^"]* { // backup rule
204 cerr << _ ("Missing end quote") << endl;
207 <chords,notes>{RESTNAME} {
208 const char *s = YYText ();
209 yylval.scm = ly_str02scm (s);
215 <INITIAL,chords,lyrics,notes>\\\${BLACK}*{WHITE} {
216 String s=YYText () + 2;
217 s=s.left_str (s.length_i () - 1);
218 return scan_escaped_word (s);
220 <INITIAL,chords,lyrics,notes>\${BLACK}*{WHITE} {
221 String s=YYText () + 1;
222 s=s.left_str (s.length_i () - 1);
223 return scan_bare_word (s);
225 <INITIAL,chords,lyrics,notes>\\\${BLACK}* { // backup rule
226 cerr << _ ("white expected") << endl;
229 <INITIAL,chords,lyrics,notes>\${BLACK}* { // backup rule
230 cerr << _ ("white expected") << endl;
233 <INITIAL,chords,lyrics,notes># { //embedded scm
234 //char const* s = YYText () + 1;
235 char const* s = here_ch_C ();
237 if (main_input_b_ && safe_global_b) {
238 error (_ ("Can't evaluate Scheme in safe mode"));
239 yylval.scm = SCM_EOL;
242 yylval.scm = ly_parse_scm (s, &n);
243 DEBUG_OUT << "Scheme: ";
245 ly_display_scm (yylval.scm);
247 for (int i=0; i < n; i++)
251 char_count_stack_.top () += n;
257 return scan_bare_word (YYText ());
261 return scan_escaped_word (YYText () + 1);
265 yylval.i = String_convert::dec2_i (String (YYText ()));
269 yylval.i = String_convert::dec2_i (String (YYText ()));
283 *yylval.string += to_str (escaped_char(YYText()[1]));
286 *yylval.string += YYText ();
289 DEBUG_OUT << "quoted string: `" << *yylval.string << "'\n";
292 /* yylval is union. Must remember STRING before setting SCM*/
293 String *sp = yylval.string;
294 yylval.scm = ly_str02scm (sp->ch_C ());
299 *yylval.string += YYText ();
308 yylval.i = String_convert::dec2_i (String (YYText ()));
312 return scan_escaped_word (YYText () + 1);
316 String s (YYText ());
318 return yylval.i = EXTENDER;
320 return yylval.i = HYPHEN;
322 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
323 *(s.ch_l () + i) = ' ';
324 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
326 *(s.ch_l () + i + 1) = 'c';
327 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
330 char c = s[s.length_i () - 1];
331 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
332 here_input ().warning (
333 "Brace found at end of lyric. Did you forget a space?");
334 yylval.scm = ly_str02scm (s.ch_C ());
336 DEBUG_OUT << "lyric : `" << s << "'\n";
345 return scan_bare_word (YYText ());
348 return scan_escaped_word (YYText () + 1);
351 yylval.i = String_convert::dec2_i (String (YYText ()));
375 DEBUG_OUT << "<<eof>>";
377 if (! close_input ()) {
378 yyterminate (); // can't move this, since it actually rets a YY_NULL
384 return scan_bare_word (YYText ());
387 return scan_escaped_word (YYText () + 1);
391 int cnv=sscanf (YYText (), "%lf", &r);
393 DEBUG_OUT << "REAL" << r<<'\n';
399 yylval.i = String_convert::dec2_i (String (YYText ()));
405 DEBUG_OUT << "parens\n";
409 char c = YYText ()[0];
410 DEBUG_OUT << "misc char" <<c<<"\n";
418 <INITIAL,lyrics,notes>\\. {
419 char c= YYText ()[1];
427 return E_EXCLAMATION;
434 String msg = _f ("invalid character: `%c'", YYText ()[0]);
435 LexerError (msg.ch_C ());
442 My_lily_lexer::push_note_state ()
444 yy_push_state (notes);
448 My_lily_lexer::push_chord_state ()
450 yy_push_state (chords);
454 My_lily_lexer::push_lyric_state ()
456 yy_push_state (lyrics);
460 My_lily_lexer::pop_state ()
466 My_lily_lexer::scan_escaped_word (String str)
468 // use more SCM for this.
470 SCM sym = ly_symbol2scm (str.ch_C());
472 int l = lookup_keyword (str);
476 SCM sid = lookup_identifier (str);
477 if (gh_string_p (sid)) {
479 return STRING_IDENTIFIER;
480 } else if (gh_number_p (sid)) {
482 return NUMBER_IDENTIFIER;
483 } else if (Translator_def* tr = unsmob_translator_def (sid)) {
485 return TRANSLATOR_IDENTIFIER;
486 } else if (Music * mus =unsmob_music (sid)) {
489 return dynamic_cast<Request*> (mus) ? REQUEST_IDENTIFIER : MUSIC_IDENTIFIER;
494 Identifier * id = unsmob_identifier (sid);
497 return id->token_code_i_;
498 } else if (sid != SCM_UNDEFINED) {
500 return SCM_IDENTIFIER;
503 if ((YYSTATE != notes) && (YYSTATE != chords)) {
504 SCM pitch = scm_hashq_ref (pitchname_tab_, sym, SCM_BOOL_F);
506 if (pitch != SCM_BOOL_F)
508 yylval.pitch = new Musical_pitch (pitch);
509 yylval.pitch->set_spot (Input (source_file_l (),
511 return NOTENAME_PITCH;
514 String msg (_f ("unknown escaped string: `\\%s'", str));
515 LexerError (msg.ch_C ());
517 yylval.scm = ly_str02scm(str.ch_C());
523 My_lily_lexer::scan_bare_word (String str)
525 SCM sym = ly_symbol2scm (str.ch_C ());
526 if ((YYSTATE == notes) || (YYSTATE == chords)) {
527 SCM pitch = scm_hashq_ref (pitchname_tab_, sym, SCM_BOOL_F);
528 if (pitch != SCM_BOOL_F) {
529 yylval.pitch = new Musical_pitch (pitch);
530 yylval.pitch->set_spot (Input (source_file_l (),
532 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
533 } else if ((pitch = scm_hashq_ref (chordmodifier_tab_, sym, SCM_BOOL_F))!= SCM_BOOL_F)
535 yylval.pitch = new Musical_pitch (pitch);
536 yylval.pitch->set_spot (Input (source_file_l (),
538 return CHORDMODIFIER_PITCH;
542 yylval.scm = ly_str02scm (str.ch_C());
547 My_lily_lexer::note_state_b () const
549 return YY_START == notes;
553 My_lily_lexer::chord_state_b () const
555 return YY_START == chords;
559 My_lily_lexer::lyric_state_b () const
561 return YY_START == lyrics;
565 urg, belong to String(_convert)
566 and should be generalised
569 strip_leading_white (String&s)
572 for (; i < s.length_i (); i++)
576 s = s.nomid_str (0, i);
580 strip_trailing_white (String&s)
587 s = s.left_str (i+1);
594 valid_version_b (String s)
596 Mudela_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
597 Mudela_version ver (s);
598 if (!((ver >= oldest_version) && (ver <= current)))
600 non_fatal_error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), current.str ()));
601 non_fatal_error (_("Consider converting the input with the convert-mudela script"));