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