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