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
78 NATIONAL [\001-\006\021-\027\031\036\200-\377]
79 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
85 REAL ({INT}\.{N}*)|(-?\.{N}+)
92 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
102 // windows-suck-suck-suck
105 <notes,incl,INITIAL,lyrics>{
107 yy_push_state (longcomment);
111 %[^{\n] { // backup rule
132 LexerError (_ ("EOF found inside a comment").ch_C ());
133 if (! close_input ())
134 yyterminate (); // can't move this, since it actually rets a YY_NULL
139 <notes,INITIAL,lyrics>\\maininput {
143 <notes,INITIAL,lyrics>\\include {
144 yy_push_state (incl);
146 <incl>\"[^"]*\";? { /* got the include file name */
147 String s (YYText ()+1);
148 s = s.left_str (s.index_last_i ('"'));
149 DOUT << "#include `" << s << "\'\n";
150 new_input (s,source_global_l);
153 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
154 String s = YYText () + 1;
155 strip_trailing_white (s);
156 if (s.length_i () && (s[s.length_i () - 1] == ';'))
157 s = s.left_str (s.length_i () - 1);
158 DOUT << "#include `\\" << s << "'\n";
159 Identifier * id = lookup_identifier (s);
162 String* s_l = id->access_content_String (false);
163 DOUT << "#include `" << *s_l << "\'\n";
164 new_input (*s_l, source_global_l);
170 String msg (_f ("undefined identifier: `%s\'", s ));
171 LexerError (msg.ch_C ());
174 <incl>\"[^"]* { // backup rule
175 cerr << _ ("missing end quote") << endl;
179 const char *s = YYText ();
180 yylval.string = new String (s);
181 DOUT << "rest:"<< yylval.string;
187 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE} {
188 String s=YYText () + 2;
189 s=s.left_str (s.length_i () - 1);
190 return scan_escaped_word (s);
192 <INITIAL,lyrics,notes>\${BLACK}*{WHITE} {
193 String s=YYText () + 1;
194 s=s.left_str (s.length_i () - 1);
195 return scan_bare_word (s);
197 <INITIAL,lyrics,notes>\\\${BLACK}* { // backup rule
198 cerr << _ ("white expected") << endl;
201 <INITIAL,lyrics,notes>\${BLACK}* { // backup rule
202 cerr << _ ("white expected") << endl;
208 return scan_bare_word (YYText ());
213 return scan_escaped_word (YYText () + 1);
217 yylval.i = String_convert::dec2_i (String (YYText ()));
222 yylval.i = String_convert::dec2_i (String (YYText ()));
236 *yylval.string += to_str (escaped_char(YYText()[1]));
239 *yylval.string += YYText ();
242 DOUT << "quoted string: `" << *yylval.string << "'\n";
247 *yylval.string += YYText ();
257 yylval.i = String_convert::dec2_i (String (YYText ()));
261 return scan_escaped_word (YYText () + 1);
265 String s (YYText ());
267 return yylval.i = EXTENDER;
269 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
270 *(s.ch_l () + i) = ' ';
271 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
273 *(s.ch_l () + i + 1) = 'c';
274 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
276 yylval.string = new String (s);
277 DOUT << "lyric : `" << s << "'\n";
281 return yylval.c = YYText ()[0];
288 if (! close_input ()) {
289 yyterminate (); // can't move this, since it actually rets a YY_NULL
295 return scan_bare_word (YYText ());
298 return scan_escaped_word (YYText () + 1);
302 int cnv=sscanf (YYText (), "%lf", &r);
304 DOUT << "REAL" << r<<'\n';
310 yylval.i = String_convert::dec2_i (String (YYText ()));
320 char c = YYText ()[0];
321 DOUT << "misc char" <<c<<"\n";
325 <lyrics,notes>{PLET} {
326 return yylval.i = PLET;
329 <lyrics,notes>{TELP} {
330 return yylval.i = TELP;
334 return yylval.c = YYText ()[0];
337 <INITIAL,lyrics,notes>\\. {
338 char c= YYText ()[1];
346 return E_EXCLAMATION;
353 String msg = _f ("illegal character: `%c\'", YYText ()[0]);
354 LexerError (msg.ch_C ());
361 My_lily_lexer::push_note_state ()
363 yy_push_state (notes);
367 My_lily_lexer::push_lyric_state ()
369 yy_push_state (lyrics);
372 My_lily_lexer::pop_state ()
378 My_lily_lexer::scan_escaped_word (String str)
380 DOUT << "\\word: `" << str<<"'\n";
381 int l = lookup_keyword (str);
383 DOUT << "(keyword)\n";
386 Identifier * id = lookup_identifier (str);
388 DOUT << "(identifier)\n";
390 return id->token_code_i_;
392 if (YYSTATE != notes) {
393 if (notename_b (str))
395 yylval.pitch = new Musical_pitch (lookup_pitch (str));
396 yylval.pitch->set_spot (Input (source_file_l (),
398 return NOTENAME_PITCH;
402 print_declarations (true);
403 String msg (_f ("unknown escaped string: `\\%s\'", str));
404 LexerError (msg.ch_C ());
406 String *sp = new String (str);
412 My_lily_lexer::scan_bare_word (String str)
414 DOUT << "word: `" << str<< "'\n";
415 if (YYSTATE == notes){
416 if (notename_b (str)) {
417 DOUT << "(notename)\n";
418 yylval.pitch = new Musical_pitch (lookup_pitch (str));
419 yylval.pitch->set_spot (Input (source_file_l (),
421 return NOTENAME_PITCH;
425 yylval.string=new String (str);
430 My_lily_lexer::note_state_b () const
432 return YY_START == notes;
436 My_lily_lexer::lyric_state_b () const
438 return YY_START == lyrics;
442 urg, belong to String(_convert)
443 and should be generalised
446 strip_leading_white (String&s)
449 for (; i < s.length_i (); i++)
453 s = s.nomid_str (0, i);
457 strip_trailing_white (String&s)
464 s = s.left_str (i+1);