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@stack.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 "identifier.hh"
37 void strip_trailing_white (String&);
38 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 ());
54 %option yyclass="My_lily_lexer"
56 %option never-interactive
72 NATIONAL [\001-\006\021-\027\031\036\200-\377]
73 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
79 REAL ({INT}\.{N}*)|(-?\.{N}+)
86 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
95 // windows-suck-suck-suck
98 <notes,incl,INITIAL,lyrics>{
100 yy_push_state (longcomment);
104 %[^{\n] { // backup rule
125 LexerError ("EOF found inside a comment");
126 if (! close_input ())
127 yyterminate (); // can't move this, since it actually rets a YY_NULL
132 <notes,INITIAL,lyrics>\\maininput {
136 <notes,INITIAL,lyrics>\\include {
137 yy_push_state (incl);
139 <incl>\"[^"]*\" { /* got the include file name */
140 String s (YYText ()+1);
141 s = s.left_str (s.length_i ()-1);
142 DOUT << "#include `" << s << "\'\n";
143 new_input (s,source_global_l);
146 <incl>\"[^"]* { // backup rule
147 cerr << "missing end quote" << endl;
151 const char *s = YYText ();
152 yylval.string = new String (s);
153 DOUT << "rest:"<< yylval.string;
156 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE} {
157 String s=YYText () + 2;
158 s=s.left_str (s.length_i () - 1);
159 return scan_escaped_word (s);
161 <INITIAL,lyrics,notes>\${BLACK}*{WHITE} {
162 String s=YYText () + 1;
163 s=s.left_str (s.length_i () - 1);
164 return scan_bare_word (s);
166 <INITIAL,lyrics,notes>\\\${BLACK}* { // backup rule
167 cerr << "white expected" << endl;
170 <INITIAL,lyrics,notes>\${BLACK}* { // backup rule
171 cerr << "white expected" << endl;
176 post_quotes_b_ = true;
177 return scan_bare_word (YYText ());
180 yylval.i = YYLeng ();
181 if (post_quotes_b_) {
182 post_quotes_b_ = false;
188 return scan_bare_word (YYText ());
193 return scan_escaped_word (YYText ()+1);
197 yylval.i = String_convert::dec2_i (String (YYText ()));
202 yylval.i = String_convert::dec2_i (String (YYText ()));
216 *yylval.string += escaped_char(YYText()[1]);
219 *yylval.string += YYText ();
222 DOUT << "quoted string: `" << *yylval.string << "'\n";
227 *yylval.string += YYText ();
237 yylval.i = String_convert::dec2_i (String (YYText ()));
241 return scan_escaped_word (YYText ()+1);
245 String s (YYText ());
247 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
248 *(s.ch_l () + i) = ' ';
249 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
251 *(s.ch_l () + i + 1) = 'c';
252 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
254 yylval.string = new String (s);
255 DOUT << "lyric : `" << s << "'\n";
259 return yylval.c = YYText ()[0];
266 if (! close_input ()) {
267 yyterminate (); // can't move this, since it actually rets a YY_NULL
271 return scan_bare_word (YYText ());
274 return scan_escaped_word (YYText ()+1);
278 int cnv=sscanf (YYText (), "%lf", &r);
280 DOUT << "REAL" << r<<'\n';
286 yylval.i = String_convert::dec2_i (String (YYText ()));
296 char c = YYText ()[0];
297 DOUT << "misc char" <<c<<"\n";
301 <lyrics,notes>{PLET} {
302 return yylval.i = PLET;
305 <lyrics,notes>{TELP} {
306 return yylval.i = TELP;
310 return yylval.c = YYText ()[0];
313 <INITIAL,lyrics,notes>\\. {
314 char c= YYText ()[1];
322 return E_EXCLAMATION;
329 String msg= String ("illegal character: ") +String (YYText ()[0]);
330 LexerError (msg.ch_C ());
337 My_lily_lexer::push_note_state ()
339 yy_push_state (notes);
343 My_lily_lexer::push_lyric_state ()
345 yy_push_state (lyrics);
348 My_lily_lexer::pop_state ()
354 My_lily_lexer::scan_escaped_word (String str)
356 DOUT << "\\word: `" << str<<"'\n";
357 int l = lookup_keyword (str);
359 DOUT << "(keyword)\n";
362 Identifier * id = lookup_identifier (str);
364 DOUT << "(identifier)\n";
366 return id->token_code_i_;
368 if (YYSTATE != notes) {
369 Melodic_req * mel_l = lookup_melodic_req_l (str);
371 DOUT << "(notename)\n";
372 yylval.melreq = mel_l;
376 String msg ("Unknown escaped string: `" + str + "'");
377 LexerError (msg.ch_C ());
379 String *sp = new String (str);
385 My_lily_lexer::scan_bare_word (String str)
387 DOUT << "word: `" << str<< "'\n";
388 if (YYSTATE == notes){
389 Melodic_req * mel_l = lookup_melodic_req_l (str);
391 DOUT << "(notename)\n";
392 yylval.melreq = mel_l;
397 yylval.string=new String (str);
402 My_lily_lexer::note_state_b () const
404 return YY_START == notes;
408 My_lily_lexer::lyric_state_b () const
410 return YY_START == lyrics;
414 strip_trailing_white (String&s)
417 for (; i < s.length_i (); i++)
421 s = s.nomid_str (0, i);
425 strip_leading_white (String&s)
432 s = s.left_str (i+1);