]> git.donarmstrong.com Git - lilypond.git/blob - lily/lexer.ll
7bff8df6f4b16026132d488f5d397afcd9e29e86
[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--2000 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 <iostream.h> /* gcc 3.0 */
28 #include <errno.h>
29
30
31 #include "score.hh"
32 #include "lily-guile.hh"
33 #include "string.hh"
34 #include "string-convert.hh"
35 #include "my-lily-lexer.hh"
36 #include "array.hh"
37 #include "interval.hh"
38 #include "lily-guile.hh"
39 #include "parser.hh"
40 #include "debug.hh"
41 #include "main.hh"
42 #include "musical-request.hh"
43 #include "version.hh"
44 #include "lilypond-input-version.hh"
45 #include "translator-def.hh"
46 #include "music-output-def.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
58 bool
59 valid_version_b (String s);
60
61
62
63 #define start_quote()   \
64         yy_push_state (quote);\
65         yylval.string = new String
66
67 #define yylval \
68         (*(YYSTYPE*)lexval_l)
69
70 #define YY_USER_ACTION  add_lexed_char (YYLeng ());
71 /*
72
73 LYRICS          ({AA}|{TEX})[^0-9 \t\n\f]*
74
75 */
76
77
78 SCM scan_fraction (String);
79
80 %}
81
82 %option c++
83 %option noyywrap
84 %option nodefault
85 %option debug
86 %option yyclass="My_lily_lexer"
87 %option stack
88 %option never-interactive 
89 %option warn
90
91 %x version
92 %x chords
93 %x incl
94 %x lyrics
95 %x notes
96 %x figures
97 %x quote
98 %x longcomment
99
100
101 A               [a-zA-Z]
102 AA              {A}|_
103 N               [0-9]
104 AN              {AA}|{N}
105 PUNCT           [?!:'`]
106 ACCENT          \\[`'"^]
107 NATIONAL        [\001-\006\021-\027\031\036\200-\377]
108 TEX             {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
109 WORD            {A}{AN}*
110 ALPHAWORD       {A}+
111 DIGIT           {N}
112 UNSIGNED        {N}+
113 FRACTION        {N}+\/{N}+
114 INT             -?{UNSIGNED}
115 REAL            ({INT}\.{N}*)|(-?\.{N}+)
116 KEYWORD         \\{WORD}
117 WHITE           [ \n\t\f\r]
118 HORIZONTALWHITE         [ \t]
119 BLACK           [^ \n\t\f\r]
120 RESTNAME        [rs]
121 NOTECOMMAND     \\{A}+
122 LYRICS          ({AA}|{TEX})[^0-9 \t\n\f]*
123 ESCAPED         [nt\\'"]
124 EXTENDER        __
125 HYPHEN          --
126 %%
127
128
129 <*>\r           {
130         // windows-suck-suck-suck
131 }
132
133 <INITIAL,chords,incl,lyrics,notes,figures>{
134   "%{"  {
135         yy_push_state (longcomment);
136   }
137   %[^{\n].*\n   {
138   }
139   %[^{\n]       { // backup rule
140   }
141   %\n   {
142   }
143   %[^{\n].*     {
144   }
145   {WHITE}+      {
146
147   }
148 }
149
150 <INITIAL,chords,lyrics,notes,figures>\\version{WHITE}*  {
151         yy_push_state (version);
152 }
153 <version>\"[^"]*\"     { /* got the version number */
154         String s (YYText ()+1);
155         s = s.left_str (s.index_last_i ('"'));
156
157         yy_pop_state ();
158         if (!valid_version_b (s))
159                 return INVALID;
160 }
161 <version>.      {
162         LexerError ("No quoted string found after \\version");
163         yy_pop_state ();
164 }
165 <longcomment>{
166         [^\%]*          {
167         }
168         \%*[^}%]*               {
169
170         }
171         "%"+"}"         {
172                 yy_pop_state ();
173         }
174         <<EOF>>         {
175                 LexerError (_ ("EOF found inside a comment").ch_C ());
176                 if (! close_input ()) 
177                   yyterminate (); // can't move this, since it actually rets a YY_NULL
178         }
179 }
180
181
182 <INITIAL,chords,lyrics,notes,figures>\\maininput           {
183         if (!main_input_b_)
184         {
185                 start_main_input ();
186                 main_input_b_ = true;
187         }
188         else
189                 error (_ ("\\maininput disallowed outside init files"));
190 }
191
192 <INITIAL,chords,lyrics,figures,notes>\\include           {
193         yy_push_state (incl);
194 }
195 <incl>\"[^"]*\";?   { /* got the include file name */
196         String s (YYText ()+1);
197         s = s.left_str (s.index_last_i ('"'));
198
199         new_input (s,source_global_l);
200         yy_pop_state ();
201 }
202 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
203         String s = YYText () + 1;
204         strip_trailing_white (s);
205         if (s.length_i () && (s[s.length_i () - 1] == ';'))
206           s = s.left_str (s.length_i () - 1);
207
208         SCM sid = lookup_identifier (s);
209         if (gh_string_p (sid)) {
210                 new_input (ly_scm2string (sid), source_global_l);
211                 yy_pop_state ();
212         } else { 
213             String msg (_f ("wrong or undefined identifier: `%s'", s ));
214
215             LexerError (msg.ch_C ());
216             SCM err = scm_current_error_port ();
217             scm_puts ("This value was found in the table: ", err);
218             scm_display (sid, err);
219           }
220 }
221 <incl>\"[^"]*   { // backup rule
222         cerr << _ ("Missing end quote") << endl;
223         exit (1);
224 }
225 <chords,notes,figures>{RESTNAME}        {
226         const char *s = YYText ();
227         yylval.scm = ly_str02scm (s);
228         return RESTNAME;
229 }
230 <chords,notes,figures>R         {
231         return MULTI_MEASURE_REST;
232 }
233 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}*{WHITE}        {
234         String s=YYText () + 2;
235         s=s.left_str (s.length_i () - 1);
236         return scan_escaped_word (s); 
237 }
238 <INITIAL,chords,lyrics,notes,figures>\${BLACK}*{WHITE}          {
239         String s=YYText () + 1;
240         s=s.left_str (s.length_i () - 1);
241         return scan_bare_word (s);
242 }
243 <INITIAL,chords,lyrics,notes,figures>\\\${BLACK}*               { // backup rule
244         cerr << _ ("white expected") << endl;
245         exit (1);
246 }
247 <INITIAL,chords,lyrics,notes,figures>\${BLACK}*         { // backup rule
248         cerr << _ ("white expected") << endl;
249         exit (1);
250 }
251
252 <INITIAL,chords,lyrics,notes,figures>#  { //embedded scm
253         //char const* s = YYText () + 1;
254         char const* s = here_ch_C ();
255         int n = 0;
256         if (main_input_b_ && safe_global_b) {
257                 error (_ ("Can't evaluate Scheme in safe mode"));
258                 yylval.scm =  SCM_EOL;
259                 return SCM_T;
260         }
261         yylval.scm = ly_parse_scm (s, &n);
262         
263         for (int i=0; i < n; i++)
264         {
265                 yyinput ();
266         }
267         char_count_stack_.top () += n;
268
269         return SCM_T;
270 }
271 <figures>{
272         _       {
273                 return FIGURE_SPACE;
274         }
275         \]      {
276                 return FIGURE_BRACKET_CLOSE;
277         }
278         \[      {
279                 return FIGURE_BRACKET_OPEN;
280         }
281         \>              {
282                 return FIGURE_CLOSE;
283         }
284         \<      {
285                 return FIGURE_OPEN;
286         }
287 }
288 <notes,figures>{
289         {ALPHAWORD}     {
290                 return scan_bare_word (YYText ());
291         }
292
293         {NOTECOMMAND}   {
294                 return scan_escaped_word (YYText () + 1); 
295         }
296         {FRACTION}      {
297                 yylval.scm =  scan_fraction (YYText ());
298                 return FRACTION;
299         }
300
301         {DIGIT}         {
302                 yylval.i = String_convert::dec2_i (String (YYText ()));
303                 return DIGIT;
304         }
305         {UNSIGNED}              {
306                 yylval.i = String_convert::dec2_i (String (YYText ()));
307                 return UNSIGNED;
308         }
309
310         \" {
311                 start_quote ();
312         }
313 }
314
315 \"              {
316         start_quote ();
317 }
318 <quote>{
319         \\{ESCAPED}     {
320                 *yylval.string += to_str (escaped_char (YYText ()[1]));
321         }
322         [^\\"]+ {
323                 *yylval.string += YYText ();
324         }
325         \"      {
326
327                 yy_pop_state ();
328
329                 /* yylval is union. Must remember STRING before setting SCM*/
330                 String *sp = yylval.string;
331                 yylval.scm = ly_str02scm (sp->ch_C ());
332                 delete sp;
333                 return STRING;
334         }
335         .       {
336                 *yylval.string += YYText ();
337         }
338 }
339
340 <lyrics>{
341         \" {
342                 start_quote ();
343         }
344         {FRACTION}      {
345                 yylval.scm =  scan_fraction (YYText ());
346                 return FRACTION;
347         }
348         {UNSIGNED}              {
349                 yylval.i = String_convert::dec2_i (String (YYText ()));
350                 return UNSIGNED;
351         }
352         {NOTECOMMAND}   {
353                 return scan_escaped_word (YYText () + 1);
354         }
355         {LYRICS} {
356                 /* ugr. This sux. */
357                 String s (YYText ()); 
358                 if (s == "__")
359                         return yylval.i = EXTENDER;
360                 if (s == "--")
361                         return yylval.i = HYPHEN;
362                 s = lyric_fudge (s);
363
364                 char c = s[s.length_i () - 1];
365                 if (c == '{' ||  c == '}') // brace open is for not confusing dumb tools.
366                         here_input ().warning (
367                                 _ ("Brace found at end of lyric. Did you forget a space?"));
368                 yylval.scm = ly_str02scm (s.ch_C ());
369
370
371                 return STRING;
372         }
373         . {
374                 return YYText ()[0];
375         }
376 }
377 <chords>{
378         {ALPHAWORD}     {
379                 return scan_bare_word (YYText ());
380         }
381         {NOTECOMMAND}   {
382                 return scan_escaped_word (YYText () + 1);
383         }
384         {FRACTION}      {
385                 yylval.scm =  scan_fraction (YYText ());
386                 return FRACTION;
387         }
388         {UNSIGNED}              {
389                 yylval.i = String_convert::dec2_i (String (YYText ()));
390                 return UNSIGNED;
391         }
392         \" {
393                 start_quote ();
394         }
395         -  {
396                 return CHORD_MINUS;
397         }
398         :  {
399                 return CHORD_COLON;
400         }
401         \/\+ {
402                 return CHORD_BASS;
403         }
404         \^  {
405                 return CHORD_CARET;
406         }
407         . {
408                 return YYText ()[0];
409         }
410 }
411
412 <<EOF>> {
413
414
415         if (! close_input ()) { 
416           yyterminate (); // can't move this, since it actually rets a YY_NULL
417         }
418 }
419
420
421 {WORD}  {
422         return scan_bare_word (YYText ());
423 }
424 {KEYWORD}       {
425         return scan_escaped_word (YYText () + 1);
426 }
427 {REAL}          {
428         Real r;
429         int cnv=sscanf (YYText (), "%lf", &r);
430         assert (cnv == 1);
431
432         yylval.scm = gh_double2scm (r);
433         return REAL;
434 }
435
436 {UNSIGNED}      {
437         yylval.i = String_convert::dec2_i (String (YYText ()));
438         return UNSIGNED;
439 }
440
441 [{}]    {
442
443         return YYText ()[0];
444 }
445 [*:=]           {
446         char c = YYText ()[0];
447
448         return c;
449 }
450
451 <INITIAL,notes,figures>.        {
452         return YYText ()[0];
453 }
454
455 <INITIAL,lyrics,notes,figures>\\. {
456     char c= YYText ()[1];
457
458     switch (c) {
459     case '>':
460         return E_BIGGER;
461     case '<':
462         return E_SMALLER;
463     case '!':
464         return E_EXCLAMATION;
465     case '(':
466         return E_OPEN;
467     case ')':
468         return E_CLOSE;
469     case '~':
470         return E_TILDE;
471     case '\\':
472         return E_BACKSLASH;
473     default:
474         return E_CHAR;
475     }
476 }
477
478 <*>.            {
479         String msg = _f ("invalid character: `%c'", YYText ()[0]);
480         LexerError (msg.ch_C ());
481         return YYText ()[0];
482 }
483
484 %%
485
486 void
487 My_lily_lexer::push_note_state ()
488 {
489         yy_push_state (notes);
490 }
491
492 void
493 My_lily_lexer::push_figuredbass_state()
494 {
495         yy_push_state (figures);
496 }
497 void
498 My_lily_lexer::push_chord_state ()
499 {
500         yy_push_state (chords);
501 }
502
503 void
504 My_lily_lexer::push_lyric_state ()
505 {
506         yy_push_state (lyrics);
507 }
508
509 void
510 My_lily_lexer::pop_state ()
511 {
512         yy_pop_state ();
513 }
514
515 int
516 My_lily_lexer::scan_escaped_word (String str)
517 {
518         // use more SCM for this.
519
520         SCM sym = ly_symbol2scm (str.ch_C ());
521
522         int l = lookup_keyword (str);
523         if (l != -1) {
524                 return l;
525         }
526         SCM sid = lookup_identifier (str);
527         if (gh_string_p (sid)) {
528                 yylval.scm = sid; 
529                 return STRING_IDENTIFIER;
530         } else if (gh_number_p (sid)) {
531                 yylval.scm = sid;
532                 return NUMBER_IDENTIFIER;
533         } else if (unsmob_translator_def (sid)) {
534                 yylval.scm = sid;
535                 return TRANSLATOR_IDENTIFIER;
536         } else if (unsmob_score (sid)) {
537                 yylval.scm =sid;
538                 return SCORE_IDENTIFIER;
539         } else if (Music * mus =unsmob_music (sid)) {
540                 yylval.scm = sid;
541                 
542                 return dynamic_cast<Request*> (mus) ? REQUEST_IDENTIFIER : MUSIC_IDENTIFIER;
543         } else if (unsmob_duration (sid)) {
544                 yylval.scm = sid;
545                 return DURATION_IDENTIFIER;
546         } else if (unsmob_music_output_def (sid)) {
547                 yylval.scm = sid;
548                 return MUSIC_OUTPUT_DEF_IDENTIFIER;
549         }
550
551         if (sid != SCM_UNDEFINED) {
552                 yylval.scm = sid;
553                 return SCM_IDENTIFIER;
554         }
555
556         if ((YYSTATE != notes) && (YYSTATE != chords)) {
557                 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
558                 
559                 if (gh_pair_p (pitch))
560                 {
561                         yylval.scm = ly_cdr (pitch);
562                         return NOTENAME_PITCH;
563                 }
564         }
565         String msg (_f ("unknown escaped string: `\\%s'", str));        
566         LexerError (msg.ch_C ());
567
568         yylval.scm = ly_str02scm (str.ch_C ());
569
570         return STRING;
571 }
572
573 int
574 My_lily_lexer::scan_bare_word (String str)
575 {
576         SCM sym = ly_symbol2scm (str.ch_C ());
577         if ((YYSTATE == notes) || (YYSTATE == chords)) {
578                 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
579                 if (gh_pair_p (pitch)) {
580                     yylval.scm = ly_cdr (pitch);
581                     return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
582                 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
583                 {
584                     yylval.scm = ly_cdr (pitch);
585                     return CHORDMODIFIER_PITCH;
586                 }
587         }
588
589         yylval.scm = ly_str02scm (str.ch_C ());
590         return STRING;
591 }
592
593 bool
594 My_lily_lexer::note_state_b () const
595 {
596         return YY_START == notes;
597 }
598
599 bool
600 My_lily_lexer::chord_state_b () const
601 {
602         return YY_START == chords;
603 }
604
605 bool
606 My_lily_lexer::lyric_state_b () const
607 {
608         return YY_START == lyrics;
609 }
610
611 bool
612 My_lily_lexer::figure_state_b () const
613 {
614         return YY_START == figures;
615 }
616
617 /*
618  urg, belong to String (_convert)
619  and should be generalised 
620  */
621 void
622 strip_leading_white (String&s)
623 {
624         int i=0;
625         for (;  i < s.length_i (); i++) 
626                 if (!isspace (s[i]))
627                         break;
628
629         s = s.nomid_str (0, i);
630 }
631
632 void
633 strip_trailing_white (String&s)
634 {
635         int i=s.length_i ();    
636         while (i--) 
637                 if (!isspace (s[i]))
638                         break;
639
640         s = s.left_str (i+1);
641 }
642
643
644
645 Lilypond_version oldest_version ("1.3.59");
646
647 void
648 print_lilypond_versions (ostream &os)
649 {
650   os << _f ("Oldest supported input version: %s", oldest_version.str ()) 
651     << endl;
652 }
653
654
655 bool
656 valid_version_b (String s)
657 {
658   Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
659   Lilypond_version ver (s);
660   if (! ((ver >= oldest_version) && (ver <= current)))
661         {       
662                 non_fatal_error (_f ("incorrect lilypond version: %s (%s, %s)", ver.str (), oldest_version.str (), current.str ()));
663                 non_fatal_error (_ ("Consider converting the input with the convert-ly script")); 
664                 return false;
665     }
666   return true;
667 }
668         
669
670 String
671 lyric_fudge (String s)
672 {
673   char  * chars  =s.copy_ch_p ();
674
675   for (char * p = chars; *p ; p++)
676     {
677       if (*p == '_' && (p == chars || *(p-1) != '\\'))
678         *p = ' ';
679     }
680   
681   s = String (chars);
682   delete[] chars;
683
684   int i =0;     
685   if ((i=s.index_i ("\\,")) != -1)   // change "\," to TeX's "\c "
686     {
687       * (s.ch_l () + i + 1) = 'c';
688       s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
689     }
690
691   return s;
692 }
693
694 /*
695 Convert "NUM/DEN" into a '(NUM . DEN) cons.
696 */
697 SCM
698 scan_fraction (String frac)
699 {
700         int i = frac.index_i ('/');
701         int l = frac.length_i ();
702         String left = frac.left_str (i);
703         String right = frac.right_str (l - i - 1);
704
705         int n = String_convert::dec2_i (left);
706         int d = String_convert::dec2_i (right);
707         return gh_cons (gh_int2scm (n), gh_int2scm (d));
708 }
709