]> git.donarmstrong.com Git - lilypond.git/blob - lily/lexer.ll
59284d07c39be8b50715bb54f85a847c5fb58e0e
[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
387 <INITIAL,chords,figures,lyrics,markup,notes>\$  { //immediate scm
388         int n = 0;
389         Input hi = here_input();
390         hi.step_forward ();
391         SCM sval = ly_parse_scm (hi.start (), &n, hi,
392                 be_safe_global && is_main_input_, parser_);
393
394         for (int i = 0; i < n; i++)
395         {
396                 yyinput ();
397         }
398         char_count_stack_.back () += n;
399
400         if (sval == SCM_UNDEFINED)
401         {
402                 yylval.scm = SCM_UNSPECIFIED;
403                 error_level_ = 1;
404
405                 LexerError (_ ("bad Scheme expression").c_str ());
406                 return SCM_IDENTIFIER;
407         }
408
409         return scan_scm_id (sval);
410 }
411
412 <INITIAL,notes,lyrics>{ 
413         \<\<    {
414                 return DOUBLE_ANGLE_OPEN;
415         }
416         \>\>    {
417                 return DOUBLE_ANGLE_CLOSE;
418         }
419 }
420
421 <INITIAL,notes>{
422         \<      {
423                 return ANGLE_OPEN;
424         }
425         \>      {
426                 return ANGLE_CLOSE;
427         }
428 }
429
430 <figures>{
431         _       {
432                 return FIGURE_SPACE;
433         }
434         \>              {
435                 return FIGURE_CLOSE;
436         }
437         \<      {
438                 return FIGURE_OPEN;
439         }
440 }
441
442 <notes,figures>{
443         {ALPHAWORD}     {
444                 return scan_bare_word (YYText ());
445         }
446
447         {NOTECOMMAND}   {
448                 return scan_escaped_word (YYText () + 1); 
449         }
450         {FRACTION}      {
451                 yylval.scm =  scan_fraction (YYText ());
452                 return FRACTION;
453         }
454         {UNSIGNED}/\/   | // backup rule
455         {UNSIGNED}              {
456                 yylval.scm = scm_c_read_string (YYText ());
457                 return UNSIGNED;
458         }
459         {E_UNSIGNED}    {
460                 yylval.i = String_convert::dec2int (string (YYText () +1));
461                 return E_UNSIGNED;
462         }
463 }
464
465 <quote,lyric_quote>{
466         \\{ESCAPED}     {
467                 *yylval.string += to_string (escaped_char (YYText ()[1]));
468         }
469         [^\\""]+        {
470                 *yylval.string += YYText ();
471         }
472         \"      {
473
474                 yy_pop_state ();
475
476                 /* yylval is union. Must remember STRING before setting SCM*/
477                 string *sp = yylval.string;
478                 yylval.scm = ly_string2scm (*sp);
479                 delete sp;
480                 return is_lyric_state () ? LYRICS_STRING : STRING;
481         }
482         .       {
483                 *yylval.string += YYText ();
484         }
485 }
486
487 <lyrics>{
488         \" {
489                 start_lyric_quote ();
490         }
491         {FRACTION}      {
492                 yylval.scm =  scan_fraction (YYText ());
493                 return FRACTION;
494         }
495         {UNSIGNED}/\/[^0-9] { // backup rule
496                 yylval.scm = scm_c_read_string (YYText ());
497                 return UNSIGNED;
498         }
499         {UNSIGNED}/\/   | // backup rule
500         {UNSIGNED}              {
501                 yylval.scm = scm_c_read_string (YYText ());
502                 return UNSIGNED;
503         }
504         {NOTECOMMAND}   {
505                 return scan_escaped_word (YYText () + 1);
506         }
507         {LYRICS} {
508                 /* ugr. This sux. */
509                 string s (YYText ()); 
510                 if (s == "__")
511                         return yylval.i = EXTENDER;
512                 if (s == "--")
513                         return yylval.i = HYPHEN;
514                 s = lyric_fudge (s);
515
516                 char c = s[s.length () - 1];
517                 if (c == '{' ||  c == '}') // brace open is for not confusing dumb tools.
518                         here_input ().warning (
519                                 _ ("Brace found at end of lyric.  Did you forget a space?"));
520                 yylval.scm = ly_string2scm (s);
521
522
523                 return LYRICS_STRING;
524         }
525         . {
526                 return YYText ()[0];
527         }
528 }
529 <chords>{
530         {ALPHAWORD}     {
531                 return scan_bare_word (YYText ());
532         }
533         {NOTECOMMAND}   {
534                 return scan_escaped_word (YYText () + 1);
535         }
536         {FRACTION}      {
537                 yylval.scm =  scan_fraction (YYText ());
538                 return FRACTION;
539         }
540         {UNSIGNED}/\/[^0-9] { // backup rule
541                 yylval.scm = scm_c_read_string (YYText ());
542                 return UNSIGNED;
543         }
544         {UNSIGNED}/\/   | // backup rule
545         {UNSIGNED}              {
546                 yylval.scm = scm_c_read_string (YYText ());
547                 return UNSIGNED;
548         }
549         -  {
550                 return CHORD_MINUS;
551         }
552         :  {
553                 return CHORD_COLON;
554         }
555         \/\+ {
556                 return CHORD_BASS;
557         }
558         \/  {
559                 return CHORD_SLASH;
560         }
561         \^  {
562                 return CHORD_CARET;
563         }
564         . {
565                 return YYText ()[0];
566         }
567 }
568
569
570 <markup>{
571         \\score {
572                 return SCORE;
573         }
574         {MARKUPCOMMAND} {
575                 string str (YYText () + 1);
576
577                 int token_type = MARKUP_FUNCTION;
578                 SCM s = lookup_markup_command (str);
579
580                 // lookup-markup-command returns a pair with the car
581                 // being the function to call, and the cdr being the
582                 // call signature specified to define-markup-command,
583                 // a list of predicates.
584
585                 if (!scm_is_pair (s)) {
586                   // If lookup-markup-command was not successful, we
587                   // try lookup-markup-list-command instead.
588                   // If this fails as well, we just scan and return
589                   // the escaped word.
590                   s = lookup_markup_list_command (str);
591                   if (scm_is_pair (s))
592                     token_type = MARKUP_LIST_FUNCTION;
593                   else
594                     return scan_escaped_word (str);
595                 }
596
597                 // If the list of predicates is, say,
598                 // (number? number? markup?), then tokens
599                 // EXPECT_MARKUP EXPECT_SCM EXPECT_SCM EXPECT_NO_MORE_ARGS
600                 // will be generated.  Note that we have to push them
601                 // in reverse order, so the first token pushed in the
602                 // loop will be EXPECT_NO_MORE_ARGS.
603
604                 yylval.scm = scm_car(s);
605
606                 // yylval now contains the function to call as token
607                 // value (for token type MARKUP_FUNCTION or
608                 // MARKUP_LIST_FUNCTION).
609
610                 push_extra_token(EXPECT_NO_MORE_ARGS);
611                 s = scm_cdr(s);
612                 for (; scm_is_pair(s); s = scm_cdr(s)) {
613                   SCM predicate = scm_car(s);
614
615                   if (predicate == ly_lily_module_constant ("markup-list?"))
616                     push_extra_token(EXPECT_MARKUP_LIST);
617                   else if (predicate == ly_lily_module_constant ("markup?"))
618                     push_extra_token(EXPECT_MARKUP);
619                   else
620                     push_extra_token(EXPECT_SCM, predicate);
621                 }
622                 return token_type;
623         }
624         [{}]    {
625                 return YYText ()[0];
626         }
627         [^$#{}\"\\ \t\n\r\f]+ {
628                 string s (YYText ()); 
629
630                 char c = s[s.length () - 1];
631                 /* brace open is for not confusing dumb tools.  */
632                 if (c == '{' ||  c == '}')
633                         here_input ().warning (
634                                 _ ("Brace found at end of markup.  Did you forget a space?"));
635                 yylval.scm = ly_string2scm (s);
636
637
638                 return STRING;
639         }
640         .  {
641                 return YYText()[0];
642         }
643 }
644
645 <longcomment><<EOF>> {
646                 LexerError (_ ("EOF found inside a comment").c_str ());
647                 is_main_input_ = false; // should be safe , can't have \include in --safe.
648                 if (!close_input ())
649                   yyterminate (); // can't move this, since it actually rets a YY_NULL
650         }
651
652 <<EOF>> { if (is_main_input_)
653         {
654                 /* 2 = init.ly + current file.
655                    > because we're before closing, but is_main_input_ should
656                    reflect after.
657                 */ 
658                 is_main_input_ = include_stack_.size () > 2;
659                 if (!close_input ())
660                 /* Returns YY_NULL */
661                         yyterminate ();
662         }
663         else if (!close_input ())
664                 /* Returns YY_NULL */
665                 yyterminate ();
666 }
667
668 <INITIAL>{
669         {DASHED_WORD}   {
670                 return scan_bare_word (YYText ());
671         }
672         {DASHED_KEY_WORD}       {
673                 return scan_escaped_word (YYText () + 1);
674         }
675 }
676
677 -{UNSIGNED}     | // backup rule
678 {REAL}          {
679         yylval.scm = scm_c_read_string (YYText ());
680         return REAL;
681 }
682 -\.     { // backup rule
683         yylval.scm = scm_from_double (0.0);
684         return REAL;
685 }
686
687 {UNSIGNED}      {
688         yylval.scm = scm_c_read_string (YYText ());
689         return UNSIGNED;
690 }
691
692
693 [{}]    {
694
695         return YYText ()[0];
696 }
697 [*:=]           {
698         char c = YYText ()[0];
699
700         return c;
701 }
702
703 <INITIAL,notes,figures>.        {
704         return YYText ()[0];
705 }
706
707 <INITIAL,lyrics,notes,figures>\\. {
708     char c = YYText ()[1];
709
710     switch (c) {
711     case '>':
712         return E_ANGLE_CLOSE;
713     case '<':
714         return E_ANGLE_OPEN;
715     case '!':
716         return E_EXCLAMATION;
717     case '(':
718         return E_OPEN;
719     case ')':
720         return E_CLOSE;
721     case '[':
722         return E_BRACKET_OPEN;
723     case '+':
724         return E_PLUS;
725     case ']':
726         return E_BRACKET_CLOSE;
727     case '~':
728         return E_TILDE;
729     case '\\':
730         return E_BACKSLASH;
731
732     default:
733         return E_CHAR;
734     }
735 }
736
737 <*>.            {
738         string msg = _f ("invalid character: `%c'", YYText ()[0]);
739         LexerError (msg.c_str ());
740         return YYText ()[0];
741 }
742
743 %%
744
745 /* Make the lexer generate a token of the given type as the next token. 
746  TODO: make it possible to define a value for the token as well */
747 void
748 Lily_lexer::push_extra_token (int token_type, SCM scm)
749 {
750         if (scm_is_null (extra_tokens_))
751         {
752                 if (YY_START != extratoken)
753                         hidden_state_ = YY_START;
754                 yy_push_state (extratoken);
755         }
756         extra_tokens_ = scm_acons (scm_from_int (token_type), scm, extra_tokens_);
757 }
758
759 void
760 Lily_lexer::push_chord_state (SCM tab)
761 {
762         pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
763         yy_push_state (chords);
764 }
765
766 void
767 Lily_lexer::push_figuredbass_state ()
768 {
769         yy_push_state (figures);
770 }
771
772 void
773 Lily_lexer::push_initial_state ()
774 {
775         yy_push_state (INITIAL);
776 }
777
778 void
779 Lily_lexer::push_lyric_state ()
780 {
781         yy_push_state (lyrics);
782 }
783
784 void
785 Lily_lexer::push_markup_state ()
786 {
787         yy_push_state (markup);
788 }
789
790 void
791 Lily_lexer::push_note_state (SCM tab)
792 {
793         pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
794         yy_push_state (notes);
795 }
796
797 void
798 Lily_lexer::pop_state ()
799 {
800         if (YYSTATE == notes || YYSTATE == chords)
801                 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
802
803         yy_pop_state ();
804 }
805
806 int
807 Lily_lexer::identifier_type (SCM sid)
808 {
809         int k = try_special_identifiers (&yylval.scm , sid);
810         return k >= 0  ? k : SCM_IDENTIFIER;
811 }
812
813
814 int
815 Lily_lexer::scan_escaped_word (string str)
816 {
817         // use more SCM for this.
818
819 //      SCM sym = ly_symbol2scm (str.c_str ());
820
821         int i = lookup_keyword (str);
822         if (i == MARKUP && is_lyric_state ())
823                 return LYRIC_MARKUP;
824         if (i != -1)
825                 return i;
826
827         SCM sid = lookup_identifier (str);
828         if (sid != SCM_UNDEFINED)
829                 return scan_scm_id (sid);
830
831         string msg (_f ("unknown escaped string: `\\%s'", str));        
832         LexerError (msg.c_str ());
833
834         yylval.scm = ly_string2scm (str);
835
836         return STRING;
837 }
838
839 int
840 Lily_lexer::scan_scm_id (SCM sid)
841 {
842         if (is_music_function (sid))
843         {
844                 int funtype = SCM_FUNCTION;
845
846                 yylval.scm = get_music_function_transform (sid);
847
848                 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
849                 SCM cs = scm_car (s);
850
851                 if (scm_is_pair (cs))
852                 {
853                         cs = SCM_CAR (cs);
854                 }
855
856                 if (scm_is_eq (cs, ly_lily_module_constant ("ly:music?")))
857                         funtype = MUSIC_FUNCTION;
858                 else if (scm_is_eq (cs, ly_lily_module_constant ("ly:event?")))
859                         funtype = EVENT_FUNCTION;
860                 else if (ly_is_procedure (cs))
861                         funtype = SCM_FUNCTION;
862                 else programming_error ("Bad syntax function predicate");
863
864                 push_extra_token (EXPECT_NO_MORE_ARGS);
865                 for (s = scm_cdr (s); scm_is_pair (s); s = scm_cdr (s))
866                 {
867                         SCM optional = SCM_UNDEFINED;
868                         cs = scm_car (s);
869
870                         if (scm_is_pair (cs))
871                         {
872                                 optional = SCM_CDR (cs);
873                                 cs = SCM_CAR (cs);
874                         }
875                         
876                         if (cs == Pitch_type_p_proc)
877                                 push_extra_token (EXPECT_PITCH);
878                         else if (cs == Duration_type_p_proc)
879                                 push_extra_token (EXPECT_DURATION);
880                         else if (ly_is_procedure (cs))
881                                 push_extra_token (EXPECT_SCM, cs);
882                         else
883                         {
884                                 programming_error ("Function parameter without type-checking predicate");
885                                 continue;
886                         }
887                         if (!scm_is_eq (optional, SCM_UNDEFINED))
888                                 push_extra_token (EXPECT_OPTIONAL, optional);
889                 }
890                 return funtype;
891         }
892         yylval.scm = sid;
893         return identifier_type (sid);
894 }
895
896 int
897 Lily_lexer::scan_bare_word (string str)
898 {
899         SCM sym = ly_symbol2scm (str.c_str ());
900         if ((YYSTATE == notes) || (YYSTATE == chords)) {
901                 SCM handle = SCM_BOOL_F;
902                 if (scm_is_pair (pitchname_tab_stack_))
903                         handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
904                 
905                 if (scm_is_pair (handle)) {
906                         yylval.scm = scm_cdr (handle);
907                         if (unsmob_pitch (yylval.scm)) 
908                             return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
909                         else if (scm_is_symbol (yylval.scm))
910                             return DRUM_PITCH;
911                 }
912                 else if ((YYSTATE == chords)
913                         && (handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
914                 {
915                     yylval.scm = scm_cdr (handle);
916                     return CHORD_MODIFIER;
917                 }
918                 if ((chord_repetition_.repetition_symbol_ != SCM_EOL)
919                     && to_boolean (scm_equal_p (chord_repetition_.repetition_symbol_, sym)))
920                         return CHORD_REPETITION;
921         }
922
923         yylval.scm = ly_string2scm (str);
924         return STRING;
925 }
926
927 int
928 Lily_lexer::get_state () const
929 {
930         if (YY_START == extratoken)
931                 return hidden_state_;
932         else
933                 return YY_START;
934 }
935
936 bool
937 Lily_lexer::is_note_state () const
938 {
939         return get_state () == notes;
940 }
941
942 bool
943 Lily_lexer::is_chord_state () const
944 {
945         return get_state () == chords;
946 }
947
948 bool
949 Lily_lexer::is_lyric_state () const
950 {
951         return get_state () == lyrics;
952 }
953
954 bool
955 Lily_lexer::is_figure_state () const
956 {
957         return get_state () == figures;
958 }
959
960 /*
961  urg, belong to string (_convert)
962  and should be generalised 
963  */
964 void
965 strip_leading_white (string&s)
966 {
967         ssize i = 0;
968         for (;  i < s.length (); i++)
969                 if (!isspace (s[i]))
970                         break;
971
972         s = s.substr (i);
973 }
974
975 void
976 strip_trailing_white (string&s)
977 {
978         ssize i = s.length ();  
979         while (i--) 
980                 if (!isspace (s[i]))
981                         break;
982
983         s = s.substr (0, i + 1);
984 }
985
986
987
988 Lilypond_version oldest_version ("2.7.38");
989
990
991 bool
992 is_valid_version (string s)
993 {
994   Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
995   Lilypond_version ver (s);
996   if (int (ver) < oldest_version)
997         {       
998                 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
999                 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
1000                 return false;
1001         }
1002
1003   if (ver > current)
1004         {
1005                 non_fatal_error (_f ("program too old: %s (file requires: %s)",  current.to_string (), ver.to_string ()));
1006                 return false;
1007         }
1008   return true;
1009 }
1010         
1011
1012 /*
1013   substitute _ and \,
1014 */
1015 string
1016 lyric_fudge (string s)
1017 {
1018   char *chars = string_copy (s);
1019
1020   for (char *p = chars; *p ; p++)
1021     {
1022       if (*p == '_' && (p == chars || *(p-1) != '\\'))
1023         *p = ' ';
1024     }
1025   
1026   s = string (chars);
1027   delete[] chars;
1028
1029   ssize i = 0;  
1030   if ((i = s.find ("\\,")) != NPOS)   // change "\," to TeX's "\c "
1031     {
1032       * (((char*)s.c_str ()) + i + 1) = 'c';
1033       s = s.substr (0, i + 2) + " " + s.substr (i - 2);
1034     }
1035
1036   return s;
1037 }
1038
1039 /*
1040 Convert "NUM/DEN" into a '(NUM . DEN) cons.
1041 */
1042 SCM
1043 scan_fraction (string frac)
1044 {
1045         ssize i = frac.find ('/');
1046         string left = frac.substr (0, i);
1047         string right = frac.substr (i + 1, (frac.length () - i + 1));
1048
1049         int n = String_convert::dec2int (left);
1050         int d = String_convert::dec2int (right);
1051         return scm_cons (scm_from_int (n), scm_from_int (d));
1052 }
1053
1054 SCM
1055 lookup_markup_command (string s)
1056 {
1057         SCM proc = ly_lily_module_constant ("lookup-markup-command");
1058         return scm_call_1 (proc, ly_string2scm (s));
1059 }
1060
1061 SCM
1062 lookup_markup_list_command (string s)
1063 {
1064         SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
1065         return scm_call_1 (proc, ly_string2scm (s));
1066 }
1067
1068 /* Shut up lexer warnings.  */
1069 #if YY_STACK_USED
1070
1071 static void
1072 yy_push_state (int)
1073 {
1074 }
1075
1076 static void
1077 yy_pop_state ()
1078 {
1079 }
1080
1081 static int
1082 yy_top_state ()
1083 {
1084   return 0;
1085 }
1086
1087 static void
1088 silence_lexer_warnings ()
1089 {
1090    (void) yy_start_stack_ptr;
1091    (void) yy_start_stack_depth;
1092    (void) yy_start_stack;
1093    (void) yy_push_state;
1094    (void) yy_pop_state;
1095    (void) yy_top_state;
1096    (void) silence_lexer_warnings;
1097 }
1098 #endif