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