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
133 <notes,INITIAL,lyrics>\\include {
134 yy_push_state (incl);
136 <incl>\"[^"]*\" { /* got the include file name */
137 String s (YYText ()+1);
138 s = s.left_str (s.length_i ()-1);
139 DOUT << "#include `" << s << "\'\n";
140 new_input (s,source_global_l);
143 <incl>\"[^"]* { // backup rule
144 cerr << "missing end quote" << endl;
148 const char *s = YYText ();
149 yylval.string = new String (s);
150 DOUT << "rest:"<< yylval.string;
153 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE} {
154 String s=YYText () + 2;
155 s=s.left_str (s.length_i () - 1);
156 return scan_escaped_word (s);
158 <INITIAL,lyrics,notes>\${BLACK}*{WHITE} {
159 String s=YYText () + 1;
160 s=s.left_str (s.length_i () - 1);
161 return scan_bare_word (s);
163 <INITIAL,lyrics,notes>\\\${BLACK}* { // backup rule
164 cerr << "white expected" << endl;
167 <INITIAL,lyrics,notes>\${BLACK}* { // backup rule
168 cerr << "white expected" << endl;
173 post_quotes_b_ = true;
174 return scan_bare_word (YYText ());
177 yylval.i = YYLeng ();
178 if (post_quotes_b_) {
179 post_quotes_b_ = false;
185 return scan_bare_word (YYText ());
190 return scan_escaped_word (YYText ()+1);
194 yylval.i = String_convert::dec2_i (String (YYText ()));
199 yylval.i = String_convert::dec2_i (String (YYText ()));
213 *yylval.string += escaped_char(YYText()[1]);
216 *yylval.string += YYText ();
219 DOUT << "quoted string: `" << *yylval.string << "'\n";
224 *yylval.string += YYText ();
234 yylval.i = String_convert::dec2_i (String (YYText ()));
238 return scan_escaped_word (YYText ()+1);
242 String s (YYText ());
244 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
245 *(s.ch_l () + i) = ' ';
246 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
248 *(s.ch_l () + i + 1) = 'c';
249 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
251 yylval.string = new String (s);
252 DOUT << "lyric : `" << s << "'\n";
256 return yylval.c = YYText ()[0];
263 if (! close_input ()) {
264 yyterminate (); // can't move this, since it actually rets a YY_NULL
268 return scan_bare_word (YYText ());
271 return scan_escaped_word (YYText ()+1);
275 int cnv=sscanf (YYText (), "%lf", &r);
277 DOUT << "REAL" << r<<'\n';
283 yylval.i = String_convert::dec2_i (String (YYText ()));
293 char c = YYText ()[0];
294 DOUT << "misc char" <<c<<"\n";
298 <lyrics,notes>{PLET} {
299 return yylval.i = PLET;
302 <lyrics,notes>{TELP} {
303 return yylval.i = TELP;
307 return yylval.c = YYText ()[0];
310 <INITIAL,lyrics,notes>\\. {
311 char c= YYText ()[1];
319 return E_EXCLAMATION;
326 String msg= String ("illegal character: ") +String (YYText ()[0]);
327 LexerError (msg.ch_C ());
334 My_lily_lexer::push_note_state ()
336 yy_push_state (notes);
340 My_lily_lexer::push_lyric_state ()
342 yy_push_state (lyrics);
345 My_lily_lexer::pop_state ()
351 My_lily_lexer::scan_escaped_word (String str)
353 DOUT << "\\word: `" << str<<"'\n";
354 int l = lookup_keyword (str);
356 DOUT << "(keyword)\n";
359 Identifier * id = lookup_identifier (str);
361 DOUT << "(identifier)\n";
363 return id->token_code_i_;
365 if (YYSTATE != notes) {
366 Melodic_req * mel_l = lookup_melodic_req_l (str);
368 DOUT << "(notename)\n";
369 yylval.melreq = mel_l;
373 String msg ("Unknown escaped string: `" + str + "'");
374 LexerError (msg.ch_C ());
376 String *sp = new String (str);
382 My_lily_lexer::scan_bare_word (String str)
384 DOUT << "word: `" << str<< "'\n";
385 if (YYSTATE == notes){
386 Melodic_req * mel_l = lookup_melodic_req_l (str);
388 DOUT << "(notename)\n";
389 yylval.melreq = mel_l;
394 yylval.string=new String (str);
399 My_lily_lexer::note_state_b () const
401 return YY_START == notes;
405 My_lily_lexer::lyric_state_b () const
407 return YY_START == lyrics;
411 strip_trailing_white (String&s)
414 for (; i < s.length_i (); i++)
418 s = s.nomid_str (0, i);
422 strip_leading_white (String&s)
429 s = s.left_str (i+1);