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