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