]> git.donarmstrong.com Git - lilypond.git/blob - lily/lexer.ll
release: 1.3.60
[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         if (!valid_version_b (s))
141                 return INVALID;
142         yy_pop_state ();
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                 return SCM_EOL;
237         }
238         yylval.scm = ly_parse_scm (s, &n);
239         DEBUG_OUT << "Scheme: ";
240         if (flower_dstream)
241                 ly_display_scm (yylval.scm);
242         
243         for (int i=0; i < n; i++)
244         {
245                 yyinput ();
246         }
247         char_count_stack_.top () += n;
248
249         return SCM_T;
250 }
251 <notes>{
252         {ALPHAWORD}     {
253                 return scan_bare_word (YYText ());
254         }
255
256         {NOTECOMMAND}   {
257                 return scan_escaped_word (YYText () + 1); 
258         }
259
260         {DIGIT}         {
261                 yylval.i = String_convert::dec2_i (String (YYText ()));
262                 return DIGIT;
263         }
264         {UNSIGNED}              {
265                 yylval.i = String_convert::dec2_i (String (YYText ()));
266                 return UNSIGNED;
267         }
268
269         \" {
270                 start_quote ();
271         }
272 }
273
274 \"              {
275         start_quote ();
276 }
277 <quote>{
278         \\{ESCAPED}     {
279                 *yylval.string += to_str (escaped_char(YYText()[1]));
280         }
281         [^\\"]+ {
282                 *yylval.string += YYText ();
283         }
284         \"      {
285                 DEBUG_OUT << "quoted string: `" << *yylval.string << "'\n";
286                 yy_pop_state ();
287
288                 /* yylval is union. Must remember STRING before setting SCM*/
289                 String *sp = yylval.string;
290                 yylval.scm = ly_str02scm  (sp->ch_C ());
291                 delete sp;
292                 return STRING;
293         }
294         .       {
295                 *yylval.string += YYText ();
296         }
297 }
298
299 <lyrics>{
300         \" {
301                 start_quote ();
302         }
303         {UNSIGNED}              {
304                 yylval.i = String_convert::dec2_i (String (YYText ()));
305                 return UNSIGNED;
306         }
307         {NOTECOMMAND}   {
308                 return scan_escaped_word (YYText () + 1);
309         }
310         {LYRICS} {
311                 /* ugr. This sux. */
312                 String s (YYText ()); 
313                 if (s == "__")
314                         return yylval.i = EXTENDER;
315                 if (s == "--")
316                         return yylval.i = HYPHEN;
317                 int i = 0;
318                 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
319                         *(s.ch_l () + i) = ' ';
320                 if ((i=s.index_i ("\\,")) != -1)   // change "\," to TeX's "\c "
321                         {
322                         *(s.ch_l () + i + 1) = 'c';
323                         s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
324                         }
325
326                 char c = s[s.length_i () - 1];
327                 if (c == '{' &&  c == '}') // brace open is for not confusing dumb tools.
328                         here_input ().warning (
329                                 "Brace found at end of lyric. Did you forget a space?");
330                 yylval.scm = ly_str02scm (s.ch_C ());
331
332                 DEBUG_OUT << "lyric : `" << s << "'\n";
333                 return STRING;
334         }
335         . {
336                 return YYText ()[0];
337         }
338 }
339 <chords>{
340         {ALPHAWORD}     {
341                 return scan_bare_word (YYText ());
342         }
343         {NOTECOMMAND}   {
344                 return scan_escaped_word (YYText () + 1);
345         }
346         {UNSIGNED}              {
347                 yylval.i = String_convert::dec2_i (String (YYText ()));
348                 return UNSIGNED;
349         }
350         \" {
351                 start_quote ();
352         }
353         -  {
354                 return CHORD_MINUS;
355         }
356         :  {
357                 return CHORD_COLON;
358         }
359         \/\+ {
360                 return CHORD_BASS;
361         }
362         \^  {
363                 return CHORD_CARET;
364         }
365         . {
366                 return YYText ()[0];
367         }
368 }
369
370 <<EOF>> {
371         DEBUG_OUT << "<<eof>>";
372
373         if (! close_input ()) { 
374           yyterminate (); // can't move this, since it actually rets a YY_NULL
375         }
376 }
377
378
379 {WORD}  {
380         return scan_bare_word (YYText ());
381 }
382 {KEYWORD}       {
383         return scan_escaped_word (YYText () + 1);
384 }
385 {REAL}          {
386         Real r;
387         int cnv=sscanf (YYText (), "%lf", &r);
388         assert (cnv == 1);
389         DEBUG_OUT  << "REAL" << r<<'\n';
390         yylval.real = r;
391         return REAL;
392 }
393
394 {UNSIGNED}      {
395         yylval.i = String_convert::dec2_i (String (YYText ()));
396         return UNSIGNED;
397 }
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     default:
425         return E_CHAR;
426     }
427 }
428
429 <*>.            {
430         String msg = _f ("invalid character: `%c'", YYText ()[0]);
431         LexerError (msg.ch_C ());
432         return YYText ()[0];
433 }
434
435 %%
436
437 void
438 My_lily_lexer::push_note_state ()
439 {
440         yy_push_state (notes);
441 }
442
443 void
444 My_lily_lexer::push_chord_state ()
445 {
446         yy_push_state (chords);
447 }
448
449 void
450 My_lily_lexer::push_lyric_state ()
451 {
452         yy_push_state (lyrics);
453 }
454
455 void
456 My_lily_lexer::pop_state ()
457 {
458         yy_pop_state ();
459 }
460
461 int
462 My_lily_lexer::scan_escaped_word (String str)
463 {
464         // use more SCM for this.
465
466         SCM sym = ly_symbol2scm (str.ch_C());
467
468         int l = lookup_keyword (str);
469         if (l != -1) {
470                 return l;
471         }
472         SCM sid = lookup_identifier (str);
473         if (gh_string_p (sid)) {
474                 yylval.scm = sid; 
475                 return STRING_IDENTIFIER;
476         }
477
478         Identifier * id = unsmob_identifier (sid);
479         if (id) {
480                 yylval.id = id;
481                 return id->token_code_i_;
482         } else if (sid != SCM_UNDEFINED) {
483                 yylval.scm = sid;
484                 return SCM_IDENTIFIER;
485         }
486
487         if ((YYSTATE != notes) && (YYSTATE != chords)) {
488                 SCM pitch = scm_hashq_ref (pitchname_tab_, sym, SCM_BOOL_F);
489                 
490                 if (pitch != SCM_BOOL_F)
491                 {
492                         yylval.pitch = new Musical_pitch (pitch);
493                         yylval.pitch->set_spot (Input (source_file_l (), 
494                           here_ch_C ()));
495                         return NOTENAME_PITCH;
496                 }
497         }
498         String msg (_f ("unknown escaped string: `\\%s'", str));        
499         LexerError (msg.ch_C ());
500
501         yylval.scm = ly_str02scm(str.ch_C());
502
503         return STRING;
504 }
505
506 int
507 My_lily_lexer::scan_bare_word (String str)
508 {
509         SCM sym = ly_symbol2scm (str.ch_C ());
510         if ((YYSTATE == notes) || (YYSTATE == chords)) {
511                 SCM pitch = scm_hashq_ref (pitchname_tab_, sym, SCM_BOOL_F);
512                 if (pitch != SCM_BOOL_F) {
513                     yylval.pitch = new Musical_pitch (pitch);
514                     yylval.pitch->set_spot (Input (source_file_l (), 
515                       here_ch_C ()));
516                     return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
517                 } else if ((pitch = scm_hashq_ref (chordmodifier_tab_, sym, SCM_BOOL_F))!= SCM_BOOL_F)
518                 {
519                     yylval.pitch = new Musical_pitch (pitch);
520                     yylval.pitch->set_spot (Input (source_file_l (), 
521                       here_ch_C ()));
522                     return CHORDMODIFIER_PITCH;
523                 }
524         }
525
526         yylval.scm = ly_str02scm (str.ch_C());
527         return STRING;
528 }
529
530 bool
531 My_lily_lexer::note_state_b () const
532 {
533         return YY_START == notes;
534 }
535
536 bool
537 My_lily_lexer::chord_state_b () const
538 {
539         return YY_START == chords;
540 }
541
542 bool
543 My_lily_lexer::lyric_state_b () const
544 {
545         return YY_START == lyrics;
546 }
547
548 /*
549  urg, belong to String(_convert)
550  and should be generalised 
551  */
552 void
553 strip_leading_white (String&s)
554 {
555         int i=0;
556         for (;  i < s.length_i (); i++) 
557                 if (!isspace (s[i]))
558                         break;
559
560         s = s.nomid_str (0, i);
561 }
562
563 void
564 strip_trailing_white (String&s)
565 {
566         int i=s.length_i ();    
567         while (i--) 
568                 if (!isspace (s[i]))
569                         break;
570
571         s = s.left_str (i+1);
572 }
573
574
575
576
577 bool
578 valid_version_b (String s)
579 {
580   Mudela_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
581   Mudela_version ver (s);
582   if (!((ver >= oldest_version) && (ver <= current)))
583         {       
584                 error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), current.str ()));
585                 if (!version_ignore_global_b)
586                         return false;
587     }
588   return true;
589 }
590