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}
77 REAL ({INT}\.{N}*)|(-?\.{N}+)
84 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
93 // windows-suck-suck-suck
96 <notes,incl,INITIAL,lyrics>{
98 yy_push_state (longcomment);
102 %[^{\n] { // backup rule
123 LexerError ("EOF found inside a comment");
124 if (! close_input ())
125 yyterminate (); // can't move this, since it actually rets a YY_NULL
131 <notes,INITIAL,lyrics>\\include {
132 yy_push_state (incl);
134 <incl>\"[^"]*\" { /* got the include file name */
135 String s (YYText ()+1);
136 s = s.left_str (s.length_i ()-1);
137 DOUT << "#include `" << s << "\'\n";
138 new_input (s,source_global_l);
141 <incl>\"[^"]* { // backup rule
142 cerr << "missing end quote" << endl;
146 const char *s = YYText ();
147 yylval.string = new String (s);
148 DOUT << "rest:"<< yylval.string;
151 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE} {
152 String s=YYText () + 2;
153 s=s.left_str (s.length_i () - 1);
154 return scan_escaped_word (s);
156 <INITIAL,lyrics,notes>\${BLACK}*{WHITE} {
157 String s=YYText () + 1;
158 s=s.left_str (s.length_i () - 1);
159 return scan_bare_word (s);
161 <INITIAL,lyrics,notes>\\\${BLACK}* { // backup rule
162 cerr << "white expected" << endl;
165 <INITIAL,lyrics,notes>\${BLACK}* { // backup rule
166 cerr << "white expected" << endl;
171 post_quotes_b_ = true;
172 return scan_bare_word (YYText ());
175 yylval.i = YYLeng ();
176 if (post_quotes_b_) {
177 post_quotes_b_ = false;
183 return scan_bare_word (YYText ());
188 return scan_escaped_word (YYText ()+1);
192 yylval.i = String_convert::dec2_i (String (YYText ()));
206 *yylval.string += escaped_char(YYText()[1]);
209 *yylval.string += YYText ();
212 DOUT << "quoted string: `" << *yylval.string << "'\n";
217 *yylval.string += YYText ();
227 yylval.i = String_convert::dec2_i (String (YYText ()));
231 return scan_escaped_word (YYText ()+1);
235 String s (YYText ());
237 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
238 *(s.ch_l () + i) = ' ';
239 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
241 *(s.ch_l () + i + 1) = 'c';
242 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
244 yylval.string = new String (s);
245 DOUT << "lyric : `" << s << "'\n";
249 return yylval.c = YYText ()[0];
256 if (! close_input ()) {
257 yyterminate (); // can't move this, since it actually rets a YY_NULL
261 return scan_bare_word (YYText ());
264 return scan_escaped_word (YYText ()+1);
268 int cnv=sscanf (YYText (), "%lf", &r);
270 DOUT << "REAL" << r<<'\n';
276 yylval.i = String_convert::dec2_i (String (YYText ()));
286 char c = YYText ()[0];
287 DOUT << "misc char" <<c<<"\n";
291 <lyrics,notes>{PLET} {
292 return yylval.i = PLET;
295 <lyrics,notes>{TELP} {
296 return yylval.i = TELP;
300 return yylval.c = YYText ()[0];
303 <INITIAL,lyrics,notes>\\. {
304 char c= YYText ()[1];
312 return E_EXCLAMATION;
319 String msg= String ("illegal character: ") +String (YYText ()[0]);
320 LexerError (msg.ch_C ());
327 My_lily_lexer::push_note_state ()
329 yy_push_state (notes);
333 My_lily_lexer::push_lyric_state ()
335 yy_push_state (lyrics);
338 My_lily_lexer::pop_state ()
344 My_lily_lexer::scan_escaped_word (String str)
346 DOUT << "\\word: `" << str<<"'\n";
347 int l = lookup_keyword (str);
349 DOUT << "(keyword)\n";
352 Identifier * id = lookup_identifier (str);
354 DOUT << "(identifier)\n";
356 return id->token_code_i_;
358 if (YYSTATE != notes) {
359 Melodic_req * mel_l = lookup_melodic_req_l (str);
361 DOUT << "(notename)\n";
362 yylval.melreq = mel_l;
366 String msg ("Unknown escaped string: `" + str + "'");
367 LexerError (msg.ch_C ());
369 String *sp = new String (str);
375 My_lily_lexer::scan_bare_word (String str)
377 DOUT << "word: `" << str<< "'\n";
378 if (YYSTATE == notes){
379 Melodic_req * mel_l = lookup_melodic_req_l (str);
381 DOUT << "(notename)\n";
382 yylval.melreq = mel_l;
387 yylval.string=new String (str);
392 My_lily_lexer::note_state_b () const
394 return YY_START == notes;
398 My_lily_lexer::lyric_state_b () const
400 return YY_START == lyrics;
404 strip_trailing_white (String&s)
407 for (; i < s.length_i (); i++)
411 s = s.nomid_str (0, i);
415 strip_leading_white (String&s)
422 s = s.left_str (i+1);