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
113 <longcomment><<EOF>> {
114 LexerError("EOF found inside a comment");
115 if (! close_input()) {
116 yyterminate(); // can't move this, since it actually rets a YY_NULL
119 <notes,INITIAL,lyrics>
123 <incl>\"[^"]*\" { /* got the include file name */
124 String s (YYText()+1);
125 s = s.left_str(s.length_i()-1);
126 mtor << "#include `" << s << "\'\n";
127 new_input(s,source_l_g);
130 <incl>\"[^"]* { // backup rule
131 cerr << "missing end quote" << endl;
135 const char *s = YYText();
136 yylval.string = new String (s);
137 mtor << "rest:"<< yylval.string;
140 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE} {
141 String s=YYText() + 2;
142 s=s.left_str(s.length_i() - 1);
143 return scan_escaped_word(s);
145 <INITIAL,lyrics,notes>\${BLACK}*{WHITE} {
146 String s=YYText() + 1;
147 s=s.left_str(s.length_i() - 1);
148 return scan_bare_word(s);
150 <INITIAL,lyrics,notes>\\\${BLACK}* { // backup rule
151 cerr << "white expected" << endl;
154 <INITIAL,lyrics,notes>\${BLACK}* { // backup rule
155 cerr << "white expected" << endl;
160 post_quotes_b_ = true;
161 return scan_bare_word(YYText());
165 if (post_quotes_b_) {
166 post_quotes_b_ = false;
172 return scan_bare_word(YYText());
177 return scan_escaped_word(YYText()+1);
181 yylval.i = strlen(YYText());
185 yylval.i = String_convert::dec2_i( String( YYText() ) );
202 *yylval.string += '\\';
205 *yylval.string +='\"';
208 *yylval.string += YYText();
211 mtor << "quoted string: `" << *yylval.string << "'\n";
223 yylval.i = strlen(YYText());
227 yylval.i = String_convert::dec2_i( String( YYText() ) );
231 return scan_escaped_word(YYText()+1);
237 while ((i=s.index_i("_")) != -1) // change word binding "_" to " "
238 *(s.ch_l() + i) = ' ';
239 if ((i=s.index_i("\\,")) != -1) // change "\," to TeX's "\c "
241 *(s.ch_l() + i + 1) = 'c';
242 s = s.left_str(i+2) + " " + s.right_str(s.length_i()-i-2);
244 yylval.string = new String(s);
245 mtor << "lyric : `" << s << "'\n";
249 return yylval.c = YYText()[0];
256 if (! close_input()) {
257 yyterminate(); // can't move this, since it actually rets a YY_NULL
261 return scan_bare_word(YYText());
264 return scan_escaped_word(YYText()+1);
268 int cnv=sscanf (YYText(), "%lf", &r);
270 mtor << "REAL" << r<<'\n';
275 {INT}\. { // backup rule
276 cerr << "int. encountered" << endl;
286 char c = YYText()[0];
287 mtor << "misc char" <<c<<"\n";
292 return yylval.c = YYText()[0];
295 <INITIAL,lyrics,notes>\\. {
304 return E_EXCLAMATION;
311 LexerError( String( "illegal character: " ) +String( YYText()[0] ));
318 My_lily_lexer::push_note_state()
320 yy_push_state(notes);
324 My_lily_lexer::push_lyric_state()
326 yy_push_state(lyrics);
329 My_lily_lexer::pop_state()
335 My_lily_lexer::scan_escaped_word(String str)
337 mtor << "\\word: `" << str<<"'\n";
338 int l = lookup_keyword(str);
340 mtor << "(keyword)\n";
343 Identifier * id = lookup_identifier(str);
345 mtor << "(identifier)\n";
347 return id->token_code_i_;
350 String *sp = new String( str);
356 My_lily_lexer::scan_bare_word(String str)
358 mtor << "word: `" << str<< "'\n";
359 if (YYSTATE == notes){
360 Melodic_req * mel_l = lookup_melodic_req_l(str);
362 mtor << "(notename)\n";
363 yylval.melreq = mel_l;
367 if (YYSTATE != notes) {
368 // ugr. Should do this in note mode?
369 Identifier * id = lookup_identifier(str);
371 mtor << "(identifier)\n";
373 return id->token_code_i_;
376 yylval.string=new String( str );
381 My_lily_lexer::note_state_b() const
383 return YY_START == notes;
387 My_lily_lexer::lyric_state_b() const
389 return YY_START == lyrics;