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 <INITIAL,chords,lyrics,notes>\\include {
145 yy_push_state (incl);
147 <incl>\"[^"]*\";? { /* got the include file name */
148 String s (YYText ()+1);
149 s = s.left_str (s.index_last_i ('"'));
150 DOUT << "#include `" << s << "\'\n";
151 new_input (s,source_global_l);
154 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
155 String s = YYText () + 1;
156 strip_trailing_white (s);
157 if (s.length_i () && (s[s.length_i () - 1] == ';'))
158 s = s.left_str (s.length_i () - 1);
159 DOUT << "#include `\\" << s << "'\n";
160 Identifier * id = lookup_identifier (s);
163 String* s_l = id->access_content_String (false);
164 DOUT << "#include `" << *s_l << "\'\n";
165 new_input (*s_l, source_global_l);
171 String msg (_f ("undefined identifier: `%s\'", s ));
172 LexerError (msg.ch_C ());
175 <incl>\"[^"]* { // backup rule
176 cerr << _ ("missing end quote") << endl;
180 const char *s = YYText ();
181 yylval.string = new String (s);
182 DOUT << "rest:"<< yylval.string;
188 <INITIAL,chords,lyrics,notes>\\\${BLACK}*{WHITE} {
189 String s=YYText () + 2;
190 s=s.left_str (s.length_i () - 1);
191 return scan_escaped_word (s);
193 <INITIAL,chords,lyrics,notes>\${BLACK}*{WHITE} {
194 String s=YYText () + 1;
195 s=s.left_str (s.length_i () - 1);
196 return scan_bare_word (s);
198 <INITIAL,chords,lyrics,notes>\\\${BLACK}* { // backup rule
199 cerr << _ ("white expected") << endl;
202 <INITIAL,chords,lyrics,notes>\${BLACK}* { // backup rule
203 cerr << _ ("white expected") << endl;
208 return scan_bare_word (YYText ());
212 return scan_escaped_word (YYText () + 1);
216 yylval.i = String_convert::dec2_i (String (YYText ()));
221 yylval.i = String_convert::dec2_i (String (YYText ()));
235 *yylval.string += to_str (escaped_char(YYText()[1]));
238 *yylval.string += YYText ();
241 DOUT << "quoted string: `" << *yylval.string << "'\n";
246 *yylval.string += YYText ();
255 yylval.i = String_convert::dec2_i (String (YYText ()));
259 return scan_escaped_word (YYText () + 1);
263 String s (YYText ());
265 return yylval.i = EXTENDER;
267 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
268 *(s.ch_l () + i) = ' ';
269 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
271 *(s.ch_l () + i + 1) = 'c';
272 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
274 yylval.string = new String (s);
275 DOUT << "lyric : `" << s << "'\n";
279 return yylval.c = YYText ()[0];
284 return scan_bare_word (YYText ());
287 yylval.i = String_convert::dec2_i (String (YYText ()));
291 return yylval.c = YYText ()[0];
298 if (! close_input ()) {
299 yyterminate (); // can't move this, since it actually rets a YY_NULL
305 return scan_bare_word (YYText ());
308 return scan_escaped_word (YYText () + 1);
312 int cnv=sscanf (YYText (), "%lf", &r);
314 DOUT << "REAL" << r<<'\n';
320 yylval.i = String_convert::dec2_i (String (YYText ()));
330 char c = YYText ()[0];
331 DOUT << "misc char" <<c<<"\n";
335 <lyrics,notes>{PLET} {
336 return yylval.i = PLET;
339 <lyrics,notes>{TELP} {
340 return yylval.i = TELP;
344 return yylval.c = YYText ()[0];
347 <INITIAL,lyrics,notes>\\. {
348 char c= YYText ()[1];
356 return E_EXCLAMATION;
363 String msg = _f ("illegal character: `%c\'", YYText ()[0]);
364 LexerError (msg.ch_C ());
371 My_lily_lexer::push_note_state ()
373 yy_push_state (notes);
377 My_lily_lexer::push_chord_state ()
379 yy_push_state (chords);
383 My_lily_lexer::push_lyric_state ()
385 yy_push_state (lyrics);
389 My_lily_lexer::pop_state ()
395 My_lily_lexer::scan_escaped_word (String str)
397 DOUT << "\\word: `" << str<<"'\n";
398 int l = lookup_keyword (str);
400 DOUT << "(keyword)\n";
403 Identifier * id = lookup_identifier (str);
405 DOUT << "(identifier)\n";
407 return id->token_code_i_;
409 if ((YYSTATE != notes) && (YYSTATE != chords)) {
410 if (notename_b (str))
412 yylval.pitch = new Musical_pitch (lookup_pitch (str));
413 yylval.pitch->set_spot (Input (source_file_l (),
415 return NOTENAME_PITCH;
419 print_declarations (true);
420 String msg (_f ("unknown escaped string: `\\%s\'", str));
421 LexerError (msg.ch_C ());
423 String *sp = new String (str);
429 My_lily_lexer::scan_bare_word (String str)
431 DOUT << "word: `" << str<< "'\n";
432 if ((YYSTATE == notes) || (YYSTATE == chords)) {
433 if (notename_b (str)) {
434 DOUT << "(notename)\n";
435 yylval.pitch = new Musical_pitch (lookup_pitch (str));
436 yylval.pitch->set_spot (Input (source_file_l (),
438 return NOTENAME_PITCH;
442 yylval.string=new String (str);
447 My_lily_lexer::note_state_b () const
449 return YY_START == notes;
453 My_lily_lexer::chord_state_b () const
455 return YY_START == chords;
459 My_lily_lexer::lyric_state_b () const
461 return YY_START == lyrics;
465 urg, belong to String(_convert)
466 and should be generalised
469 strip_leading_white (String&s)
472 for (; i < s.length_i (); i++)
476 s = s.nomid_str (0, i);
480 strip_trailing_white (String&s)
487 s = s.left_str (i+1);