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&);
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 ());
54 %option yyclass="My_lily_lexer"
56 %option never-interactive
72 NATIONAL [\001-\006\021-\027\031\036\200-\377]
73 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
79 REAL ({INT}\.{N}*)|(-?\.{N}+)
86 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
95 // windows-suck-suck-suck
98 <notes,incl,INITIAL,lyrics>{
100 yy_push_state (longcomment);
104 %[^{\n] { // backup rule
125 LexerError ("EOF found inside a comment");
126 if (! close_input ())
127 yyterminate (); // can't move this, since it actually rets a YY_NULL
132 <notes,INITIAL,lyrics>\\maininput {
136 <notes,INITIAL,lyrics>\\include {
137 yy_push_state (incl);
139 <incl>\"[^"]*\";? { /* got the include file name */
140 String s (YYText ()+1);
141 s = s.left_str (s.index_last_i ('"'));
142 DOUT << "#include `" << s << "\'\n";
143 new_input (s,source_global_l);
146 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
147 String s = YYText () + 1;
148 strip_trailing_white (s);
149 if (s.length_i () && (s[s.length_i () - 1] == ';'))
150 s = s.left_str (s.length_i () - 1);
151 DOUT << "#include `\\" << s << "'\n";
152 Identifier * id = lookup_identifier (s);
155 String* s_p = id->string ();
156 DOUT << "#include `" << *s_p << "\'\n";
157 new_input (*s_p, source_global_l);
163 String msg ("Undefined identifier: `" + s + "'");
164 LexerError (msg.ch_C ());
167 <incl>\"[^"]* { // backup rule
168 cerr << "missing end quote" << endl;
172 const char *s = YYText ();
173 yylval.string = new String (s);
174 DOUT << "rest:"<< yylval.string;
180 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE} {
181 String s=YYText () + 2;
182 s=s.left_str (s.length_i () - 1);
183 return scan_escaped_word (s);
185 <INITIAL,lyrics,notes>\${BLACK}*{WHITE} {
186 String s=YYText () + 1;
187 s=s.left_str (s.length_i () - 1);
188 return scan_bare_word (s);
190 <INITIAL,lyrics,notes>\\\${BLACK}* { // backup rule
191 cerr << "white expected" << endl;
194 <INITIAL,lyrics,notes>\${BLACK}* { // backup rule
195 cerr << "white expected" << endl;
201 return scan_bare_word (YYText ());
206 return scan_escaped_word (YYText ()+1);
210 yylval.i = String_convert::dec2_i (String (YYText ()));
215 yylval.i = String_convert::dec2_i (String (YYText ()));
229 *yylval.string += escaped_char(YYText()[1]);
232 *yylval.string += YYText ();
235 DOUT << "quoted string: `" << *yylval.string << "'\n";
240 *yylval.string += YYText ();
250 yylval.i = String_convert::dec2_i (String (YYText ()));
254 return scan_escaped_word (YYText ()+1);
258 String s (YYText ());
260 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
261 *(s.ch_l () + i) = ' ';
262 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
264 *(s.ch_l () + i + 1) = 'c';
265 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
267 yylval.string = new String (s);
268 DOUT << "lyric : `" << s << "'\n";
272 return yylval.c = YYText ()[0];
279 if (! close_input ()) {
280 yyterminate (); // can't move this, since it actually rets a YY_NULL
284 return scan_bare_word (YYText ());
287 return scan_escaped_word (YYText ()+1);
291 int cnv=sscanf (YYText (), "%lf", &r);
293 DOUT << "REAL" << r<<'\n';
299 yylval.i = String_convert::dec2_i (String (YYText ()));
309 char c = YYText ()[0];
310 DOUT << "misc char" <<c<<"\n";
314 <lyrics,notes>{PLET} {
315 return yylval.i = PLET;
318 <lyrics,notes>{TELP} {
319 return yylval.i = TELP;
323 return yylval.c = YYText ()[0];
326 <INITIAL,lyrics,notes>\\. {
327 char c= YYText ()[1];
335 return E_EXCLAMATION;
342 String msg= String ("illegal character: ") +String (YYText ()[0]);
343 LexerError (msg.ch_C ());
350 My_lily_lexer::push_note_state ()
352 yy_push_state (notes);
356 My_lily_lexer::push_lyric_state ()
358 yy_push_state (lyrics);
361 My_lily_lexer::pop_state ()
367 My_lily_lexer::scan_escaped_word (String str)
369 DOUT << "\\word: `" << str<<"'\n";
370 int l = lookup_keyword (str);
372 DOUT << "(keyword)\n";
375 Identifier * id = lookup_identifier (str);
377 DOUT << "(identifier)\n";
379 return id->token_code_i_;
381 if (YYSTATE != notes) {
382 if (notename_b (str))
384 yylval.pitch = new Musical_pitch (lookup_pitch (str));
385 return NOTENAME_PITCH;
389 print_declarations (true);
390 String msg ("Unknown escaped string: `" + str + "'");
391 LexerError (msg.ch_C ());
393 String *sp = new String (str);
399 My_lily_lexer::scan_bare_word (String str)
401 DOUT << "word: `" << str<< "'\n";
402 if (YYSTATE == notes){
403 if (notename_b (str)) {
404 DOUT << "(notename)\n";
405 yylval.pitch = new Musical_pitch (lookup_pitch (str));
407 return NOTENAME_PITCH;
411 yylval.string=new String (str);
416 My_lily_lexer::note_state_b () const
418 return YY_START == notes;
422 My_lily_lexer::lyric_state_b () const
424 return YY_START == lyrics;
428 urg, belong to String(_convert)
429 and should be generalised
432 strip_leading_white (String&s)
435 for (; i < s.length_i (); i++)
439 s = s.nomid_str (0, i);
443 strip_trailing_white (String&s)
450 s = s.left_str (i+1);