]> git.donarmstrong.com Git - lilypond.git/blob - lily/lexer.ll
Fix some bugs in the dynamic engraver and PostScript backend
[lilypond.git] / lily / lexer.ll
1 %{ // -*-Fundamental-*-
2 /*
3   lexer.ll -- implement the Flex lexer
4
5   source file of the LilyPond music typesetter
6
7   (c) 1996--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
8            Jan Nieuwenhuizen <janneke@gnu.org>
9 */
10
11
12 /*
13   backup rules
14
15   after making a change to the lexer rules, run 
16       flex -b <this lexer file>
17   and make sure that 
18       lex.backup
19   contains no backup states, but only the reminder
20       Compressed tables always back up.
21  (don-t forget to rm lex.yy.cc :-)
22  */
23
24
25
26 #include <cstdio>
27 #include <cctype>
28 #include <cerrno>
29
30 /* Flex >= 2.5.29 fix; FlexLexer.h's multiple include bracing breaks
31    when building the actual lexer.  */
32
33 #define LEXER_CC
34
35 #include <iostream>
36 using namespace std;
37
38 #include "context-def.hh"
39 #include "identifier-smob.hh"
40 #include "international.hh"
41 #include "interval.hh"
42 #include "lily-guile.hh"
43 #include "lily-lexer.hh"
44 #include "lilypond-input-version.hh"
45 #include "main.hh"
46 #include "music-function.hh"
47 #include "parse-scm.hh"
48 #include "parser.hh"
49 #include "source-file.hh"
50 #include "std-string.hh"
51 #include "string-convert.hh"
52 #include "version.hh"
53 #include "warn.hh"
54
55 /*
56 RH 7 fix (?)
57 */
58 #define isatty HORRIBLEKLUDGE
59
60 void strip_trailing_white (string&);
61 void strip_leading_white (string&);
62 string lyric_fudge (string s);
63 int music_function_type (SCM);
64 SCM lookup_markup_command (string s);
65 bool is_valid_version (string s);
66
67
68 #define start_quote()   \
69         yy_push_state (quote);\
70         yylval.string = new string
71
72 #define start_lyric_quote()     \
73         yy_push_state (lyric_quote);\
74         yylval.string = new string
75
76 #define yylval \
77         (*(YYSTYPE*)lexval)
78
79 #define yylloc \
80         (*(YYLTYPE*)lexloc)
81
82 #define YY_USER_ACTION  add_lexed_char (YYLeng ());
83 /*
84
85 LYRICS          ({AA}|{TEX})[^0-9 \t\n\f]*
86
87 */
88
89
90 SCM scan_fraction (string);
91 SCM (* scm_parse_error_handler) (void *);
92
93
94
95 %}
96
97 %option c++
98 %option noyywrap
99 %option nodefault
100 %option debug
101 %option yyclass="Lily_lexer"
102 %option stack
103 %option never-interactive 
104 %option warn
105
106 %x chords
107 %x figures
108 %x incl
109 %x lyrics
110 %x lyric_quote
111 %x longcomment
112 %x markup
113 %x notes
114 %x quote
115 %x sourcefilename
116 %x version
117
118 A               [a-zA-Z]
119 AA              {A}|_
120 N               [0-9]
121 AN              {AA}|{N}
122 PUNCT           [?!:'`]
123 ACCENT          \\[`'"^]
124 NATIONAL        [\001-\006\021-\027\031\036\200-\377]
125 TEX             {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
126 WORD            {A}{AN}*
127 DASHED_WORD             {A}({AN}|-)*
128 DASHED_KEY_WORD         \\{DASHED_WORD}
129 ALPHAWORD       {A}+
130 DIGIT           {N}
131 UNSIGNED        {N}+
132 E_UNSIGNED      \\{N}+
133 FRACTION        {N}+\/{N}+
134 INT             -?{UNSIGNED}
135 REAL            ({INT}\.{N}*)|(-?\.{N}+)
136 KEYWORD         \\{WORD}
137 WHITE           [ \n\t\f\r]
138 HORIZONTALWHITE         [ \t]
139 BLACK           [^ \n\t\f\r]
140 RESTNAME        [rs]
141 NOTECOMMAND     \\{A}+
142 MARKUPCOMMAND   \\({A}|[-_])+
143 LYRICS          ({AA}|{TEX})[^0-9 \t\n\r\f]*
144 ESCAPED         [nt\\'"]
145 EXTENDER        __
146 HYPHEN          --
147 BOM_UTF8        \357\273\277
148 %%
149
150
151 <*>\r           {
152         // windows-suck-suck-suck
153 }
154
155 <INITIAL,chords,lyrics,figures,notes>{BOM_UTF8} {
156   if (this->lexloc->line_number () != 1 || this->lexloc->column_number () != 0)
157     {
158       LexerError (_ ("stray UTF-8 BOM encountered").c_str ());
159       exit (1);
160     }
161   if (be_verbose_global)
162      message (_ ("Skipping UTF-8 BOM"));
163 }
164
165 <INITIAL,chords,figures,incl,lyrics,markup,notes>{
166   "%{"  {
167         yy_push_state (longcomment);
168   }
169   %[^{\n\r].*[\n\r]     {
170   }
171   %[^{\n\r]     { // backup rule
172   }
173   %[\n\r]       {
174   }
175   %[^{\n\r].*   {
176   }
177   {WHITE}+      {
178
179   }
180 }
181
182 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}*  {
183         yy_push_state (version);
184 }
185 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}*   {
186         yy_push_state (sourcefilename);
187 }
188 <version>\"[^"]*\"     { /* got the version number */
189         string s (YYText () + 1);
190         s = s.substr (0, s.rfind ('\"'));
191
192         yy_pop_state ();
193         if (!is_valid_version (s))
194                 return INVALID;
195
196         SCM top_scope = scm_car (scm_last_pair (scopes_));
197         scm_module_define (top_scope, ly_symbol2scm ("version-seen?"), SCM_BOOL_T);
198
199 }
200 <sourcefilename>\"[^"]*\"     {
201         string s (YYText () + 1);
202         s = s.substr (0, s.rfind ('\"'));
203
204         yy_pop_state ();
205         this->here_input().get_source_file ()->name_ = s;
206         message (_f ("Renaming input to: `%s'", s.c_str ()));
207         progress_indication ("\n");
208         scm_module_define (scm_car (scopes_),
209                      ly_symbol2scm ("input-file-name"),
210                      scm_makfrom0str (s.c_str ()));
211
212 }
213 <version>.      {
214         LexerError (_ ("quoted string expected after \\version").c_str ());
215         yy_pop_state ();
216 }
217 <sourcefilename>.       {
218         LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
219         yy_pop_state ();
220 }
221 <longcomment>{
222         [^\%]*          {
223         }
224         \%*[^}%]*               {
225
226         }
227         "%"+"}"         {
228                 yy_pop_state ();
229         }
230         <<EOF>>         {
231                 LexerError (_ ("EOF found inside a comment").c_str ());
232                 is_main_input_ = false;
233                 if (! close_input ()) 
234                   yyterminate (); // can't move this, since it actually rets a YY_NULL
235         }
236 }
237
238
239 <INITIAL,chords,lyrics,notes,figures>\\maininput           {
240         if (!is_main_input_)
241         {
242                 start_main_input ();
243                 is_main_input_ = true;
244         }
245         else
246                 error (_ ("\\maininput not allowed outside init files"));
247 }
248
249 <INITIAL,chords,lyrics,figures,notes>\\include           {
250         yy_push_state (incl);
251 }
252 <incl>\"[^"]*\"   { /* got the include file name */
253         string s (YYText ()+1);
254         s = s.substr (0, s.rfind ('"'));
255
256         new_input (s, sources_);
257         yy_pop_state ();
258 }
259 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
260         string s = YYText () + 1;
261         strip_trailing_white (s);
262         if (s.length () && (s[s.length () - 1] == ';'))
263           s = s.substr (0, s.length () - 1);
264
265         SCM sid = lookup_identifier (s);
266         if (scm_is_string (sid)) {
267                 new_input (ly_scm2string (sid), sources_);
268                 yy_pop_state ();
269         } else { 
270             string msg (_f ("wrong or undefined identifier: `%s'", s ));
271
272             LexerError (msg.c_str ());
273             SCM err = scm_current_error_port ();
274             scm_puts ("This value was found in the table: ", err);
275             scm_display (sid, err);
276           }
277 }
278 <incl>\"[^"]*   { // backup rule
279         error (_ ("end quote missing"));
280         exit (1);
281 }
282 <chords,notes,figures>{RESTNAME}        {
283         char const *s = YYText ();
284         yylval.scm = scm_makfrom0str (s);
285         return RESTNAME;
286 }
287 <chords,notes,figures>R         {
288         return MULTI_MEASURE_REST;
289 }
290 <INITIAL,chords,figures,lyrics,markup,notes>#   { //embedded scm
291         int n = 0;
292         Input hi = here_input();
293         hi.step_forward ();
294         SCM sval = ly_parse_scm (hi.start (), &n, hi,
295                 be_safe_global && is_main_input_);
296
297         if (sval == SCM_UNDEFINED)
298         {
299                 sval = SCM_UNSPECIFIED;
300                 error_level_ = 1;
301         }
302
303         for (int i = 0; i < n; i++)
304         {
305                 yyinput ();
306         }
307         char_count_stack_.back () += n;
308
309         if (unpack_identifier (sval) != SCM_UNDEFINED)
310         {
311                 yylval.scm = unpack_identifier(sval);
312                 return identifier_type (yylval.scm);
313         }
314                 
315         yylval.scm = sval;
316         return SCM_T;
317 }
318 <INITIAL,notes,lyrics>{ 
319         \<\<   {
320                 return DOUBLE_ANGLE_OPEN;
321         }
322         \>\>   {
323                 return DOUBLE_ANGLE_CLOSE;
324         }
325 }
326 <figures>{
327         _       {
328                 return FIGURE_SPACE;
329         }
330         \>              {
331                 return FIGURE_CLOSE;
332         }
333         \<      {
334                 return FIGURE_OPEN;
335         }
336 }
337
338 <notes,figures>{
339         {ALPHAWORD}     {
340                 return scan_bare_word (YYText ());
341         }
342
343         {NOTECOMMAND}   {
344                 return scan_escaped_word (YYText () + 1); 
345         }
346         {FRACTION}      {
347                 yylval.scm =  scan_fraction (YYText ());
348                 return FRACTION;
349         }
350
351         {DIGIT}         {
352                 yylval.i = String_convert::dec2int (string (YYText ()));
353                 return DIGIT;
354         }
355         {UNSIGNED}              {
356                 yylval.i = String_convert::dec2int (string (YYText ()));
357                 return UNSIGNED;
358         }
359         {E_UNSIGNED}    {
360                 yylval.i = String_convert::dec2int (string (YYText () +1));
361                 return E_UNSIGNED;
362         }
363         \" {
364                 start_quote ();
365         }
366 }
367
368 \"              {
369         start_quote ();
370 }
371 <quote>{
372         \\{ESCAPED}     {
373                 *yylval.string += to_string (escaped_char (YYText ()[1]));
374         }
375         [^\\"]+ {
376                 *yylval.string += YYText ();
377         }
378         \"      {
379
380                 yy_pop_state ();
381
382                 /* yylval is union. Must remember STRING before setting SCM*/
383                 string *sp = yylval.string;
384                 yylval.scm = scm_makfrom0str (sp->c_str ());
385                 delete sp;
386                 return STRING;
387         }
388         .       {
389                 *yylval.string += YYText ();
390         }
391 }
392 <lyric_quote>{
393         \\{ESCAPED}     {
394                 *yylval.string += to_string (escaped_char (YYText ()[1]));
395         }
396         [^\\"]+ {
397                 *yylval.string += YYText ();
398         }
399         \"      {
400
401                 yy_pop_state ();
402
403                 /* yylval is union. Must remember STRING before setting SCM*/
404                 string *sp = yylval.string;
405                 yylval.scm = scm_makfrom0str (sp->c_str ());
406                 delete sp;
407                 return LYRICS_STRING;
408         }
409         .       {
410                 *yylval.string += YYText ();
411         }
412 }
413
414 <lyrics>{
415         \" {
416                 start_lyric_quote ();
417         }
418         {FRACTION}      {
419                 yylval.scm =  scan_fraction (YYText ());
420                 return FRACTION;
421         }
422         {UNSIGNED}              {
423                 yylval.i = String_convert::dec2int (string (YYText ()));
424                 return UNSIGNED;
425         }
426         {NOTECOMMAND}   {
427                 return scan_escaped_word (YYText () + 1);
428         }
429         {LYRICS} {
430                 /* ugr. This sux. */
431                 string s (YYText ()); 
432                 if (s == "__")
433                         return yylval.i = EXTENDER;
434                 if (s == "--")
435                         return yylval.i = HYPHEN;
436                 s = lyric_fudge (s);
437
438                 char c = s[s.length () - 1];
439                 if (c == '{' ||  c == '}') // brace open is for not confusing dumb tools.
440                         here_input ().warning (
441                                 _ ("Brace found at end of lyric.  Did you forget a space?"));
442                 yylval.scm = scm_makfrom0str (s.c_str ());
443
444
445                 return LYRICS_STRING;
446         }
447         . {
448                 return YYText ()[0];
449         }
450 }
451 <chords>{
452         {ALPHAWORD}     {
453                 return scan_bare_word (YYText ());
454         }
455         {NOTECOMMAND}   {
456                 return scan_escaped_word (YYText () + 1);
457         }
458         {FRACTION}      {
459                 yylval.scm =  scan_fraction (YYText ());
460                 return FRACTION;
461         }
462         {UNSIGNED}              {
463                 yylval.i = String_convert::dec2int (string (YYText ()));
464                 return UNSIGNED;
465         }
466         \" {
467                 start_quote ();
468         }
469         -  {
470                 return CHORD_MINUS;
471         }
472         :  {
473                 return CHORD_COLON;
474         }
475         \/\+ {
476                 return CHORD_BASS;
477         }
478         \/  {
479                 return CHORD_SLASH;
480         }
481         \^  {
482                 return CHORD_CARET;
483         }
484         . {
485                 return YYText ()[0];
486         }
487 }
488
489
490 <markup>{
491         \" {
492                 start_quote ();
493         }
494         \\score {
495                 return SCORE;
496         }
497         {MARKUPCOMMAND} {
498                 string str (YYText () + 1);
499                 SCM s = lookup_markup_command (str);
500
501                 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
502                         yylval.scm = scm_car(s);
503                         SCM tag = scm_cdr(s);
504                         if (tag == ly_symbol2scm("markup0"))
505                                 return MARKUP_HEAD_MARKUP0;
506                         if (tag == ly_symbol2scm("empty"))
507                                 return MARKUP_HEAD_EMPTY;
508                         else if (tag == ly_symbol2scm ("markup0-markup1"))
509                                 return MARKUP_HEAD_MARKUP0_MARKUP1;
510                         else if (tag == ly_symbol2scm ("markup-list0"))
511                                 return MARKUP_HEAD_LIST0;
512                         else if (tag == ly_symbol2scm ("scheme0"))
513                                 return MARKUP_HEAD_SCM0;
514                         else if (tag == ly_symbol2scm ("scheme0-scheme1"))
515                                 return MARKUP_HEAD_SCM0_SCM1;
516                         else if (tag == ly_symbol2scm ("scheme0-markup1"))
517                                 return MARKUP_HEAD_SCM0_MARKUP1;
518                         else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
519                                 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
520                         else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
521                                 return MARKUP_HEAD_SCM0_SCM1_SCM2;
522                         else {
523                                 programming_error ("no parser tag defined for this markup signature"); 
524                                 ly_display_scm (s);
525                                 assert(false);
526                         }
527                 } else
528                         return scan_escaped_word (str);
529         }
530         [{}]    {
531                 return YYText ()[0];
532         }
533         [^#{}"\\ \t\n\r\f]+ {
534                 string s (YYText ()); 
535
536                 char c = s[s.length () - 1];
537                 /* brace open is for not confusing dumb tools.  */
538                 if (c == '{' ||  c == '}')
539                         here_input ().warning (
540                                 _ ("Brace found at end of markup.  Did you forget a space?"));
541                 yylval.scm = scm_makfrom0str (s.c_str ());
542
543
544                 return STRING;
545         }
546         .  {
547                 return YYText()[0];
548         }
549 }
550
551 <<EOF>> {
552         if (is_main_input_)
553         {
554                 is_main_input_ = false;
555                 if (!close_input ())
556                 /* Returns YY_NULL */
557                         yyterminate ();
558         }
559         else if (!close_input ())
560                 /* Returns YY_NULL */
561                 yyterminate ();
562 }
563
564 <INITIAL>{
565         {DASHED_WORD}   {
566                 return scan_bare_word (YYText ());
567         }
568         {DASHED_KEY_WORD}       {
569                 return scan_escaped_word (YYText () + 1);
570         }
571 }
572
573 {WORD}  {
574         return scan_bare_word (YYText ());
575 }
576 {KEYWORD}       {
577         return scan_escaped_word (YYText () + 1);
578 }
579 {REAL}          {
580         Real r;
581         int cnv = sscanf (YYText (), "%lf", &r);
582         assert (cnv == 1);
583         (void) cnv;
584
585         yylval.scm = scm_from_double (r);
586         return REAL;
587 }
588
589 {UNSIGNED}      {
590         yylval.i = String_convert::dec2int (string (YYText ()));
591         return UNSIGNED;
592 }
593
594
595 [{}]    {
596
597         return YYText ()[0];
598 }
599 [*:=]           {
600         char c = YYText ()[0];
601
602         return c;
603 }
604
605 <INITIAL,notes,figures>.        {
606         return YYText ()[0];
607 }
608
609 <INITIAL,lyrics,notes,figures>\\. {
610     char c = YYText ()[1];
611
612     switch (c) {
613     case '>':
614         return E_ANGLE_CLOSE;
615     case '<':
616         return E_ANGLE_OPEN;
617     case '!':
618         return E_EXCLAMATION;
619     case '(':
620         return E_OPEN;
621     case ')':
622         return E_CLOSE;
623     case '[':
624         return E_BRACKET_OPEN;
625     case '+':
626         return E_PLUS;
627     case ']':
628         return E_BRACKET_CLOSE;
629     case '~':
630         return E_TILDE;
631     case '\\':
632         return E_BACKSLASH;
633
634     default:
635         return E_CHAR;
636     }
637 }
638
639 <*>.            {
640         string msg = _f ("invalid character: `%c'", YYText ()[0]);
641         LexerError (msg.c_str ());
642         return YYText ()[0];
643 }
644
645 %%
646
647 void
648 Lily_lexer::push_chord_state (SCM tab)
649 {
650         pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
651         yy_push_state (chords);
652 }
653
654 void
655 Lily_lexer::push_figuredbass_state ()
656 {
657         yy_push_state (figures);
658 }
659
660 void
661 Lily_lexer::push_initial_state ()
662 {
663         yy_push_state (INITIAL);
664 }
665
666 void
667 Lily_lexer::push_lyric_state ()
668 {
669         yy_push_state (lyrics);
670 }
671
672 void
673 Lily_lexer::push_markup_state ()
674 {
675         yy_push_state (markup);
676 }
677
678 void
679 Lily_lexer::push_note_state (SCM tab)
680 {
681         pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
682         yy_push_state (notes);
683 }
684
685 void
686 Lily_lexer::pop_state ()
687 {
688         if (YYSTATE == notes || YYSTATE == chords)
689                 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
690         yy_pop_state ();
691 }
692
693 int
694 Lily_lexer::identifier_type (SCM sid)
695 {
696         int k = try_special_identifiers (&yylval.scm , sid);
697         return k >= 0  ? k : SCM_IDENTIFIER;
698 }
699
700
701 int
702 Lily_lexer::scan_escaped_word (string str)
703 {
704         // use more SCM for this.
705
706 //      SCM sym = ly_symbol2scm (str.c_str ());
707
708         int i = lookup_keyword (str);
709         if (i == MARKUP && is_lyric_state ())
710                 return LYRIC_MARKUP;
711         if (i != -1)
712                 return i;
713
714         SCM sid = lookup_identifier (str);
715         if (is_music_function (sid))
716         {
717                 yylval.scm = get_music_function_transform (sid);
718                 return music_function_type (yylval.scm);
719         }
720
721         if (sid != SCM_UNDEFINED)
722         {
723                 yylval.scm = sid;
724                 return identifier_type (sid);
725         }
726
727         string msg (_f ("unknown escaped string: `\\%s'", str));        
728         LexerError (msg.c_str ());
729
730         yylval.scm = scm_makfrom0str (str.c_str ());
731
732         return STRING;
733 }
734
735 int
736 Lily_lexer::scan_bare_word (string str)
737 {
738         SCM sym = ly_symbol2scm (str.c_str ());
739         if ((YYSTATE == notes) || (YYSTATE == chords)) {
740                 SCM handle = SCM_BOOL_F;
741                 if (scm_is_pair (pitchname_tab_stack_))
742                         handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
743                 
744                 if (scm_is_pair (handle)) {
745                         yylval.scm = scm_cdr (handle);
746                         if (unsmob_pitch (yylval.scm)) 
747                             return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
748                         else if (scm_is_symbol (yylval.scm))
749                             return DRUM_PITCH;
750                 }
751                 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
752                 {
753                     yylval.scm = scm_cdr (handle);
754                     return CHORD_MODIFIER;
755                 }
756         }
757
758         yylval.scm = scm_makfrom0str (str.c_str ());
759         return STRING;
760 }
761
762 bool
763 Lily_lexer::is_note_state () const
764 {
765         return YY_START == notes;
766 }
767
768 bool
769 Lily_lexer::is_chord_state () const
770 {
771         return YY_START == chords;
772 }
773
774 bool
775 Lily_lexer::is_lyric_state () const
776 {
777         return YY_START == lyrics;
778 }
779
780 bool
781 Lily_lexer::is_figure_state () const
782 {
783         return YY_START == figures;
784 }
785
786 /*
787  urg, belong to string (_convert)
788  and should be generalised 
789  */
790 void
791 strip_leading_white (string&s)
792 {
793         ssize i = 0;
794         for (;  i < s.length (); i++)
795                 if (!isspace (s[i]))
796                         break;
797
798         s = s.substr (i);
799 }
800
801 void
802 strip_trailing_white (string&s)
803 {
804         ssize i = s.length ();  
805         while (i--) 
806                 if (!isspace (s[i]))
807                         break;
808
809         s = s.substr (0, i + 1);
810 }
811
812
813
814 Lilypond_version oldest_version ("2.7.38");
815
816
817 bool
818 is_valid_version (string s)
819 {
820   Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
821   Lilypond_version ver (s);
822   if (! ((ver >= oldest_version) && (ver <= current)))
823         {       
824                 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
825                 non_fatal_error (_ ("Consider updating the input with the convert-ly script")); 
826                 return false;
827     }
828   return true;
829 }
830         
831
832 /*
833   substitute _ and \,
834 */
835 string
836 lyric_fudge (string s)
837 {
838   char *chars = string_copy (s);
839
840   for (char *p = chars; *p ; p++)
841     {
842       if (*p == '_' && (p == chars || *(p-1) != '\\'))
843         *p = ' ';
844     }
845   
846   s = string (chars);
847   delete[] chars;
848
849   ssize i = 0;  
850   if ((i = s.find ("\\,")) != NPOS)   // change "\," to TeX's "\c "
851     {
852       * (((char*)s.c_str ()) + i + 1) = 'c';
853       s = s.substr (0, i + 2) + " " + s.substr (i - 2);
854     }
855
856   return s;
857 }
858
859 /*
860 Convert "NUM/DEN" into a '(NUM . DEN) cons.
861 */
862 SCM
863 scan_fraction (string frac)
864 {
865         ssize i = frac.find ('/');
866         string left = frac.substr (0, i);
867         string right = frac.substr (i + 1, (frac.length () - i + 1));
868
869         int n = String_convert::dec2int (left);
870         int d = String_convert::dec2int (right);
871         return scm_cons (scm_from_int (n), scm_from_int (d));
872 }
873
874 SCM
875 lookup_markup_command (string s)
876 {
877         SCM proc = ly_lily_module_constant ("lookup-markup-command");
878         return scm_call_1 (proc, scm_makfrom0str (s.c_str ()));
879 }
880
881 struct Parser_signature
882 {
883         char *symbol;
884         int token_type;
885 };
886 static SCM signature_hash_table;
887
888 static void init_signature_hash_table ()
889 {
890         signature_hash_table = scm_gc_protect_object (scm_c_make_hash_table (31));
891         Parser_signature sigs[]  = {
892                 {"scm", MUSIC_FUNCTION_SCM},
893                 {"music", MUSIC_FUNCTION_MUSIC},
894                 {"scm-music", MUSIC_FUNCTION_SCM_MUSIC},
895                 {"scm-scm", MUSIC_FUNCTION_SCM_SCM},
896                 {"music-music", MUSIC_FUNCTION_MUSIC_MUSIC},
897                 {"scm-music-music", MUSIC_FUNCTION_SCM_MUSIC_MUSIC},
898                 {"scm-scm-music-music", MUSIC_FUNCTION_SCM_SCM_MUSIC_MUSIC},
899                 {"scm-scm-music", MUSIC_FUNCTION_SCM_SCM_MUSIC},
900                 {"scm-scm-scm-music", MUSIC_FUNCTION_SCM_SCM_SCM_SCM_MUSIC},
901                 {"scm-scm-scm-scm-music", MUSIC_FUNCTION_SCM_SCM_SCM_MUSIC},
902                 {"scm-scm-scm", MUSIC_FUNCTION_SCM_SCM_SCM},
903                 {"markup", MUSIC_FUNCTION_MARKUP},
904                 {"markup-music", MUSIC_FUNCTION_MARKUP_MUSIC},
905                 {"markup-markup", MUSIC_FUNCTION_MARKUP_MARKUP},
906                 {"markup-music-music", MUSIC_FUNCTION_MARKUP_MUSIC_MUSIC},
907                 {"markup-markup-music", MUSIC_FUNCTION_MARKUP_MARKUP_MUSIC},
908                 {"noarg", MUSIC_FUNCTION},
909                 {0,0}
910         };
911
912         for (int i = 0; sigs[i].symbol; i++)
913                 scm_hashq_set_x (signature_hash_table, scm_gc_protect_object (ly_symbol2scm (sigs[i].symbol)),
914                                  scm_from_int (sigs[i].token_type));
915 }
916
917 int
918 music_function_type (SCM func)
919 {
920         if (!signature_hash_table)
921                 init_signature_hash_table ();
922
923         SCM type = scm_object_property (func, ly_symbol2scm ("music-function-signature-keyword"));
924         SCM token_type = scm_hashq_ref (signature_hash_table, type, SCM_BOOL_F);
925         if (!scm_is_number (token_type))
926                 {
927                 programming_error (_ ("can't find signature for music function"));
928                 return MUSIC_FUNCTION_SCM;
929                 }
930         
931         return scm_to_int (token_type);
932 }
933
934 /* Shut up lexer warnings.  */
935 #if YY_STACK_USED
936
937 static void
938 yy_push_state (int)
939 {
940 }
941
942 static void
943 yy_pop_state ()
944 {
945 }
946
947 static int
948 yy_top_state ()
949 {
950   return 0;
951 }
952
953 static void
954 silence_lexer_warnings ()
955 {
956    (void) yy_start_stack_ptr;
957    (void) yy_start_stack_depth;
958    (void) yy_start_stack;
959    (void) yy_push_state;
960    (void) yy_pop_state;
961    (void) yy_top_state;
962    (void) silence_lexer_warnings;
963 }
964 #endif