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 :-)
27 #include "string-convert.hh"
28 #include "my-lily-lexer.hh"
32 #include "parseconstruct.hh"
34 #include "identifier.hh"
36 #define start_quote() \
37 yy_push_state(quote);\
38 yylval.string = new String
40 #define yylval (*(YYSTYPE*)lexval_l)
42 #define YY_USER_ACTION add_lexed_char(YYLeng());
49 %option yyclass="My_lily_lexer"
51 %option never-interactive
68 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
79 LYRICS ({AA}|{NATIONAL})[^0-9 \t\n\f]*
84 <notes,incl,INITIAL,lyrics>{
86 yy_push_state(longcomment);
90 %[^{\n] { // backup rule
111 <longcomment><<EOF>> {
112 LexerError("EOF found inside a comment");
113 if (! close_input()) {
114 yyterminate(); // can't move this, since it actually rets a YY_NULL
117 <notes,INITIAL,lyrics>
121 <incl>\"[^"]*\" { /* got the include file name */
122 String s (YYText()+1);
123 s = s.left_str(s.length_i()-1);
124 mtor << "#include `" << s << "\'\n";
125 new_input(s,source_l_g);
128 <incl>\"[^"]* { // backup rule
129 cerr << "missing end quote" << endl;
133 const char *s = YYText();
134 yylval.string = new String (s);
135 mtor << "rest:"<< yylval.string;
138 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE} {
139 String s=YYText() + 2;
140 s=s.left_str(s.length_i() - 1);
141 return scan_escaped_word(s);
143 <INITIAL,lyrics,notes>\${BLACK}*{WHITE} {
144 String s=YYText() + 1;
145 s=s.left_str(s.length_i() - 1);
146 return scan_bare_word(s);
148 <INITIAL,lyrics,notes>\\\${BLACK}* { // backup rule
149 cerr << "white expected" << endl;
152 <INITIAL,lyrics,notes>\${BLACK}* { // backup rule
153 cerr << "white expected" << endl;
158 post_quotes_b_ = true;
159 return scan_bare_word(YYText());
163 if (post_quotes_b_) {
164 post_quotes_b_ = false;
170 return scan_bare_word(YYText());
175 return scan_escaped_word(YYText()+1);
179 yylval.i = String_convert::dec2_i( String( YYText() ) );
196 *yylval.string += '\\';
199 *yylval.string +='\"';
202 *yylval.string += YYText();
205 mtor << "quoted string: `" << *yylval.string << "'\n";
217 yylval.i = String_convert::dec2_i( String( YYText() ) );
221 return scan_escaped_word(YYText()+1);
227 while ((i=s.index_i("_")) != -1) // change word binding "_" to " "
228 *(s.ch_l() + i) = ' ';
229 if ((i=s.index_i("\\,")) != -1) // change "\," to TeX's "\c "
231 *(s.ch_l() + i + 1) = 'c';
232 s = s.left_str(i+2) + " " + s.right_str(s.length_i()-i-2);
234 yylval.string = new String(s);
235 mtor << "lyric : `" << s << "'\n";
239 return yylval.c = YYText()[0];
246 if (! close_input()) {
247 yyterminate(); // can't move this, since it actually rets a YY_NULL
251 return scan_bare_word(YYText());
254 return scan_escaped_word(YYText()+1);
258 int cnv=sscanf (YYText(), "%lf", &r);
260 mtor << "REAL" << r<<'\n';
266 yylval.i = String_convert::dec2_i( String( YYText() ) );
276 char c = YYText()[0];
277 mtor << "misc char" <<c<<"\n";
282 return yylval.c = YYText()[0];
285 <INITIAL,lyrics,notes>\\. {
294 return E_EXCLAMATION;
301 LexerError( String( "illegal character: " ) +String( YYText()[0] ));
308 My_lily_lexer::push_note_state()
310 yy_push_state(notes);
314 My_lily_lexer::push_lyric_state()
316 yy_push_state(lyrics);
319 My_lily_lexer::pop_state()
325 My_lily_lexer::scan_escaped_word(String str)
327 mtor << "\\word: `" << str<<"'\n";
328 int l = lookup_keyword(str);
330 mtor << "(keyword)\n";
333 Identifier * id = lookup_identifier(str);
335 mtor << "(identifier)\n";
337 return id->token_code_i_;
339 LexerError( "Unknown escaped string: `" + str + "'");
341 String *sp = new String( str);
347 My_lily_lexer::scan_bare_word(String str)
349 mtor << "word: `" << str<< "'\n";
350 if (YYSTATE == notes){
351 Melodic_req * mel_l = lookup_melodic_req_l(str);
353 mtor << "(notename)\n";
354 yylval.melreq = mel_l;
359 if (YYSTATE != notes) {
360 // ugr. Should do this in note mode?
361 // Identifier * id = lookup_identifier(str);
363 mtor << "(identifier)\n";
365 return id->token_code_i_;
370 yylval.string=new String( str );
375 My_lily_lexer::note_state_b() const
377 return YY_START == notes;
381 My_lily_lexer::lyric_state_b() const
383 return YY_START == lyrics;