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