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