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