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"
33 #include "parseconstruct.hh"
35 #include "identifier.hh"
36 void strip_trailing_white (String&);
37 void strip_leading_white (String&);
40 #define start_quote() \
41 yy_push_state (quote);\
42 yylval.string = new String
44 #define yylval (*(YYSTYPE*)lexval_l)
46 #define YY_USER_ACTION add_lexed_char (YYLeng ());
53 %option yyclass="My_lily_lexer"
55 %option never-interactive
73 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
78 REAL ({INT}\.{N}*)|(-?\.{N}+)
85 LYRICS ({AA}|{NATIONAL})[^0-9 \t\n\f]*
91 // windows-suck-suck-suck
94 <notes,incl,INITIAL,lyrics>{
96 yy_push_state (longcomment);
100 %[^{\n] { // backup rule
121 LexerError ("EOF found inside a comment");
122 if (! close_input ())
123 yyterminate (); // can't move this, since it actually rets a YY_NULL
132 yylval.string = new String (s);
135 {HORIZONTALWHITE}+{BLACK}.*\n {
137 strip_leading_white (s);
138 strip_trailing_white (s);
139 yylval.string = new String (s);
151 <notes,INITIAL,lyrics>\\include {
152 yy_push_state (incl);
154 <incl>\"[^"]*\" { /* got the include file name */
155 String s (YYText ()+1);
156 s = s.left_str (s.length_i ()-1);
157 DOUT << "#include `" << s << "\'\n";
158 new_input (s,source_l_g);
161 <incl>\"[^"]* { // backup rule
162 cerr << "missing end quote" << endl;
166 const char *s = YYText ();
167 yylval.string = new String (s);
168 DOUT << "rest:"<< yylval.string;
171 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE} {
172 String s=YYText () + 2;
173 s=s.left_str (s.length_i () - 1);
174 return scan_escaped_word (s);
176 <INITIAL,lyrics,notes>\${BLACK}*{WHITE} {
177 String s=YYText () + 1;
178 s=s.left_str (s.length_i () - 1);
179 return scan_bare_word (s);
181 <INITIAL,lyrics,notes>\\\${BLACK}* { // backup rule
182 cerr << "white expected" << endl;
185 <INITIAL,lyrics,notes>\${BLACK}* { // backup rule
186 cerr << "white expected" << endl;
191 post_quotes_b_ = true;
192 return scan_bare_word (YYText ());
195 yylval.i = YYLeng ();
196 if (post_quotes_b_) {
197 post_quotes_b_ = false;
203 return scan_bare_word (YYText ());
208 return scan_escaped_word (YYText ()+1);
212 yylval.i = String_convert::dec2_i (String (YYText ()));
226 *yylval.string += '\\';
229 *yylval.string +='\"';
232 *yylval.string += YYText ();
235 DOUT << "quoted string: `" << *yylval.string << "'\n";
247 yylval.i = String_convert::dec2_i (String (YYText ()));
251 return scan_escaped_word (YYText ()+1);
255 String s (YYText ());
257 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
258 *(s.ch_l () + i) = ' ';
259 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
261 *(s.ch_l () + i + 1) = 'c';
262 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
264 yylval.string = new String (s);
265 DOUT << "lyric : `" << s << "'\n";
269 return yylval.c = YYText ()[0];
276 if (! close_input ()) {
277 yyterminate (); // can't move this, since it actually rets a YY_NULL
281 return scan_bare_word (YYText ());
284 return scan_escaped_word (YYText ()+1);
288 int cnv=sscanf (YYText (), "%lf", &r);
290 DOUT << "REAL" << r<<'\n';
296 yylval.i = String_convert::dec2_i (String (YYText ()));
306 char c = YYText ()[0];
307 DOUT << "misc char" <<c<<"\n";
312 return yylval.c = YYText ()[0];
315 <INITIAL,lyrics,notes>\\. {
316 char c= YYText ()[1];
324 return E_EXCLAMATION;
331 String msg= String ("illegal character: ") +String (YYText ()[0]);
332 LexerError (msg.ch_C ());
339 My_lily_lexer::push_note_state ()
341 yy_push_state (notes);
345 My_lily_lexer::push_lyric_state ()
347 yy_push_state (lyrics);
350 My_lily_lexer::pop_state ()
356 My_lily_lexer::scan_escaped_word (String str)
358 DOUT << "\\word: `" << str<<"'\n";
359 int l = lookup_keyword (str);
361 DOUT << "(keyword)\n";
364 Identifier * id = lookup_identifier (str);
366 DOUT << "(identifier)\n";
368 return id->token_code_i_;
370 if (YYSTATE != notes) {
371 Melodic_req * mel_l = lookup_melodic_req_l (str);
373 DOUT << "(notename)\n";
374 yylval.melreq = mel_l;
378 String msg ("Unknown escaped string: `" + str + "'");
379 LexerError (msg.ch_C ());
381 String *sp = new String (str);
387 My_lily_lexer::scan_bare_word (String str)
389 DOUT << "word: `" << str<< "'\n";
390 if (YYSTATE == notes){
391 Melodic_req * mel_l = lookup_melodic_req_l (str);
393 DOUT << "(notename)\n";
394 yylval.melreq = mel_l;
399 yylval.string=new String (str);
404 My_lily_lexer::note_state_b () const
406 return YY_START == notes;
410 My_lily_lexer::lyric_state_b () const
412 return YY_START == lyrics;
416 My_lily_lexer::push_header_state ()
418 yy_push_state (header);
421 void strip_trailing_white (String&s)
424 for (; i < s.length_i (); i++)
428 s = s.nomid_str (0, i);
430 void strip_leading_white (String&s)
437 s = s.left_str (i+1);