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]*
92 // windows-suck-suck-suck
95 <notes,incl,INITIAL,lyrics>{
97 yy_push_state (longcomment);
101 %[^{\n] { // backup rule
122 LexerError ("EOF found inside a comment");
123 if (! close_input ())
124 yyterminate (); // can't move this, since it actually rets a YY_NULL
130 <notes,INITIAL,lyrics>\\include {
131 yy_push_state (incl);
133 <incl>\"[^"]*\" { /* got the include file name */
134 String s (YYText ()+1);
135 s = s.left_str (s.length_i ()-1);
136 DOUT << "#include `" << s << "\'\n";
137 new_input (s,source_global_l);
140 <incl>\"[^"]* { // backup rule
141 cerr << "missing end quote" << endl;
145 const char *s = YYText ();
146 yylval.string = new String (s);
147 DOUT << "rest:"<< yylval.string;
150 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE} {
151 String s=YYText () + 2;
152 s=s.left_str (s.length_i () - 1);
153 return scan_escaped_word (s);
155 <INITIAL,lyrics,notes>\${BLACK}*{WHITE} {
156 String s=YYText () + 1;
157 s=s.left_str (s.length_i () - 1);
158 return scan_bare_word (s);
160 <INITIAL,lyrics,notes>\\\${BLACK}* { // backup rule
161 cerr << "white expected" << endl;
164 <INITIAL,lyrics,notes>\${BLACK}* { // backup rule
165 cerr << "white expected" << endl;
170 post_quotes_b_ = true;
171 return scan_bare_word (YYText ());
174 yylval.i = YYLeng ();
175 if (post_quotes_b_) {
176 post_quotes_b_ = false;
182 return scan_bare_word (YYText ());
187 return scan_escaped_word (YYText ()+1);
191 yylval.i = String_convert::dec2_i (String (YYText ()));
205 *yylval.string += escaped_char(YYText()[1]);
208 *yylval.string += YYText ();
211 DOUT << "quoted string: `" << *yylval.string << "'\n";
216 *yylval.string += YYText ();
226 yylval.i = String_convert::dec2_i (String (YYText ()));
230 return scan_escaped_word (YYText ()+1);
234 String s (YYText ());
236 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
237 *(s.ch_l () + i) = ' ';
238 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
240 *(s.ch_l () + i + 1) = 'c';
241 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
243 yylval.string = new String (s);
244 DOUT << "lyric : `" << s << "'\n";
248 return yylval.c = YYText ()[0];
255 if (! close_input ()) {
256 yyterminate (); // can't move this, since it actually rets a YY_NULL
260 return scan_bare_word (YYText ());
263 return scan_escaped_word (YYText ()+1);
267 int cnv=sscanf (YYText (), "%lf", &r);
269 DOUT << "REAL" << r<<'\n';
275 yylval.i = String_convert::dec2_i (String (YYText ()));
285 char c = YYText ()[0];
286 DOUT << "misc char" <<c<<"\n";
290 <lyrics,notes>{PLET} {
291 return yylval.i = PLET;
294 <lyrics,notes>{TELP} {
295 return yylval.i = TELP;
299 return yylval.c = YYText ()[0];
302 <INITIAL,lyrics,notes>\\. {
303 char c= YYText ()[1];
311 return E_EXCLAMATION;
318 String msg= String ("illegal character: ") +String (YYText ()[0]);
319 LexerError (msg.ch_C ());
326 My_lily_lexer::push_note_state ()
328 yy_push_state (notes);
332 My_lily_lexer::push_lyric_state ()
334 yy_push_state (lyrics);
337 My_lily_lexer::pop_state ()
343 My_lily_lexer::scan_escaped_word (String str)
345 DOUT << "\\word: `" << str<<"'\n";
346 int l = lookup_keyword (str);
348 DOUT << "(keyword)\n";
351 Identifier * id = lookup_identifier (str);
353 DOUT << "(identifier)\n";
355 return id->token_code_i_;
357 if (YYSTATE != notes) {
358 Melodic_req * mel_l = lookup_melodic_req_l (str);
360 DOUT << "(notename)\n";
361 yylval.melreq = mel_l;
365 String msg ("Unknown escaped string: `" + str + "'");
366 LexerError (msg.ch_C ());
368 String *sp = new String (str);
374 My_lily_lexer::scan_bare_word (String str)
376 DOUT << "word: `" << str<< "'\n";
377 if (YYSTATE == notes){
378 Melodic_req * mel_l = lookup_melodic_req_l (str);
380 DOUT << "(notename)\n";
381 yylval.melreq = mel_l;
386 yylval.string=new String (str);
391 My_lily_lexer::note_state_b () const
393 return YY_START == notes;
397 My_lily_lexer::lyric_state_b () const
399 return YY_START == lyrics;
403 strip_trailing_white (String&s)
406 for (; i < s.length_i (); i++)
410 s = s.nomid_str (0, i);
414 strip_leading_white (String&s)
421 s = s.left_str (i+1);