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