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@cs.uu.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&);
41 #define start_quote() \
42 yy_push_state (quote);\
43 yylval.string = new String
45 #define yylval (*(YYSTYPE*)lexval_l)
47 #define YY_USER_ACTION add_lexed_char (YYLeng ());
50 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
60 %option yyclass="My_lily_lexer"
62 %option never-interactive
78 NATIONAL [\001-\006\021-\027\031\036\200-\377]
79 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
85 REAL ({INT}\.{N}*)|(-?\.{N}+)
92 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
101 // windows-suck-suck-suck
104 <notes,incl,INITIAL,lyrics>{
106 yy_push_state (longcomment);
110 %[^{\n] { // backup rule
131 LexerError (_ ("EOF found inside a comment").ch_C ());
132 if (! close_input ())
133 yyterminate (); // can't move this, since it actually rets a YY_NULL
138 <notes,INITIAL,lyrics>\\maininput {
142 <notes,INITIAL,lyrics>\\include {
143 yy_push_state (incl);
145 <incl>\"[^"]*\";? { /* got the include file name */
146 String s (YYText ()+1);
147 s = s.left_str (s.index_last_i ('"'));
148 DOUT << "#include `" << s << "\'\n";
149 new_input (s,source_global_l);
152 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
153 String s = YYText () + 1;
154 strip_trailing_white (s);
155 if (s.length_i () && (s[s.length_i () - 1] == ';'))
156 s = s.left_str (s.length_i () - 1);
157 DOUT << "#include `\\" << s << "'\n";
158 Identifier * id = lookup_identifier (s);
161 String* s_l = id->access_content_String (false);
162 DOUT << "#include `" << *s_l << "\'\n";
163 new_input (*s_l, source_global_l);
169 String msg (_f ("undefined identifier: `%s\'", s ));
170 LexerError (msg.ch_C ());
173 <incl>\"[^"]* { // backup rule
174 cerr << _ ("missing end quote") << endl;
178 const char *s = YYText ();
179 yylval.string = new String (s);
180 DOUT << "rest:"<< yylval.string;
186 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE} {
187 String s=YYText () + 2;
188 s=s.left_str (s.length_i () - 1);
189 return scan_escaped_word (s);
191 <INITIAL,lyrics,notes>\${BLACK}*{WHITE} {
192 String s=YYText () + 1;
193 s=s.left_str (s.length_i () - 1);
194 return scan_bare_word (s);
196 <INITIAL,lyrics,notes>\\\${BLACK}* { // backup rule
197 cerr << _ ("white expected") << endl;
200 <INITIAL,lyrics,notes>\${BLACK}* { // backup rule
201 cerr << _ ("white expected") << endl;
207 return scan_bare_word (YYText ());
212 return scan_escaped_word (YYText () + 1);
216 yylval.i = String_convert::dec2_i (String (YYText ()));
221 yylval.i = String_convert::dec2_i (String (YYText ()));
235 *yylval.string += to_str (escaped_char(YYText()[1]));
238 *yylval.string += YYText ();
241 DOUT << "quoted string: `" << *yylval.string << "'\n";
246 *yylval.string += YYText ();
256 yylval.i = String_convert::dec2_i (String (YYText ()));
260 return scan_escaped_word (YYText () + 1);
264 String s (YYText ());
266 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
267 *(s.ch_l () + i) = ' ';
268 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
270 *(s.ch_l () + i + 1) = 'c';
271 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
273 yylval.string = new String (s);
274 DOUT << "lyric : `" << s << "'\n";
278 return yylval.c = YYText ()[0];
285 if (! close_input ()) {
286 yyterminate (); // can't move this, since it actually rets a YY_NULL
290 return scan_bare_word (YYText ());
293 return scan_escaped_word (YYText () + 1);
297 int cnv=sscanf (YYText (), "%lf", &r);
299 DOUT << "REAL" << r<<'\n';
305 yylval.i = String_convert::dec2_i (String (YYText ()));
315 char c = YYText ()[0];
316 DOUT << "misc char" <<c<<"\n";
320 <lyrics,notes>{PLET} {
321 return yylval.i = PLET;
324 <lyrics,notes>{TELP} {
325 return yylval.i = TELP;
329 return yylval.c = YYText ()[0];
332 <INITIAL,lyrics,notes>\\. {
333 char c= YYText ()[1];
341 return E_EXCLAMATION;
348 String msg = _f ("illegal character: `%c\'", YYText ()[0]);
349 LexerError (msg.ch_C ());
356 My_lily_lexer::push_note_state ()
358 yy_push_state (notes);
362 My_lily_lexer::push_lyric_state ()
364 yy_push_state (lyrics);
367 My_lily_lexer::pop_state ()
373 My_lily_lexer::scan_escaped_word (String str)
375 DOUT << "\\word: `" << str<<"'\n";
376 int l = lookup_keyword (str);
378 DOUT << "(keyword)\n";
381 Identifier * id = lookup_identifier (str);
383 DOUT << "(identifier)\n";
385 return id->token_code_i_;
387 if (YYSTATE != notes) {
388 if (notename_b (str))
390 yylval.pitch = new Musical_pitch (lookup_pitch (str));
391 yylval.pitch->set_spot (Input (source_file_l (),
393 return NOTENAME_PITCH;
397 print_declarations (true);
398 String msg (_f ("unknown escaped string: `\\%s\'", str));
399 LexerError (msg.ch_C ());
401 String *sp = new String (str);
407 My_lily_lexer::scan_bare_word (String str)
409 DOUT << "word: `" << str<< "'\n";
410 if (YYSTATE == notes){
411 if (notename_b (str)) {
412 DOUT << "(notename)\n";
413 yylval.pitch = new Musical_pitch (lookup_pitch (str));
414 yylval.pitch->set_spot (Input (source_file_l (),
416 return NOTENAME_PITCH;
420 yylval.string=new String (str);
425 My_lily_lexer::note_state_b () const
427 return YY_START == notes;
431 My_lily_lexer::lyric_state_b () const
433 return YY_START == lyrics;
437 urg, belong to String(_convert)
438 and should be generalised
441 strip_leading_white (String&s)
444 for (; i < s.length_i (); i++)
448 s = s.nomid_str (0, i);
452 strip_trailing_white (String&s)
459 s = s.left_str (i+1);