1 %{ // -*-Fundamental-*-
3 lexer.l -- implement the Flex lexer
5 source file of the LilyPond music typesetter
7 (c) 1996--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 Jan Nieuwenhuizen <janneke@gnu.org>
15 after making a change to the lexer rules, run
16 flex -b <this lexer file>
19 contains no backup states, but only the reminder
20 Compressed tables always back up.
21 (don-t forget to rm lex.yy.cc :-)
28 #include "lily-guile.hh"
30 #include "string-convert.hh"
31 #include "my-lily-lexer.hh"
33 #include "interval.hh"
34 #include "lily-guile.hh"
38 #include "musical-request.hh"
39 #include "identifier.hh"
40 #include "mudela-version.hh"
43 void strip_trailing_white (String&);
44 void strip_leading_white (String&);
48 valid_version_b (String s);
52 #define start_quote() \
53 yy_push_state (quote);\
54 yylval.string = new String
56 #define yylval (*(YYSTYPE*)lexval_l)
58 #define YY_USER_ACTION add_lexed_char (YYLeng ());
61 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
71 %option yyclass="My_lily_lexer"
73 %option never-interactive
91 NATIONAL [\001-\006\021-\027\031\036\200-\377]
92 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
98 REAL ({INT}\.{N}*)|(-?\.{N}+)
101 HORIZONTALWHITE [ \t]
105 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
113 // windows-suck-suck-suck
116 <INITIAL,chords,incl,lyrics,notes>{
118 yy_push_state (longcomment);
122 %[^{\n] { // backup rule
133 <INITIAL,chords,lyrics,notes>\\version{WHITE}* {
134 yy_push_state (version);
136 <version>\"[^"]*\";? { /* got the include file name */
137 String s (YYText ()+1);
138 s = s.left_str (s.index_last_i ('"'));
139 DEBUG_OUT << "#version `" << s << "'\n";
140 if (!valid_version_b (s))
145 LexerError ("No quoted string found after \\version");
158 LexerError (_ ("EOF found inside a comment").ch_C ());
159 if (! close_input ())
160 yyterminate (); // can't move this, since it actually rets a YY_NULL
165 <INITIAL,chords,lyrics,notes>\\maininput {
169 main_input_b_ = true;
172 error (_ ("\\maininput disallowed outside init files"));
175 <INITIAL,chords,lyrics,notes>\\include {
176 yy_push_state (incl);
178 <incl>\"[^"]*\";? { /* got the include file name */
179 String s (YYText ()+1);
180 s = s.left_str (s.index_last_i ('"'));
181 DEBUG_OUT << "#include `" << s << "'\n";
182 new_input (s,source_global_l);
185 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
186 String s = YYText () + 1;
187 strip_trailing_white (s);
188 if (s.length_i () && (s[s.length_i () - 1] == ';'))
189 s = s.left_str (s.length_i () - 1);
190 DEBUG_OUT << "#include `\\" << s << "'\n";
191 Identifier * id = lookup_identifier (s);
194 String* s_l = id->access_content_String (false);
195 DEBUG_OUT << "#include `" << *s_l << "'\n";
196 new_input (*s_l, source_global_l);
202 String msg (_f ("undefined identifier: `%s'", s ));
203 LexerError (msg.ch_C ());
206 <incl>\"[^"]* { // backup rule
207 cerr << _ ("Missing end quote") << endl;
210 <chords,notes>{RESTNAME} {
211 const char *s = YYText ();
212 yylval.scm = ly_ch_C_to_scm (s);
218 <INITIAL,chords,lyrics,notes>\\\${BLACK}*{WHITE} {
219 String s=YYText () + 2;
220 s=s.left_str (s.length_i () - 1);
221 return scan_escaped_word (s);
223 <INITIAL,chords,lyrics,notes>\${BLACK}*{WHITE} {
224 String s=YYText () + 1;
225 s=s.left_str (s.length_i () - 1);
226 return scan_bare_word (s);
228 <INITIAL,chords,lyrics,notes>\\\${BLACK}* { // backup rule
229 cerr << _ ("white expected") << endl;
232 <INITIAL,chords,lyrics,notes>\${BLACK}* { // backup rule
233 cerr << _ ("white expected") << endl;
236 <INITIAL,chords,lyrics,notes># { //embedded scm
237 //char const* s = YYText () + 1;
238 char const* s = here_ch_C ();
240 if (main_input_b_ && safe_global_b) {
241 error (_ ("Can't evaluate Scheme in safe mode"));
244 yylval.scm = ly_parse_scm (s, &n);
245 DEBUG_OUT << "Scheme: ";
247 ly_display_scm (yylval.scm);
249 for (int i=0; i < n; i++)
253 char_count_stack_.top () += n;
259 return scan_bare_word (YYText ());
263 return scan_escaped_word (YYText () + 1);
267 yylval.i = String_convert::dec2_i (String (YYText ()));
271 yylval.i = String_convert::dec2_i (String (YYText ()));
285 *yylval.string += to_str (escaped_char(YYText()[1]));
288 *yylval.string += YYText ();
291 DEBUG_OUT << "quoted string: `" << *yylval.string << "'\n";
294 /* yylval is union. Must remember STRING before setting SCM*/
295 String *sp = yylval.string;
296 yylval.scm = ly_ch_C_to_scm (sp->ch_C ());
301 *yylval.string += YYText ();
310 yylval.i = String_convert::dec2_i (String (YYText ()));
314 return scan_escaped_word (YYText () + 1);
318 String s (YYText ());
320 return yylval.i = EXTENDER;
322 return yylval.i = HYPHEN;
324 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
325 *(s.ch_l () + i) = ' ';
326 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
328 *(s.ch_l () + i + 1) = 'c';
329 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
332 char c = s[s.length_i () - 1];
333 if (c == '{' && c == '}') // brace open is for not confusing dumb tools.
334 here_input ().warning (
335 "Brace found at end of lyric. Did you forget a space?");
336 yylval.scm = ly_ch_C_to_scm (YYText());
338 DEBUG_OUT << "lyric : `" << s << "'\n";
342 return yylval.c = YYText ()[0];
347 return scan_bare_word (YYText ());
350 return scan_escaped_word (YYText () + 1);
353 yylval.i = String_convert::dec2_i (String (YYText ()));
366 return yylval.c = YYText ()[0];
371 DEBUG_OUT << "<<eof>>";
373 if (! close_input ()) {
374 yyterminate (); // can't move this, since it actually rets a YY_NULL
380 return scan_bare_word (YYText ());
383 return scan_escaped_word (YYText () + 1);
387 int cnv=sscanf (YYText (), "%lf", &r);
389 DEBUG_OUT << "REAL" << r<<'\n';
395 yylval.i = String_convert::dec2_i (String (YYText ()));
401 DEBUG_OUT << "parens\n";
405 char c = YYText ()[0];
406 DEBUG_OUT << "misc char" <<c<<"\n";
411 return yylval.c = YYText ()[0];
414 <INITIAL,lyrics,notes>\\. {
415 char c= YYText ()[1];
423 return E_EXCLAMATION;
430 String msg = _f ("invalid character: `%c'", YYText ()[0]);
431 LexerError (msg.ch_C ());
438 My_lily_lexer::push_note_state ()
440 yy_push_state (notes);
444 My_lily_lexer::push_chord_state ()
446 yy_push_state (chords);
450 My_lily_lexer::push_lyric_state ()
452 yy_push_state (lyrics);
456 My_lily_lexer::pop_state ()
462 My_lily_lexer::scan_escaped_word (String str)
464 DEBUG_OUT << "\\word: `" << str<<"'\n";
465 int l = lookup_keyword (str);
467 DEBUG_OUT << "(keyword)\n";
470 Identifier * id = lookup_identifier (str);
472 DEBUG_OUT << "(identifier)\n";
474 return id->token_code_i_;
476 if ((YYSTATE != notes) && (YYSTATE != chords)) {
477 if (notename_b (str)) {
478 yylval.pitch = new Musical_pitch (lookup_notename (str));
479 yylval.pitch->set_spot (Input (source_file_l (),
481 return NOTENAME_PITCH;
485 print_declarations (true);
486 String msg (_f ("unknown escaped string: `\\%s'", str));
487 LexerError (msg.ch_C ());
488 DEBUG_OUT << "(string)";
489 yylval.scm = ly_ch_C_to_scm(str.ch_C());
495 My_lily_lexer::scan_bare_word (String str)
497 DEBUG_OUT << "word: `" << str<< "'\n";
498 if ((YYSTATE == notes) || (YYSTATE == chords)) {
499 if (notename_b (str)) {
500 DEBUG_OUT << "(notename)\n";
501 yylval.pitch = new Musical_pitch (lookup_notename (str));
502 yylval.pitch->set_spot (Input (source_file_l (),
504 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
505 } else if (chordmodifier_b (str)) {
506 DEBUG_OUT << "(chordmodifier)\n";
507 yylval.pitch = new Musical_pitch (lookup_chordmodifier (str));
508 yylval.pitch->set_spot (Input (source_file_l (),
510 return CHORDMODIFIER_PITCH;
514 yylval.scm = ly_ch_C_to_scm (str.ch_C());
519 My_lily_lexer::note_state_b () const
521 return YY_START == notes;
525 My_lily_lexer::chord_state_b () const
527 return YY_START == chords;
531 My_lily_lexer::lyric_state_b () const
533 return YY_START == lyrics;
537 urg, belong to String(_convert)
538 and should be generalised
541 strip_leading_white (String&s)
544 for (; i < s.length_i (); i++)
548 s = s.nomid_str (0, i);
552 strip_trailing_white (String&s)
559 s = s.left_str (i+1);
566 valid_version_b (String s)
568 Mudela_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
569 Mudela_version ver (s);
570 if (!((ver >= oldest_version) && (ver <= current)))
572 error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), current.str ()));
573 if (!version_ignore_global_b)