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