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 "interval.hh"
43 #include "lily-guile.hh"
48 #include "lilypond-input-version.hh"
49 #include "context-def.hh"
50 #include "identifier-smob.hh"
55 #define isatty HORRIBLEKLUDGE
57 void strip_trailing_white (String&);
58 void strip_leading_white (String&);
59 String lyric_fudge (String s);
61 SCM lookup_markup_command (String s);
62 bool is_valid_version (String s);
65 #define start_quote() \
66 yy_push_state (quote);\
67 yylval.string = new String
72 #define YY_USER_ACTION add_lexed_char (YYLeng ());
75 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
80 SCM scan_fraction (String);
81 SCM (* scm_parse_error_handler) (void *);
91 %option yyclass="My_lily_lexer"
93 %option never-interactive
114 NATIONAL [\001-\006\021-\027\031\036\200-\377]
115 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
123 REAL ({INT}\.{N}*)|(-?\.{N}+)
126 HORIZONTALWHITE [ \t]
130 MARKUPCOMMAND \\({A}|[-_])+
131 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
139 // windows-suck-suck-suck
142 <INITIAL,chords,incl,markup,lyrics,notes,figures>{
144 yy_push_state (longcomment);
148 %[^{\n\r] { // backup rule
159 <INITIAL>\\encoding{WHITE}* {
160 yy_push_state (encoding);
162 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}* {
163 yy_push_state (version);
165 <INITIAL,chords,lyrics,notes,figures>\\renameinput{WHITE}* {
166 yy_push_state (renameinput);
168 <encoding>\"[^"]*\" {
169 String s (YYText () + 1);
170 s = s.left_string (s.index_last ('\"'));
174 <version>\"[^"]*\" { /* got the version number */
175 String s (YYText () + 1);
176 s = s.left_string (s.index_last ('\"'));
179 if (!is_valid_version (s))
182 <renameinput>\"[^"]*\" {
183 String s (YYText ()+1);
184 s = s.left_string (s.index_last ('\"'));
187 this->here_input().source_file_->name_ = s;
188 progress_indication ("\n");
189 progress_indication (_f ("input renamed to: `%s'", s.to_str0 ()));
190 progress_indication ("\n");
191 scm_module_define (ly_car (scopes_),
192 ly_symbol2scm ("input-file-name"),
193 scm_makfrom0str (s.to_str0()));
197 LexerError (_ ("No quoted string found after \\encoding").to_str0 ());
201 LexerError (_ ("No quoted string found after \\version").to_str0 ());
205 LexerError (_ ("No quoted string found after \\renameinput").to_str0 ());
218 LexerError (_ ("EOF found inside a comment").to_str0 ());
219 main_input_b_ = false;
220 if (! close_input ())
221 yyterminate (); // can't move this, since it actually rets a YY_NULL
226 <INITIAL,chords,lyrics,notes,figures>\\maininput {
230 main_input_b_ = true;
233 error (_ ("\\maininput not allowed outside init files"));
236 <INITIAL,chords,lyrics,figures,notes>\\include {
237 yy_push_state (incl);
239 <incl>\"[^"]*\";? { /* got the include file name */
240 String s (YYText ()+1);
241 s = s.left_string (s.index_last ('"'));
243 new_input (s, sources_);
246 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
247 String s = YYText () + 1;
248 strip_trailing_white (s);
249 if (s.length () && (s[s.length () - 1] == ';'))
250 s = s.left_string (s.length () - 1);
252 SCM sid = lookup_identifier (s);
253 if (ly_c_string_p (sid)) {
254 new_input (ly_scm2string (sid), sources_);
257 String msg (_f ("wrong or undefined identifier: `%s'", s ));
259 LexerError (msg.to_str0 ());
260 SCM err = scm_current_error_port ();
261 scm_puts ("This value was found in the table: ", err);
262 scm_display (sid, err);
265 <incl>\"[^"]* { // backup rule
266 error (_ ("Missing end quote"));
269 <chords,notes,figures>{RESTNAME} {
270 const char *s = YYText ();
271 yylval.scm = scm_makfrom0str (s);
274 <chords,notes,figures>R {
275 return MULTI_MEASURE_REST;
277 <INITIAL,markup,chords,lyrics,notes,figures># { //embedded scm
278 //char const* s = YYText () + 1;
279 char const* s = here_str0 ();
281 SCM sval = ly_parse_scm (s, &n, here_input (),
282 safe_global_b && main_input_b_);
284 if (sval == SCM_UNDEFINED)
286 sval = SCM_UNSPECIFIED;
290 for (int i=0; i < n; i++)
294 char_count_stack_.top () += n;
296 if (unpack_identifier (sval) != SCM_UNDEFINED)
298 yylval.scm = unpack_identifier(sval);
299 return identifier_type (yylval.scm);
305 <INITIAL,notes,lyrics>{
327 return scan_bare_word (YYText ());
331 return scan_escaped_word (YYText () + 1);
334 yylval.scm = scan_fraction (YYText ());
339 yylval.i = String_convert::dec2int (String (YYText ()));
343 yylval.i = String_convert::dec2int (String (YYText ()));
347 yylval.i = String_convert::dec2int (String (YYText () +1));
361 *yylval.string += to_string (escaped_char (YYText ()[1]));
364 *yylval.string += YYText ();
370 /* yylval is union. Must remember STRING before setting SCM*/
371 String *sp = yylval.string;
372 yylval.scm = scm_makfrom0str (sp->to_str0 ());
377 *yylval.string += YYText ();
386 yylval.scm = scan_fraction (YYText ());
390 yylval.i = String_convert::dec2int (String (YYText ()));
394 return scan_escaped_word (YYText () + 1);
398 String s (YYText ());
400 return yylval.i = EXTENDER;
402 return yylval.i = HYPHEN;
405 char c = s[s.length () - 1];
406 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
407 here_input ().warning (
408 _ ("Brace found at end of lyric. Did you forget a space?"));
409 yylval.scm = scm_makfrom0str (s.to_str0 ());
420 return scan_bare_word (YYText ());
423 return scan_escaped_word (YYText () + 1);
426 yylval.scm = scan_fraction (YYText ());
430 yylval.i = String_convert::dec2int (String (YYText ()));
468 String str (YYText () + 1);
469 SCM s = lookup_markup_command (str);
471 if (ly_c_pair_p (s) && ly_c_symbol_p (ly_cdr (s)) ) {
472 yylval.scm = ly_car(s);
474 if (tag == ly_symbol2scm("markup0"))
475 return MARKUP_HEAD_MARKUP0;
476 if (tag == ly_symbol2scm("empty"))
477 return MARKUP_HEAD_EMPTY;
478 else if (tag == ly_symbol2scm ("markup0-markup1"))
479 return MARKUP_HEAD_MARKUP0_MARKUP1;
480 else if (tag == ly_symbol2scm ("markup-list0"))
481 return MARKUP_HEAD_LIST0;
482 else if (tag == ly_symbol2scm ("scheme0"))
483 return MARKUP_HEAD_SCM0;
484 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
485 return MARKUP_HEAD_SCM0_SCM1;
486 else if (tag == ly_symbol2scm ("scheme0-markup1"))
487 return MARKUP_HEAD_SCM0_MARKUP1;
488 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
489 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
490 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
491 return MARKUP_HEAD_SCM0_SCM1_SCM2;
493 programming_error ("No parser tag defined for this signature. Abort");
498 return scan_escaped_word (str);
503 [^#{}"\\ \t\n\r\f]+ {
504 String s (YYText ());
506 char c = s[s.length () - 1];
507 /* brace open is for not confusing dumb tools. */
508 if (c == '{' || c == '}')
509 here_input ().warning (
510 _ ("Brace found at end of markup. Did you forget a space?"));
511 yylval.scm = scm_makfrom0str (s.to_str0 ());
522 main_input_b_ = false;
523 if (! close_input ()) {
524 yyterminate (); // can't move this, since it actually rets a YY_NULL
530 return scan_bare_word (YYText ());
533 return scan_escaped_word (YYText () + 1);
537 int cnv=sscanf (YYText (), "%lf", &r);
540 yylval.scm = scm_make_real (r);
545 yylval.i = String_convert::dec2int (String (YYText ()));
555 char c = YYText ()[0];
560 <INITIAL,notes,figures>. {
564 <INITIAL,lyrics,notes,figures>\\. {
565 char c= YYText ()[1];
573 return E_EXCLAMATION;
581 return E_RIGHTSQUARE;
593 String msg = _f ("invalid character: `%c'", YYText ()[0]);
594 LexerError (msg.to_str0 ());
601 My_lily_lexer::push_note_state (SCM tab)
603 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
604 yy_push_state (notes);
608 My_lily_lexer::push_figuredbass_state()
610 yy_push_state (figures);
613 My_lily_lexer::push_chord_state (SCM tab)
615 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
616 yy_push_state (chords);
620 My_lily_lexer::push_lyric_state ()
622 yy_push_state (lyrics);
626 My_lily_lexer::push_markup_state ()
628 yy_push_state (markup);
632 My_lily_lexer::pop_state ()
634 if (YYSTATE == notes || YYSTATE == chords)
635 pitchname_tab_stack_ = ly_cdr (pitchname_tab_stack_);
640 My_lily_lexer::identifier_type (SCM sid)
642 int k = try_special_identifiers (&yylval.scm , sid);
643 return k >= 0 ? k : SCM_IDENTIFIER;
648 My_lily_lexer::scan_escaped_word (String str)
650 // use more SCM for this.
652 // SCM sym = ly_symbol2scm (str.to_str0 ());
654 int l = lookup_keyword (str);
658 SCM sid = lookup_identifier (str);
659 if (sid != SCM_UNDEFINED)
662 return identifier_type (sid);
665 String msg (_f ("unknown escaped string: `\\%s'", str));
666 LexerError (msg.to_str0 ());
668 yylval.scm = scm_makfrom0str (str.to_str0 ());
674 My_lily_lexer::scan_bare_word (String str)
676 SCM sym = ly_symbol2scm (str.to_str0 ());
677 if ((YYSTATE == notes) || (YYSTATE == chords)) {
678 SCM handle = SCM_BOOL_F;
679 if (ly_c_pair_p (pitchname_tab_stack_))
680 handle = scm_hashq_get_handle (ly_car (pitchname_tab_stack_), sym);
682 if (ly_c_pair_p (handle)) {
683 yylval.scm = ly_cdr (handle);
684 if (unsmob_pitch (yylval.scm))
685 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
686 else if (ly_c_symbol_p (yylval.scm))
689 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
691 yylval.scm = ly_cdr (handle);
692 return CHORD_MODIFIER;
696 yylval.scm = scm_makfrom0str (str.to_str0 ());
701 My_lily_lexer::is_note_state () const
703 return YY_START == notes;
707 My_lily_lexer::is_chord_state () const
709 return YY_START == chords;
713 My_lily_lexer::is_lyric_state () const
715 return YY_START == lyrics;
719 My_lily_lexer::is_figure_state () const
721 return YY_START == figures;
725 urg, belong to String (_convert)
726 and should be generalised
729 strip_leading_white (String&s)
732 for (; i < s.length (); i++)
736 s = s.nomid_string (0, i);
740 strip_trailing_white (String&s)
747 s = s.left_string (i+1);
752 /* 2.1.2x something -> \property -> \set. */
753 Lilypond_version oldest_version ("2.2.0");
757 is_valid_version (String s)
759 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
760 Lilypond_version ver (s);
761 if (! ((ver >= oldest_version) && (ver <= current)))
763 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
764 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
775 lyric_fudge (String s)
777 char * chars =s.get_copy_str0 ();
779 for (char * p = chars; *p ; p++)
781 if (*p == '_' && (p == chars || *(p-1) != '\\'))
789 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
791 * (s.get_str0 () + i + 1) = 'c';
792 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
799 Convert "NUM/DEN" into a '(NUM . DEN) cons.
802 scan_fraction (String frac)
804 int i = frac.index ('/');
805 int l = frac.length ();
806 String left = frac.left_string (i);
807 String right = frac.right_string (l - i - 1);
809 int n = String_convert::dec2int (left);
810 int d = String_convert::dec2int (right);
811 return scm_cons (scm_int2num (n), scm_int2num (d));
814 // Breaks for flex 2.5.31
816 /* avoid silly flex induced gcc warnings */
817 static void yy_push_state (int) {;}
818 static void yy_pop_state () {;}
819 static int yy_top_state () { return 0; }
822 avoid_silly_flex_induced_gcc_warnings ()
824 (void)yy_start_stack_ptr;
825 (void)yy_start_stack_depth;
826 (void)yy_start_stack;
830 avoid_silly_flex_induced_gcc_warnings ();
835 lookup_markup_command (String s)
837 SCM proc = ly_scheme_function ("lookup-markup-command");
838 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));