1 %{ // -*-Fundamental-*-
3 lexer.l -- implement the Flex lexer
5 source file of the LilyPond music typesetter
7 (c) 1996--2000 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 "notename-table.hh"
29 #include "lily-guile.hh"
31 #include "string-convert.hh"
32 #include "my-lily-lexer.hh"
34 #include "interval.hh"
35 #include "lily-guile.hh"
39 #include "musical-request.hh"
40 #include "identifier.hh"
41 #include "mudela-version.hh"
44 void strip_trailing_white (String&);
45 void strip_leading_white (String&);
49 valid_version_b (String s);
53 #define start_quote() \
54 yy_push_state (quote);\
55 yylval.string = new String
57 #define yylval (*(YYSTYPE*)lexval_l)
59 #define YY_USER_ACTION add_lexed_char (YYLeng ());
62 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
72 %option yyclass="My_lily_lexer"
74 %option never-interactive
92 NATIONAL [\001-\006\021-\027\031\036\200-\377]
93 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
99 REAL ({INT}\.{N}*)|(-?\.{N}+)
102 HORIZONTALWHITE [ \t]
106 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
114 // windows-suck-suck-suck
117 <INITIAL,chords,incl,lyrics,notes>{
119 yy_push_state (longcomment);
123 %[^{\n] { // backup rule
134 <INITIAL,chords,lyrics,notes>\\version{WHITE}* {
135 yy_push_state (version);
137 <version>\"[^"]*\";? { /* got the include file name */
138 String s (YYText ()+1);
139 s = s.left_str (s.index_last_i ('"'));
140 DEBUG_OUT << "#version `" << s << "'\n";
141 if (!valid_version_b (s))
146 LexerError ("No quoted string found after \\version");
159 LexerError (_ ("EOF found inside a comment").ch_C ());
160 if (! close_input ())
161 yyterminate (); // can't move this, since it actually rets a YY_NULL
166 <INITIAL,chords,lyrics,notes>\\maininput {
170 main_input_b_ = true;
173 error (_ ("\\maininput disallowed outside init files"));
176 <INITIAL,chords,lyrics,notes>\\include {
177 yy_push_state (incl);
179 <incl>\"[^"]*\";? { /* got the include file name */
180 String s (YYText ()+1);
181 s = s.left_str (s.index_last_i ('"'));
182 DEBUG_OUT << "#include `" << s << "'\n";
183 new_input (s,source_global_l);
186 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
187 String s = YYText () + 1;
188 strip_trailing_white (s);
189 if (s.length_i () && (s[s.length_i () - 1] == ';'))
190 s = s.left_str (s.length_i () - 1);
191 DEBUG_OUT << "#include `\\" << s << "'\n";
192 Identifier * id = lookup_identifier (s);
195 String* s_l = id->access_content_String (false);
196 DEBUG_OUT << "#include `" << *s_l << "'\n";
197 new_input (*s_l, source_global_l);
203 String msg (_f ("undefined identifier: `%s'", s ));
204 LexerError (msg.ch_C ());
207 <incl>\"[^"]* { // backup rule
208 cerr << _ ("Missing end quote") << endl;
211 <chords,notes>{RESTNAME} {
212 const char *s = YYText ();
213 yylval.scm = ly_str02scm (s);
219 <INITIAL,chords,lyrics,notes>\\\${BLACK}*{WHITE} {
220 String s=YYText () + 2;
221 s=s.left_str (s.length_i () - 1);
222 return scan_escaped_word (s);
224 <INITIAL,chords,lyrics,notes>\${BLACK}*{WHITE} {
225 String s=YYText () + 1;
226 s=s.left_str (s.length_i () - 1);
227 return scan_bare_word (s);
229 <INITIAL,chords,lyrics,notes>\\\${BLACK}* { // backup rule
230 cerr << _ ("white expected") << endl;
233 <INITIAL,chords,lyrics,notes>\${BLACK}* { // backup rule
234 cerr << _ ("white expected") << endl;
237 <INITIAL,chords,lyrics,notes># { //embedded scm
238 //char const* s = YYText () + 1;
239 char const* s = here_ch_C ();
241 if (main_input_b_ && safe_global_b) {
242 error (_ ("Can't evaluate Scheme in safe mode"));
245 yylval.scm = ly_parse_scm (s, &n);
246 DEBUG_OUT << "Scheme: ";
248 ly_display_scm (yylval.scm);
250 for (int i=0; i < n; i++)
254 char_count_stack_.top () += n;
260 return scan_bare_word (YYText ());
264 return scan_escaped_word (YYText () + 1);
268 yylval.i = String_convert::dec2_i (String (YYText ()));
272 yylval.i = String_convert::dec2_i (String (YYText ()));
286 *yylval.string += to_str (escaped_char(YYText()[1]));
289 *yylval.string += YYText ();
292 DEBUG_OUT << "quoted string: `" << *yylval.string << "'\n";
295 /* yylval is union. Must remember STRING before setting SCM*/
296 String *sp = yylval.string;
297 yylval.scm = ly_str02scm (sp->ch_C ());
302 *yylval.string += YYText ();
311 yylval.i = String_convert::dec2_i (String (YYText ()));
315 return scan_escaped_word (YYText () + 1);
319 String s (YYText ());
321 return yylval.i = EXTENDER;
323 return yylval.i = HYPHEN;
325 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
326 *(s.ch_l () + i) = ' ';
327 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
329 *(s.ch_l () + i + 1) = 'c';
330 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
333 char c = s[s.length_i () - 1];
334 if (c == '{' && c == '}') // brace open is for not confusing dumb tools.
335 here_input ().warning (
336 "Brace found at end of lyric. Did you forget a space?");
337 yylval.scm = ly_str02scm (s.ch_C ());
339 DEBUG_OUT << "lyric : `" << s << "'\n";
348 return scan_bare_word (YYText ());
351 return scan_escaped_word (YYText () + 1);
354 yylval.i = String_convert::dec2_i (String (YYText ()));
378 DEBUG_OUT << "<<eof>>";
380 if (! close_input ()) {
381 yyterminate (); // can't move this, since it actually rets a YY_NULL
387 return scan_bare_word (YYText ());
390 return scan_escaped_word (YYText () + 1);
394 int cnv=sscanf (YYText (), "%lf", &r);
396 DEBUG_OUT << "REAL" << r<<'\n';
402 yylval.i = String_convert::dec2_i (String (YYText ()));
408 DEBUG_OUT << "parens\n";
412 char c = YYText ()[0];
413 DEBUG_OUT << "misc char" <<c<<"\n";
421 <INITIAL,lyrics,notes>\\. {
422 char c= YYText ()[1];
430 return E_EXCLAMATION;
437 String msg = _f ("invalid character: `%c'", YYText ()[0]);
438 LexerError (msg.ch_C ());
445 My_lily_lexer::push_note_state ()
447 yy_push_state (notes);
451 My_lily_lexer::push_chord_state ()
453 yy_push_state (chords);
457 My_lily_lexer::push_lyric_state ()
459 yy_push_state (lyrics);
463 My_lily_lexer::pop_state ()
469 My_lily_lexer::scan_escaped_word (String str)
471 // use more SCM for this.
473 SCM sym = ly_symbol2scm (str.ch_C());
475 int l = lookup_keyword (str);
479 Identifier * id = lookup_identifier (str);
482 return id->token_code_i_;
484 if ((YYSTATE != notes) && (YYSTATE != chords)) {
485 SCM pitch = scm_hashq_ref (pitchname_tab_, sym, SCM_BOOL_F);
487 if (pitch != SCM_BOOL_F)
489 yylval.pitch = new Musical_pitch (pitch);
490 yylval.pitch->set_spot (Input (source_file_l (),
492 return NOTENAME_PITCH;
495 String msg (_f ("unknown escaped string: `\\%s'", str));
496 LexerError (msg.ch_C ());
498 yylval.scm = ly_str02scm(str.ch_C());
504 My_lily_lexer::scan_bare_word (String str)
506 SCM sym = ly_symbol2scm (str.ch_C ());
507 if ((YYSTATE == notes) || (YYSTATE == chords)) {
508 SCM pitch = scm_hashq_ref (pitchname_tab_, sym, SCM_BOOL_F);
509 if (pitch != SCM_BOOL_F) {
510 yylval.pitch = new Musical_pitch (pitch);
511 yylval.pitch->set_spot (Input (source_file_l (),
513 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
514 } else if ((pitch = scm_hashq_ref (pitchname_tab_, sym, SCM_BOOL_F))!= SCM_BOOL_F)
516 yylval.pitch = new Musical_pitch (pitch);
517 yylval.pitch->set_spot (Input (source_file_l (),
519 return CHORDMODIFIER_PITCH;
523 yylval.scm = ly_str02scm (str.ch_C());
528 My_lily_lexer::note_state_b () const
530 return YY_START == notes;
534 My_lily_lexer::chord_state_b () const
536 return YY_START == chords;
540 My_lily_lexer::lyric_state_b () const
542 return YY_START == lyrics;
546 urg, belong to String(_convert)
547 and should be generalised
550 strip_leading_white (String&s)
553 for (; i < s.length_i (); i++)
557 s = s.nomid_str (0, i);
561 strip_trailing_white (String&s)
568 s = s.left_str (i+1);
575 valid_version_b (String s)
577 Mudela_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
578 Mudela_version ver (s);
579 if (!((ver >= oldest_version) && (ver <= current)))
581 error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), current.str ()));
582 if (!version_ignore_global_b)