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