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