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