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 ("\n");
184 progress_indication (_f ("input renamed to: `%s'", s.to_str0 ()));
185 progress_indication ("\n");
186 scm_module_define (gh_car (scopes_),
187 ly_symbol2scm ("input-file-name"),
188 scm_makfrom0str (s.to_str0()));
193 LexerError ("No quoted string found after \\version");
197 LexerError ("No quoted string found after \\renameinput");
210 LexerError (_ ("EOF found inside a comment").to_str0 ());
211 main_input_b_ = false;
212 if (! close_input ())
213 yyterminate (); // can't move this, since it actually rets a YY_NULL
218 <INITIAL,chords,lyrics,notes,figures>\\maininput {
222 main_input_b_ = true;
225 error (_ ("\\maininput not allowed outside init files"));
228 <INITIAL,chords,lyrics,figures,notes>\\include {
229 yy_push_state (incl);
231 <incl>\"[^"]*\";? { /* got the include file name */
232 String s (YYText ()+1);
233 s = s.left_string (s.index_last ('"'));
235 new_input (s, &global_input_file->sources_);
238 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
239 String s = YYText () + 1;
240 strip_trailing_white (s);
241 if (s.length () && (s[s.length () - 1] == ';'))
242 s = s.left_string (s.length () - 1);
244 SCM sid = lookup_identifier (s);
245 if (gh_string_p (sid)) {
246 new_input (ly_scm2string (sid), &global_input_file->sources_);
249 String msg (_f ("wrong or undefined identifier: `%s'", s ));
251 LexerError (msg.to_str0 ());
252 SCM err = scm_current_error_port ();
253 scm_puts ("This value was found in the table: ", err);
254 scm_display (sid, err);
257 <incl>\"[^"]* { // backup rule
258 error (_ ("Missing end quote"));
261 <chords,notes,figures>{RESTNAME} {
262 const char *s = YYText ();
263 yylval.scm = scm_makfrom0str (s);
266 <chords,notes,figures>R {
267 return MULTI_MEASURE_REST;
269 <INITIAL,markup,chords,lyrics,notes,figures># { //embedded scm
270 //char const* s = YYText () + 1;
271 char const* s = here_str0 ();
273 SCM sval = ly_parse_scm (s, &n, here_input (),
274 safe_global_b && main_input_b_);
276 if (sval == SCM_UNDEFINED)
278 sval = SCM_UNSPECIFIED;
282 for (int i=0; i < n; i++)
286 char_count_stack_.top () += n;
288 if (unpack_identifier (sval) != SCM_UNDEFINED)
290 yylval.scm = unpack_identifier(sval);
291 return identifier_type (yylval.scm);
297 <INITIAL,notes,lyrics>{
319 return scan_bare_word (YYText ());
323 return scan_escaped_word (YYText () + 1);
326 yylval.scm = scan_fraction (YYText ());
331 yylval.i = String_convert::dec2int (String (YYText ()));
335 yylval.i = String_convert::dec2int (String (YYText ()));
339 yylval.i = String_convert::dec2int (String (YYText () +1));
353 *yylval.string += to_string (escaped_char (YYText ()[1]));
356 *yylval.string += YYText ();
362 /* yylval is union. Must remember STRING before setting SCM*/
363 String *sp = yylval.string;
364 yylval.scm = scm_makfrom0str (sp->to_str0 ());
369 *yylval.string += YYText ();
378 yylval.scm = scan_fraction (YYText ());
382 yylval.i = String_convert::dec2int (String (YYText ()));
386 return scan_escaped_word (YYText () + 1);
390 String s (YYText ());
392 return yylval.i = EXTENDER;
394 return yylval.i = HYPHEN;
397 char c = s[s.length () - 1];
398 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
399 here_input ().warning (
400 _ ("Brace found at end of lyric. Did you forget a space?"));
401 yylval.scm = scm_makfrom0str (s.to_str0 ());
412 return scan_bare_word (YYText ());
415 return scan_escaped_word (YYText () + 1);
418 yylval.scm = scan_fraction (YYText ());
422 yylval.i = String_convert::dec2int (String (YYText ()));
460 String str (YYText() + 1);
461 SCM s = lookup_markup_command (str);
463 if (gh_pair_p (s) && gh_symbol_p (gh_cdr (s)) ) {
464 yylval.scm = gh_car(s);
466 if (tag == ly_symbol2scm("markup0"))
467 return MARKUP_HEAD_MARKUP0;
468 if (tag == ly_symbol2scm("empty"))
469 return MARKUP_HEAD_EMPTY;
470 else if (tag == ly_symbol2scm ("markup0-markup1"))
471 return MARKUP_HEAD_MARKUP0_MARKUP1;
472 else if (tag == ly_symbol2scm ("markup-list0"))
473 return MARKUP_HEAD_LIST0;
474 else if (tag == ly_symbol2scm ("scheme0"))
475 return MARKUP_HEAD_SCM0;
476 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
477 return MARKUP_HEAD_SCM0_SCM1;
478 else if (tag == ly_symbol2scm ("scheme0-markup1"))
479 return MARKUP_HEAD_SCM0_MARKUP1;
480 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
481 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
482 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
483 return MARKUP_HEAD_SCM0_SCM1_SCM2;
485 programming_error ("No parser tag defined for this signature. Abort");
490 return scan_escaped_word (str);
495 [^#{}"\\ \t\n\r\f]+ {
496 String s (YYText ());
498 char c = s[s.length () - 1];
499 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
500 here_input ().warning (
501 _ ("Brace found at end of markup. Did you forget a space?"));
502 yylval.scm = scm_makfrom0str (s.to_str0 ());
513 main_input_b_ = false;
514 if (! close_input ()) {
515 yyterminate (); // can't move this, since it actually rets a YY_NULL
521 return scan_bare_word (YYText ());
524 return scan_escaped_word (YYText () + 1);
528 int cnv=sscanf (YYText (), "%lf", &r);
531 yylval.scm = gh_double2scm (r);
536 yylval.i = String_convert::dec2int (String (YYText ()));
546 char c = YYText ()[0];
551 <INITIAL,notes,figures>. {
555 <INITIAL,lyrics,notes,figures>\\. {
556 char c= YYText ()[1];
564 return E_EXCLAMATION;
572 return E_RIGHTSQUARE;
584 String msg = _f ("invalid character: `%c'", YYText ()[0]);
585 LexerError (msg.to_str0 ());
592 My_lily_lexer::push_note_state (SCM tab)
594 pitchname_tab_stack_ = gh_cons (tab, pitchname_tab_stack_);
595 yy_push_state (notes);
599 My_lily_lexer::push_figuredbass_state()
601 yy_push_state (figures);
604 My_lily_lexer::push_chord_state (SCM tab)
606 pitchname_tab_stack_ = gh_cons (tab, pitchname_tab_stack_);
607 yy_push_state (chords);
611 My_lily_lexer::push_lyric_state ()
613 yy_push_state (lyrics);
617 My_lily_lexer::push_markup_state ()
619 yy_push_state (markup);
623 My_lily_lexer::pop_state ()
625 if (YYSTATE == notes || YYSTATE == chords)
626 pitchname_tab_stack_ = gh_cdr (pitchname_tab_stack_);
631 My_lily_lexer::identifier_type (SCM sid)
633 int k = try_special_identifiers (&yylval.scm , sid);
634 return k >= 0 ? k : SCM_IDENTIFIER;
639 My_lily_lexer::scan_escaped_word (String str)
641 // use more SCM for this.
643 // SCM sym = ly_symbol2scm (str.to_str0 ());
645 int l = lookup_keyword (str);
649 SCM sid = lookup_identifier (str);
650 if (sid != SCM_UNDEFINED)
653 return identifier_type (sid);
656 String msg (_f ("unknown escaped string: `\\%s'", str));
657 LexerError (msg.to_str0 ());
659 yylval.scm = scm_makfrom0str (str.to_str0 ());
665 My_lily_lexer::scan_bare_word (String str)
667 SCM sym = ly_symbol2scm (str.to_str0 ());
668 if ((YYSTATE == notes) || (YYSTATE == chords)) {
669 SCM handle = SCM_BOOL_F;
670 if (gh_pair_p (pitchname_tab_stack_))
671 handle = scm_hashq_get_handle (gh_car (pitchname_tab_stack_), sym);
673 if (gh_pair_p (handle)) {
674 yylval.scm = ly_cdr (handle);
675 if (unsmob_pitch (yylval.scm))
676 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
677 else if (gh_symbol_p (yylval.scm))
680 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
682 yylval.scm = ly_cdr (handle);
683 return CHORD_MODIFIER;
687 yylval.scm = scm_makfrom0str (str.to_str0 ());
692 My_lily_lexer::is_note_state () const
694 return YY_START == notes;
698 My_lily_lexer::is_chord_state () const
700 return YY_START == chords;
704 My_lily_lexer::is_lyric_state () const
706 return YY_START == lyrics;
710 My_lily_lexer::is_figure_state () const
712 return YY_START == figures;
716 urg, belong to String (_convert)
717 and should be generalised
720 strip_leading_white (String&s)
723 for (; i < s.length (); i++)
727 s = s.nomid_string (0, i);
731 strip_trailing_white (String&s)
738 s = s.left_string (i+1);
743 /* 1.9.0 == postfix articulations */
744 Lilypond_version oldest_version ("1.9.0");
748 is_valid_version (String s)
750 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
751 Lilypond_version ver (s);
752 if (! ((ver >= oldest_version) && (ver <= current)))
754 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
755 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
766 lyric_fudge (String s)
768 char * chars =s.get_copy_str0 ();
770 for (char * p = chars; *p ; p++)
772 if (*p == '_' && (p == chars || *(p-1) != '\\'))
780 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
782 * (s.get_str0 () + i + 1) = 'c';
783 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
790 Convert "NUM/DEN" into a '(NUM . DEN) cons.
793 scan_fraction (String frac)
795 int i = frac.index ('/');
796 int l = frac.length ();
797 String left = frac.left_string (i);
798 String right = frac.right_string (l - i - 1);
800 int n = String_convert::dec2int (left);
801 int d = String_convert::dec2int (right);
802 return gh_cons (gh_int2scm (n), gh_int2scm (d));
805 // Breaks for flex 2.5.31
807 /* avoid silly flex induced gcc warnings */
808 static void yy_push_state (int) {;}
809 static void yy_pop_state () {;}
810 static int yy_top_state () { return 0; }
813 avoid_silly_flex_induced_gcc_warnings ()
815 (void)yy_start_stack_ptr;
816 (void)yy_start_stack_depth;
817 (void)yy_start_stack;
821 avoid_silly_flex_induced_gcc_warnings ();
826 lookup_markup_command (String s)
828 SCM proc = ly_scheme_function ("lookup-markup-command");
830 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));