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