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