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