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