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