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