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"
34 #include "parseconstruct.hh"
36 #include "musical-request.hh"
37 #include "identifier.hh"
38 void strip_trailing_white (String&);
39 void strip_leading_white (String&);
41 #define start_quote() \
42 yy_push_state (quote);\
43 yylval.string = new String
45 #define yylval (*(YYSTYPE*)lexval_l)
47 #define YY_USER_ACTION add_lexed_char (YYLeng ());
50 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
60 %option yyclass="My_lily_lexer"
62 %option never-interactive
79 NATIONAL [\001-\006\021-\027\031\036\200-\377]
80 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
86 REAL ({INT}\.{N}*)|(-?\.{N}+)
93 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
101 // windows-suck-suck-suck
104 <INITIAL,chords,incl,lyrics,notes>{
106 yy_push_state (longcomment);
110 %[^{\n] { // backup rule
131 LexerError (_ ("EOF found inside a comment").ch_C ());
132 if (! close_input ())
133 yyterminate (); // can't move this, since it actually rets a YY_NULL
138 <INITIAL,chords,lyrics,notes>\\maininput {
142 main_input_b_ = true;
145 error ("\\maininput disallowed outside init files.");
148 <INITIAL,chords,lyrics,notes>\\include {
149 yy_push_state (incl);
151 <incl>\"[^"]*\";? { /* got the include file name */
152 String s (YYText ()+1);
153 s = s.left_str (s.index_last_i ('"'));
154 DOUT << "#include `" << s << "\'\n";
155 new_input (s,source_global_l);
158 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
159 String s = YYText () + 1;
160 strip_trailing_white (s);
161 if (s.length_i () && (s[s.length_i () - 1] == ';'))
162 s = s.left_str (s.length_i () - 1);
163 DOUT << "#include `\\" << s << "'\n";
164 Identifier * id = lookup_identifier (s);
167 String* s_l = id->access_content_String (false);
168 DOUT << "#include `" << *s_l << "\'\n";
169 new_input (*s_l, source_global_l);
175 String msg (_f ("undefined identifier: `%s\'", s ));
176 LexerError (msg.ch_C ());
179 <incl>\"[^"]* { // backup rule
180 cerr << _ ("missing end quote") << endl;
184 const char *s = YYText ();
185 yylval.string = new String (s);
186 DOUT << "rest:"<< yylval.string;
192 <INITIAL,chords,lyrics,notes>\\\${BLACK}*{WHITE} {
193 String s=YYText () + 2;
194 s=s.left_str (s.length_i () - 1);
195 return scan_escaped_word (s);
197 <INITIAL,chords,lyrics,notes>\${BLACK}*{WHITE} {
198 String s=YYText () + 1;
199 s=s.left_str (s.length_i () - 1);
200 return scan_bare_word (s);
202 <INITIAL,chords,lyrics,notes>\\\${BLACK}* { // backup rule
203 cerr << _ ("white expected") << endl;
206 <INITIAL,chords,lyrics,notes>\${BLACK}* { // backup rule
207 cerr << _ ("white expected") << endl;
212 return scan_bare_word (YYText ());
216 return scan_escaped_word (YYText () + 1);
220 yylval.i = String_convert::dec2_i (String (YYText ()));
225 yylval.i = String_convert::dec2_i (String (YYText ()));
239 *yylval.string += to_str (escaped_char(YYText()[1]));
242 *yylval.string += YYText ();
245 DOUT << "quoted string: `" << *yylval.string << "'\n";
250 *yylval.string += YYText ();
259 yylval.i = String_convert::dec2_i (String (YYText ()));
263 return scan_escaped_word (YYText () + 1);
267 String s (YYText ());
269 return yylval.i = EXTENDER;
271 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
272 *(s.ch_l () + i) = ' ';
273 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
275 *(s.ch_l () + i + 1) = 'c';
276 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
278 yylval.string = new String (s);
279 DOUT << "lyric : `" << s << "'\n";
283 return yylval.c = YYText ()[0];
288 return scan_bare_word (YYText ());
291 return scan_escaped_word (YYText () + 1);
294 yylval.i = String_convert::dec2_i (String (YYText ()));
298 return yylval.c = YYText ()[0];
305 if (! close_input ()) {
306 yyterminate (); // can't move this, since it actually rets a YY_NULL
312 return scan_bare_word (YYText ());
315 return scan_escaped_word (YYText () + 1);
319 int cnv=sscanf (YYText (), "%lf", &r);
321 DOUT << "REAL" << r<<'\n';
327 yylval.i = String_convert::dec2_i (String (YYText ()));
337 char c = YYText ()[0];
338 DOUT << "misc char" <<c<<"\n";
343 return yylval.c = YYText ()[0];
346 <INITIAL,lyrics,notes>\\. {
347 char c= YYText ()[1];
355 return E_EXCLAMATION;
362 String msg = _f ("illegal character: `%c\'", YYText ()[0]);
363 LexerError (msg.ch_C ());
370 My_lily_lexer::push_note_state ()
372 yy_push_state (notes);
376 My_lily_lexer::push_chord_state ()
378 yy_push_state (chords);
382 My_lily_lexer::push_lyric_state ()
384 yy_push_state (lyrics);
388 My_lily_lexer::pop_state ()
394 My_lily_lexer::scan_escaped_word (String str)
396 DOUT << "\\word: `" << str<<"'\n";
397 int l = lookup_keyword (str);
399 DOUT << "(keyword)\n";
402 Identifier * id = lookup_identifier (str);
404 DOUT << "(identifier)\n";
406 return id->token_code_i_;
408 if ((YYSTATE != notes) && (YYSTATE != chords)) {
409 if (notename_b (str)) {
410 yylval.pitch = new Musical_pitch (lookup_notename (str));
411 yylval.pitch->set_spot (Input (source_file_l (),
413 return NOTENAME_PITCH;
417 print_declarations (true);
418 String msg (_f ("unknown escaped string: `\\%s\'", str));
419 LexerError (msg.ch_C ());
421 String *sp = new String (str);
427 My_lily_lexer::scan_bare_word (String str)
429 DOUT << "word: `" << str<< "'\n";
430 if ((YYSTATE == notes) || (YYSTATE == chords)) {
431 if (notename_b (str)) {
432 DOUT << "(notename)\n";
433 yylval.pitch = new Musical_pitch (lookup_notename (str));
434 yylval.pitch->set_spot (Input (source_file_l (),
436 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
437 } else if (chordmodifier_b (str)) {
438 DOUT << "(chordmodifier)\n";
439 yylval.pitch = new Musical_pitch (lookup_chordmodifier (str));
440 yylval.pitch->set_spot (Input (source_file_l (),
442 return CHORDMODIFIER_PITCH;
446 yylval.string=new String (str);
451 My_lily_lexer::note_state_b () const
453 return YY_START == notes;
457 My_lily_lexer::chord_state_b () const
459 return YY_START == chords;
463 My_lily_lexer::lyric_state_b () const
465 return YY_START == lyrics;
469 urg, belong to String(_convert)
470 and should be generalised
473 strip_leading_white (String&s)
476 for (; i < s.length_i (); i++)
480 s = s.nomid_str (0, i);
484 strip_trailing_white (String&s)
491 s = s.left_str (i+1);