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