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