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