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