1 %{ // -*-Fundamental-*-
3 lexer.l -- implement the Flex lexer
5 source file of the LilyPond music typesetter
7 (c) 1996,1997 Han-Wen Nienhuys <hanwen@cs.uu.nl>
14 after making a change to the lexer rules, run
15 flex -b <this lexer file>
18 contains no backup states, but only the reminder
19 Compressed tables always back up.
20 (don-t forget to rm lex.yy.cc :-)
28 #include "string-convert.hh"
29 #include "my-lily-lexer.hh"
31 #include "interval.hh"
35 #include "musical-request.hh"
36 #include "identifier.hh"
37 #include "mudela-version.hh"
40 void strip_trailing_white (String&);
41 void strip_leading_white (String&);
45 valid_version_b (String s);
49 #define start_quote() \
50 yy_push_state (quote);\
51 yylval.string = new String
53 #define yylval (*(YYSTYPE*)lexval_l)
55 #define YY_USER_ACTION add_lexed_char (YYLeng ());
58 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
68 %option yyclass="My_lily_lexer"
70 %option never-interactive
88 NATIONAL [\001-\006\021-\027\031\036\200-\377]
89 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
95 REAL ({INT}\.{N}*)|(-?\.{N}+)
102 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
110 // windows-suck-suck-suck
113 <INITIAL,chords,incl,lyrics,notes>{
115 yy_push_state (longcomment);
119 %[^{\n] { // backup rule
130 <INITIAL,chords,lyrics,notes>\\version{WHITE}* {
131 yy_push_state (version);
133 <version>\"[^"]*\";? { /* got the include file name */
134 String s (YYText ()+1);
135 s = s.left_str (s.index_last_i ('"'));
136 DEBUG_OUT << "#version `" << s << "'\n";
137 if (!valid_version_b (s))
142 LexerError ("No quoted string found after \\version");
155 LexerError (_ ("EOF found inside a comment").ch_C ());
156 if (! close_input ())
157 yyterminate (); // can't move this, since it actually rets a YY_NULL
162 <INITIAL,chords,lyrics,notes>\\maininput {
166 main_input_b_ = true;
169 error (_ ("\\maininput disallowed outside init files"));
172 <INITIAL,chords,lyrics,notes>\\include {
173 yy_push_state (incl);
175 <incl>\"[^"]*\";? { /* got the include file name */
176 String s (YYText ()+1);
177 s = s.left_str (s.index_last_i ('"'));
178 DEBUG_OUT << "#include `" << s << "'\n";
179 new_input (s,source_global_l);
182 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
183 String s = YYText () + 1;
184 strip_trailing_white (s);
185 if (s.length_i () && (s[s.length_i () - 1] == ';'))
186 s = s.left_str (s.length_i () - 1);
187 DEBUG_OUT << "#include `\\" << s << "'\n";
188 Identifier * id = lookup_identifier (s);
191 String* s_l = id->access_content_String (false);
192 DEBUG_OUT << "#include `" << *s_l << "'\n";
193 new_input (*s_l, source_global_l);
199 String msg (_f ("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.string = new String (s);
210 DEBUG_OUT << "rest:"<< yylval.string;
216 <INITIAL,chords,lyrics,notes>\\\${BLACK}*{WHITE} {
217 String s=YYText () + 2;
218 s=s.left_str (s.length_i () - 1);
219 return scan_escaped_word (s);
221 <INITIAL,chords,lyrics,notes>\${BLACK}*{WHITE} {
222 String s=YYText () + 1;
223 s=s.left_str (s.length_i () - 1);
224 return scan_bare_word (s);
226 <INITIAL,chords,lyrics,notes>\\\${BLACK}* { // backup rule
227 cerr << _ ("white expected") << endl;
230 <INITIAL,chords,lyrics,notes>\${BLACK}* { // backup rule
231 cerr << _ ("white expected") << endl;
236 return scan_bare_word (YYText ());
240 return scan_escaped_word (YYText () + 1);
244 yylval.i = String_convert::dec2_i (String (YYText ()));
248 yylval.i = String_convert::dec2_i (String (YYText ()));
262 *yylval.string += to_str (escaped_char(YYText()[1]));
265 *yylval.string += YYText ();
268 DEBUG_OUT << "quoted string: `" << *yylval.string << "'\n";
273 *yylval.string += YYText ();
282 yylval.i = String_convert::dec2_i (String (YYText ()));
286 return scan_escaped_word (YYText () + 1);
290 String s (YYText ());
292 return yylval.i = EXTENDER;
294 return yylval.i = HYPHEN;
296 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
297 *(s.ch_l () + i) = ' ';
298 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
300 *(s.ch_l () + i + 1) = 'c';
301 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
304 char c = s[s.length_i () - 1];
305 if (c == '{' && c == '}') // brace open is for not confusing dumb tools.
306 here_input ().warning (
307 "Brace found at end of lyric. Did you forget a space?");
308 yylval.string = new String (s);
309 DEBUG_OUT << "lyric : `" << s << "'\n";
313 return yylval.c = YYText ()[0];
318 return scan_bare_word (YYText ());
321 return scan_escaped_word (YYText () + 1);
324 yylval.i = String_convert::dec2_i (String (YYText ()));
337 return yylval.c = YYText ()[0];
342 DEBUG_OUT << "<<eof>>";
344 if (! close_input ()) {
345 yyterminate (); // can't move this, since it actually rets a YY_NULL
351 return scan_bare_word (YYText ());
354 return scan_escaped_word (YYText () + 1);
358 int cnv=sscanf (YYText (), "%lf", &r);
360 DEBUG_OUT << "REAL" << r<<'\n';
366 yylval.i = String_convert::dec2_i (String (YYText ()));
372 DEBUG_OUT << "parens\n";
376 char c = YYText ()[0];
377 DEBUG_OUT << "misc char" <<c<<"\n";
382 return yylval.c = YYText ()[0];
385 <INITIAL,lyrics,notes>\\. {
386 char c= YYText ()[1];
394 return E_EXCLAMATION;
401 String msg = _f ("invalid character: `%c'", YYText ()[0]);
402 LexerError (msg.ch_C ());
409 My_lily_lexer::push_note_state ()
411 yy_push_state (notes);
415 My_lily_lexer::push_chord_state ()
417 yy_push_state (chords);
421 My_lily_lexer::push_lyric_state ()
423 yy_push_state (lyrics);
427 My_lily_lexer::pop_state ()
433 My_lily_lexer::scan_escaped_word (String str)
435 DEBUG_OUT << "\\word: `" << str<<"'\n";
436 int l = lookup_keyword (str);
438 DEBUG_OUT << "(keyword)\n";
441 Identifier * id = lookup_identifier (str);
443 DEBUG_OUT << "(identifier)\n";
445 return id->token_code_i_;
447 if ((YYSTATE != notes) && (YYSTATE != chords)) {
448 if (notename_b (str)) {
449 yylval.pitch = new Musical_pitch (lookup_notename (str));
450 yylval.pitch->set_spot (Input (source_file_l (),
452 return NOTENAME_PITCH;
456 print_declarations (true);
457 String msg (_f ("unknown escaped string: `\\%s'", str));
458 LexerError (msg.ch_C ());
459 DEBUG_OUT << "(string)";
460 String *sp = new String (str);
466 My_lily_lexer::scan_bare_word (String str)
468 DEBUG_OUT << "word: `" << str<< "'\n";
469 if ((YYSTATE == notes) || (YYSTATE == chords)) {
470 if (notename_b (str)) {
471 DEBUG_OUT << "(notename)\n";
472 yylval.pitch = new Musical_pitch (lookup_notename (str));
473 yylval.pitch->set_spot (Input (source_file_l (),
475 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
476 } else if (chordmodifier_b (str)) {
477 DEBUG_OUT << "(chordmodifier)\n";
478 yylval.pitch = new Musical_pitch (lookup_chordmodifier (str));
479 yylval.pitch->set_spot (Input (source_file_l (),
481 return CHORDMODIFIER_PITCH;
485 yylval.string=new String (str);
490 My_lily_lexer::note_state_b () const
492 return YY_START == notes;
496 My_lily_lexer::chord_state_b () const
498 return YY_START == chords;
502 My_lily_lexer::lyric_state_b () const
504 return YY_START == lyrics;
508 urg, belong to String(_convert)
509 and should be generalised
512 strip_leading_white (String&s)
515 for (; i < s.length_i (); i++)
519 s = s.nomid_str (0, i);
523 strip_trailing_white (String&s)
530 s = s.left_str (i+1);
537 valid_version_b (String s)
539 Mudela_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
540 Mudela_version ver (s);
541 if (!((ver >= oldest_version) && (ver <= current)))
543 error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), current.str ()));
544 if (!version_ignore_global_b)