]> git.donarmstrong.com Git - lilypond.git/blob - lily/lexer.ll
* lily/parser.yy, lily/lexer.ll: added some simplifications by
[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,notes,figures,chords,markup>{
183         \"              {
184                 start_quote ();
185         }
186 }
187
188 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}*  {
189         yy_push_state (version);
190 }
191 <INITIAL,chords,lyrics,notes,figures>\\sourcefilename{WHITE}*   {
192         yy_push_state (sourcefilename);
193 }
194 <version>\"[^"]*\"     { /* got the version number */
195         string s (YYText () + 1);
196         s = s.substr (0, s.rfind ('\"'));
197
198         yy_pop_state ();
199
200         SCM top_scope = scm_car (scm_last_pair (scopes_));
201         scm_module_define (top_scope, ly_symbol2scm ("version-seen"), SCM_BOOL_T);
202
203         if (!is_valid_version (s))
204                 return INVALID;
205
206
207 }
208 <sourcefilename>\"[^"]*\"     {
209         string s (YYText () + 1);
210         s = s.substr (0, s.rfind ('\"'));
211
212         yy_pop_state ();
213         this->here_input().get_source_file ()->name_ = s;
214         message (_f ("Renaming input to: `%s'", s.c_str ()));
215         progress_indication ("\n");
216         scm_module_define (scm_car (scopes_),
217                      ly_symbol2scm ("input-file-name"),
218                      scm_makfrom0str (s.c_str ()));
219
220 }
221 <version>.      {
222         LexerError (_ ("quoted string expected after \\version").c_str ());
223         yy_pop_state ();
224 }
225 <sourcefilename>.       {
226         LexerError (_ ("quoted string expected after \\sourcefilename").c_str ());
227         yy_pop_state ();
228 }
229 <longcomment>{
230         [^\%]*          {
231         }
232         \%*[^}%]*               {
233
234         }
235         "%"+"}"         {
236                 yy_pop_state ();
237         }
238         <<EOF>>         {
239                 LexerError (_ ("EOF found inside a comment").c_str ());
240                 is_main_input_ = false;
241                 if (! close_input ()) 
242                   yyterminate (); // can't move this, since it actually rets a YY_NULL
243         }
244 }
245
246
247 <INITIAL,chords,lyrics,notes,figures>\\maininput           {
248         if (!is_main_input_)
249         {
250                 start_main_input ();
251                 is_main_input_ = true;
252         }
253         else
254                 error (_ ("\\maininput not allowed outside init files"));
255 }
256
257 <INITIAL,chords,lyrics,figures,notes>\\include           {
258         yy_push_state (incl);
259 }
260 <incl>\"[^"]*\"   { /* got the include file name */
261         string s (YYText ()+1);
262         s = s.substr (0, s.rfind ('"'));
263
264         new_input (s, sources_);
265         yy_pop_state ();
266 }
267 <incl>\\{BLACK}*{WHITE} { /* got the include identifier */
268         string s = YYText () + 1;
269         strip_trailing_white (s);
270         if (s.length () && (s[s.length () - 1] == ';'))
271           s = s.substr (0, s.length () - 1);
272
273         SCM sid = lookup_identifier (s);
274         if (scm_is_string (sid)) {
275                 new_input (ly_scm2string (sid), sources_);
276                 yy_pop_state ();
277         } else { 
278             string msg (_f ("wrong or undefined identifier: `%s'", s ));
279
280             LexerError (msg.c_str ());
281             SCM err = scm_current_error_port ();
282             scm_puts ("This value was found in the table: ", err);
283             scm_display (sid, err);
284           }
285 }
286 <incl>\"[^"]*   { // backup rule
287         error (_ ("end quote missing"));
288         exit (1);
289 }
290 <chords,notes,figures>{RESTNAME}        {
291         char const *s = YYText ();
292         yylval.scm = scm_makfrom0str (s);
293         return RESTNAME;
294 }
295 <chords,notes,figures>R         {
296         return MULTI_MEASURE_REST;
297 }
298 <INITIAL,chords,figures,lyrics,markup,notes>#   { //embedded scm
299         int n = 0;
300         Input hi = here_input();
301         hi.step_forward ();
302         SCM sval = ly_parse_scm (hi.start (), &n, hi,
303                 be_safe_global && is_main_input_);
304
305         if (sval == SCM_UNDEFINED)
306         {
307                 sval = SCM_UNSPECIFIED;
308                 error_level_ = 1;
309         }
310
311         for (int i = 0; i < n; i++)
312         {
313                 yyinput ();
314         }
315         char_count_stack_.back () += n;
316
317         if (unpack_identifier (sval) != SCM_UNDEFINED)
318         {
319                 yylval.scm = unpack_identifier(sval);
320                 return identifier_type (yylval.scm);
321         }
322                 
323         yylval.scm = sval;
324         return SCM_T;
325 }
326 <INITIAL,notes,lyrics>{ 
327         \<\<    {
328                 return DOUBLE_ANGLE_OPEN;
329         }
330         \>\>    {
331                 return DOUBLE_ANGLE_CLOSE;
332         }
333 }
334
335 <INITIAL,notes>{
336         \<      {
337                 return ANGLE_OPEN;
338         }
339         \>      {
340                 return ANGLE_CLOSE;
341         }
342 }
343
344 <figures>{
345         _       {
346                 return FIGURE_SPACE;
347         }
348         \>              {
349                 return FIGURE_CLOSE;
350         }
351         \<      {
352                 return FIGURE_OPEN;
353         }
354 }
355
356 <notes,figures>{
357         {ALPHAWORD}     {
358                 return scan_bare_word (YYText ());
359         }
360
361         {NOTECOMMAND}   {
362                 return scan_escaped_word (YYText () + 1); 
363         }
364         {FRACTION}      {
365                 yylval.scm =  scan_fraction (YYText ());
366                 return FRACTION;
367         }
368
369         {DIGIT}         {
370                 yylval.i = String_convert::dec2int (string (YYText ()));
371                 return DIGIT;
372         }
373         {UNSIGNED}              {
374                 yylval.i = String_convert::dec2int (string (YYText ()));
375                 return UNSIGNED;
376         }
377         {E_UNSIGNED}    {
378                 yylval.i = String_convert::dec2int (string (YYText () +1));
379                 return E_UNSIGNED;
380         }
381 }
382
383 <quote,lyric_quote>{
384         \\{ESCAPED}     {
385                 *yylval.string += to_string (escaped_char (YYText ()[1]));
386         }
387         [^\\"]+ {
388                 *yylval.string += YYText ();
389         }
390         \"      {
391
392                 yy_pop_state ();
393
394                 /* yylval is union. Must remember STRING before setting SCM*/
395                 string *sp = yylval.string;
396                 yylval.scm = scm_makfrom0str (sp->c_str ());
397                 delete sp;
398                 return is_lyric_state () ? LYRICS_STRING : STRING;
399         }
400         .       {
401                 *yylval.string += YYText ();
402         }
403 }
404
405 <lyrics>{
406         \" {
407                 start_lyric_quote ();
408         }
409         {FRACTION}      {
410                 yylval.scm =  scan_fraction (YYText ());
411                 return FRACTION;
412         }
413         {UNSIGNED}              {
414                 yylval.i = String_convert::dec2int (string (YYText ()));
415                 return UNSIGNED;
416         }
417         {NOTECOMMAND}   {
418                 return scan_escaped_word (YYText () + 1);
419         }
420         {LYRICS} {
421                 /* ugr. This sux. */
422                 string s (YYText ()); 
423                 if (s == "__")
424                         return yylval.i = EXTENDER;
425                 if (s == "--")
426                         return yylval.i = HYPHEN;
427                 s = lyric_fudge (s);
428
429                 char c = s[s.length () - 1];
430                 if (c == '{' ||  c == '}') // brace open is for not confusing dumb tools.
431                         here_input ().warning (
432                                 _ ("Brace found at end of lyric.  Did you forget a space?"));
433                 yylval.scm = scm_makfrom0str (s.c_str ());
434
435
436                 return LYRICS_STRING;
437         }
438         . {
439                 return YYText ()[0];
440         }
441 }
442 <chords>{
443         {ALPHAWORD}     {
444                 return scan_bare_word (YYText ());
445         }
446         {NOTECOMMAND}   {
447                 return scan_escaped_word (YYText () + 1);
448         }
449         {FRACTION}      {
450                 yylval.scm =  scan_fraction (YYText ());
451                 return FRACTION;
452         }
453         {UNSIGNED}              {
454                 yylval.i = String_convert::dec2int (string (YYText ()));
455                 return UNSIGNED;
456         }
457         -  {
458                 return CHORD_MINUS;
459         }
460         :  {
461                 return CHORD_COLON;
462         }
463         \/\+ {
464                 return CHORD_BASS;
465         }
466         \/  {
467                 return CHORD_SLASH;
468         }
469         \^  {
470                 return CHORD_CARET;
471         }
472         . {
473                 return YYText ()[0];
474         }
475 }
476
477
478 <markup>{
479         \\score {
480                 return SCORE;
481         }
482         {MARKUPCOMMAND} {
483                 string str (YYText () + 1);
484                 SCM s = lookup_markup_command (str);
485
486                 if (scm_is_pair (s) && scm_is_symbol (scm_cdr (s)) ) {
487                         yylval.scm = scm_car(s);
488                         SCM tag = scm_cdr(s);
489                         if (tag == ly_symbol2scm("markup0"))
490                                 return MARKUP_HEAD_MARKUP0;
491                         if (tag == ly_symbol2scm("empty"))
492                                 return MARKUP_HEAD_EMPTY;
493                         else if (tag == ly_symbol2scm ("markup0-markup1"))
494                                 return MARKUP_HEAD_MARKUP0_MARKUP1;
495                         else if (tag == ly_symbol2scm ("markup-list0"))
496                                 return MARKUP_HEAD_LIST0;
497                         else if (tag == ly_symbol2scm ("scheme0"))
498                                 return MARKUP_HEAD_SCM0;
499                         else if (tag == ly_symbol2scm ("scheme0-scheme1"))
500                                 return MARKUP_HEAD_SCM0_SCM1;
501                         else if (tag == ly_symbol2scm ("scheme0-markup1"))
502                                 return MARKUP_HEAD_SCM0_MARKUP1;
503                         else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
504                                 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
505                         else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
506                                 return MARKUP_HEAD_SCM0_SCM1_SCM2;
507                         else {
508                                 programming_error ("no parser tag defined for this markup signature"); 
509                                 ly_display_scm (s);
510                                 assert(false);
511                         }
512                 } else
513                         return scan_escaped_word (str);
514         }
515         [{}]    {
516                 return YYText ()[0];
517         }
518         [^#{}"\\ \t\n\r\f]+ {
519                 string s (YYText ()); 
520
521                 char c = s[s.length () - 1];
522                 /* brace open is for not confusing dumb tools.  */
523                 if (c == '{' ||  c == '}')
524                         here_input ().warning (
525                                 _ ("Brace found at end of markup.  Did you forget a space?"));
526                 yylval.scm = scm_makfrom0str (s.c_str ());
527
528
529                 return STRING;
530         }
531         .  {
532                 return YYText()[0];
533         }
534 }
535
536 <<EOF>> {
537         if (is_main_input_)
538         {
539                 is_main_input_ = false;
540                 if (!close_input ())
541                 /* Returns YY_NULL */
542                         yyterminate ();
543         }
544         else if (!close_input ())
545                 /* Returns YY_NULL */
546                 yyterminate ();
547 }
548
549 <INITIAL>{
550         {DASHED_WORD}   {
551                 return scan_bare_word (YYText ());
552         }
553         {DASHED_KEY_WORD}       {
554                 return scan_escaped_word (YYText () + 1);
555         }
556 }
557
558 {WORD}  {
559         return scan_bare_word (YYText ());
560 }
561 {KEYWORD}       {
562         return scan_escaped_word (YYText () + 1);
563 }
564 {REAL}          {
565         Real r;
566         int cnv = sscanf (YYText (), "%lf", &r);
567         assert (cnv == 1);
568         (void) cnv;
569
570         yylval.scm = scm_from_double (r);
571         return REAL;
572 }
573
574 {UNSIGNED}      {
575         yylval.i = String_convert::dec2int (string (YYText ()));
576         return UNSIGNED;
577 }
578
579
580 [{}]    {
581
582         return YYText ()[0];
583 }
584 [*:=]           {
585         char c = YYText ()[0];
586
587         return c;
588 }
589
590 <INITIAL,notes,figures>.        {
591         return YYText ()[0];
592 }
593
594 <INITIAL,lyrics,notes,figures>\\. {
595     char c = YYText ()[1];
596
597     switch (c) {
598     case '>':
599         return E_ANGLE_CLOSE;
600     case '<':
601         return E_ANGLE_OPEN;
602     case '!':
603         return E_EXCLAMATION;
604     case '(':
605         return E_OPEN;
606     case ')':
607         return E_CLOSE;
608     case '[':
609         return E_BRACKET_OPEN;
610     case '+':
611         return E_PLUS;
612     case ']':
613         return E_BRACKET_CLOSE;
614     case '~':
615         return E_TILDE;
616     case '\\':
617         return E_BACKSLASH;
618
619     default:
620         return E_CHAR;
621     }
622 }
623
624 <*>.            {
625         string msg = _f ("invalid character: `%c'", YYText ()[0]);
626         LexerError (msg.c_str ());
627         return YYText ()[0];
628 }
629
630 %%
631
632 void
633 Lily_lexer::push_chord_state (SCM tab)
634 {
635         pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
636         yy_push_state (chords);
637 }
638
639 void
640 Lily_lexer::push_figuredbass_state ()
641 {
642         yy_push_state (figures);
643 }
644
645 void
646 Lily_lexer::push_initial_state ()
647 {
648         yy_push_state (INITIAL);
649 }
650
651 void
652 Lily_lexer::push_lyric_state ()
653 {
654         yy_push_state (lyrics);
655 }
656
657 void
658 Lily_lexer::push_markup_state ()
659 {
660         yy_push_state (markup);
661 }
662
663 void
664 Lily_lexer::push_note_state (SCM tab)
665 {
666         pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
667         yy_push_state (notes);
668 }
669
670 void
671 Lily_lexer::pop_state ()
672 {
673         if (YYSTATE == notes || YYSTATE == chords)
674                 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
675         yy_pop_state ();
676 }
677
678 int
679 Lily_lexer::identifier_type (SCM sid)
680 {
681         int k = try_special_identifiers (&yylval.scm , sid);
682         return k >= 0  ? k : SCM_IDENTIFIER;
683 }
684
685
686 int
687 Lily_lexer::scan_escaped_word (string str)
688 {
689         // use more SCM for this.
690
691 //      SCM sym = ly_symbol2scm (str.c_str ());
692
693         int i = lookup_keyword (str);
694         if (i == MARKUP && is_lyric_state ())
695                 return LYRIC_MARKUP;
696         if (i != -1)
697                 return i;
698
699         SCM sid = lookup_identifier (str);
700         if (is_music_function (sid))
701         {
702                 yylval.scm = get_music_function_transform (sid);
703                 return music_function_type (yylval.scm);
704         }
705
706         if (sid != SCM_UNDEFINED)
707         {
708                 yylval.scm = sid;
709                 return identifier_type (sid);
710         }
711
712         string msg (_f ("unknown escaped string: `\\%s'", str));        
713         LexerError (msg.c_str ());
714
715         yylval.scm = scm_makfrom0str (str.c_str ());
716
717         return STRING;
718 }
719
720 int
721 Lily_lexer::scan_bare_word (string str)
722 {
723         SCM sym = ly_symbol2scm (str.c_str ());
724         if ((YYSTATE == notes) || (YYSTATE == chords)) {
725                 SCM handle = SCM_BOOL_F;
726                 if (scm_is_pair (pitchname_tab_stack_))
727                         handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
728                 
729                 if (scm_is_pair (handle)) {
730                         yylval.scm = scm_cdr (handle);
731                         if (unsmob_pitch (yylval.scm)) 
732                             return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
733                         else if (scm_is_symbol (yylval.scm))
734                             return DRUM_PITCH;
735                 }
736                 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
737                 {
738                     yylval.scm = scm_cdr (handle);
739                     return CHORD_MODIFIER;
740                 }
741         }
742
743         yylval.scm = scm_makfrom0str (str.c_str ());
744         return STRING;
745 }
746
747 bool
748 Lily_lexer::is_note_state () const
749 {
750         return YY_START == notes;
751 }
752
753 bool
754 Lily_lexer::is_chord_state () const
755 {
756         return YY_START == chords;
757 }
758
759 bool
760 Lily_lexer::is_lyric_state () const
761 {
762         return YY_START == lyrics;
763 }
764
765 bool
766 Lily_lexer::is_figure_state () const
767 {
768         return YY_START == figures;
769 }
770
771 /*
772  urg, belong to string (_convert)
773  and should be generalised 
774  */
775 void
776 strip_leading_white (string&s)
777 {
778         ssize i = 0;
779         for (;  i < s.length (); i++)
780                 if (!isspace (s[i]))
781                         break;
782
783         s = s.substr (i);
784 }
785
786 void
787 strip_trailing_white (string&s)
788 {
789         ssize i = s.length ();  
790         while (i--) 
791                 if (!isspace (s[i]))
792                         break;
793
794         s = s.substr (0, i + 1);
795 }
796
797
798
799 Lilypond_version oldest_version ("2.7.38");
800
801
802 bool
803 is_valid_version (string s)
804 {
805   Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
806   Lilypond_version ver (s);
807   if (! ((ver >= oldest_version) && (ver <= current)))
808         {       
809                 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
810                 non_fatal_error (_ ("Consider updating the input with the convert-ly script")); 
811                 return false;
812     }
813   return true;
814 }
815         
816
817 /*
818   substitute _ and \,
819 */
820 string
821 lyric_fudge (string s)
822 {
823   char *chars = string_copy (s);
824
825   for (char *p = chars; *p ; p++)
826     {
827       if (*p == '_' && (p == chars || *(p-1) != '\\'))
828         *p = ' ';
829     }
830   
831   s = string (chars);
832   delete[] chars;
833
834   ssize i = 0;  
835   if ((i = s.find ("\\,")) != NPOS)   // change "\," to TeX's "\c "
836     {
837       * (((char*)s.c_str ()) + i + 1) = 'c';
838       s = s.substr (0, i + 2) + " " + s.substr (i - 2);
839     }
840
841   return s;
842 }
843
844 /*
845 Convert "NUM/DEN" into a '(NUM . DEN) cons.
846 */
847 SCM
848 scan_fraction (string frac)
849 {
850         ssize i = frac.find ('/');
851         string left = frac.substr (0, i);
852         string right = frac.substr (i + 1, (frac.length () - i + 1));
853
854         int n = String_convert::dec2int (left);
855         int d = String_convert::dec2int (right);
856         return scm_cons (scm_from_int (n), scm_from_int (d));
857 }
858
859 SCM
860 lookup_markup_command (string s)
861 {
862         SCM proc = ly_lily_module_constant ("lookup-markup-command");
863         return scm_call_1 (proc, scm_makfrom0str (s.c_str ()));
864 }
865
866 struct Parser_signature
867 {
868         char *symbol;
869         int token_type;
870 };
871 static SCM signature_hash_table;
872
873 static void init_signature_hash_table ()
874 {
875         signature_hash_table = scm_gc_protect_object (scm_c_make_hash_table (31));
876         Parser_signature sigs[]  = {
877                 {"scm", MUSIC_FUNCTION_SCM},
878                 {"music", MUSIC_FUNCTION_MUSIC},
879                 {"scm-music", MUSIC_FUNCTION_SCM_MUSIC},
880                 {"scm-scm", MUSIC_FUNCTION_SCM_SCM},
881                 {"music-music", MUSIC_FUNCTION_MUSIC_MUSIC},
882                 {"scm-music-music", MUSIC_FUNCTION_SCM_MUSIC_MUSIC},
883                 {"scm-scm-music-music", MUSIC_FUNCTION_SCM_SCM_MUSIC_MUSIC},
884                 {"scm-scm-music", MUSIC_FUNCTION_SCM_SCM_MUSIC},
885                 {"scm-scm-scm-music", MUSIC_FUNCTION_SCM_SCM_SCM_SCM_MUSIC},
886                 {"scm-scm-scm-scm-music", MUSIC_FUNCTION_SCM_SCM_SCM_MUSIC},
887                 {"scm-scm-scm", MUSIC_FUNCTION_SCM_SCM_SCM},
888                 {"markup", MUSIC_FUNCTION_MARKUP},
889                 {"markup-music", MUSIC_FUNCTION_MARKUP_MUSIC},
890                 {"markup-markup", MUSIC_FUNCTION_MARKUP_MARKUP},
891                 {"markup-music-music", MUSIC_FUNCTION_MARKUP_MUSIC_MUSIC},
892                 {"markup-markup-music", MUSIC_FUNCTION_MARKUP_MARKUP_MUSIC},
893                 {"noarg", MUSIC_FUNCTION},
894                 {0,0}
895         };
896
897         for (int i = 0; sigs[i].symbol; i++)
898                 scm_hashq_set_x (signature_hash_table, scm_gc_protect_object (ly_symbol2scm (sigs[i].symbol)),
899                                  scm_from_int (sigs[i].token_type));
900 }
901
902 int
903 music_function_type (SCM func)
904 {
905         if (!signature_hash_table)
906                 init_signature_hash_table ();
907
908         SCM type = scm_object_property (func, ly_symbol2scm ("music-function-signature-keyword"));
909         SCM token_type = scm_hashq_ref (signature_hash_table, type, SCM_BOOL_F);
910         if (!scm_is_number (token_type))
911                 {
912                 programming_error (_ ("can't find signature for music function"));
913                 return MUSIC_FUNCTION_SCM;
914                 }
915         
916         return scm_to_int (token_type);
917 }
918
919 /* Shut up lexer warnings.  */
920 #if YY_STACK_USED
921
922 static void
923 yy_push_state (int)
924 {
925 }
926
927 static void
928 yy_pop_state ()
929 {
930 }
931
932 static int
933 yy_top_state ()
934 {
935   return 0;
936 }
937
938 static void
939 silence_lexer_warnings ()
940 {
941    (void) yy_start_stack_ptr;
942    (void) yy_start_stack_depth;
943    (void) yy_start_stack;
944    (void) yy_push_state;
945    (void) yy_pop_state;
946    (void) yy_top_state;
947    (void) silence_lexer_warnings;
948 }
949 #endif