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
71 NATIONAL [\001-\006\021-\027\031\036\200-\377]
72 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
76 REAL ({INT}\.{N}*)|(-?\.{N}+)
83 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
90 // windows-suck-suck-suck
93 <notes,incl,INITIAL,lyrics>{
95 yy_push_state (longcomment);
99 %[^{\n] { // backup rule
120 LexerError ("EOF found inside a comment");
121 if (! close_input ())
122 yyterminate (); // can't move this, since it actually rets a YY_NULL
128 <notes,INITIAL,lyrics>\\include {
129 yy_push_state (incl);
131 <incl>\"[^"]*\" { /* got the include file name */
132 String s (YYText ()+1);
133 s = s.left_str (s.length_i ()-1);
134 DOUT << "#include `" << s << "\'\n";
135 new_input (s,source_l_g);
138 <incl>\"[^"]* { // backup rule
139 cerr << "missing end quote" << endl;
143 const char *s = YYText ();
144 yylval.string = new String (s);
145 DOUT << "rest:"<< yylval.string;
148 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE} {
149 String s=YYText () + 2;
150 s=s.left_str (s.length_i () - 1);
151 return scan_escaped_word (s);
153 <INITIAL,lyrics,notes>\${BLACK}*{WHITE} {
154 String s=YYText () + 1;
155 s=s.left_str (s.length_i () - 1);
156 return scan_bare_word (s);
158 <INITIAL,lyrics,notes>\\\${BLACK}* { // backup rule
159 cerr << "white expected" << endl;
162 <INITIAL,lyrics,notes>\${BLACK}* { // backup rule
163 cerr << "white expected" << endl;
168 post_quotes_b_ = true;
169 return scan_bare_word (YYText ());
172 yylval.i = YYLeng ();
173 if (post_quotes_b_) {
174 post_quotes_b_ = false;
180 return scan_bare_word (YYText ());
185 return scan_escaped_word (YYText ()+1);
189 yylval.i = String_convert::dec2_i (String (YYText ()));
203 *yylval.string += escaped_char(YYText()[1]);
206 *yylval.string += YYText ();
209 DOUT << "quoted string: `" << *yylval.string << "'\n";
214 *yylval.string += YYText ();
224 yylval.i = String_convert::dec2_i (String (YYText ()));
228 return scan_escaped_word (YYText ()+1);
232 String s (YYText ());
234 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
235 *(s.ch_l () + i) = ' ';
236 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
238 *(s.ch_l () + i + 1) = 'c';
239 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
241 yylval.string = new String (s);
242 DOUT << "lyric : `" << s << "'\n";
246 return yylval.c = YYText ()[0];
253 if (! close_input ()) {
254 yyterminate (); // can't move this, since it actually rets a YY_NULL
258 return scan_bare_word (YYText ());
261 return scan_escaped_word (YYText ()+1);
265 int cnv=sscanf (YYText (), "%lf", &r);
267 DOUT << "REAL" << r<<'\n';
273 yylval.i = String_convert::dec2_i (String (YYText ()));
283 char c = YYText ()[0];
284 DOUT << "misc char" <<c<<"\n";
289 return yylval.c = YYText ()[0];
292 <INITIAL,lyrics,notes>\\. {
293 char c= YYText ()[1];
301 return E_EXCLAMATION;
308 String msg= String ("illegal character: ") +String (YYText ()[0]);
309 LexerError (msg.ch_C ());
316 My_lily_lexer::push_note_state ()
318 yy_push_state (notes);
322 My_lily_lexer::push_lyric_state ()
324 yy_push_state (lyrics);
327 My_lily_lexer::pop_state ()
333 My_lily_lexer::scan_escaped_word (String str)
335 DOUT << "\\word: `" << str<<"'\n";
336 int l = lookup_keyword (str);
338 DOUT << "(keyword)\n";
341 Identifier * id = lookup_identifier (str);
343 DOUT << "(identifier)\n";
345 return id->token_code_i_;
347 if (YYSTATE != notes) {
348 Melodic_req * mel_l = lookup_melodic_req_l (str);
350 DOUT << "(notename)\n";
351 yylval.melreq = mel_l;
355 String msg ("Unknown escaped string: `" + str + "'");
356 LexerError (msg.ch_C ());
358 String *sp = new String (str);
364 My_lily_lexer::scan_bare_word (String str)
366 DOUT << "word: `" << str<< "'\n";
367 if (YYSTATE == notes){
368 Melodic_req * mel_l = lookup_melodic_req_l (str);
370 DOUT << "(notename)\n";
371 yylval.melreq = mel_l;
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;
393 strip_trailing_white (String&s)
396 for (; i < s.length_i (); i++)
400 s = s.nomid_str (0, i);
404 strip_leading_white (String&s)
411 s = s.left_str (i+1);