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