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 "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"
41 #include "lilypond-input-version.hh"
42 #include "translator-def.hh"
47 #define isatty HORRIBLEKLUDGE
49 void strip_trailing_white (String&);
50 void strip_leading_white (String&);
54 valid_version_b (String s);
58 #define start_quote() \
59 yy_push_state (quote);\
60 yylval.string = new String
62 #define yylval (*(YYSTYPE*)lexval_l)
64 #define YY_USER_ACTION add_lexed_char (YYLeng ());
67 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
77 %option yyclass="My_lily_lexer"
79 %option never-interactive
97 NATIONAL [\001-\006\021-\027\031\036\200-\377]
98 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
104 REAL ({INT}\.{N}*)|(-?\.{N}+)
107 HORIZONTALWHITE [ \t]
111 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
119 // windows-suck-suck-suck
122 <INITIAL,chords,incl,lyrics,notes>{
124 yy_push_state (longcomment);
128 %[^{\n] { // backup rule
139 <INITIAL,chords,lyrics,notes>\\version{WHITE}* {
140 yy_push_state (version);
142 <version>\"[^"]*\";? { /* got the include file name */
143 String s (YYText ()+1);
144 s = s.left_str (s.index_last_i ('"'));
145 DEBUG_OUT << "#version `" << s << "'\n";
147 if (!valid_version_b (s))
151 LexerError ("No quoted string found after \\version");
164 LexerError (_ ("EOF found inside a comment").ch_C ());
165 if (! close_input ())
166 yyterminate (); // can't move this, since it actually rets a YY_NULL
171 <INITIAL,chords,lyrics,notes>\\maininput {
175 main_input_b_ = true;
178 error (_ ("\\maininput disallowed outside init files"));
181 <INITIAL,chords,lyrics,notes>\\include {
182 yy_push_state (incl);
184 <incl>\"[^"]*\";? { /* got the include file name */
185 String s (YYText ()+1);
186 s = s.left_str (s.index_last_i ('"'));
187 DEBUG_OUT << "#include `" << s << "'\n";
188 new_input (s,source_global_l);
191 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
192 String s = YYText () + 1;
193 strip_trailing_white (s);
194 if (s.length_i () && (s[s.length_i () - 1] == ';'))
195 s = s.left_str (s.length_i () - 1);
196 DEBUG_OUT << "#include `\\" << s << "'\n";
197 SCM sid = lookup_identifier (s);
198 if (gh_string_p (sid)) {
199 new_input (ly_scm2string (sid), source_global_l);
202 String msg (_f ("wrong or 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_str02scm (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"));
242 yylval.scm = SCM_EOL;
245 yylval.scm = ly_parse_scm (s, &n);
247 for (int i=0; i < n; i++)
251 char_count_stack_.top () += n;
257 return scan_bare_word (YYText ());
261 return scan_escaped_word (YYText () + 1);
265 yylval.i = String_convert::dec2_i (String (YYText ()));
269 yylval.i = String_convert::dec2_i (String (YYText ()));
283 *yylval.string += to_str (escaped_char(YYText()[1]));
286 *yylval.string += YYText ();
289 DEBUG_OUT << "quoted string: `" << *yylval.string << "'\n";
292 /* yylval is union. Must remember STRING before setting SCM*/
293 String *sp = yylval.string;
294 yylval.scm = ly_str02scm (sp->ch_C ());
299 *yylval.string += YYText ();
308 yylval.i = String_convert::dec2_i (String (YYText ()));
312 return scan_escaped_word (YYText () + 1);
316 String s (YYText ());
318 return yylval.i = EXTENDER;
320 return yylval.i = HYPHEN;
322 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
323 *(s.ch_l () + i) = ' ';
324 if ((i=s.index_i ("\\,")) != -1) // change "\," to TeX's "\c "
326 *(s.ch_l () + i + 1) = 'c';
327 s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
330 char c = s[s.length_i () - 1];
331 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
332 here_input ().warning (
333 "Brace found at end of lyric. Did you forget a space?");
334 yylval.scm = ly_str02scm (s.ch_C ());
336 DEBUG_OUT << "lyric : `" << s << "'\n";
345 return scan_bare_word (YYText ());
348 return scan_escaped_word (YYText () + 1);
351 yylval.i = String_convert::dec2_i (String (YYText ()));
375 DEBUG_OUT << "<<eof>>";
377 if (! close_input ()) {
378 yyterminate (); // can't move this, since it actually rets a YY_NULL
384 return scan_bare_word (YYText ());
387 return scan_escaped_word (YYText () + 1);
391 int cnv=sscanf (YYText (), "%lf", &r);
393 DEBUG_OUT << "REAL" << r<<'\n';
399 yylval.i = String_convert::dec2_i (String (YYText ()));
404 DEBUG_OUT << "parens\n";
408 char c = YYText ()[0];
409 DEBUG_OUT << "misc char" <<c<<"\n";
417 <INITIAL,lyrics,notes>\\. {
418 char c= YYText ()[1];
426 return E_EXCLAMATION;
433 String msg = _f ("invalid character: `%c'", YYText ()[0]);
434 LexerError (msg.ch_C ());
441 My_lily_lexer::push_note_state ()
443 yy_push_state (notes);
447 My_lily_lexer::push_chord_state ()
449 yy_push_state (chords);
453 My_lily_lexer::push_lyric_state ()
455 yy_push_state (lyrics);
459 My_lily_lexer::pop_state ()
465 My_lily_lexer::scan_escaped_word (String str)
467 // use more SCM for this.
469 SCM sym = ly_symbol2scm (str.ch_C());
471 int l = lookup_keyword (str);
475 SCM sid = lookup_identifier (str);
476 if (gh_string_p (sid)) {
478 return STRING_IDENTIFIER;
479 } else if (gh_number_p (sid)) {
481 return NUMBER_IDENTIFIER;
482 } else if (unsmob_translator_def (sid)) {
484 return TRANSLATOR_IDENTIFIER;
485 } else if (Music * mus =unsmob_music (sid)) {
488 return dynamic_cast<Request*> (mus) ? REQUEST_IDENTIFIER : MUSIC_IDENTIFIER;
489 } else if (unsmob_duration (sid)) {
491 return DURATION_IDENTIFIER;
497 Identifier * id = unsmob_identifier (sid);
500 return id->token_code_i_;
501 } else if (sid != SCM_UNDEFINED) {
503 return SCM_IDENTIFIER;
506 if ((YYSTATE != notes) && (YYSTATE != chords)) {
507 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
509 if (gh_pair_p (pitch))
511 yylval.scm = gh_cdr (pitch);
512 return NOTENAME_PITCH;
515 String msg (_f ("unknown escaped string: `\\%s'", str));
516 LexerError (msg.ch_C ());
518 yylval.scm = ly_str02scm(str.ch_C());
524 My_lily_lexer::scan_bare_word (String str)
526 SCM sym = ly_symbol2scm (str.ch_C ());
527 if ((YYSTATE == notes) || (YYSTATE == chords)) {
528 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
529 if (gh_pair_p (pitch)) {
530 yylval.scm = gh_cdr (pitch);
531 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
532 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
534 yylval.scm = gh_cdr (pitch);
535 return CHORDMODIFIER_PITCH;
539 yylval.scm = ly_str02scm (str.ch_C());
544 My_lily_lexer::note_state_b () const
546 return YY_START == notes;
550 My_lily_lexer::chord_state_b () const
552 return YY_START == chords;
556 My_lily_lexer::lyric_state_b () const
558 return YY_START == lyrics;
562 urg, belong to String(_convert)
563 and should be generalised
566 strip_leading_white (String&s)
569 for (; i < s.length_i (); i++)
573 s = s.nomid_str (0, i);
577 strip_trailing_white (String&s)
584 s = s.left_str (i+1);
591 valid_version_b (String s)
593 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
594 Lilypond_version ver (s);
595 if (!((ver >= oldest_version) && (ver <= current)))
597 non_fatal_error (_f ("incorrect lilypond version: %s (%s, %s)", ver.str (), oldest_version.str (), current.str ()));
598 non_fatal_error (_("Consider converting the input with the convert-ly script"));