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 :-)
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"
41 #include "lilypond-input-version.hh"
42 #include "translator-def.hh"
43 #include "music-output-def.hh"
48 #define isatty HORRIBLEKLUDGE
50 void strip_trailing_white (String&);
51 void strip_leading_white (String&);
55 valid_version_b (String s);
59 #define start_quote() \
60 yy_push_state (quote);\
61 yylval.string = new String
66 #define YY_USER_ACTION add_lexed_char (YYLeng ());
69 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
79 %option yyclass="My_lily_lexer"
81 %option never-interactive
99 NATIONAL [\001-\006\021-\027\031\036\200-\377]
100 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
106 REAL ({INT}\.{N}*)|(-?\.{N}+)
109 HORIZONTALWHITE [ \t]
113 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
121 // windows-suck-suck-suck
124 <INITIAL,chords,incl,lyrics,notes>{
126 yy_push_state (longcomment);
130 %[^{\n] { // backup rule
141 <INITIAL,chords,lyrics,notes>\\version{WHITE}* {
142 yy_push_state (version);
144 <version>\"[^"]*\";? { /* got the include file name */
145 String s (YYText ()+1);
146 s = s.left_str (s.index_last_i ('"'));
147 DEBUG_OUT << "#version `" << s << "'\n";
149 if (!valid_version_b (s))
153 LexerError ("No quoted string found after \\version");
166 LexerError (_ ("EOF found inside a comment").ch_C ());
167 if (! close_input ())
168 yyterminate (); // can't move this, since it actually rets a YY_NULL
173 <INITIAL,chords,lyrics,notes>\\maininput {
177 main_input_b_ = true;
180 error (_ ("\\maininput disallowed outside init files"));
183 <INITIAL,chords,lyrics,notes>\\include {
184 yy_push_state (incl);
186 <incl>\"[^"]*\";? { /* got the include file name */
187 String s (YYText ()+1);
188 s = s.left_str (s.index_last_i ('"'));
189 DEBUG_OUT << "#include `" << s << "'\n";
190 new_input (s,source_global_l);
193 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
194 String s = YYText () + 1;
195 strip_trailing_white (s);
196 if (s.length_i () && (s[s.length_i () - 1] == ';'))
197 s = s.left_str (s.length_i () - 1);
198 DEBUG_OUT << "#include `\\" << s << "'\n";
199 SCM sid = lookup_identifier (s);
200 if (gh_string_p (sid)) {
201 new_input (ly_scm2string (sid), source_global_l);
204 String msg (_f ("wrong or undefined identifier: `%s'", s ));
205 LexerError (msg.ch_C ());
208 <incl>\"[^"]* { // backup rule
209 cerr << _ ("Missing end quote") << endl;
212 <chords,notes>{RESTNAME} {
213 const char *s = YYText ();
214 yylval.scm = ly_str02scm (s);
218 return MULTI_MEASURE_REST;
220 <INITIAL,chords,lyrics,notes>\\\${BLACK}*{WHITE} {
221 String s=YYText () + 2;
222 s=s.left_str (s.length_i () - 1);
223 return scan_escaped_word (s);
225 <INITIAL,chords,lyrics,notes>\${BLACK}*{WHITE} {
226 String s=YYText () + 1;
227 s=s.left_str (s.length_i () - 1);
228 return scan_bare_word (s);
230 <INITIAL,chords,lyrics,notes>\\\${BLACK}* { // backup rule
231 cerr << _ ("white expected") << endl;
234 <INITIAL,chords,lyrics,notes>\${BLACK}* { // backup rule
235 cerr << _ ("white expected") << endl;
239 <INITIAL,chords,lyrics,notes># { //embedded scm
240 //char const* s = YYText () + 1;
241 char const* s = here_ch_C ();
243 if (main_input_b_ && safe_global_b) {
244 error (_ ("Can't evaluate Scheme in safe mode"));
245 yylval.scm = SCM_EOL;
248 yylval.scm = ly_parse_scm (s, &n);
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 ()));
407 DEBUG_OUT << "parens\n";
411 char c = YYText ()[0];
412 DEBUG_OUT << "misc char" <<c<<"\n";
420 <INITIAL,lyrics,notes>\\. {
421 char c= YYText ()[1];
429 return E_EXCLAMATION;
440 String msg = _f ("invalid character: `%c'", YYText ()[0]);
441 LexerError (msg.ch_C ());
448 My_lily_lexer::push_note_state ()
450 yy_push_state (notes);
454 My_lily_lexer::push_chord_state ()
456 yy_push_state (chords);
460 My_lily_lexer::push_lyric_state ()
462 yy_push_state (lyrics);
466 My_lily_lexer::pop_state ()
472 My_lily_lexer::scan_escaped_word (String str)
474 // use more SCM for this.
476 SCM sym = ly_symbol2scm (str.ch_C ());
478 int l = lookup_keyword (str);
482 SCM sid = lookup_identifier (str);
483 if (gh_string_p (sid)) {
485 return STRING_IDENTIFIER;
486 } else if (gh_number_p (sid)) {
488 return NUMBER_IDENTIFIER;
489 } else if (unsmob_translator_def (sid)) {
491 return TRANSLATOR_IDENTIFIER;
492 } else if (unsmob_score (sid)) {
494 return SCORE_IDENTIFIER;
495 } else if (Music * mus =unsmob_music (sid)) {
498 return dynamic_cast<Request*> (mus) ? REQUEST_IDENTIFIER : MUSIC_IDENTIFIER;
499 } else if (unsmob_duration (sid)) {
501 return DURATION_IDENTIFIER;
502 } else if (unsmob_music_output_def (sid)) {
504 return MUSIC_OUTPUT_DEF_IDENTIFIER;
507 if (sid != SCM_UNDEFINED) {
509 return SCM_IDENTIFIER;
512 if ((YYSTATE != notes) && (YYSTATE != chords)) {
513 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
515 if (gh_pair_p (pitch))
517 yylval.scm = gh_cdr (pitch);
518 return NOTENAME_PITCH;
521 String msg (_f ("unknown escaped string: `\\%s'", str));
522 LexerError (msg.ch_C ());
524 yylval.scm = ly_str02scm (str.ch_C ());
530 My_lily_lexer::scan_bare_word (String str)
532 SCM sym = ly_symbol2scm (str.ch_C ());
533 if ((YYSTATE == notes) || (YYSTATE == chords)) {
534 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
535 if (gh_pair_p (pitch)) {
536 yylval.scm = gh_cdr (pitch);
537 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
538 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
540 yylval.scm = gh_cdr (pitch);
541 return CHORDMODIFIER_PITCH;
545 yylval.scm = ly_str02scm (str.ch_C ());
550 My_lily_lexer::note_state_b () const
552 return YY_START == notes;
556 My_lily_lexer::chord_state_b () const
558 return YY_START == chords;
562 My_lily_lexer::lyric_state_b () const
564 return YY_START == lyrics;
568 urg, belong to String (_convert)
569 and should be generalised
572 strip_leading_white (String&s)
575 for (; i < s.length_i (); i++)
579 s = s.nomid_str (0, i);
583 strip_trailing_white (String&s)
590 s = s.left_str (i+1);
597 valid_version_b (String s)
599 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
600 Lilypond_version ver (s);
601 if (! ((ver >= oldest_version) && (ver <= current)))
603 non_fatal_error (_f ("incorrect lilypond version: %s (%s, %s)", ver.str (), oldest_version.str (), current.str ()));
604 non_fatal_error (_ ("Consider converting the input with the convert-ly script"));