]> git.donarmstrong.com Git - lilypond.git/blob - lily/lexer.ll
c2cb3077ca71401c2a27c611c065f2c858712c7f
[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 "context-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                         if (tag == ly_symbol2scm("empty"))
488                                 return MARKUP_HEAD_EMPTY;
489                         else if (tag == ly_symbol2scm ("markup0-markup1"))
490                                 return MARKUP_HEAD_MARKUP0_MARKUP1;
491                         else if (tag == ly_symbol2scm ("markup-list0"))
492                                 return MARKUP_HEAD_LIST0;
493                         else if (tag == ly_symbol2scm ("scheme0"))
494                                 return MARKUP_HEAD_SCM0;
495                         else if (tag == ly_symbol2scm ("scheme0-scheme1"))
496                                 return MARKUP_HEAD_SCM0_SCM1;
497                         else if (tag == ly_symbol2scm ("scheme0-markup1"))
498                                 return MARKUP_HEAD_SCM0_MARKUP1;
499                         else if (tag == ly_symbol2scm ("scheme0-scheme1-markup2"))
500                                 return MARKUP_HEAD_SCM0_SCM1_MARKUP2;
501                         else if (tag == ly_symbol2scm ("scheme0-scheme1-scheme2"))
502                                 return MARKUP_HEAD_SCM0_SCM1_SCM2;
503                         else {
504                                 programming_error ("No parser tag defined for this signature. Abort"); 
505                                 ly_display_scm (s);
506                                 assert(false);
507                         }
508                 } else
509                         return scan_escaped_word (str);
510         }
511         [{}]    {
512                 return YYText()[0];
513         }
514         [^#{}"\\ \t\n\r\f]+ {
515                 String s (YYText ()); 
516
517                 char c = s[s.length () - 1];
518                 if (c == '{' ||  c == '}') // brace open is for not confusing dumb tools.
519                         here_input ().warning (
520                                 _ ("Brace found at end of markup.  Did you forget a space?"));
521                 yylval.scm = scm_makfrom0str (s.to_str0 ());
522
523
524                 return STRING;
525         }
526         .  {
527                 return YYText()[0];
528         }
529 }
530
531 <<EOF>> {
532
533
534         if (! close_input ()) { 
535           yyterminate (); // can't move this, since it actually rets a YY_NULL
536         }
537 }
538
539
540 {WORD}  {
541         return scan_bare_word (YYText ());
542 }
543 {KEYWORD}       {
544         return scan_escaped_word (YYText () + 1);
545 }
546 {REAL}          {
547         Real r;
548         int cnv=sscanf (YYText (), "%lf", &r);
549         assert (cnv == 1);
550
551         yylval.scm = gh_double2scm (r);
552         return REAL;
553 }
554
555 {UNSIGNED}      {
556         yylval.i = String_convert::dec2int (String (YYText ()));
557         return UNSIGNED;
558 }
559
560
561 [{}]    {
562
563         return YYText ()[0];
564 }
565 [*:=]           {
566         char c = YYText ()[0];
567
568         return c;
569 }
570
571 <INITIAL,notes,figures>.        {
572         return YYText ()[0];
573 }
574
575 <INITIAL,lyrics,notes,figures>\\. {
576     char c= YYText ()[1];
577
578     switch (c) {
579     case '>':
580         return E_BIGGER;
581     case '<':
582         return E_SMALLER;
583     case '!':
584         return E_EXCLAMATION;
585     case '(':
586         return E_OPEN;
587     case ')':
588         return E_CLOSE;
589     case '[':
590         return E_LEFTSQUARE;
591     case ']':
592         return E_RIGHTSQUARE;
593     case '~':
594         return E_TILDE;
595     case '\\':
596         return E_BACKSLASH;
597
598     default:
599         return E_CHAR;
600     }
601 }
602
603 <*>.            {
604         String msg = _f ("invalid character: `%c'", YYText ()[0]);
605         LexerError (msg.to_str0 ());
606         return YYText ()[0];
607 }
608
609 %%
610
611 void
612 My_lily_lexer::push_note_state ()
613 {
614         yy_push_state (notes);
615 }
616
617 void
618 My_lily_lexer::push_figuredbass_state()
619 {
620         yy_push_state (figures);
621 }
622 void
623 My_lily_lexer::push_chord_state ()
624 {
625         yy_push_state (chords);
626 }
627
628 void
629 My_lily_lexer::push_lyric_state ()
630 {
631         yy_push_state (lyrics);
632 }
633
634 void
635 My_lily_lexer::push_markup_state ()
636 {
637         yy_push_state (markup);
638 }
639
640 void
641 My_lily_lexer::pop_state ()
642 {
643         yy_pop_state ();
644 }
645
646 int
647 My_lily_lexer::identifier_type(SCM sid)
648 {
649         int k = try_special_identifiers(&yylval.scm , sid);
650         return k >= 0  ? k : SCM_IDENTIFIER;
651 }
652
653
654 int
655 My_lily_lexer::scan_escaped_word (String str)
656 {
657         // use more SCM for this.
658
659         SCM sym = ly_symbol2scm (str.to_str0 ());
660
661         int l = lookup_keyword (str);
662         if (l != -1) {
663                 return l;
664         }
665         SCM sid = lookup_identifier (str);
666         if (sid != SCM_UNDEFINED)
667         {
668                 yylval.scm = sid;
669                 return identifier_type (sid);
670         }
671
672         if ((YYSTATE != notes) && (YYSTATE != chords)) {
673                 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
674                 
675                 if (gh_pair_p (pitch))
676                 {
677                         yylval.scm = ly_cdr (pitch);
678                         return NOTENAME_PITCH;
679                 }
680         }
681         String msg (_f ("unknown escaped string: `\\%s'", str));        
682         LexerError (msg.to_str0 ());
683
684         yylval.scm = scm_makfrom0str (str.to_str0 ());
685
686         return STRING;
687 }
688
689 int
690 My_lily_lexer::scan_bare_word (String str)
691 {
692         SCM sym = ly_symbol2scm (str.to_str0 ());
693         if ((YYSTATE == notes) || (YYSTATE == chords)) {
694                 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
695                 if (gh_pair_p (pitch)) {
696                     yylval.scm = ly_cdr (pitch);
697                     return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
698                 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
699                 {
700                     yylval.scm = ly_cdr (pitch);
701                     return CHORD_MODIFIER;
702                 }
703         }
704
705         yylval.scm = scm_makfrom0str (str.to_str0 ());
706         return STRING;
707 }
708
709 bool
710 My_lily_lexer::note_state_b () const
711 {
712         return YY_START == notes;
713 }
714
715 bool
716 My_lily_lexer::chord_state_b () const
717 {
718         return YY_START == chords;
719 }
720
721 bool
722 My_lily_lexer::lyric_state_b () const
723 {
724         return YY_START == lyrics;
725 }
726
727 bool
728 My_lily_lexer::figure_state_b () const
729 {
730         return YY_START == figures;
731 }
732
733 /*
734  urg, belong to String (_convert)
735  and should be generalised 
736  */
737 void
738 strip_leading_white (String&s)
739 {
740         int i=0;
741         for (;  i < s.length (); i++) 
742                 if (!isspace (s[i]))
743                         break;
744
745         s = s.nomid_string (0, i);
746 }
747
748 void
749 strip_trailing_white (String&s)
750 {
751         int i=s.length ();      
752         while (i--) 
753                 if (!isspace (s[i]))
754                         break;
755
756         s = s.left_string (i+1);
757 }
758
759
760
761 /* 1.9.0 == postfix articulations */ 
762 Lilypond_version oldest_version ("1.9.0");
763
764
765 bool
766 valid_version_b (String s)
767 {
768   Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
769   Lilypond_version ver (s);
770   if (! ((ver >= oldest_version) && (ver <= current)))
771         {       
772                 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.to_string (), oldest_version.to_string (), current.to_string ()));
773                 non_fatal_error (_ ("Consider updating the input with the convert-ly script")); 
774                 return false;
775     }
776   return true;
777 }
778         
779
780 /*
781   substittute _ adn \,
782 */
783 String
784 lyric_fudge (String s)
785 {
786   char  * chars  =s.get_copy_str0 ();
787
788   for (char * p = chars; *p ; p++)
789     {
790       if (*p == '_' && (p == chars || *(p-1) != '\\'))
791         *p = ' ';
792     }
793   
794   s = String (chars);
795   delete[] chars;
796
797   int i =0;     
798   if ((i=s.index ("\\,")) != -1)   // change "\," to TeX's "\c "
799     {
800       * (s.get_str0 () + i + 1) = 'c';
801       s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
802     }
803
804   return s;
805 }
806
807 /*
808 Convert "NUM/DEN" into a '(NUM . DEN) cons.
809 */
810 SCM
811 scan_fraction (String frac)
812 {
813         int i = frac.index ('/');
814         int l = frac.length ();
815         String left = frac.left_string (i);
816         String right = frac.right_string (l - i - 1);
817
818         int n = String_convert::dec2int (left);
819         int d = String_convert::dec2int (right);
820         return gh_cons (gh_int2scm (n), gh_int2scm (d));
821 }
822
823 // Breaks for flex 2.5.31
824 #if 0
825 /* avoid silly flex induced gcc warnings */
826 static void yy_push_state (int) {;}
827 static void yy_pop_state () {;}
828 static int yy_top_state () { return 0; }
829
830 static void
831 avoid_silly_flex_induced_gcc_warnings ()
832 {
833         (void)yy_start_stack_ptr;
834         (void)yy_start_stack_depth;
835         (void)yy_start_stack;
836         yy_push_state (0);
837         yy_pop_state ();
838         yy_top_state ();
839         avoid_silly_flex_induced_gcc_warnings ();
840 }
841 #endif
842
843 SCM
844 lookup_markup_command (String s)
845 {
846         static SCM proc ;
847         if (!proc)
848                 proc = scm_c_eval_string ("lookup-markup-command");
849
850         return scm_call_1 (proc, scm_makfrom0str (s.to_str0 ()));
851 }