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"
31 #include "interval.hh"
34 #include "parseconstruct.hh"
36 #include "musical-request.hh"
37 #include "identifier.hh"
38 void strip_trailing_white (String&);
39 void strip_leading_white (String&);
42 #define start_quote() \
43 yy_push_state (quote);\
44 yylval.string = new String
46 #define yylval (*(YYSTYPE*)lexval_l)
48 #define YY_USER_ACTION add_lexed_char (YYLeng ());
55 %option yyclass="My_lily_lexer"
57 %option never-interactive
73 NATIONAL [\001-\006\021-\027\031\036\200-\377]
74 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
80 REAL ({INT}\.{N}*)|(-?\.{N}+)
87 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
96 // windows-suck-suck-suck
99 <notes,incl,INITIAL,lyrics>{
101 yy_push_state (longcomment);
105 %[^{\n] { // backup rule
126 LexerError ("EOF found inside a comment");
127 if (! close_input ())
128 yyterminate (); // can't move this, since it actually rets a YY_NULL
133 <notes,INITIAL,lyrics>\\maininput {
137 <notes,INITIAL,lyrics>\\include {
138 yy_push_state (incl);
140 <incl>\"[^"]*\";? { /* got the include file name */
141 String s (YYText ()+1);
142 s = s.left_str (s.index_last_i ('"'));
143 DOUT << "#include `" << s << "\'\n";
144 new_input (s,source_global_l);
147 <incl>\"[^"]* { // backup rule
148 cerr << "missing end quote" << endl;
152 const char *s = YYText ();
153 yylval.string = new String (s);
154 DOUT << "rest:"<< yylval.string;
160 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE} {
161 String s=YYText () + 2;
162 s=s.left_str (s.length_i () - 1);
163 return scan_escaped_word (s);
165 <INITIAL,lyrics,notes>\${BLACK}*{WHITE} {
166 String s=YYText () + 1;
167 s=s.left_str (s.length_i () - 1);
168 return scan_bare_word (s);
170 <INITIAL,lyrics,notes>\\\${BLACK}* { // backup rule
171 cerr << "white expected" << endl;
174 <INITIAL,lyrics,notes>\${BLACK}* { // backup rule
175 cerr << "white expected" << endl;
180 post_quotes_b_ = true;
181 return scan_bare_word (YYText ());
184 yylval.i = YYLeng ();
185 if (post_quotes_b_) {
186 post_quotes_b_ = false;
192 return scan_bare_word (YYText ());
197 return scan_escaped_word (YYText ()+1);
201 yylval.i = String_convert::dec2_i (String (YYText ()));
206 yylval.i = String_convert::dec2_i (String (YYText ()));
220 *yylval.string += escaped_char(YYText()[1]);
223 *yylval.string += YYText ();
226 DOUT << "quoted string: `" << *yylval.string << "'\n";
231 *yylval.string += YYText ();
241 yylval.i = String_convert::dec2_i (String (YYText ()));
245 return scan_escaped_word (YYText ()+1);
249 String s (YYText ());
251 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
252 *(s.ch_l () + i) = ' ';
253 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
255 *(s.ch_l () + i + 1) = 'c';
256 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
258 yylval.string = new String (s);
259 DOUT << "lyric : `" << s << "'\n";
263 return yylval.c = YYText ()[0];
270 if (! close_input ()) {
271 yyterminate (); // can't move this, since it actually rets a YY_NULL
275 return scan_bare_word (YYText ());
278 return scan_escaped_word (YYText ()+1);
282 int cnv=sscanf (YYText (), "%lf", &r);
284 DOUT << "REAL" << r<<'\n';
290 yylval.i = String_convert::dec2_i (String (YYText ()));
300 char c = YYText ()[0];
301 DOUT << "misc char" <<c<<"\n";
305 <lyrics,notes>{PLET} {
306 return yylval.i = PLET;
309 <lyrics,notes>{TELP} {
310 return yylval.i = TELP;
314 return yylval.c = YYText ()[0];
317 <INITIAL,lyrics,notes>\\. {
318 char c= YYText ()[1];
326 return E_EXCLAMATION;
333 String msg= String ("illegal character: ") +String (YYText ()[0]);
334 LexerError (msg.ch_C ());
341 My_lily_lexer::push_note_state ()
343 yy_push_state (notes);
347 My_lily_lexer::push_lyric_state ()
349 yy_push_state (lyrics);
352 My_lily_lexer::pop_state ()
358 My_lily_lexer::scan_escaped_word (String str)
360 DOUT << "\\word: `" << str<<"'\n";
361 int l = lookup_keyword (str);
363 DOUT << "(keyword)\n";
366 Identifier * id = lookup_identifier (str);
368 DOUT << "(identifier)\n";
370 return id->token_code_i_;
372 if (YYSTATE != notes) {
373 Melodic_req * mel_l = lookup_melodic_req_l (str);
375 DOUT << "(notename)\n";
376 yylval.melreq = mel_l;
377 mel_l->set_spot (Input (source_file_l (), here_ch_C ()));
381 String msg ("Unknown escaped string: `" + str + "'");
382 LexerError (msg.ch_C ());
384 String *sp = new String (str);
390 My_lily_lexer::scan_bare_word (String str)
392 DOUT << "word: `" << str<< "'\n";
393 if (YYSTATE == notes){
394 Melodic_req * mel_l = lookup_melodic_req_l (str);
396 DOUT << "(notename)\n";
397 yylval.melreq = mel_l;
398 mel_l->set_spot (Input (source_file_l (), here_ch_C ()));
403 yylval.string=new String (str);
408 My_lily_lexer::note_state_b () const
410 return YY_START == notes;
414 My_lily_lexer::lyric_state_b () const
416 return YY_START == lyrics;
420 strip_trailing_white (String&s)
423 for (; i < s.length_i (); i++)
427 s = s.nomid_str (0, i);
431 strip_leading_white (String&s)
438 s = s.left_str (i+1);