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