]> git.donarmstrong.com Git - lilypond.git/blob - lily/lexer.ll
compile fixes while han-wen's so long away
[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 "score.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 "event.hh"
44 #include "version.hh"
45 #include "lilypond-input-version.hh"
46 #include "translator-def.hh"
47 #include "music-output-def.hh"
48 #include "identifier-smob.hh"
49
50 /*
51 RH 7 fix (?)
52 */
53 #define isatty HORRIBLEKLUDGE
54
55 void strip_trailing_white (String&);
56 void strip_leading_white (String&);
57 String lyric_fudge (String s);
58
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 %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
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,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,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
268         for (int i=0; i < n; i++)
269         {
270                 yyinput ();
271         }
272         char_count_stack_.top () += n;
273
274         if (unpack_identifier (sval) != SCM_UNDEFINED)
275         {
276                 yylval.scm = unpack_identifier(sval);
277                 return identifier_type (yylval.scm);
278         }
279                 
280         yylval.scm = sval;
281         return SCM_T;
282 }
283 <figures>{
284         _       {
285                 return FIGURE_SPACE;
286         }
287         \>              {
288                 return FIGURE_CLOSE;
289         }
290         \<      {
291                 return FIGURE_OPEN;
292         }
293 }
294 <notes,figures>{
295         {ALPHAWORD}     {
296                 return scan_bare_word (YYText ());
297         }
298
299         {NOTECOMMAND}   {
300                 return scan_escaped_word (YYText () + 1); 
301         }
302         {FRACTION}      {
303                 yylval.scm =  scan_fraction (YYText ());
304                 return FRACTION;
305         }
306
307         {DIGIT}         {
308                 yylval.i = String_convert::dec2int (String (YYText ()));
309                 return DIGIT;
310         }
311         {UNSIGNED}              {
312                 yylval.i = String_convert::dec2int (String (YYText ()));
313                 return UNSIGNED;
314         }
315         {E_UNSIGNED}    {
316                 yylval.i = String_convert::dec2int (String (YYText () +1));
317                 return E_UNSIGNED;
318         }
319
320         \" {
321                 start_quote ();
322         }
323 }
324
325 \"              {
326         start_quote ();
327 }
328 <quote>{
329         \\{ESCAPED}     {
330                 *yylval.string += to_string (escaped_char (YYText ()[1]));
331         }
332         [^\\"]+ {
333                 *yylval.string += YYText ();
334         }
335         \"      {
336
337                 yy_pop_state ();
338
339                 /* yylval is union. Must remember STRING before setting SCM*/
340                 String *sp = yylval.string;
341                 yylval.scm = scm_makfrom0str (sp->to_str0 ());
342                 delete sp;
343                 return STRING;
344         }
345         .       {
346                 *yylval.string += YYText ();
347         }
348 }
349
350 <lyrics>{
351         \" {
352                 start_quote ();
353         }
354         {FRACTION}      {
355                 yylval.scm =  scan_fraction (YYText ());
356                 return FRACTION;
357         }
358         {UNSIGNED}              {
359                 yylval.i = String_convert::dec2int (String (YYText ()));
360                 return UNSIGNED;
361         }
362         {NOTECOMMAND}   {
363                 return scan_escaped_word (YYText () + 1);
364         }
365         {LYRICS} {
366                 /* ugr. This sux. */
367                 String s (YYText ()); 
368                 if (s == "__")
369                         return yylval.i = EXTENDER;
370                 if (s == "--")
371                         return yylval.i = HYPHEN;
372                 s = lyric_fudge (s);
373
374                 char c = s[s.length () - 1];
375                 if (c == '{' ||  c == '}') // brace open is for not confusing dumb tools.
376                         here_input ().warning (
377                                 _ ("Brace found at end of lyric.  Did you forget a space?"));
378                 yylval.scm = scm_makfrom0str (s.to_str0 ());
379
380
381                 return STRING;
382         }
383         . {
384                 return YYText ()[0];
385         }
386 }
387 <chords>{
388         {ALPHAWORD}     {
389                 return scan_bare_word (YYText ());
390         }
391         {NOTECOMMAND}   {
392                 return scan_escaped_word (YYText () + 1);
393         }
394         {FRACTION}      {
395                 yylval.scm =  scan_fraction (YYText ());
396                 return FRACTION;
397         }
398         {UNSIGNED}              {
399                 yylval.i = String_convert::dec2int (String (YYText ()));
400                 return UNSIGNED;
401         }
402         \" {
403                 start_quote ();
404         }
405         -  {
406                 return CHORD_MINUS;
407         }
408         :  {
409                 return CHORD_COLON;
410         }
411         \/\+ {
412                 return CHORD_BASS;
413         }
414         \/  {
415                 return CHORD_SLASH;
416         }
417         \^  {
418                 return CHORD_CARET;
419         }
420         . {
421                 return YYText ()[0];
422         }
423 }
424
425 <<EOF>> {
426
427
428         if (! close_input ()) { 
429           yyterminate (); // can't move this, since it actually rets a YY_NULL
430         }
431 }
432
433
434 {WORD}  {
435         return scan_bare_word (YYText ());
436 }
437 {KEYWORD}       {
438         return scan_escaped_word (YYText () + 1);
439 }
440 {REAL}          {
441         Real r;
442         int cnv=sscanf (YYText (), "%lf", &r);
443         assert (cnv == 1);
444
445         yylval.scm = gh_double2scm (r);
446         return REAL;
447 }
448
449 {UNSIGNED}      {
450         yylval.i = String_convert::dec2int (String (YYText ()));
451         return UNSIGNED;
452 }
453
454
455 [{}]    {
456
457         return YYText ()[0];
458 }
459 [*:=]           {
460         char c = YYText ()[0];
461
462         return c;
463 }
464
465 <INITIAL,notes,figures>.        {
466         return YYText ()[0];
467 }
468
469 <INITIAL,lyrics,notes,figures>\\. {
470     char c= YYText ()[1];
471
472     switch (c) {
473     case '>':
474         return E_BIGGER;
475     case '<':
476         return E_SMALLER;
477     case '!':
478         return E_EXCLAMATION;
479     case '(':
480         return E_OPEN;
481     case ')':
482         return E_CLOSE;
483     case '[':
484         return E_LEFTSQUARE;
485     case ']':
486         return E_RIGHTSQUARE;
487     case '~':
488         return E_TILDE;
489     case '\\':
490         return E_BACKSLASH;
491
492     default:
493         return E_CHAR;
494     }
495 }
496
497 <*>.            {
498         String msg = _f ("invalid character: `%c'", YYText ()[0]);
499         LexerError (msg.to_str0 ());
500         return YYText ()[0];
501 }
502
503 %%
504
505 void
506 My_lily_lexer::push_note_state ()
507 {
508         yy_push_state (notes);
509 }
510
511 void
512 My_lily_lexer::push_figuredbass_state()
513 {
514         yy_push_state (figures);
515 }
516 void
517 My_lily_lexer::push_chord_state ()
518 {
519         yy_push_state (chords);
520 }
521
522 void
523 My_lily_lexer::push_lyric_state ()
524 {
525         yy_push_state (lyrics);
526 }
527
528 void
529 My_lily_lexer::pop_state ()
530 {
531         yy_pop_state ();
532 }
533
534 int
535 My_lily_lexer::identifier_type(SCM sid)
536 {
537         if (gh_string_p (sid)) {
538                 yylval.scm = sid; 
539                 return STRING_IDENTIFIER;
540         } else if (gh_number_p (sid)) {
541                 yylval.scm = sid;
542                 return NUMBER_IDENTIFIER;
543         } else if (unsmob_translator_def (sid)) {
544                 yylval.scm = sid;
545                 return TRANSLATOR_IDENTIFIER;
546         } else if (unsmob_score (sid)) {
547                 yylval.scm =sid;
548                 return SCORE_IDENTIFIER;
549         } else if (Music * mus =unsmob_music (sid)) {
550                 yylval.scm = sid;
551                 
552                 return dynamic_cast<Event*> (mus) ? EVENT_IDENTIFIER : MUSIC_IDENTIFIER;
553         } else if (unsmob_duration (sid)) {
554                 yylval.scm = sid;
555                 return DURATION_IDENTIFIER;
556         } else if (unsmob_music_output_def (sid)) {
557                 yylval.scm = sid;
558                 return MUSIC_OUTPUT_DEF_IDENTIFIER;
559         }
560         return SCM_IDENTIFIER;
561 }
562
563
564 int
565 My_lily_lexer::scan_escaped_word (String str)
566 {
567         // use more SCM for this.
568
569         SCM sym = ly_symbol2scm (str.to_str0 ());
570
571         int l = lookup_keyword (str);
572         if (l != -1) {
573                 return l;
574         }
575         SCM sid = lookup_identifier (str);
576         if (sid != SCM_UNDEFINED)
577         {
578                 yylval.scm = sid;
579                 return identifier_type (sid);
580         }
581
582         if ((YYSTATE != notes) && (YYSTATE != chords)) {
583                 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
584                 
585                 if (gh_pair_p (pitch))
586                 {
587                         yylval.scm = ly_cdr (pitch);
588                         return NOTENAME_PITCH;
589                 }
590         }
591         String msg (_f ("unknown escaped string: `\\%s'", str));        
592         LexerError (msg.to_str0 ());
593
594         yylval.scm = scm_makfrom0str (str.to_str0 ());
595
596         return STRING;
597 }
598
599 int
600 My_lily_lexer::scan_bare_word (String str)
601 {
602         SCM sym = ly_symbol2scm (str.to_str0 ());
603         if ((YYSTATE == notes) || (YYSTATE == chords)) {
604                 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
605                 if (gh_pair_p (pitch)) {
606                     yylval.scm = ly_cdr (pitch);
607                     return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
608                 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
609                 {
610                     yylval.scm = ly_cdr (pitch);
611                     return CHORDMODIFIER_PITCH;
612                 }
613         }
614
615         yylval.scm = scm_makfrom0str (str.to_str0 ());
616         return STRING;
617 }
618
619 bool
620 My_lily_lexer::note_state_b () const
621 {
622         return YY_START == notes;
623 }
624
625 bool
626 My_lily_lexer::chord_state_b () const
627 {
628         return YY_START == chords;
629 }
630
631 bool
632 My_lily_lexer::lyric_state_b () const
633 {
634         return YY_START == lyrics;
635 }
636
637 bool
638 My_lily_lexer::figure_state_b () const
639 {
640         return YY_START == figures;
641 }
642
643 /*
644  urg, belong to String (_convert)
645  and should be generalised 
646  */
647 void
648 strip_leading_white (String&s)
649 {
650         int i=0;
651         for (;  i < s.length (); i++) 
652                 if (!isspace (s[i]))
653                         break;
654
655         s = s.nomid_string (0, i);
656 }
657
658 void
659 strip_trailing_white (String&s)
660 {
661         int i=s.length ();      
662         while (i--) 
663                 if (!isspace (s[i]))
664                         break;
665
666         s = s.left_string (i+1);
667 }
668
669
670
671 /* 1.3.146 == removal of ; */ 
672 Lilypond_version oldest_version ("1.3.146");
673
674
675 bool
676 valid_version_b (String s)
677 {
678   Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
679   Lilypond_version ver (s);
680   if (! ((ver >= oldest_version) && (ver <= current)))
681         {       
682                 non_fatal_error (_f ("Incorrect lilypond version: %s (%s, %s)", ver.string (), oldest_version.string (), current.string ()));
683                 non_fatal_error (_ ("Consider updating the input with the convert-ly script")); 
684                 return false;
685     }
686   return true;
687 }
688         
689
690 String
691 lyric_fudge (String s)
692 {
693   char  * chars  =s.get_copy_str0 ();
694
695   for (char * p = chars; *p ; p++)
696     {
697       if (*p == '_' && (p == chars || *(p-1) != '\\'))
698         *p = ' ';
699     }
700   
701   s = String (chars);
702   delete[] chars;
703
704   int i =0;     
705   if ((i=s.index ("\\,")) != -1)   // change "\," to TeX's "\c "
706     {
707       * (s.get_str0 () + i + 1) = 'c';
708       s = s.left_string (i+2) + " " + s.right_string (s.length ()-i-2);
709     }
710
711   return s;
712 }
713
714 /*
715 Convert "NUM/DEN" into a '(NUM . DEN) cons.
716 */
717 SCM
718 scan_fraction (String frac)
719 {
720         int i = frac.index ('/');
721         int l = frac.length ();
722         String left = frac.left_string (i);
723         String right = frac.right_string (l - i - 1);
724
725         int n = String_convert::dec2int (left);
726         int d = String_convert::dec2int (right);
727         return gh_cons (gh_int2scm (n), gh_int2scm (d));
728 }
729