]> git.donarmstrong.com Git - lilypond.git/blob - lily/lexer.ll
Merge branch 'master' of ssh://kainhofer@git.sv.gnu.org/srv/git/lilypond into dev...
[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-markup1-markup2"))
543                                 return MARKUP_HEAD_SCM0_MARKUP1_MARKUP2;
544                         else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
545                                 return MARKUP_HEAD_SCM0_SCM1_SCM2;
546                         else {
547                                 programming_error ("no parser tag defined for this markup signature"); 
548                                 ly_display_scm (s);
549                                 assert(false);
550                         }
551                 } else if (scm_is_pair (s2) && scm_is_symbol (scm_cdr (s2))) {
552                         yylval.scm = scm_car(s2);
553                         SCM tag = scm_cdr(s2);
554                         if (tag == ly_symbol2scm("empty"))
555                                 return MARKUP_LIST_HEAD_EMPTY;
556                         else if (tag == ly_symbol2scm ("scheme0"))
557                                 return MARKUP_LIST_HEAD_SCM0;
558                         else if (tag == ly_symbol2scm ("markup-list0"))
559                                 return MARKUP_LIST_HEAD_LIST0;
560                         else if (tag == ly_symbol2scm ("scheme0-markup-list1"))
561                                 return MARKUP_LIST_HEAD_SCM0_LIST1;
562                         else if (tag == ly_symbol2scm ("scheme0-scheme1-markup-list2"))
563                                 return MARKUP_LIST_HEAD_SCM0_SCM1_LIST2;
564                         else {
565                                 programming_error ("no parser tag defined for this markup list signature"); 
566                                 ly_display_scm (s);
567                                 assert(false);
568                         }
569                 } else
570                         return scan_escaped_word (str);
571         }
572         [{}]    {
573                 return YYText ()[0];
574         }
575         [^#{}"\\ \t\n\r\f]+ {
576                 string s (YYText ()); 
577
578                 char c = s[s.length () - 1];
579                 /* brace open is for not confusing dumb tools.  */
580                 if (c == '{' ||  c == '}')
581                         here_input ().warning (
582                                 _ ("Brace found at end of markup.  Did you forget a space?"));
583                 yylval.scm = ly_string2scm (s);
584
585
586                 return STRING;
587         }
588         .  {
589                 return YYText()[0];
590         }
591 }
592
593 <*><<EOF>> {
594         if (is_main_input_)
595         {
596                 /* 2 = init.ly + current file.
597                    > because we're before closing, but is_main_input_ should
598                    reflect after.
599                 */ 
600                 is_main_input_ = include_stack_.size () > 2;
601                 if (!close_input ())
602                 /* Returns YY_NULL */
603                         yyterminate ();
604         }
605         else if (!close_input ())
606                 /* Returns YY_NULL */
607                 yyterminate ();
608 }
609
610 <INITIAL>{
611         {DASHED_WORD}   {
612                 return scan_bare_word (YYText ());
613         }
614         {DASHED_KEY_WORD}       {
615                 return scan_escaped_word (YYText () + 1);
616         }
617 }
618
619 {WORD}  {
620         return scan_bare_word (YYText ());
621 }
622 {KEYWORD}       {
623         return scan_escaped_word (YYText () + 1);
624 }
625 {REAL}          {
626         Real r;
627         int cnv = sscanf (YYText (), "%lf", &r);
628         assert (cnv == 1);
629         (void) cnv;
630
631         yylval.scm = scm_from_double (r);
632         return REAL;
633 }
634
635 {UNSIGNED}      {
636         yylval.i = String_convert::dec2int (string (YYText ()));
637         return UNSIGNED;
638 }
639
640
641 [{}]    {
642
643         return YYText ()[0];
644 }
645 [*:=]           {
646         char c = YYText ()[0];
647
648         return c;
649 }
650
651 <INITIAL,notes,figures>.        {
652         return YYText ()[0];
653 }
654
655 <INITIAL,lyrics,notes,figures>\\. {
656     char c = YYText ()[1];
657
658     switch (c) {
659     case '>':
660         return E_ANGLE_CLOSE;
661     case '<':
662         return E_ANGLE_OPEN;
663     case '!':
664         return E_EXCLAMATION;
665     case '(':
666         return E_OPEN;
667     case ')':
668         return E_CLOSE;
669     case '[':
670         return E_BRACKET_OPEN;
671     case '+':
672         return E_PLUS;
673     case ']':
674         return E_BRACKET_CLOSE;
675     case '~':
676         return E_TILDE;
677     case '\\':
678         return E_BACKSLASH;
679
680     default:
681         return E_CHAR;
682     }
683 }
684
685 <*>.            {
686         string msg = _f ("invalid character: `%c'", YYText ()[0]);
687         LexerError (msg.c_str ());
688         return YYText ()[0];
689 }
690
691 %%
692
693 /* Make the lexer generate a token of the given type as the next token. 
694  TODO: make it possible to define a value for the token as well */
695 void
696 Lily_lexer::push_extra_token (int token_type)
697 {
698         if (extra_token_types_.empty ())
699         {
700                 if (YY_START != extratoken)
701                         hidden_state_ = YY_START;
702                 yy_push_state (extratoken);
703         }
704         extra_token_types_.push_back (token_type);
705 }
706
707 void
708 Lily_lexer::push_chord_state (SCM tab)
709 {
710         pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
711         yy_push_state (chords);
712 }
713
714 void
715 Lily_lexer::push_figuredbass_state ()
716 {
717         yy_push_state (figures);
718 }
719
720 void
721 Lily_lexer::push_initial_state ()
722 {
723         yy_push_state (INITIAL);
724 }
725
726 void
727 Lily_lexer::push_lyric_state ()
728 {
729         yy_push_state (lyrics);
730 }
731
732 void
733 Lily_lexer::push_markup_state ()
734 {
735         yy_push_state (markup);
736 }
737
738 void
739 Lily_lexer::push_note_state (SCM tab)
740 {
741         pitchname_tab_stack_ = scm_cons (tab, pitchname_tab_stack_);
742         yy_push_state (notes);
743 }
744
745 void
746 Lily_lexer::pop_state ()
747 {
748         if (YYSTATE == notes || YYSTATE == chords)
749                 pitchname_tab_stack_ = scm_cdr (pitchname_tab_stack_);
750
751         yy_pop_state ();
752 }
753
754 int
755 Lily_lexer::identifier_type (SCM sid)
756 {
757         int k = try_special_identifiers (&yylval.scm , sid);
758         return k >= 0  ? k : SCM_IDENTIFIER;
759 }
760
761
762 int
763 Lily_lexer::scan_escaped_word (string str)
764 {
765         // use more SCM for this.
766
767 //      SCM sym = ly_symbol2scm (str.c_str ());
768
769         int i = lookup_keyword (str);
770         if (i == MARKUP && is_lyric_state ())
771                 return LYRIC_MARKUP;
772         if (i != -1)
773                 return i;
774
775         SCM sid = lookup_identifier (str);
776         if (is_music_function (sid))
777         {
778                 yylval.scm = get_music_function_transform (sid);
779
780                 SCM s = scm_object_property (yylval.scm, ly_symbol2scm ("music-function-signature"));
781                 push_extra_token (EXPECT_NO_MORE_ARGS);
782                 for (; scm_is_pair (s); s = scm_cdr (s))
783                 {
784                         if (scm_car (s) == ly_music_p_proc)
785                                 push_extra_token (EXPECT_MUSIC);
786                         else if (scm_car (s) == ly_lily_module_constant ("markup?"))
787                                 push_extra_token (EXPECT_MARKUP);
788                         else if (ly_is_procedure (scm_car (s)))
789                                 push_extra_token (EXPECT_SCM);
790                         else programming_error ("Function parameter without type-checking predicate");
791                 }
792                 return MUSIC_FUNCTION;
793         }
794
795         if (sid != SCM_UNDEFINED)
796         {
797                 yylval.scm = sid;
798                 return identifier_type (sid);
799         }
800
801         string msg (_f ("unknown escaped string: `\\%s'", str));        
802         LexerError (msg.c_str ());
803
804         yylval.scm = ly_string2scm (str);
805
806         return STRING;
807 }
808
809 int
810 Lily_lexer::scan_bare_word (string str)
811 {
812         SCM sym = ly_symbol2scm (str.c_str ());
813         if ((YYSTATE == notes) || (YYSTATE == chords)) {
814                 SCM handle = SCM_BOOL_F;
815                 if (scm_is_pair (pitchname_tab_stack_))
816                         handle = scm_hashq_get_handle (scm_car (pitchname_tab_stack_), sym);
817                 
818                 if (scm_is_pair (handle)) {
819                         yylval.scm = scm_cdr (handle);
820                         if (unsmob_pitch (yylval.scm)) 
821                             return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
822                         else if (scm_is_symbol (yylval.scm))
823                             return DRUM_PITCH;
824                 }
825                 else if ((handle = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
826                 {
827                     yylval.scm = scm_cdr (handle);
828                     return CHORD_MODIFIER;
829                 }
830         }
831
832         yylval.scm = ly_string2scm (str);
833         return STRING;
834 }
835
836 int
837 Lily_lexer::get_state () const
838 {
839         if (YY_START == extratoken)
840                 return hidden_state_;
841         else
842                 return YY_START;
843 }
844
845 bool
846 Lily_lexer::is_note_state () const
847 {
848         return get_state () == notes;
849 }
850
851 bool
852 Lily_lexer::is_chord_state () const
853 {
854         return get_state () == chords;
855 }
856
857 bool
858 Lily_lexer::is_lyric_state () const
859 {
860         return get_state () == lyrics;
861 }
862
863 bool
864 Lily_lexer::is_figure_state () const
865 {
866         return get_state () == figures;
867 }
868
869 /*
870  urg, belong to string (_convert)
871  and should be generalised 
872  */
873 void
874 strip_leading_white (string&s)
875 {
876         ssize i = 0;
877         for (;  i < s.length (); i++)
878                 if (!isspace (s[i]))
879                         break;
880
881         s = s.substr (i);
882 }
883
884 void
885 strip_trailing_white (string&s)
886 {
887         ssize i = s.length ();  
888         while (i--) 
889                 if (!isspace (s[i]))
890                         break;
891
892         s = s.substr (0, i + 1);
893 }
894
895
896
897 Lilypond_version oldest_version ("2.7.38");
898
899
900 bool
901 is_valid_version (string s)
902 {
903   Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
904   Lilypond_version ver (s);
905   if (int (ver) < oldest_version)
906         {       
907                 non_fatal_error (_f ("file too old: %s (oldest supported: %s)", ver.to_string (), oldest_version.to_string ()));
908                 non_fatal_error (_ ("consider updating the input with the convert-ly script"));
909                 return false;
910         }
911
912   if (ver > current)
913         {
914                 non_fatal_error (_f ("program too old: %s (file requires: %s)",  current.to_string (), ver.to_string ()));
915                 return false;
916         }
917   return true;
918 }
919         
920
921 /*
922   substitute _ and \,
923 */
924 string
925 lyric_fudge (string s)
926 {
927   char *chars = string_copy (s);
928
929   for (char *p = chars; *p ; p++)
930     {
931       if (*p == '_' && (p == chars || *(p-1) != '\\'))
932         *p = ' ';
933     }
934   
935   s = string (chars);
936   delete[] chars;
937
938   ssize i = 0;  
939   if ((i = s.find ("\\,")) != NPOS)   // change "\," to TeX's "\c "
940     {
941       * (((char*)s.c_str ()) + i + 1) = 'c';
942       s = s.substr (0, i + 2) + " " + s.substr (i - 2);
943     }
944
945   return s;
946 }
947
948 /*
949 Convert "NUM/DEN" into a '(NUM . DEN) cons.
950 */
951 SCM
952 scan_fraction (string frac)
953 {
954         ssize i = frac.find ('/');
955         string left = frac.substr (0, i);
956         string right = frac.substr (i + 1, (frac.length () - i + 1));
957
958         int n = String_convert::dec2int (left);
959         int d = String_convert::dec2int (right);
960         return scm_cons (scm_from_int (n), scm_from_int (d));
961 }
962
963 SCM
964 lookup_markup_command (string s)
965 {
966         SCM proc = ly_lily_module_constant ("lookup-markup-command");
967         return scm_call_1 (proc, ly_string2scm (s));
968 }
969
970 SCM
971 lookup_markup_list_command (string s)
972 {
973         SCM proc = ly_lily_module_constant ("lookup-markup-list-command");
974         return scm_call_1 (proc, ly_string2scm (s));
975 }
976
977 /* Shut up lexer warnings.  */
978 #if YY_STACK_USED
979
980 static void
981 yy_push_state (int)
982 {
983 }
984
985 static void
986 yy_pop_state ()
987 {
988 }
989
990 static int
991 yy_top_state ()
992 {
993   return 0;
994 }
995
996 static void
997 silence_lexer_warnings ()
998 {
999    (void) yy_start_stack_ptr;
1000    (void) yy_start_stack_depth;
1001    (void) yy_start_stack;
1002    (void) yy_push_state;
1003    (void) yy_pop_state;
1004    (void) yy_top_state;
1005    (void) silence_lexer_warnings;
1006 }
1007 #endif