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