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");
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 {
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());
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);
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>\\. {
324 return E_EXCLAMATION;
331 LexerError( String( "illegal character: " ) +String( YYText()[0] ));
338 My_lily_lexer::push_note_state()
340 yy_push_state(notes);
344 My_lily_lexer::push_lyric_state()
346 yy_push_state(lyrics);
349 My_lily_lexer::pop_state()
355 My_lily_lexer::scan_escaped_word(String str)
357 DOUT << "\\word: `" << str<<"'\n";
358 int l = lookup_keyword(str);
360 DOUT << "(keyword)\n";
363 Identifier * id = lookup_identifier(str);
365 DOUT << "(identifier)\n";
367 return id->token_code_i_;
369 if ( YYSTATE != notes ) {
370 Melodic_req * mel_l = lookup_melodic_req_l(str);
372 DOUT << "(notename)\n";
373 yylval.melreq = mel_l;
377 LexerError( "Unknown escaped string: `" + str + "'");
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 My_lily_lexer::push_header_state()
416 yy_push_state(header);
419 void strip_trailing_white(String&s)
422 for (; i < s.length_i(); i++)
426 s = s.nomid_str(0, i);
428 void strip_leading_white(String&s)