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