1 %{ // -*-Fundamental-*-
3 lexer.ll -- implement the Flex lexer
5 source file of the LilyPond music typesetter
7 (c) 1996--2003 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 valid_version_b (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 (!valid_version_b (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'", 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 if (! close_input ())
210 yyterminate (); // can't move this, since it actually rets a YY_NULL
215 <INITIAL,chords,lyrics,notes,figures>\\maininput {
219 main_input_b_ = true;
222 error (_ ("\\maininput disallowed outside init files"));
225 <INITIAL,chords,lyrics,figures,notes>\\include {
226 yy_push_state (incl);
228 <incl>\"[^"]*\";? { /* got the include file name */
229 String s (YYText ()+1);
230 s = s.left_string (s.index_last ('"'));
232 new_input (s, &global_input_file->sources_ );
235 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
236 String s = YYText () + 1;
237 strip_trailing_white (s);
238 if (s.length () && (s[s.length () - 1] == ';'))
239 s = s.left_string (s.length () - 1);
241 SCM sid = lookup_identifier (s);
242 if (gh_string_p (sid)) {
243 new_input (ly_scm2string (sid), &global_input_file->sources_);
246 String msg (_f ("wrong or undefined identifier: `%s'", s ));
248 LexerError (msg.to_str0 ());
249 SCM err = scm_current_error_port ();
250 scm_puts ("This value was found in the table: ", err);
251 scm_display (sid, err);
254 <incl>\"[^"]* { // backup rule
255 error (_ ("Missing end quote"));
258 <chords,notes,figures>{RESTNAME} {
259 const char *s = YYText ();
260 yylval.scm = scm_makfrom0str (s);
263 <chords,notes,figures>R {
264 return MULTI_MEASURE_REST;
266 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}*{WHITE} {
267 String s=YYText () + 2;
268 s=s.left_string (s.length () - 1);
269 return scan_escaped_word (s);
271 <INITIAL,chords,lyrics,notes,figures>\${BLACK}*{WHITE} {
272 String s=YYText () + 1;
273 s=s.left_string (s.length () - 1);
274 return scan_bare_word (s);
276 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}* { // backup rule
277 error (_("white expected"));
280 <INITIAL,chords,lyrics,notes,figures>\${BLACK}* { // backup rule
281 error (_("white expected"));
285 <INITIAL,markup,chords,lyrics,notes,figures># { //embedded scm
286 //char const* s = YYText () + 1;
287 char const* s = here_str0 ();
289 if (main_input_b_ && safe_global_b) {
290 error (_ ("Can't evaluate Scheme in safe mode"));
291 yylval.scm = SCM_EOL;
294 SCM sval = ly_parse_scm (s, &n, here_input());
295 if (sval == SCM_UNDEFINED)
297 sval = SCM_UNSPECIFIED;
301 for (int i=0; i < n; i++)
305 char_count_stack_.top () += n;
307 if (unpack_identifier (sval) != SCM_UNDEFINED)
309 yylval.scm = unpack_identifier(sval);
310 return identifier_type (yylval.scm);
316 <INITIAL,notes,lyrics>{
338 return scan_bare_word (YYText ());
342 return scan_escaped_word (YYText () + 1);
345 yylval.scm = scan_fraction (YYText ());
350 yylval.i = String_convert::dec2int (String (YYText ()));
354 yylval.i = String_convert::dec2int (String (YYText ()));
358 yylval.i = String_convert::dec2int (String (YYText () +1));
372 *yylval.string += to_string (escaped_char (YYText ()[1]));
375 *yylval.string += YYText ();
381 /* yylval is union. Must remember STRING before setting SCM*/
382 String *sp = yylval.string;
383 yylval.scm = scm_makfrom0str (sp->to_str0 ());
388 *yylval.string += YYText ();
397 yylval.scm = scan_fraction (YYText ());
401 yylval.i = String_convert::dec2int (String (YYText ()));
405 return scan_escaped_word (YYText () + 1);
409 String s (YYText ());
411 return yylval.i = EXTENDER;
413 return yylval.i = HYPHEN;
416 char c = s[s.length () - 1];
417 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
418 here_input ().warning (
419 _ ("Brace found at end of lyric. Did you forget a space?"));
420 yylval.scm = scm_makfrom0str (s.to_str0 ());
431 return scan_bare_word (YYText ());
434 return scan_escaped_word (YYText () + 1);
437 yylval.scm = scan_fraction (YYText ());
441 yylval.i = String_convert::dec2int (String (YYText ()));
479 String str (YYText() + 1);
480 SCM s = lookup_markup_command (str);
482 if (gh_pair_p (s) && gh_symbol_p (gh_cdr (s)) ) {
483 yylval.scm = gh_car(s);
485 if (tag == ly_symbol2scm("markup0"))
486 return MARKUP_HEAD_MARKUP0;
487 if (tag == ly_symbol2scm("empty"))
488 return MARKUP_HEAD_EMPTY;
489 else if (tag == ly_symbol2scm ("markup0-markup1"))
490 return MARKUP_HEAD_MARKUP0_MARKUP1;
491 else if (tag == ly_symbol2scm ("markup-list0"))
492 return MARKUP_HEAD_LIST0;
493 else if (tag == ly_symbol2scm ("scheme0"))
494 return MARKUP_HEAD_SCM0;
495 else if (tag == ly_symbol2scm ("scheme0-scheme1"))
496 return MARKUP_HEAD_SCM0_SCM1;
497 else if (tag == ly_symbol2scm ("scheme0-markup1"))
498 return MARKUP_HEAD_SCM0_MARKUP1;
499 else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
500 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
501 else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
502 return MARKUP_HEAD_SCM0_SCM1_SCM2;
504 programming_error ("No parser tag defined for this signature. Abort");
509 return scan_escaped_word (str);
514 [^#{}"\\ \t\n\r\f]+ {
515 String s (YYText ());
517 char c = s[s.length () - 1];
518 if (c == '{' || c == '}') // brace open is for not confusing dumb tools.
519 here_input ().warning (
520 _ ("Brace found at end of markup. Did you forget a space?"));
521 yylval.scm = scm_makfrom0str (s.to_str0 ());
534 if (! close_input ()) {
535 yyterminate (); // can't move this, since it actually rets a YY_NULL
541 return scan_bare_word (YYText ());
544 return scan_escaped_word (YYText () + 1);
548 int cnv=sscanf (YYText (), "%lf", &r);
551 yylval.scm = gh_double2scm (r);
556 yylval.i = String_convert::dec2int (String (YYText ()));
566 char c = YYText ()[0];
571 <INITIAL,notes,figures>. {
575 <INITIAL,lyrics,notes,figures>\\. {
576 char c= YYText ()[1];
584 return E_EXCLAMATION;
592 return E_RIGHTSQUARE;
604 String msg = _f ("invalid character: `%c'", YYText ()[0]);
605 LexerError (msg.to_str0 ());
612 My_lily_lexer::push_note_state ()
614 yy_push_state (notes);
618 My_lily_lexer::push_figuredbass_state()
620 yy_push_state (figures);
623 My_lily_lexer::push_chord_state ()
625 yy_push_state (chords);
629 My_lily_lexer::push_lyric_state ()
631 yy_push_state (lyrics);
635 My_lily_lexer::push_markup_state ()
637 yy_push_state (markup);
641 My_lily_lexer::pop_state ()
647 My_lily_lexer::identifier_type(SCM sid)
649 int k = try_special_identifiers(&yylval.scm , sid);
650 return k >= 0 ? k : SCM_IDENTIFIER;
655 My_lily_lexer::scan_escaped_word (String str)
657 // use more SCM for this.
659 SCM sym = ly_symbol2scm (str.to_str0 ());
661 int l = lookup_keyword (str);
665 SCM sid = lookup_identifier (str);
666 if (sid != SCM_UNDEFINED)
669 return identifier_type (sid);
672 if ((YYSTATE != notes) && (YYSTATE != chords)) {
673 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
675 if (gh_pair_p (pitch))
677 yylval.scm = ly_cdr (pitch);
678 return NOTENAME_PITCH;
681 String msg (_f ("unknown escaped string: `\\%s'", str));
682 LexerError (msg.to_str0 ());
684 yylval.scm = scm_makfrom0str (str.to_str0 ());
690 My_lily_lexer::scan_bare_word (String str)
692 SCM sym = ly_symbol2scm (str.to_str0 ());
693 if ((YYSTATE == notes) || (YYSTATE == chords)) {
694 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
695 if (gh_pair_p (pitch)) {
696 yylval.scm = ly_cdr (pitch);
697 return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
698 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
700 yylval.scm = ly_cdr (pitch);
701 return CHORD_MODIFIER;
705 yylval.scm = scm_makfrom0str (str.to_str0 ());
710 My_lily_lexer::note_state_b () const
712 return YY_START == notes;
716 My_lily_lexer::chord_state_b () const
718 return YY_START == chords;
722 My_lily_lexer::lyric_state_b () const
724 return YY_START == lyrics;
728 My_lily_lexer::figure_state_b () const
730 return YY_START == figures;
734 urg, belong to String (_convert)
735 and should be generalised
738 strip_leading_white (String&s)
741 for (; i < s.length (); i++)
745 s = s.nomid_string (0, i);
749 strip_trailing_white (String&s)
756 s = s.left_string (i+1);
761 /* 1.9.0 == postfix articulations */
762 Lilypond_version oldest_version ("1.9.0");
766 valid_version_b (String s)
768 Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
769 Lilypond_version ver (s);
770 if (! ((ver >= oldest_version) && (ver <= current)))
772 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
773 non_fatal_error (_ ("Consider updating the input with the convert-ly script"));
784 lyric_fudge (String s)
786 char * chars =s.get_copy_str0 ();
788 for (char * p = chars; *p ; p++)
790 if (*p == '_' && (p == chars || *(p-1) != '\\'))
798 if ((i=s.index ("\\,")) != -1) // change "\," to TeX's "\c "
800 * (s.get_str0 () + i + 1) = 'c';
801 s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
808 Convert "NUM/DEN" into a '(NUM . DEN) cons.
811 scan_fraction (String frac)
813 int i = frac.index ('/');
814 int l = frac.length ();
815 String left = frac.left_string (i);
816 String right = frac.right_string (l - i - 1);
818 int n = String_convert::dec2int (left);
819 int d = String_convert::dec2int (right);
820 return gh_cons (gh_int2scm (n), gh_int2scm (d));
823 // Breaks for flex 2.5.31
825 /* avoid silly flex induced gcc warnings */
826 static void yy_push_state (int) {;}
827 static void yy_pop_state () {;}
828 static int yy_top_state () { return 0; }
831 avoid_silly_flex_induced_gcc_warnings ()
833 (void)yy_start_stack_ptr;
834 (void)yy_start_stack_depth;
835 (void)yy_start_stack;
839 avoid_silly_flex_induced_gcc_warnings ();
844 lookup_markup_command (String s)
848 proc = scm_c_eval_string ("lookup-markup-command");
850 return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));