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);
62 SCM lookup_markup_command (String s);
63 bool is_valid_version (String s);
66 #define start_quote() \
67 yy_push_state (quote);\
68 yylval.string = new String
73 #define YY_USER_ACTION add_lexed_char (YYLeng ());
76 LYRICS ({AA}|{TEX})[^0-9 \t\n\f]*
81 SCM scan_fraction (String);
82 SCM (* scm_parse_error_handler) (void *);
92 %option yyclass="My_lily_lexer"
94 %option never-interactive
115 NATIONAL [\001-\006\021-\027\031\036\200-\377]
116 TEX {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
124 REAL ({INT}\.{N}*)|(-?\.{N}+)
127 HORIZONTALWHITE [ \t]
131 MARKUPCOMMAND \\({A}|[-_])+
132 LYRICS ({AA}|{TEX})[^0-9 \t\n\r\f]*
140 // windows-suck-suck-suck
143 <INITIAL,chords,incl,markup,lyrics,notes,figures>{
145 yy_push_state (longcomment);
149 %[^{\n\r] { // backup rule
160 <INITIAL>\\encoding{WHITE}* {
161 yy_push_state (encoding);
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 <encoding>\"[^"]*\" {
170 String s (YYText () + 1);
171 s = s.left_string (s.index_last ('\"'));
175 <version>\"[^"]*\" { /* got the version number */
176 String s (YYText () + 1);
177 s = s.left_string (s.index_last ('\"'));
180 if (!is_valid_version (s))
183 <renameinput>\"[^"]*\" {
184 String s (YYText ()+1);
185 s = s.left_string (s.index_last ('\"'));
188 this->here_input().source_file_->name_ = s;
189 progress_indication ("\n");
190 progress_indication (_f ("input renamed to: `%s'", s.to_str0 ()));
191 progress_indication ("\n");
192 scm_module_define (ly_car (scopes_),
193 ly_symbol2scm ("input-file-name"),
194 scm_makfrom0str (s.to_str0()));
198 LexerError (_ ("No quoted string found after \\encoding").to_str0 ());
202 LexerError (_ ("No quoted string found after \\version").to_str0 ());
206 LexerError (_ ("No quoted string found after \\renameinput").to_str0 ());
219 LexerError (_ ("EOF found inside a comment").to_str0 ());
220 main_input_b_ = false;
221 if (! close_input ())
222 yyterminate (); // can't move this, since it actually rets a YY_NULL
227 <INITIAL,chords,lyrics,notes,figures>\\maininput {
231 main_input_b_ = true;
234 error (_ ("\\maininput not allowed outside init files"));
237 <INITIAL,chords,lyrics,figures,notes>\\include {
238 yy_push_state (incl);
240 <incl>\"[^"]*\";? { /* got the include file name */
241 String s (YYText ()+1);
242 s = s.left_string (s.index_last ('"'));
244 new_input (s, sources_);
247 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
248 String s = YYText () + 1;
249 strip_trailing_white (s);
250 if (s.length () && (s[s.length () - 1] == ';'))
251 s = s.left_string (s.length () - 1);
253 SCM sid = lookup_identifier (s);
254 if (is_string (sid)) {
255 new_input (ly_scm2string (sid), sources_);
258 String msg (_f ("wrong or undefined identifier: `%s'", s ));
260 LexerError (msg.to_str0 ());
261 SCM err = scm_current_error_port ();
262 scm_puts ("This value was found in the table: ", err);
263 scm_display (sid, err);
266 <incl>\"[^"]* { // backup rule
267 error (_ ("Missing end quote"));
270 <chords,notes,figures>{RESTNAME} {
271 const char *s = YYText ();
272 yylval.scm = scm_makfrom0str (s);
275 <chords,notes,figures>R {
276 return MULTI_MEASURE_REST;
278 <INITIAL,markup,chords,lyrics,notes,figures># { //embedded scm
279 //char const* s = YYText () + 1;
280 char const* s = here_str0 ();
282 SCM sval = ly_parse_scm (s, &n, here_input (),
283 safe_global_b && main_input_b_);
285 if (sval == SCM_UNDEFINED)
287 sval = SCM_UNSPECIFIED;
291 for (int i=0; i < n; i++)
295 char_count_stack_.top () += n;
297 if (unpack_identifier (sval) != SCM_UNDEFINED)
299 yylval.scm = unpack_identifier(sval);
300 return identifier_type (yylval.scm);
306 <INITIAL,notes,lyrics>{
328 return scan_bare_word (YYText ());
332 return scan_escaped_word (YYText () + 1);
335 yylval.scm = scan_fraction (YYText ());
340 yylval.i = String_convert::dec2int (String (YYText ()));
344 yylval.i = String_convert::dec2int (String (YYText ()));
348 yylval.i = String_convert::dec2int (String (YYText () +1));
362 *yylval.string += to_string (escaped_char (YYText ()[1]));
365 *yylval.string += YYText ();
371 /* yylval is union. Must remember STRING before setting SCM*/
372 String *sp = yylval.string;
373 yylval.scm = scm_makfrom0str (sp->to_str0 ());
378 *yylval.string += YYText ();
387 yylval.scm = scan_fraction (YYText ());
391 yylval.i = String_convert::dec2int (String (YYText ()));
395 return scan_escaped_word (YYText () + 1);
399 String s (YYText ());
401 return yylval.i = EXTENDER;
403 return yylval.i = HYPHEN;
406 char c = s[s.length () - 1];
407 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
408 here_input ().warning (
409 _ ("Brace found at end of lyric. Did you forget a space?"));
410 yylval.scm = scm_makfrom0str (s.to_str0 ());
421 return scan_bare_word (YYText ());
424 return scan_escaped_word (YYText () + 1);
427 yylval.scm = scan_fraction (YYText ());
431 yylval.i = String_convert::dec2int (String (YYText ()));
469 String str (YYText () + 1);
470 SCM s = lookup_markup_command (str);
472 if (is_pair (s) && is_symbol (ly_cdr (s)) ) {
473 yylval.scm = ly_car(s);
475 if (tag == ly_symbol2scm("markup0"))
476 return MARKUP_HEAD_MARKUP0;
477 if (tag == ly_symbol2scm("empty"))
478 return MARKUP_HEAD_EMPTY;
479 else if (tag == ly_symbol2scm ("markup0-markup1"))
480 return MARKUP_HEAD_MARKUP0_MARKUP1;
481 else if (tag == ly_symbol2scm ("markup-list0"))
482 return MARKUP_HEAD_LIST0;
483 else if (tag == ly_symbol2scm ("scheme0"))
484 return MARKUP_HEAD_SCM0;
485 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
486 return MARKUP_HEAD_SCM0_SCM1;
487 else if (tag == ly_symbol2scm ("scheme0-markup1"))
488 return MARKUP_HEAD_SCM0_MARKUP1;
489 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
490 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
491 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
492 return MARKUP_HEAD_SCM0_SCM1_SCM2;
494 programming_error ("No parser tag defined for this signature. Abort");
499 return scan_escaped_word (str);
504 [^#{}"\\ \t\n\r\f]+ {
505 String s (YYText ());
507 char c = s[s.length () - 1];
508 /* brace open is for not confusing dumb tools. */
509 if (c == '{' || c == '}')
510 here_input ().warning (
511 _ ("Brace found at end of markup. Did you forget a space?"));
512 yylval.scm = scm_makfrom0str (s.to_str0 ());
523 main_input_b_ = false;
524 if (! close_input ()) {
525 yyterminate (); // can't move this, since it actually rets a YY_NULL
531 return scan_bare_word (YYText ());
534 return scan_escaped_word (YYText () + 1);
538 int cnv=sscanf (YYText (), "%lf", &r);
541 yylval.scm = scm_make_real (r);
546 yylval.i = String_convert::dec2int (String (YYText ()));
556 char c = YYText ()[0];
561 <INITIAL,notes,figures>. {
565 <INITIAL,lyrics,notes,figures>\\. {
566 char c= YYText ()[1];
574 return E_EXCLAMATION;
582 return E_RIGHTSQUARE;
594 String msg = _f ("invalid character: `%c'", YYText ()[0]);
595 LexerError (msg.to_str0 ());
602 My_lily_lexer::push_note_state (SCM tab)
604 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
605 yy_push_state (notes);
609 My_lily_lexer::push_figuredbass_state()
611 yy_push_state (figures);
614 My_lily_lexer::push_chord_state (SCM tab)
616 pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
617 yy_push_state (chords);
621 My_lily_lexer::push_lyric_state ()
623 yy_push_state (lyrics);
627 My_lily_lexer::push_markup_state ()
629 yy_push_state (markup);
633 My_lily_lexer::pop_state ()
635 if (YYSTATE == notes || YYSTATE == chords)
636 pitchname_tab_stack_ = ly_cdr (pitchname_tab_stack_);
641 My_lily_lexer::identifier_type (SCM sid)
643 int k = try_special_identifiers (&yylval.scm , sid);
644 return k >= 0 ? k : SCM_IDENTIFIER;
649 My_lily_lexer::scan_escaped_word (String str)
651 // use more SCM for this.
653 // SCM sym = ly_symbol2scm (str.to_str0 ());
655 int l = lookup_keyword (str);
659 SCM sid = lookup_identifier (str);
660 if (sid != SCM_UNDEFINED)
663 return identifier_type (sid);
666 String msg (_f ("unknown escaped string: `\\%s'", str));
667 LexerError (msg.to_str0 ());
669 yylval.scm = scm_makfrom0str (str.to_str0 ());
675 My_lily_lexer::scan_bare_word (String str)
677 SCM sym = ly_symbol2scm (str.to_str0 ());
678 if ((YYSTATE == notes) || (YYSTATE == chords)) {
679 SCM handle = SCM_BOOL_F;
680 if (is_pair (pitchname_tab_stack_))
681 handle = scm_hashq_get_handle (ly_car (pitchname_tab_stack_), sym);
683 if (is_pair (handle)) {
684 yylval.scm = ly_cdr (handle);
685 if (unsmob_pitch (yylval.scm))
686 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
687 else if (is_symbol (yylval.scm))
690 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
692 yylval.scm = ly_cdr (handle);
693 return CHORD_MODIFIER;
697 yylval.scm = scm_makfrom0str (str.to_str0 ());
702 My_lily_lexer::is_note_state () const
704 return YY_START == notes;
708 My_lily_lexer::is_chord_state () const
710 return YY_START == chords;
714 My_lily_lexer::is_lyric_state () const
716 return YY_START == lyrics;
720 My_lily_lexer::is_figure_state () const
722 return YY_START == figures;
726 urg, belong to String (_convert)
727 and should be generalised
730 strip_leading_white (String&s)
733 for (; i < s.length (); i++)
737 s = s.nomid_string (0, i);
741 strip_trailing_white (String&s)
748 s = s.left_string (i+1);
753 /* 2.1.2x something -> \property -> \set. */
754 Lilypond_version oldest_version ("2.1.25");
758 is_valid_version (String s)
760 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
761 Lilypond_version ver (s);
762 if (! ((ver >= oldest_version) && (ver <= current)))
764 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
765 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
776 lyric_fudge (String s)
778 char * chars =s.get_copy_str0 ();
780 for (char * p = chars; *p ; p++)
782 if (*p == '_' && (p == chars || *(p-1) != '\\'))
790 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
792 * (s.get_str0 () + i + 1) = 'c';
793 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
800 Convert "NUM/DEN" into a '(NUM . DEN) cons.
803 scan_fraction (String frac)
805 int i = frac.index ('/');
806 int l = frac.length ();
807 String left = frac.left_string (i);
808 String right = frac.right_string (l - i - 1);
810 int n = String_convert::dec2int (left);
811 int d = String_convert::dec2int (right);
812 return scm_cons (scm_int2num (n), scm_int2num (d));
815 // Breaks for flex 2.5.31
817 /* avoid silly flex induced gcc warnings */
818 static void yy_push_state (int) {;}
819 static void yy_pop_state () {;}
820 static int yy_top_state () { return 0; }
823 avoid_silly_flex_induced_gcc_warnings ()
825 (void)yy_start_stack_ptr;
826 (void)yy_start_stack_depth;
827 (void)yy_start_stack;
831 avoid_silly_flex_induced_gcc_warnings ();
836 lookup_markup_command (String s)
838 SCM proc = ly_scheme_function ("lookup-markup-command");
839 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));