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