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]*
103 // windows-suck-suck-suck
106 <INITIAL,chords,incl,lyrics,notes>{
108 yy_push_state (longcomment);
112 %[^{\n] { // backup rule
133 LexerError (_ ("EOF found inside a comment").ch_C ());
134 if (! close_input ())
135 yyterminate (); // can't move this, since it actually rets a YY_NULL
140 <INITIAL,chords,lyrics,notes>\\maininput {
144 main_input_b_ = true;
147 error ("\\maininput disallowed outside init files.");
150 <INITIAL,chords,lyrics,notes>\\include {
151 yy_push_state (incl);
153 <incl>\"[^"]*\";? { /* got the include file name */
154 String s (YYText ()+1);
155 s = s.left_str (s.index_last_i ('"'));
156 DOUT << "#include `" << s << "\'\n";
157 new_input (s,source_global_l);
160 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
161 String s = YYText () + 1;
162 strip_trailing_white (s);
163 if (s.length_i () && (s[s.length_i () - 1] == ';'))
164 s = s.left_str (s.length_i () - 1);
165 DOUT << "#include `\\" << s << "'\n";
166 Identifier * id = lookup_identifier (s);
169 String* s_l = id->access_content_String (false);
170 DOUT << "#include `" << *s_l << "\'\n";
171 new_input (*s_l, source_global_l);
177 String msg (_f ("undefined identifier: `%s\'", s ));
178 LexerError (msg.ch_C ());
181 <incl>\"[^"]* { // backup rule
182 cerr << _ ("missing end quote") << endl;
186 const char *s = YYText ();
187 yylval.string = new String (s);
188 DOUT << "rest:"<< yylval.string;
194 <INITIAL,chords,lyrics,notes>\\\${BLACK}*{WHITE} {
195 String s=YYText () + 2;
196 s=s.left_str (s.length_i () - 1);
197 return scan_escaped_word (s);
199 <INITIAL,chords,lyrics,notes>\${BLACK}*{WHITE} {
200 String s=YYText () + 1;
201 s=s.left_str (s.length_i () - 1);
202 return scan_bare_word (s);
204 <INITIAL,chords,lyrics,notes>\\\${BLACK}* { // backup rule
205 cerr << _ ("white expected") << endl;
208 <INITIAL,chords,lyrics,notes>\${BLACK}* { // backup rule
209 cerr << _ ("white expected") << endl;
214 return scan_bare_word (YYText ());
218 return scan_escaped_word (YYText () + 1);
222 yylval.i = String_convert::dec2_i (String (YYText ()));
227 yylval.i = String_convert::dec2_i (String (YYText ()));
241 *yylval.string += to_str (escaped_char(YYText()[1]));
244 *yylval.string += YYText ();
247 DOUT << "quoted string: `" << *yylval.string << "'\n";
252 *yylval.string += YYText ();
261 yylval.i = String_convert::dec2_i (String (YYText ()));
265 return scan_escaped_word (YYText () + 1);
269 String s (YYText ());
271 return yylval.i = EXTENDER;
273 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
274 *(s.ch_l () + i) = ' ';
275 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
277 *(s.ch_l () + i + 1) = 'c';
278 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
280 yylval.string = new String (s);
281 DOUT << "lyric : `" << s << "'\n";
285 return yylval.c = YYText ()[0];
290 return scan_bare_word (YYText ());
293 return scan_escaped_word (YYText () + 1);
296 return scan_escaped_word (YYText () + 1);
299 yylval.i = String_convert::dec2_i (String (YYText ()));
303 return yylval.c = YYText ()[0];
310 if (! close_input ()) {
311 yyterminate (); // can't move this, since it actually rets a YY_NULL
317 return scan_bare_word (YYText ());
320 return scan_escaped_word (YYText () + 1);
324 int cnv=sscanf (YYText (), "%lf", &r);
326 DOUT << "REAL" << r<<'\n';
332 yylval.i = String_convert::dec2_i (String (YYText ()));
342 char c = YYText ()[0];
343 DOUT << "misc char" <<c<<"\n";
347 <lyrics,notes>{PLET} {
348 return yylval.i = PLET;
351 <lyrics,notes>{TELP} {
352 return yylval.i = TELP;
356 return yylval.c = YYText ()[0];
359 <INITIAL,lyrics,notes>\\. {
360 char c= YYText ()[1];
368 return E_EXCLAMATION;
375 String msg = _f ("illegal character: `%c\'", YYText ()[0]);
376 LexerError (msg.ch_C ());
383 My_lily_lexer::push_note_state ()
385 yy_push_state (notes);
389 My_lily_lexer::push_chord_state ()
391 yy_push_state (chords);
395 My_lily_lexer::push_lyric_state ()
397 yy_push_state (lyrics);
401 My_lily_lexer::pop_state ()
407 My_lily_lexer::scan_escaped_word (String str)
409 DOUT << "\\word: `" << str<<"'\n";
410 int l = lookup_keyword (str);
412 DOUT << "(keyword)\n";
415 Identifier * id = lookup_identifier (str);
417 DOUT << "(identifier)\n";
419 return id->token_code_i_;
421 if ((YYSTATE != notes) && (YYSTATE != chords)) {
422 if (notename_b (str)) {
423 yylval.pitch = new Musical_pitch (lookup_notename (str));
424 yylval.pitch->set_spot (Input (source_file_l (),
426 return NOTENAME_PITCH;
430 print_declarations (true);
431 String msg (_f ("unknown escaped string: `\\%s\'", str));
432 LexerError (msg.ch_C ());
434 String *sp = new String (str);
440 My_lily_lexer::scan_bare_word (String str)
442 DOUT << "word: `" << str<< "'\n";
443 if ((YYSTATE == notes) || (YYSTATE == chords)) {
444 if (notename_b (str)) {
445 DOUT << "(notename)\n";
446 yylval.pitch = new Musical_pitch (lookup_notename (str));
447 yylval.pitch->set_spot (Input (source_file_l (),
449 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
450 } else if (chordmodifier_b (str)) {
451 DOUT << "(chordmodifier)\n";
452 yylval.pitch = new Musical_pitch (lookup_chordmodifier (str));
453 yylval.pitch->set_spot (Input (source_file_l (),
455 return CHORDMODIFIER_PITCH;
459 yylval.string=new String (str);
464 My_lily_lexer::note_state_b () const
466 return YY_START == notes;
470 My_lily_lexer::chord_state_b () const
472 return YY_START == chords;
476 My_lily_lexer::lyric_state_b () const
478 return YY_START == lyrics;
482 urg, belong to String(_convert)
483 and should be generalised
486 strip_leading_white (String&s)
489 for (; i < s.length_i (); i++)
493 s = s.nomid_str (0, i);
497 strip_trailing_white (String&s)
504 s = s.left_str (i+1);