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}
73 REAL ({INT}\.{N}*)|(-?\.{N}+)
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>\\include {
120 <incl>\"[^"]*\" { /* got the include file name */
121 String s (YYText()+1);
122 s = s.left_str(s.length_i()-1);
123 mtor << "#include `" << s << "\'\n";
124 new_input(s,source_l_g);
127 <incl>\"[^"]* { // backup rule
128 cerr << "missing end quote" << endl;
132 const char *s = YYText();
133 yylval.string = new String (s);
134 mtor << "rest:"<< yylval.string;
137 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE} {
138 String s=YYText() + 2;
139 s=s.left_str(s.length_i() - 1);
140 return scan_escaped_word(s);
142 <INITIAL,lyrics,notes>\${BLACK}*{WHITE} {
143 String s=YYText() + 1;
144 s=s.left_str(s.length_i() - 1);
145 return scan_bare_word(s);
147 <INITIAL,lyrics,notes>\\\${BLACK}* { // backup rule
148 cerr << "white expected" << endl;
151 <INITIAL,lyrics,notes>\${BLACK}* { // backup rule
152 cerr << "white expected" << endl;
157 post_quotes_b_ = true;
158 return scan_bare_word(YYText());
162 if (post_quotes_b_) {
163 post_quotes_b_ = false;
169 return scan_bare_word(YYText());
174 return scan_escaped_word(YYText()+1);
178 yylval.i = String_convert::dec2_i( String( YYText() ) );
192 *yylval.string += '\\';
195 *yylval.string +='\"';
198 *yylval.string += YYText();
201 mtor << "quoted string: `" << *yylval.string << "'\n";
213 yylval.i = String_convert::dec2_i( String( YYText() ) );
217 return scan_escaped_word(YYText()+1);
223 while ((i=s.index_i("_")) != -1) // change word binding "_" to " "
224 *(s.ch_l() + i) = ' ';
225 if ((i=s.index_i("\\,")) != -1) // change "\," to TeX's "\c "
227 *(s.ch_l() + i + 1) = 'c';
228 s = s.left_str(i+2) + " " + s.right_str(s.length_i()-i-2);
230 yylval.string = new String(s);
231 mtor << "lyric : `" << s << "'\n";
235 return yylval.c = YYText()[0];
242 if (! close_input()) {
243 yyterminate(); // can't move this, since it actually rets a YY_NULL
247 return scan_bare_word(YYText());
250 return scan_escaped_word(YYText()+1);
254 int cnv=sscanf (YYText(), "%lf", &r);
256 mtor << "REAL" << r<<'\n';
262 yylval.i = String_convert::dec2_i( String( YYText() ) );
272 char c = YYText()[0];
273 mtor << "misc char" <<c<<"\n";
278 return yylval.c = YYText()[0];
281 <INITIAL,lyrics,notes>\\. {
290 return E_EXCLAMATION;
297 LexerError( String( "illegal character: " ) +String( YYText()[0] ));
304 My_lily_lexer::push_note_state()
306 yy_push_state(notes);
310 My_lily_lexer::push_lyric_state()
312 yy_push_state(lyrics);
315 My_lily_lexer::pop_state()
321 My_lily_lexer::scan_escaped_word(String str)
323 mtor << "\\word: `" << str<<"'\n";
324 int l = lookup_keyword(str);
326 mtor << "(keyword)\n";
329 Identifier * id = lookup_identifier(str);
331 mtor << "(identifier)\n";
333 return id->token_code_i_;
335 if ( YYSTATE != notes ) {
336 Melodic_req * mel_l = lookup_melodic_req_l(str);
338 mtor << "(notename)\n";
339 yylval.melreq = mel_l;
343 LexerError( "Unknown escaped string: `" + str + "'");
345 String *sp = new String( str);
351 My_lily_lexer::scan_bare_word(String str)
353 mtor << "word: `" << str<< "'\n";
354 if (YYSTATE == notes){
355 Melodic_req * mel_l = lookup_melodic_req_l(str);
357 mtor << "(notename)\n";
358 yylval.melreq = mel_l;
363 yylval.string=new String( str );
368 My_lily_lexer::note_state_b() const
370 return YY_START == notes;
374 My_lily_lexer::lyric_state_b() const
376 return YY_START == lyrics;