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