1 %{ // -*-Fundamental-*-
3 lexer.ll -- implement the Flex lexer
5 source file of the LilyPond music typesetter
7 (c) 1996--2004 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 /* Flex >= 2.5.29 fix; FlexLexer.h's multiple include bracing breaks
30 when building the actual lexer. */
36 #include "source-file.hh"
37 #include "parse-scm.hh"
38 #include "lily-guile.hh"
40 #include "string-convert.hh"
41 #include "my-lily-lexer.hh"
42 #include "input-file-results.hh"
43 #include "interval.hh"
44 #include "lily-guile.hh"
49 #include "lilypond-input-version.hh"
50 #include "context-def.hh"
51 #include "identifier-smob.hh"
56 #define isatty HORRIBLEKLUDGE
58 void strip_trailing_white (String&);
59 void strip_leading_white (String&);
60 String lyric_fudge (String s);
63 lookup_markup_command (String s);
66 is_valid_version (String s);
70 #define start_quote() \
71 yy_push_state (quote);\
72 yylval.string = new String
77 #define YY_USER_ACTION add_lexed_char (YYLeng ());
80 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
85 SCM scan_fraction (String);
86 SCM (* scm_parse_error_handler) (void *);
96 %option yyclass="My_lily_lexer"
98 %option never-interactive
118 NATIONAL [\001-\006\021-\027\031\036\200-\377]
119 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
127 REAL ({INT}\.{N}*)|(-?\.{N}+)
130 HORIZONTALWHITE [ \t]
134 MARKUPCOMMAND \\({A}|[-_])+
135 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
143 // windows-suck-suck-suck
146 <INITIAL,chords,incl,markup,lyrics,notes,figures>{
148 yy_push_state (longcomment);
152 %[^{\n\r] { // backup rule
163 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
164 yy_push_state (version);
166 <INITIAL,chords,lyrics,notes,figures>\\renameinput{WHITE}* {
167 yy_push_state (renameinput);
169 <version>\"[^"]*\" { /* got the version number */
170 String s (YYText ()+1);
171 s = s.left_string (s.index_last ('\"'));
174 if (!is_valid_version (s))
177 <renameinput>\"[^"]*\" { /* got the version number */
178 String s (YYText ()+1);
179 s = s.left_string (s.index_last ('\"'));
182 this->here_input().source_file_->name_ = s;
183 progress_indication (_f("\nRenamed input to `%s'\n", s.to_str0()));
184 scm_module_define (gh_car (scopes_),
185 ly_symbol2scm ("input-file-name"),
186 scm_makfrom0str (s.to_str0()));
191 LexerError ("No quoted string found after \\version");
195 LexerError ("No quoted string found after \\renameinput");
208 LexerError (_ ("EOF found inside a comment").to_str0 ());
209 main_input_b_ = false;
210 if (! close_input ())
211 yyterminate (); // can't move this, since it actually rets a YY_NULL
216 <INITIAL,chords,lyrics,notes,figures>\\maininput {
220 main_input_b_ = true;
223 error (_ ("\\maininput not allowed outside init files"));
226 <INITIAL,chords,lyrics,figures,notes>\\include {
227 yy_push_state (incl);
229 <incl>\"[^"]*\";? { /* got the include file name */
230 String s (YYText ()+1);
231 s = s.left_string (s.index_last ('"'));
233 new_input (s, &global_input_file->sources_ );
236 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
237 String s = YYText () + 1;
238 strip_trailing_white (s);
239 if (s.length () && (s[s.length () - 1] == ';'))
240 s = s.left_string (s.length () - 1);
242 SCM sid = lookup_identifier (s);
243 if (gh_string_p (sid)) {
244 new_input (ly_scm2string (sid), &global_input_file->sources_);
247 String msg (_f ("wrong or undefined identifier: `%s'", s ));
249 LexerError (msg.to_str0 ());
250 SCM err = scm_current_error_port ();
251 scm_puts ("This value was found in the table: ", err);
252 scm_display (sid, err);
255 <incl>\"[^"]* { // backup rule
256 error (_ ("Missing end quote"));
259 <chords,notes,figures>{RESTNAME} {
260 const char *s = YYText ();
261 yylval.scm = scm_makfrom0str (s);
264 <chords,notes,figures>R {
265 return MULTI_MEASURE_REST;
267 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}*{WHITE} {
268 String s=YYText () + 2;
269 s=s.left_string (s.length () - 1);
270 return scan_escaped_word (s);
272 <INITIAL,chords,lyrics,notes,figures>\${BLACK}*{WHITE} {
273 String s=YYText () + 1;
274 s=s.left_string (s.length () - 1);
275 return scan_bare_word (s);
277 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}* { // backup rule
278 error (_("white expected"));
281 <INITIAL,chords,lyrics,notes,figures>\${BLACK}* { // backup rule
282 error (_("white expected"));
286 <INITIAL,markup,chords,lyrics,notes,figures># { //embedded scm
287 //char const* s = YYText () + 1;
288 char const* s = here_str0 ();
290 SCM sval = ly_parse_scm (s, &n, here_input (),
291 safe_global_b && main_input_b_);
293 if (sval == SCM_UNDEFINED)
295 sval = SCM_UNSPECIFIED;
299 for (int i=0; i < n; i++)
303 char_count_stack_.top () += n;
305 if (unpack_identifier (sval) != SCM_UNDEFINED)
307 yylval.scm = unpack_identifier(sval);
308 return identifier_type (yylval.scm);
314 <INITIAL,notes,lyrics>{
336 return scan_bare_word (YYText ());
340 return scan_escaped_word (YYText () + 1);
343 yylval.scm = scan_fraction (YYText ());
348 yylval.i = String_convert::dec2int (String (YYText ()));
352 yylval.i = String_convert::dec2int (String (YYText ()));
356 yylval.i = String_convert::dec2int (String (YYText () +1));
370 *yylval.string += to_string (escaped_char (YYText ()[1]));
373 *yylval.string += YYText ();
379 /* yylval is union. Must remember STRING before setting SCM*/
380 String *sp = yylval.string;
381 yylval.scm = scm_makfrom0str (sp->to_str0 ());
386 *yylval.string += YYText ();
395 yylval.scm = scan_fraction (YYText ());
399 yylval.i = String_convert::dec2int (String (YYText ()));
403 return scan_escaped_word (YYText () + 1);
407 String s (YYText ());
409 return yylval.i = EXTENDER;
411 return yylval.i = HYPHEN;
414 char c = s[s.length () - 1];
415 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
416 here_input ().warning (
417 _ ("Brace found at end of lyric. Did you forget a space?"));
418 yylval.scm = scm_makfrom0str (s.to_str0 ());
429 return scan_bare_word (YYText ());
432 return scan_escaped_word (YYText () + 1);
435 yylval.scm = scan_fraction (YYText ());
439 yylval.i = String_convert::dec2int (String (YYText ()));
477 String str (YYText() + 1);
478 SCM s = lookup_markup_command (str);
480 if (gh_pair_p (s) && gh_symbol_p (gh_cdr (s)) ) {
481 yylval.scm = gh_car(s);
483 if (tag == ly_symbol2scm("markup0"))
484 return MARKUP_HEAD_MARKUP0;
485 if (tag == ly_symbol2scm("empty"))
486 return MARKUP_HEAD_EMPTY;
487 else if (tag == ly_symbol2scm ("markup0-markup1"))
488 return MARKUP_HEAD_MARKUP0_MARKUP1;
489 else if (tag == ly_symbol2scm ("markup-list0"))
490 return MARKUP_HEAD_LIST0;
491 else if (tag == ly_symbol2scm ("scheme0"))
492 return MARKUP_HEAD_SCM0;
493 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
494 return MARKUP_HEAD_SCM0_SCM1;
495 else if (tag == ly_symbol2scm ("scheme0-markup1"))
496 return MARKUP_HEAD_SCM0_MARKUP1;
497 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
498 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
499 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
500 return MARKUP_HEAD_SCM0_SCM1_SCM2;
502 programming_error ("No parser tag defined for this signature. Abort");
507 return scan_escaped_word (str);
512 [^#{}"\\ \t\n\r\f]+ {
513 String s (YYText ());
515 char c = s[s.length () - 1];
516 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
517 here_input ().warning (
518 _ ("Brace found at end of markup. Did you forget a space?"));
519 yylval.scm = scm_makfrom0str (s.to_str0 ());
530 main_input_b_ = false;
531 if (! close_input ()) {
532 yyterminate (); // can't move this, since it actually rets a YY_NULL
538 return scan_bare_word (YYText ());
541 return scan_escaped_word (YYText () + 1);
545 int cnv=sscanf (YYText (), "%lf", &r);
548 yylval.scm = gh_double2scm (r);
553 yylval.i = String_convert::dec2int (String (YYText ()));
563 char c = YYText ()[0];
568 <INITIAL,notes,figures>. {
572 <INITIAL,lyrics,notes,figures>\\. {
573 char c= YYText ()[1];
581 return E_EXCLAMATION;
589 return E_RIGHTSQUARE;
601 String msg = _f ("invalid character: `%c'", YYText ()[0]);
602 LexerError (msg.to_str0 ());
609 My_lily_lexer::push_note_state (SCM tab)
611 pitchname_tab_stack_ = gh_cons (tab, pitchname_tab_stack_);
612 yy_push_state (notes);
616 My_lily_lexer::push_figuredbass_state()
618 yy_push_state (figures);
621 My_lily_lexer::push_chord_state (SCM tab)
623 pitchname_tab_stack_ = gh_cons (tab, pitchname_tab_stack_);
624 yy_push_state (chords);
628 My_lily_lexer::push_lyric_state ()
630 yy_push_state (lyrics);
634 My_lily_lexer::push_markup_state ()
636 yy_push_state (markup);
640 My_lily_lexer::pop_state ()
642 if (YYSTATE == notes || YYSTATE == chords)
643 pitchname_tab_stack_ = gh_cdr (pitchname_tab_stack_);
648 My_lily_lexer::identifier_type(SCM sid)
650 int k = try_special_identifiers(&yylval.scm , sid);
651 return k >= 0 ? k : SCM_IDENTIFIER;
656 My_lily_lexer::scan_escaped_word (String str)
658 // use more SCM for this.
660 // SCM sym = ly_symbol2scm (str.to_str0 ());
662 int l = lookup_keyword (str);
666 SCM sid = lookup_identifier (str);
667 if (sid != SCM_UNDEFINED)
670 return identifier_type (sid);
673 String msg (_f ("unknown escaped string: `\\%s'", str));
674 LexerError (msg.to_str0 ());
676 yylval.scm = scm_makfrom0str (str.to_str0 ());
682 My_lily_lexer::scan_bare_word (String str)
684 SCM sym = ly_symbol2scm (str.to_str0 ());
685 if ((YYSTATE == notes) || (YYSTATE == chords)) {
686 SCM handle = SCM_BOOL_F;
687 if (gh_pair_p (pitchname_tab_stack_))
688 handle = scm_hashq_get_handle (gh_car (pitchname_tab_stack_), sym);
690 if (gh_pair_p (handle)) {
691 yylval.scm = ly_cdr (handle);
692 if (unsmob_pitch (yylval.scm))
693 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
694 else if (gh_symbol_p (yylval.scm))
697 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
699 yylval.scm = ly_cdr (handle);
700 return CHORD_MODIFIER;
704 yylval.scm = scm_makfrom0str (str.to_str0 ());
709 My_lily_lexer::is_note_state () const
711 return YY_START == notes;
715 My_lily_lexer::is_chord_state () const
717 return YY_START == chords;
721 My_lily_lexer::is_lyric_state () const
723 return YY_START == lyrics;
727 My_lily_lexer::is_figure_state () const
729 return YY_START == figures;
733 urg, belong to String (_convert)
734 and should be generalised
737 strip_leading_white (String&s)
740 for (; i < s.length (); i++)
744 s = s.nomid_string (0, i);
748 strip_trailing_white (String&s)
755 s = s.left_string (i+1);
760 /* 1.9.0 == postfix articulations */
761 Lilypond_version oldest_version ("1.9.0");
765 is_valid_version (String s)
767 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
768 Lilypond_version ver (s);
769 if (! ((ver >= oldest_version) && (ver <= current)))
771 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
772 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
783 lyric_fudge (String s)
785 char * chars =s.get_copy_str0 ();
787 for (char * p = chars; *p ; p++)
789 if (*p == '_' && (p == chars || *(p-1) != '\\'))
797 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
799 * (s.get_str0 () + i + 1) = 'c';
800 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
807 Convert "NUM/DEN" into a '(NUM . DEN) cons.
810 scan_fraction (String frac)
812 int i = frac.index ('/');
813 int l = frac.length ();
814 String left = frac.left_string (i);
815 String right = frac.right_string (l - i - 1);
817 int n = String_convert::dec2int (left);
818 int d = String_convert::dec2int (right);
819 return gh_cons (gh_int2scm (n), gh_int2scm (d));
822 // Breaks for flex 2.5.31
824 /* avoid silly flex induced gcc warnings */
825 static void yy_push_state (int) {;}
826 static void yy_pop_state () {;}
827 static int yy_top_state () { return 0; }
830 avoid_silly_flex_induced_gcc_warnings ()
832 (void)yy_start_stack_ptr;
833 (void)yy_start_stack_depth;
834 (void)yy_start_stack;
838 avoid_silly_flex_induced_gcc_warnings ();
843 lookup_markup_command (String s)
847 proc = scm_c_eval_string ("lookup-markup-command");
849 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));