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]*
90 <notes,incl,INITIAL,lyrics>{
92 yy_push_state(longcomment);
96 %[^{\n] { // backup rule
117 LexerError("EOF found inside a comment");
119 yyterminate(); // can't move this, since it actually rets a YY_NULL
128 yylval.string = new String(s);
131 {HORIZONTALWHITE}+{BLACK}.*\n {
133 strip_leading_white(s);
134 strip_trailing_white(s);
135 yylval.string = new String(s);
147 <notes,INITIAL,lyrics>\\include {
150 <incl>\"[^"]*\" { /* got the include file name */
151 String s (YYText()+1);
152 s = s.left_str(s.length_i()-1);
153 DOUT << "#include `" << s << "\'\n";
154 new_input(s,source_l_g);
157 <incl>\"[^"]* { // backup rule
158 cerr << "missing end quote" << endl;
162 const char *s = YYText();
163 yylval.string = new String (s);
164 DOUT << "rest:"<< yylval.string;
167 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE} {
168 String s=YYText() + 2;
169 s=s.left_str(s.length_i() - 1);
170 return scan_escaped_word(s);
172 <INITIAL,lyrics,notes>\${BLACK}*{WHITE} {
173 String s=YYText() + 1;
174 s=s.left_str(s.length_i() - 1);
175 return scan_bare_word(s);
177 <INITIAL,lyrics,notes>\\\${BLACK}* { // backup rule
178 cerr << "white expected" << endl;
181 <INITIAL,lyrics,notes>\${BLACK}* { // backup rule
182 cerr << "white expected" << endl;
187 post_quotes_b_ = true;
188 return scan_bare_word(YYText());
192 if (post_quotes_b_) {
193 post_quotes_b_ = false;
199 return scan_bare_word(YYText());
204 return scan_escaped_word(YYText()+1);
208 yylval.i = String_convert::dec2_i( String( YYText() ) );
222 *yylval.string += '\\';
225 *yylval.string +='\"';
228 *yylval.string += YYText();
231 DOUT << "quoted string: `" << *yylval.string << "'\n";
243 yylval.i = String_convert::dec2_i( String( YYText() ) );
247 return scan_escaped_word(YYText()+1);
253 while ((i=s.index_i("_")) != -1) // change word binding "_" to " "
254 *(s.ch_l() + i) = ' ';
255 if ((i=s.index_i("\\,")) != -1) // change "\," to TeX's "\c "
257 *(s.ch_l() + i + 1) = 'c';
258 s = s.left_str(i+2) + " " + s.right_str(s.length_i()-i-2);
260 yylval.string = new String(s);
261 DOUT << "lyric : `" << s << "'\n";
265 return yylval.c = YYText()[0];
272 if (! close_input()) {
273 yyterminate(); // can't move this, since it actually rets a YY_NULL
277 return scan_bare_word(YYText());
280 return scan_escaped_word(YYText()+1);
284 int cnv=sscanf (YYText(), "%lf", &r);
286 DOUT << "REAL" << r<<'\n';
292 yylval.i = String_convert::dec2_i( String( YYText() ) );
302 char c = YYText()[0];
303 DOUT << "misc char" <<c<<"\n";
308 return yylval.c = YYText()[0];
311 <INITIAL,lyrics,notes>\\. {
320 return E_EXCLAMATION;
327 LexerError( String( "illegal character: " ) +String( YYText()[0] ));
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 LexerError( "Unknown escaped string: `" + str + "'");
375 String *sp = new String( str);
381 My_lily_lexer::scan_bare_word(String str)
383 DOUT << "word: `" << str<< "'\n";
384 if (YYSTATE == notes){
385 Melodic_req * mel_l = lookup_melodic_req_l(str);
387 DOUT << "(notename)\n";
388 yylval.melreq = mel_l;
393 yylval.string=new String( str );
398 My_lily_lexer::note_state_b() const
400 return YY_START == notes;
404 My_lily_lexer::lyric_state_b() const
406 return YY_START == lyrics;
410 My_lily_lexer::push_header_state()
412 yy_push_state(header);
415 void strip_trailing_white(String&s)
418 for (; i < s.length_i(); i++)
422 s = s.nomid_str(0, i);
424 void strip_leading_white(String&s)