]> git.donarmstrong.com Git - lilypond.git/blob - lily/lexer.ll
release: 1.5.11
[lilypond.git] / lily / lexer.ll
1 %{ // -*-Fundamental-*-
2 /*
3   lexer.l -- implement the Flex lexer
4
5   source file of the LilyPond music typesetter
6
7   (c) 1996--2000 Han-Wen Nienhuys <hanwen@cs.uu.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 #include <stdio.h>
26 #include <ctype.h>
27 #include <iostream.h> /* gcc 3.0 */
28 #include <errno.h>
29
30
31 #include "score.hh"
32 #include "lily-guile.hh"
33 #include "string.hh"
34 #include "string-convert.hh"
35 #include "my-lily-lexer.hh"
36 #include "array.hh"
37 #include "interval.hh"
38 #include "lily-guile.hh"
39 #include "parser.hh"
40 #include "debug.hh"
41 #include "main.hh"
42 #include "musical-request.hh"
43 #include "version.hh"
44 #include "lilypond-input-version.hh"
45 #include "translator-def.hh"
46 #include "music-output-def.hh"
47
48 /*
49 RH 7 fix (?)
50 */
51 #define isatty HORRIBLEKLUDGE
52
53 void strip_trailing_white (String&);
54 void strip_leading_white (String&);
55 String lyric_fudge (String s);
56
57
58 bool
59 valid_version_b (String s);
60
61
62
63 #define start_quote()   \
64         yy_push_state (quote);\
65         yylval.string = new String
66
67 #define yylval \
68         (*(YYSTYPE*)lexval_l)
69
70 #define YY_USER_ACTION  add_lexed_char (YYLeng ());
71 /*
72
73 LYRICS          ({AA}|{TEX})[^0-9 \t\n\f]*
74
75 */
76
77
78 SCM scan_fraction (String);
79
80 %}
81
82 %option c++
83 %option noyywrap
84 %option nodefault
85 %option debug
86 %option yyclass="My_lily_lexer"
87 %option stack
88 %option never-interactive 
89 %option warn
90
91 %x version
92 %x chords
93 %x incl
94 %x lyrics
95 %x notes
96 %x figures
97 %x quote
98 %x longcomment
99
100
101 A               [a-zA-Z]
102 AA              {A}|_
103 N               [0-9]
104 AN              {AA}|{N}
105 PUNCT           [?!:'`]
106 ACCENT          \\[`'"^]
107 NATIONAL        [\001-\006\021-\027\031\036\200-\377]
108 TEX             {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
109 WORD            {A}{AN}*
110 ALPHAWORD       {A}+
111 DIGIT           {N}
112 UNSIGNED        {N}+
113 FRACTION        {N}+\/{N}+
114 INT             -?{UNSIGNED}
115 REAL            ({INT}\.{N}*)|(-?\.{N}+)
116 KEYWORD         \\{WORD}
117 WHITE           [ \n\t\f\r]
118 HORIZONTALWHITE         [ \t]
119 BLACK           [^ \n\t\f\r]
120 RESTNAME        [rs]
121 NOTECOMMAND     \\{A}+
122 LYRICS          ({AA}|{TEX})[^0-9 \t\n\f]*
123 ESCAPED         [nt\\'"]
124 EXTENDER        __
125 HYPHEN          --
126 %%
127
128
129 <*>\r           {
130         // windows-suck-suck-suck
131 }
132
133 <INITIAL,chords,incl,lyrics,notes,figures>{
134   "%{"  {
135         yy_push_state (longcomment);
136   }
137   %[^{\n].*\n   {
138   }
139   %[^{\n]       { // backup rule
140   }
141   %\n   {
142   }
143   %[^{\n].*     {
144   }
145   {WHITE}+      {
146
147   }
148 }
149
150 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}*  {
151         yy_push_state (version);
152 }
153 <version>\"[^"]*\"     { /* got the version number */
154         String s (YYText ()+1);
155         s = s.left_str (s.index_last_i ('"'));
156
157         yy_pop_state ();
158         if (!valid_version_b (s))
159                 return INVALID;
160 }
161 <version>.      {
162         LexerError ("No quoted string found after \\version");
163         yy_pop_state ();
164 }
165 <longcomment>{
166         [^\%]*          {
167         }
168         \%*[^}%]*               {
169
170         }
171         "%"+"}"         {
172                 yy_pop_state ();
173         }
174         <<EOF>>         {
175                 LexerError (_ ("EOF found inside a comment").ch_C ());
176                 if (! close_input ()) 
177                   yyterminate (); // can't move this, since it actually rets a YY_NULL
178         }
179 }
180
181
182 <INITIAL,chords,lyrics,notes,figures>\\maininput           {
183         if (!main_input_b_)
184         {
185                 start_main_input ();
186                 main_input_b_ = true;
187         }
188         else
189                 error (_ ("\\maininput disallowed outside init files"));
190 }
191
192 <INITIAL,chords,lyrics,figures,notes>\\include           {
193         yy_push_state (incl);
194 }
195 <incl>\"[^"]*\";?   { /* got the include file name */
196         String s (YYText ()+1);
197         s = s.left_str (s.index_last_i ('"'));
198
199         new_input (s,source_global_l);
200         yy_pop_state ();
201 }
202 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
203         String s = YYText () + 1;
204         strip_trailing_white (s);
205         if (s.length_i () && (s[s.length_i () - 1] == ';'))
206           s = s.left_str (s.length_i () - 1);
207
208         SCM sid = lookup_identifier (s);
209         if (gh_string_p (sid)) {
210                 new_input (ly_scm2string (sid), source_global_l);
211                 yy_pop_state ();
212         } else { 
213             String msg (_f ("wrong or undefined identifier: `%s'", s ));
214
215             LexerError (msg.ch_C ());
216             SCM err = scm_current_error_port ();
217             scm_puts ("This value was found in the table: ", err);
218             scm_display (sid, err);
219           }
220 }
221 <incl>\"[^"]*   { // backup rule
222         cerr << _ ("Missing end quote") << endl;
223         exit (1);
224 }
225 <chords,notes,figures>{RESTNAME}        {
226         const char *s = YYText ();
227         yylval.scm = ly_str02scm (s);
228         return RESTNAME;
229 }
230 <chords,notes,figures>R         {
231         return MULTI_MEASURE_REST;
232 }
233 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}*{WHITE}        {
234         String s=YYText () + 2;
235         s=s.left_str (s.length_i () - 1);
236         return scan_escaped_word (s); 
237 }
238 <INITIAL,chords,lyrics,notes,figures>\${BLACK}*{WHITE}          {
239         String s=YYText () + 1;
240         s=s.left_str (s.length_i () - 1);
241         return scan_bare_word (s);
242 }
243 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}*               { // backup rule
244         cerr << _ ("white expected") << endl;
245         exit (1);
246 }
247 <INITIAL,chords,lyrics,notes,figures>\${BLACK}*         { // backup rule
248         cerr << _ ("white expected") << endl;
249         exit (1);
250 }
251
252 <INITIAL,chords,lyrics,notes,figures>#  { //embedded scm
253         //char const* s = YYText () + 1;
254         char const* s = here_ch_C ();
255         int n = 0;
256         if (main_input_b_ && safe_global_b) {
257                 error (_ ("Can't evaluate Scheme in safe mode"));
258                 yylval.scm =  SCM_EOL;
259                 return SCM_T;
260         }
261         yylval.scm = ly_parse_scm (s, &n);
262         
263         for (int i=0; i < n; i++)
264         {
265                 yyinput ();
266         }
267         char_count_stack_.top () += n;
268
269         return SCM_T;
270 }
271 <figures>{
272         \>              {
273                 return FIGURE_CLOSE;
274         }
275         \<      {
276                 return FIGURE_OPEN;
277         }
278 }
279 <notes,figures>{
280         {ALPHAWORD}     {
281                 return scan_bare_word (YYText ());
282         }
283
284         {NOTECOMMAND}   {
285                 return scan_escaped_word (YYText () + 1); 
286         }
287         {FRACTION}      {
288                 yylval.scm =  scan_fraction (YYText ());
289                 return FRACTION;
290         }
291
292         {DIGIT}         {
293                 yylval.i = String_convert::dec2_i (String (YYText ()));
294                 return DIGIT;
295         }
296         {UNSIGNED}              {
297                 yylval.i = String_convert::dec2_i (String (YYText ()));
298                 return UNSIGNED;
299         }
300
301         \" {
302                 start_quote ();
303         }
304 }
305
306 \"              {
307         start_quote ();
308 }
309 <quote>{
310         \\{ESCAPED}     {
311                 *yylval.string += to_str (escaped_char (YYText ()[1]));
312         }
313         [^\\"]+ {
314                 *yylval.string += YYText ();
315         }
316         \"      {
317
318                 yy_pop_state ();
319
320                 /* yylval is union. Must remember STRING before setting SCM*/
321                 String *sp = yylval.string;
322                 yylval.scm = ly_str02scm (sp->ch_C ());
323                 delete sp;
324                 return STRING;
325         }
326         .       {
327                 *yylval.string += YYText ();
328         }
329 }
330
331 <lyrics>{
332         \" {
333                 start_quote ();
334         }
335         {FRACTION}      {
336                 yylval.scm =  scan_fraction (YYText ());
337                 return FRACTION;
338         }
339         {UNSIGNED}              {
340                 yylval.i = String_convert::dec2_i (String (YYText ()));
341                 return UNSIGNED;
342         }
343         {NOTECOMMAND}   {
344                 return scan_escaped_word (YYText () + 1);
345         }
346         {LYRICS} {
347                 /* ugr. This sux. */
348                 String s (YYText ()); 
349                 if (s == "__")
350                         return yylval.i = EXTENDER;
351                 if (s == "--")
352                         return yylval.i = HYPHEN;
353                 s = lyric_fudge (s);
354
355                 char c = s[s.length_i () - 1];
356                 if (c == '{' ||  c == '}') // brace open is for not confusing dumb tools.
357                         here_input ().warning (
358                                 _ ("Brace found at end of lyric. Did you forget a space?"));
359                 yylval.scm = ly_str02scm (s.ch_C ());
360
361
362                 return STRING;
363         }
364         . {
365                 return YYText ()[0];
366         }
367 }
368 <chords>{
369         {ALPHAWORD}     {
370                 return scan_bare_word (YYText ());
371         }
372         {NOTECOMMAND}   {
373                 return scan_escaped_word (YYText () + 1);
374         }
375         {FRACTION}      {
376                 yylval.scm =  scan_fraction (YYText ());
377                 return FRACTION;
378         }
379         {UNSIGNED}              {
380                 yylval.i = String_convert::dec2_i (String (YYText ()));
381                 return UNSIGNED;
382         }
383         \" {
384                 start_quote ();
385         }
386         -  {
387                 return CHORD_MINUS;
388         }
389         :  {
390                 return CHORD_COLON;
391         }
392         \/\+ {
393                 return CHORD_BASS;
394         }
395         \^  {
396                 return CHORD_CARET;
397         }
398         . {
399                 return YYText ()[0];
400         }
401 }
402
403 <<EOF>> {
404
405
406         if (! close_input ()) { 
407           yyterminate (); // can't move this, since it actually rets a YY_NULL
408         }
409 }
410
411
412 {WORD}  {
413         return scan_bare_word (YYText ());
414 }
415 {KEYWORD}       {
416         return scan_escaped_word (YYText () + 1);
417 }
418 {REAL}          {
419         Real r;
420         int cnv=sscanf (YYText (), "%lf", &r);
421         assert (cnv == 1);
422
423         yylval.scm = gh_double2scm (r);
424         return REAL;
425 }
426
427 {UNSIGNED}      {
428         yylval.i = String_convert::dec2_i (String (YYText ()));
429         return UNSIGNED;
430 }
431
432 [{}]    {
433
434         return YYText ()[0];
435 }
436 [*:=]           {
437         char c = YYText ()[0];
438
439         return c;
440 }
441
442 <INITIAL,notes,figures>.        {
443         return YYText ()[0];
444 }
445
446 <INITIAL,lyrics,notes,figures>\\. {
447     char c= YYText ()[1];
448
449     switch (c) {
450     case '>':
451         return E_BIGGER;
452     case '<':
453         return E_SMALLER;
454     case '!':
455         return E_EXCLAMATION;
456     case '(':
457         return E_OPEN;
458     case ')':
459         return E_CLOSE;
460     case '~':
461         return E_TILDE;
462     default:
463         return E_CHAR;
464     }
465 }
466
467 <*>.            {
468         String msg = _f ("invalid character: `%c'", YYText ()[0]);
469         LexerError (msg.ch_C ());
470         return YYText ()[0];
471 }
472
473 %%
474
475 void
476 My_lily_lexer::push_note_state ()
477 {
478         yy_push_state (notes);
479 }
480
481 void
482 My_lily_lexer::push_figuredbass_state()
483 {
484         yy_push_state (figures);
485 }
486 void
487 My_lily_lexer::push_chord_state ()
488 {
489         yy_push_state (chords);
490 }
491
492 void
493 My_lily_lexer::push_lyric_state ()
494 {
495         yy_push_state (lyrics);
496 }
497
498 void
499 My_lily_lexer::pop_state ()
500 {
501         yy_pop_state ();
502 }
503
504 int
505 My_lily_lexer::scan_escaped_word (String str)
506 {
507         // use more SCM for this.
508
509         SCM sym = ly_symbol2scm (str.ch_C ());
510
511         int l = lookup_keyword (str);
512         if (l != -1) {
513                 return l;
514         }
515         SCM sid = lookup_identifier (str);
516         if (gh_string_p (sid)) {
517                 yylval.scm = sid; 
518                 return STRING_IDENTIFIER;
519         } else if (gh_number_p (sid)) {
520                 yylval.scm = sid;
521                 return NUMBER_IDENTIFIER;
522         } else if (unsmob_translator_def (sid)) {
523                 yylval.scm = sid;
524                 return TRANSLATOR_IDENTIFIER;
525         } else if (unsmob_score (sid)) {
526                 yylval.scm =sid;
527                 return SCORE_IDENTIFIER;
528         } else if (Music * mus =unsmob_music (sid)) {
529                 yylval.scm = sid;
530                 
531                 return dynamic_cast<Request*> (mus) ? REQUEST_IDENTIFIER : MUSIC_IDENTIFIER;
532         } else if (unsmob_duration (sid)) {
533                 yylval.scm = sid;
534                 return DURATION_IDENTIFIER;
535         } else if (unsmob_music_output_def (sid)) {
536                 yylval.scm = sid;
537                 return MUSIC_OUTPUT_DEF_IDENTIFIER;
538         }
539
540         if (sid != SCM_UNDEFINED) {
541                 yylval.scm = sid;
542                 return SCM_IDENTIFIER;
543         }
544
545         if ((YYSTATE != notes) && (YYSTATE != chords)) {
546                 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
547                 
548                 if (gh_pair_p (pitch))
549                 {
550                         yylval.scm = gh_cdr (pitch);
551                         return NOTENAME_PITCH;
552                 }
553         }
554         String msg (_f ("unknown escaped string: `\\%s'", str));        
555         LexerError (msg.ch_C ());
556
557         yylval.scm = ly_str02scm (str.ch_C ());
558
559         return STRING;
560 }
561
562 int
563 My_lily_lexer::scan_bare_word (String str)
564 {
565         SCM sym = ly_symbol2scm (str.ch_C ());
566         if ((YYSTATE == notes) || (YYSTATE == chords)) {
567                 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
568                 if (gh_pair_p (pitch)) {
569                     yylval.scm = gh_cdr (pitch);
570                     return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
571                 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
572                 {
573                     yylval.scm = gh_cdr (pitch);
574                     return CHORDMODIFIER_PITCH;
575                 }
576         }
577
578         yylval.scm = ly_str02scm (str.ch_C ());
579         return STRING;
580 }
581
582 bool
583 My_lily_lexer::note_state_b () const
584 {
585         return YY_START == notes;
586 }
587
588 bool
589 My_lily_lexer::chord_state_b () const
590 {
591         return YY_START == chords;
592 }
593
594 bool
595 My_lily_lexer::lyric_state_b () const
596 {
597         return YY_START == lyrics;
598 }
599
600 bool
601 My_lily_lexer::figure_state_b () const
602 {
603         return YY_START == figures;
604 }
605
606 /*
607  urg, belong to String (_convert)
608  and should be generalised 
609  */
610 void
611 strip_leading_white (String&s)
612 {
613         int i=0;
614         for (;  i < s.length_i (); i++) 
615                 if (!isspace (s[i]))
616                         break;
617
618         s = s.nomid_str (0, i);
619 }
620
621 void
622 strip_trailing_white (String&s)
623 {
624         int i=s.length_i ();    
625         while (i--) 
626                 if (!isspace (s[i]))
627                         break;
628
629         s = s.left_str (i+1);
630 }
631
632
633
634 Lilypond_version oldest_version ("1.3.59");
635
636 void
637 print_lilypond_versions (ostream &os)
638 {
639   os << _f ("Oldest supported input version: %s", oldest_version.str ()) 
640     << endl;
641 }
642
643
644 bool
645 valid_version_b (String s)
646 {
647   Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
648   Lilypond_version ver (s);
649   if (! ((ver >= oldest_version) && (ver <= current)))
650         {       
651                 non_fatal_error (_f ("incorrect lilypond version: %s (%s, %s)", ver.str (), oldest_version.str (), current.str ()));
652                 non_fatal_error (_ ("Consider converting the input with the convert-ly script")); 
653                 return false;
654     }
655   return true;
656 }
657         
658
659 String
660 lyric_fudge (String s)
661 {
662   char  * chars  =s.copy_ch_p ();
663
664   for (char * p = chars; *p ; p++)
665     {
666       if (*p == '_' && (p == chars || *(p-1) != '\\'))
667         *p = ' ';
668     }
669   
670   s = String (chars);
671   delete[] chars;
672
673   int i =0;     
674   if ((i=s.index_i ("\\,")) != -1)   // change "\," to TeX's "\c "
675     {
676       * (s.ch_l () + i + 1) = 'c';
677       s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
678     }
679
680   return s;
681 }
682
683 /*
684 Convert "NUM/DEN" into a '(NUM . DEN) cons.
685 */
686 SCM
687 scan_fraction (String frac)
688 {
689         int i = frac.index_i ('/');
690         int l = frac.length_i ();
691         String left = frac.left_str (i);
692         String right = frac.right_str (l - i - 1);
693
694         int n = String_convert::dec2_i (left);
695         int d = String_convert::dec2_i (right);
696         return gh_cons (gh_int2scm (n), gh_int2scm (d));
697 }
698