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