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 ANDREWLYRICS ([^ \n\t\f\\{}<>0-9";.|]|\\[^a-zA-Z\]\[<>])[^0-9 \t\n\f]
93 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
102 // windows-suck-suck-suck
105 <notes,incl,INITIAL,lyrics>{
107 yy_push_state (longcomment);
111 %[^{\n] { // backup rule
132 LexerError (_ ("EOF found inside a comment").ch_C ());
133 if (! close_input ())
134 yyterminate (); // can't move this, since it actually rets a YY_NULL
139 <notes,INITIAL,lyrics>\\maininput {
143 <notes,INITIAL,lyrics>\\include {
144 yy_push_state (incl);
146 <incl>\"[^"]*\";? { /* got the include file name */
147 String s (YYText ()+1);
148 s = s.left_str (s.index_last_i ('"'));
149 DOUT << "#include `" << s << "\'\n";
150 new_input (s,source_global_l);
153 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
154 String s = YYText () + 1;
155 strip_trailing_white (s);
156 if (s.length_i () && (s[s.length_i () - 1] == ';'))
157 s = s.left_str (s.length_i () - 1);
158 DOUT << "#include `\\" << s << "'\n";
159 Identifier * id = lookup_identifier (s);
162 String* s_p = id->access_String ();
163 DOUT << "#include `" << *s_p << "\'\n";
164 new_input (*s_p, source_global_l);
170 String msg (_f ("undefined identifier: `%s\'", s ));
171 LexerError (msg.ch_C ());
174 <incl>\"[^"]* { // backup rule
175 cerr << _ ("missing end quote") << endl;
179 const char *s = YYText ();
180 yylval.string = new String (s);
181 DOUT << "rest:"<< yylval.string;
187 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE} {
188 String s=YYText () + 2;
189 s=s.left_str (s.length_i () - 1);
190 return scan_escaped_word (s);
192 <INITIAL,lyrics,notes>\${BLACK}*{WHITE} {
193 String s=YYText () + 1;
194 s=s.left_str (s.length_i () - 1);
195 return scan_bare_word (s);
197 <INITIAL,lyrics,notes>\\\${BLACK}* { // backup rule
198 cerr << _ ("white expected") << endl;
201 <INITIAL,lyrics,notes>\${BLACK}* { // backup rule
202 cerr << _ ("white expected") << endl;
208 return scan_bare_word (YYText ());
213 return scan_escaped_word (YYText () + 1);
217 yylval.i = String_convert::dec2_i (String (YYText ()));
222 yylval.i = String_convert::dec2_i (String (YYText ()));
236 *yylval.string += to_str (escaped_char(YYText()[1]));
239 *yylval.string += YYText ();
242 DOUT << "quoted string: `" << *yylval.string << "'\n";
247 *yylval.string += YYText ();
257 yylval.i = String_convert::dec2_i (String (YYText ()));
261 return scan_escaped_word (YYText () + 1);
265 String s (YYText ());
267 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
268 *(s.ch_l () + i) = ' ';
269 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
271 *(s.ch_l () + i + 1) = 'c';
272 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
274 yylval.string = new String (s);
275 DOUT << "lyric : `" << s << "'\n";
279 return yylval.c = YYText ()[0];
286 if (! close_input ()) {
287 yyterminate (); // can't move this, since it actually rets a YY_NULL
291 return scan_bare_word (YYText ());
294 return scan_escaped_word (YYText () + 1);
298 int cnv=sscanf (YYText (), "%lf", &r);
300 DOUT << "REAL" << r<<'\n';
306 yylval.i = String_convert::dec2_i (String (YYText ()));
316 char c = YYText ()[0];
317 DOUT << "misc char" <<c<<"\n";
321 <lyrics,notes>{PLET} {
322 return yylval.i = PLET;
325 <lyrics,notes>{TELP} {
326 return yylval.i = TELP;
330 return yylval.c = YYText ()[0];
333 <INITIAL,lyrics,notes>\\. {
334 char c= YYText ()[1];
342 return E_EXCLAMATION;
349 String msg = _f ("illegal character: `%c\'", YYText ()[0]);
350 LexerError (msg.ch_C ());
357 My_lily_lexer::push_note_state ()
359 yy_push_state (notes);
363 My_lily_lexer::push_lyric_state ()
365 yy_push_state (lyrics);
368 My_lily_lexer::pop_state ()
374 My_lily_lexer::scan_escaped_word (String str)
376 DOUT << "\\word: `" << str<<"'\n";
377 int l = lookup_keyword (str);
379 DOUT << "(keyword)\n";
382 Identifier * id = lookup_identifier (str);
384 DOUT << "(identifier)\n";
386 return id->token_code_i_;
388 if (YYSTATE != notes) {
389 if (notename_b (str))
391 yylval.pitch = new Musical_pitch (lookup_pitch (str));
392 yylval.pitch->set_spot (Input (source_file_l (),
394 return NOTENAME_PITCH;
398 print_declarations (true);
399 String msg (_f ("unknown escaped string: `\\%s\'", str));
400 LexerError (msg.ch_C ());
402 String *sp = new String (str);
408 My_lily_lexer::scan_bare_word (String str)
410 DOUT << "word: `" << str<< "'\n";
411 if (YYSTATE == notes){
412 if (notename_b (str)) {
413 DOUT << "(notename)\n";
414 yylval.pitch = new Musical_pitch (lookup_pitch (str));
415 yylval.pitch->set_spot (Input (source_file_l (),
417 return NOTENAME_PITCH;
421 yylval.string=new String (str);
426 My_lily_lexer::note_state_b () const
428 return YY_START == notes;
432 My_lily_lexer::lyric_state_b () const
434 return YY_START == lyrics;
438 urg, belong to String(_convert)
439 and should be generalised
442 strip_leading_white (String&s)
445 for (; i < s.length_i (); i++)
449 s = s.nomid_str (0, i);
453 strip_trailing_white (String&s)
460 s = s.left_str (i+1);