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