]> git.donarmstrong.com Git - lilypond.git/blob - lily/lexer.ll
Merge http://git.sv.gnu.org/r/lilypond
[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 ("scheme0"))
555                                 return MARKUP_LIST_HEAD_SCM0;
556                         else if (tag == ly_symbol2scm ("markup-list0"))
557                                 return MARKUP_LIST_HEAD_LIST0;
558                         else if (tag == ly_symbol2scm ("scheme0-markup-list1"))
559                                 return MARKUP_LIST_HEAD_SCM0_LIST1;
560                         else if (tag == ly_symbol2scm ("scheme0-scheme1-markup-list2"))
561                                 return MARKUP_LIST_HEAD_SCM0_SCM1_LIST2;
562                         else {
563                                 programming_error ("no parser tag defined for this markup list signature"); 
564                                 ly_display_scm (s);
565                                 assert(false);
566                         }
567                 } else
568                         return scan_escaped_word (str);
569         }
570         [{}]    {
571                 return YYText ()[0];
572         }
573         [^#{}"\\ \t\n\r\f]+ {
574                 string s (YYText ()); 
575
576                 char c = s[s.length () - 1];
577                 /* brace open is for not confusing dumb tools.  */
578                 if (c == '{' ||  c == '}')
579                         here_input ().warning (
580                                 _ ("Brace found at end of markup.  Did you forget a space?"));
581                 yylval.scm = ly_string2scm (s);
582
583
584                 return STRING;
585         }
586         .  {
587                 return YYText()[0];
588         }
589 }
590
591 <*><<EOF>> {
592         if (is_main_input_)
593         {
594                 /* 2 = init.ly + current file.
595                    > because we're before closing, but is_main_input_ should
596                    reflect after.
597                 */ 
598                 is_main_input_ = include_stack_.size () > 2;
599                 if (!close_input ())
600                 /* Returns YY_NULL */
601                         yyterminate ();
602         }
603         else if (!close_input ())
604                 /* Returns YY_NULL */
605                 yyterminate ();
606 }
607
608 <INITIAL>{
609         {DASHED_WORD}   {
610                 return scan_bare_word (YYText ());
611         }
612         {DASHED_KEY_WORD}       {
613                 return scan_escaped_word (YYText () + 1);
614         }
615 }
616
617 {WORD}  {
618         return scan_bare_word (YYText ());
619 }
620 {KEYWORD}       {
621         return scan_escaped_word (YYText () + 1);
622 }
623 {REAL}          {
624         Real r;
625         int cnv = sscanf (YYText (), "%lf", &r);
626         assert (cnv == 1);
627         (void) cnv;
628
629         yylval.scm = scm_from_double (r);
630         return REAL;
631 }
632
633 {UNSIGNED}      {
634         yylval.i = String_convert::dec2int (string (YYText ()));
635         return UNSIGNED;
636 }
637
638
639 [{}]    {
640
641         return YYText ()[0];
642 }
643 [*:=]           {
644         char c = YYText ()[0];
645
646         return c;
647 }
648
649 <INITIAL,notes,figures>.        {
650         return YYText ()[0];
651 }
652
653 <INITIAL,lyrics,notes,figures>\\. {
654     char c = YYText ()[1];
655
656     switch (c) {
657     case '>':
658         return E_ANGLE_CLOSE;
659     case '<':
660         return E_ANGLE_OPEN;
661     case '!':
662         return E_EXCLAMATION;
663     case '(':
664         return E_OPEN;
665     case ')':
666         return E_CLOSE;
667     case '[':
668         return E_BRACKET_OPEN;
669     case '+':
670         return E_PLUS;
671     case ']':
672         return E_BRACKET_CLOSE;
673     case '~':
674         return E_TILDE;
675     case '\\':
676         return E_BACKSLASH;
677
678     default:
679         return E_CHAR;
680     }
681 }
682
683 <*>.            {
684         string msg = _f ("invalid character: `%c'", YYText ()[0]);
685         LexerError (msg.c_str ());
686         return YYText ()[0];
687 }
688
689 %%
690
691 /* Make the lexer generate a token of the given type as the next token. 
692  TODO: make it possible to define a value for the token as well */
693 void
694 Lily_lexer::push_extra_token (int token_type)
695 {
696         if (extra_token_types_.empty ())
697         {
698                 if (YY_START != extratoken)
699                         hidden_state_ = YY_START;
700                 yy_push_state (extratoken);
701         }
702         extra_token_types_.push_back (token_type);
703 }
704
705 void
706 Lily_lexer::push_chord_state (SCM tab)
707 {
708         pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
709         yy_push_state (chords);
710 }
711
712 void
713 Lily_lexer::push_figuredbass_state ()
714 {
715         yy_push_state (figures);
716 }
717
718 void
719 Lily_lexer::push_initial_state ()
720 {
721         yy_push_state (INITIAL);
722 }
723
724 void
725 Lily_lexer::push_lyric_state ()
726 {
727         yy_push_state (lyrics);
728 }
729
730 void
731 Lily_lexer::push_markup_state ()
732 {
733         yy_push_state (markup);
734 }
735
736 void
737 Lily_lexer::push_note_state (SCM tab)
738 {
739         pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
740         yy_push_state (notes);
741 }
742
743 void
744 Lily_lexer::pop_state ()
745 {
746         if (YYSTATE == notes || YYSTATE == chords)
747                 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
748
749         yy_pop_state ();
750 }
751
752 int
753 Lily_lexer::identifier_type (SCM sid)
754 {
755         int k = try_special_identifiers (&yylval.scm , sid);
756         return k >= 0  ? k : SCM_IDENTIFIER;
757 }
758
759
760 int
761 Lily_lexer::scan_escaped_word (string str)
762 {
763         // use more SCM for this.
764
765 //      SCM sym = ly_symbol2scm (str.c_str ());
766
767         int i = lookup_keyword (str);
768         if (i == MARKUP && is_lyric_state ())
769                 return LYRIC_MARKUP;
770         if (i != -1)
771                 return i;
772
773         SCM sid = lookup_identifier (str);
774         if (is_music_function (sid))
775         {
776                 yylval.scm = get_music_function_transform (sid);
777
778                 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
779                 push_extra_token (EXPECT_NO_MORE_ARGS);
780                 for (; scm_is_pair (s); s = scm_cdr (s))
781                 {
782                         if (scm_car (s) == ly_music_p_proc)
783                                 push_extra_token (EXPECT_MUSIC);
784                         else if (scm_car (s) == ly_lily_module_constant ("markup?"))
785                                 push_extra_token (EXPECT_MARKUP);
786                         else if (ly_is_procedure (scm_car (s)))
787                                 push_extra_token (EXPECT_SCM);
788                         else programming_error ("Function parameter without type-checking predicate");
789                 }
790                 return MUSIC_FUNCTION;
791         }
792
793         if (sid != SCM_UNDEFINED)
794         {
795                 yylval.scm = sid;
796                 return identifier_type (sid);
797         }
798
799         string msg (_f ("unknown escaped string: `\\%s'", str));        
800         LexerError (msg.c_str ());
801
802         yylval.scm = ly_string2scm (str);
803
804         return STRING;
805 }
806
807 int
808 Lily_lexer::scan_bare_word (string str)
809 {
810         SCM sym = ly_symbol2scm (str.c_str ());
811         if ((YYSTATE == notes) || (YYSTATE == chords)) {
812                 SCM handle = SCM_BOOL_F;
813                 if (scm_is_pair (pitchname_tab_stack_))
814                         handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
815                 
816                 if (scm_is_pair (handle)) {
817                         yylval.scm = scm_cdr (handle);
818                         if (unsmob_pitch (yylval.scm)) 
819                             return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
820                         else if (scm_is_symbol (yylval.scm))
821                             return DRUM_PITCH;
822                 }
823                 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
824                 {
825                     yylval.scm = scm_cdr (handle);
826                     return CHORD_MODIFIER;
827                 }
828         }
829
830         yylval.scm = ly_string2scm (str);
831         return STRING;
832 }
833
834 int
835 Lily_lexer::get_state () const
836 {
837         if (YY_START == extratoken)
838                 return hidden_state_;
839         else
840                 return YY_START;
841 }
842
843 bool
844 Lily_lexer::is_note_state () const
845 {
846         return get_state () == notes;
847 }
848
849 bool
850 Lily_lexer::is_chord_state () const
851 {
852         return get_state () == chords;
853 }
854
855 bool
856 Lily_lexer::is_lyric_state () const
857 {
858         return get_state () == lyrics;
859 }
860
861 bool
862 Lily_lexer::is_figure_state () const
863 {
864         return get_state () == figures;
865 }
866
867 /*
868  urg, belong to string (_convert)
869  and should be generalised 
870  */
871 void
872 strip_leading_white (string&s)
873 {
874         ssize i = 0;
875         for (;  i < s.length (); i++)
876                 if (!isspace (s[i]))
877                         break;
878
879         s = s.substr (i);
880 }
881
882 void
883 strip_trailing_white (string&s)
884 {
885         ssize i = s.length ();  
886         while (i--) 
887                 if (!isspace (s[i]))
888                         break;
889
890         s = s.substr (0, i + 1);
891 }
892
893
894
895 Lilypond_version oldest_version ("2.7.38");
896
897
898 bool
899 is_valid_version (string s)
900 {
901   Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
902   Lilypond_version ver (s);
903   if (int (ver) < oldest_version)
904         {       
905                 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
906                 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
907                 return false;
908         }
909
910   if (ver > current)
911         {
912                 non_fatal_error (_f ("program too old: %s (file requires: %s)",  current.to_string (), ver.to_string ()));
913                 return false;
914         }
915   return true;
916 }
917         
918
919 /*
920   substitute _ and \,
921 */
922 string
923 lyric_fudge (string s)
924 {
925   char *chars = string_copy (s);
926
927   for (char *p = chars; *p ; p++)
928     {
929       if (*p == '_' && (p == chars || *(p-1) != '\\'))
930         *p = ' ';
931     }
932   
933   s = string (chars);
934   delete[] chars;
935
936   ssize i = 0;  
937   if ((i = s.find ("\\,")) != NPOS)   // change "\," to TeX's "\c "
938     {
939       * (((char*)s.c_str ()) + i + 1) = 'c';
940       s = s.substr (0, i + 2) + " " + s.substr (i - 2);
941     }
942
943   return s;
944 }
945
946 /*
947 Convert "NUM/DEN" into a '(NUM . DEN) cons.
948 */
949 SCM
950 scan_fraction (string frac)
951 {
952         ssize i = frac.find ('/');
953         string left = frac.substr (0, i);
954         string right = frac.substr (i + 1, (frac.length () - i + 1));
955
956         int n = String_convert::dec2int (left);
957         int d = String_convert::dec2int (right);
958         return scm_cons (scm_from_int (n), scm_from_int (d));
959 }
960
961 SCM
962 lookup_markup_command (string s)
963 {
964         SCM proc = ly_lily_module_constant ("lookup-markup-command");
965         return scm_call_1 (proc, ly_string2scm (s));
966 }
967
968 SCM
969 lookup_markup_list_command (string s)
970 {
971         SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
972         return scm_call_1 (proc, ly_string2scm (s));
973 }
974
975 /* Shut up lexer warnings.  */
976 #if YY_STACK_USED
977
978 static void
979 yy_push_state (int)
980 {
981 }
982
983 static void
984 yy_pop_state ()
985 {
986 }
987
988 static int
989 yy_top_state ()
990 {
991   return 0;
992 }
993
994 static void
995 silence_lexer_warnings ()
996 {
997    (void) yy_start_stack_ptr;
998    (void) yy_start_stack_depth;
999    (void) yy_start_stack;
1000    (void) yy_push_state;
1001    (void) yy_pop_state;
1002    (void) yy_top_state;
1003    (void) silence_lexer_warnings;
1004 }
1005 #endif