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