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 "input-score.hh"
33 #include "parseconstruct.hh"
35 #include "identifier.hh"
37 #define start_quote() \
38 yy_push_state(quote);\
39 yylval.string = new String
41 #define yylval (*(YYSTYPE*)lexval_l)
43 #define YY_USER_ACTION add_lexed_char(YYLeng());
50 %option yyclass="My_lily_lexer"
52 %option never-interactive
69 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
81 LYRICS ({AA}|{NATIONAL})[^0-9 \t\n\f]*
86 <notes,incl,INITIAL,lyrics>{
88 yy_push_state(longcomment);
92 %[^{\n] { // backup rule
114 <notes,INITIAL,lyrics>
118 <incl>\"[^"]*\" { /* got the include file name */
119 String s (YYText()+1);
120 s = s.left_str(s.length_i()-1);
121 mtor << "#include `" << s << "\'\n";
122 new_input(s,source_l_g);
125 <incl>\"[^"]* { // backup rule
126 cerr << "missing end quote" << endl;
130 const char *s = YYText();
131 yylval.string = new String (s);
132 mtor << "rest:"<< yylval.string;
135 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE} {
136 String s=YYText() + 2;
137 s=s.left_str(s.length_i() - 1);
138 return scan_escaped_word(s);
140 <INITIAL,lyrics,notes>\${BLACK}*{WHITE} {
141 String s=YYText() + 1;
142 s=s.left_str(s.length_i() - 1);
143 return scan_bare_word(s);
145 <INITIAL,lyrics,notes>\\\${BLACK}* { // backup rule
146 cerr << "white expected" << endl;
149 <INITIAL,lyrics,notes>\${BLACK}* { // backup rule
150 cerr << "white expected" << endl;
155 post_quotes_b_ = true;
156 return scan_bare_word(YYText());
160 if (post_quotes_b_) {
161 post_quotes_b_ = false;
167 return scan_bare_word(YYText());
172 return scan_escaped_word(YYText()+1);
176 yylval.i = strlen(YYText());
180 yylval.i = String_convert::dec2_i( String( YYText() ) );
197 *yylval.string += '\\';
200 *yylval.string +='\"';
203 *yylval.string += YYText();
206 mtor << "quoted string: `" << *yylval.string << "'\n";
218 yylval.i = strlen(YYText());
222 yylval.i = String_convert::dec2_i( String( YYText() ) );
226 return scan_escaped_word(YYText()+1);
232 while ((i=s.index_i("_")) != -1) // change word binding "_" to " "
233 *(s.ch_l() + i) = ' ';
234 if ((i=s.index_i("\\,")) != -1) // change "\," to TeX's "\c "
236 *(s.ch_l() + i + 1) = 'c';
237 s = s.left_str(i+2) + " " + s.right_str(s.length_i()-i-2);
239 yylval.string = new String(s);
240 mtor << "lyric : `" << s << "'\n";
244 return yylval.c = YYText()[0];
251 if (! close_input()) {
252 yyterminate(); // can't move this, since it actually rets a YY_NULL
256 return scan_bare_word(YYText());
259 return scan_escaped_word(YYText()+1);
263 int cnv=sscanf (YYText(), "%lf", &r);
265 mtor << "REAL" << r<<'\n';
270 {INT}\. { // backup rule
271 cerr << "int. encountered" << endl;
281 char c = YYText()[0];
282 mtor << "misc char" <<c<<"\n";
287 return yylval.c = YYText()[0];
290 <INITIAL,lyrics,notes>\\. {
299 return E_EXCLAMATION;
306 LexerError( String( "illegal character: " ) +String( YYText()[0] ));
313 My_lily_lexer::push_note_state()
315 yy_push_state(notes);
319 My_lily_lexer::push_lyric_state()
321 yy_push_state(lyrics);
324 My_lily_lexer::pop_state()
330 My_lily_lexer::scan_escaped_word(String str)
332 mtor << "\\word: `" << str<<"'\n";
333 int l = lookup_keyword(str);
335 mtor << "(keyword)\n";
338 Identifier * id = lookup_identifier(str);
340 mtor << "(identifier)\n";
342 return id->token_code_i_;
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;
362 if (YYSTATE != notes) {
363 // ugr. Should do this in note mode?
364 Identifier * id = lookup_identifier(str);
366 mtor << "(identifier)\n";
368 return id->token_code_i_;
371 yylval.string=new String( str );
376 My_lily_lexer::note_state_b() const
378 return YY_START == notes;
382 My_lily_lexer::lyric_state_b() const
384 return YY_START == lyrics;