]> git.donarmstrong.com Git - lilypond.git/blob - lily/lexer.ll
patch::: 1.3.44.jcn1
[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         Identifier * id = lookup_identifier (s);
192         if (id) 
193           {
194             String* s_l = id->access_content_String (false);
195             DEBUG_OUT << "#include `" << *s_l << "'\n";
196             new_input (*s_l, source_global_l);
197
198             yy_pop_state ();
199           }
200         else
201           {
202             String msg (_f ("undefined identifier: `%s'", s )); 
203             LexerError (msg.ch_C ());
204           }
205 }
206 <incl>\"[^"]*   { // backup rule
207         cerr << _ ("Missing end quote") << endl;
208         exit (1);
209 }
210 <chords,notes>{RESTNAME}        {
211         const char *s = YYText ();
212         yylval.scm = ly_str02scm (s);
213         return RESTNAME;
214 }
215 <chords,notes>R         {
216         return MEASURES;
217 }
218 <INITIAL,chords,lyrics,notes>\\\${BLACK}*{WHITE}        {
219         String s=YYText () + 2;
220         s=s.left_str (s.length_i () - 1);
221         return scan_escaped_word (s); 
222 }
223 <INITIAL,chords,lyrics,notes>\${BLACK}*{WHITE}          {
224         String s=YYText () + 1;
225         s=s.left_str (s.length_i () - 1);
226         return scan_bare_word (s);
227 }
228 <INITIAL,chords,lyrics,notes>\\\${BLACK}*               { // backup rule
229         cerr << _ ("white expected") << endl;
230         exit (1);
231 }
232 <INITIAL,chords,lyrics,notes>\${BLACK}*         { // backup rule
233         cerr << _ ("white expected") << endl;
234         exit (1);
235 }
236 <INITIAL,chords,lyrics,notes>#  { //embedded scm
237         //char const* s = YYText () + 1;
238         char const* s = here_ch_C ();
239         int n = 0;
240         if (main_input_b_ && safe_global_b) {
241                 error (_ ("Can't evaluate Scheme in safe mode"));
242                 return SCM_EOL;
243         }
244         yylval.scm = ly_parse_scm (s, &n);
245         DEBUG_OUT << "Scheme: ";
246         if (flower_dstream)
247                 ly_display_scm (yylval.scm);
248         
249         for (int i=0; i < n; i++)
250         {
251                 yyinput ();
252         }
253         char_count_stack_.top () += n;
254
255         return SCM_T;
256 }
257 <notes>{
258         {ALPHAWORD}     {
259                 return scan_bare_word (YYText ());
260         }
261
262         {NOTECOMMAND}   {
263                 return scan_escaped_word (YYText () + 1); 
264         }
265
266         {DIGIT}         {
267                 yylval.i = String_convert::dec2_i (String (YYText ()));
268                 return DIGIT;
269         }
270         {UNSIGNED}              {
271                 yylval.i = String_convert::dec2_i (String (YYText ()));
272                 return UNSIGNED;
273         }
274
275         \" {
276                 start_quote ();
277         }
278 }
279
280 \"              {
281         start_quote ();
282 }
283 <quote>{
284         \\{ESCAPED}     {
285                 *yylval.string += to_str (escaped_char(YYText()[1]));
286         }
287         [^\\"]+ {
288                 *yylval.string += YYText ();
289         }
290         \"      {
291                 DEBUG_OUT << "quoted string: `" << *yylval.string << "'\n";
292                 yy_pop_state ();
293
294                 /* yylval is union. Must remember STRING before setting SCM*/
295                 String *sp = yylval.string;
296                 yylval.scm = ly_str02scm  (sp->ch_C ());
297                 delete sp;
298                 return STRING;
299         }
300         .       {
301                 *yylval.string += YYText ();
302         }
303 }
304
305 <lyrics>{
306         \" {
307                 start_quote ();
308         }
309         {UNSIGNED}              {
310                 yylval.i = String_convert::dec2_i (String (YYText ()));
311                 return UNSIGNED;
312         }
313         {NOTECOMMAND}   {
314                 return scan_escaped_word (YYText () + 1);
315         }
316         {LYRICS} {
317                 /* ugr. This sux. */
318                 String s (YYText ()); 
319                 if (s == "__")
320                         return yylval.i = EXTENDER;
321                 if (s == "--")
322                         return yylval.i = HYPHEN;
323                 int i = 0;
324                 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
325                         *(s.ch_l () + i) = ' ';
326                 if ((i=s.index_i ("\\,")) != -1)   // change "\," to TeX's "\c "
327                         {
328                         *(s.ch_l () + i + 1) = 'c';
329                         s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
330                         }
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                 DEBUG_OUT << "lyric : `" << s << "'\n";
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         DEBUG_OUT << "<<eof>>";
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         DEBUG_OUT  << "REAL" << r<<'\n';
396         yylval.real = 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         DEBUG_OUT << "parens\n";
408         return YYText ()[0];
409 }
410 [*:=]           {
411         char c = YYText ()[0];
412         DEBUG_OUT << "misc char" <<c<<"\n";
413         return c;
414 }
415
416 <INITIAL,notes>.        {
417         return YYText ()[0];
418 }
419
420 <INITIAL,lyrics,notes>\\. {
421     char c= YYText ()[1];
422
423     switch (c) {
424     case '>':
425         return E_BIGGER;
426     case '<':
427         return E_SMALLER;
428     case '!':
429         return E_EXCLAMATION;
430     default:
431         return E_CHAR;
432     }
433 }
434
435 <*>.            {
436         String msg = _f ("invalid character: `%c'", YYText ()[0]);
437         LexerError (msg.ch_C ());
438         return YYText ()[0];
439 }
440
441 %%
442
443 void
444 My_lily_lexer::push_note_state ()
445 {
446         yy_push_state (notes);
447 }
448
449 void
450 My_lily_lexer::push_chord_state ()
451 {
452         yy_push_state (chords);
453 }
454
455 void
456 My_lily_lexer::push_lyric_state ()
457 {
458         yy_push_state (lyrics);
459 }
460
461 void
462 My_lily_lexer::pop_state ()
463 {
464         yy_pop_state ();
465 }
466
467 int
468 My_lily_lexer::scan_escaped_word (String str)
469 {
470         // use more SCM for this.
471
472         SCM sym = ly_symbol2scm (str.ch_C());
473
474         int l = lookup_keyword (str);
475         if (l != -1) {
476                 return l;
477         }
478         Identifier * id = lookup_identifier (str);
479         if (id) {
480                 yylval.id = id;
481                 return id->token_code_i_;
482         }
483         if ((YYSTATE != notes) && (YYSTATE != chords)) {
484                 SCM pitch = scm_hashq_ref (pitchname_tab_, sym, SCM_BOOL_F);
485                 
486                 if (pitch != SCM_BOOL_F)
487                 {
488                         yylval.pitch = new Musical_pitch (pitch);
489                         yylval.pitch->set_spot (Input (source_file_l (), 
490                           here_ch_C ()));
491                         return NOTENAME_PITCH;
492                 }
493         }
494         String msg (_f ("unknown escaped string: `\\%s'", str));        
495         LexerError (msg.ch_C ());
496
497         yylval.scm = ly_str02scm(str.ch_C());
498
499         return STRING;
500 }
501
502 int
503 My_lily_lexer::scan_bare_word (String str)
504 {
505         SCM sym = ly_symbol2scm (str.ch_C ());
506         if ((YYSTATE == notes) || (YYSTATE == chords)) {
507                 SCM pitch = scm_hashq_ref (pitchname_tab_, sym, SCM_BOOL_F);
508                 if (pitch != SCM_BOOL_F) {
509                     yylval.pitch = new Musical_pitch (pitch);
510                     yylval.pitch->set_spot (Input (source_file_l (), 
511                       here_ch_C ()));
512                     return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
513                 } else if ((pitch = scm_hashq_ref (chordmodifier_tab_, sym, SCM_BOOL_F))!= SCM_BOOL_F)
514                 {
515                     yylval.pitch = new Musical_pitch (pitch);
516                     yylval.pitch->set_spot (Input (source_file_l (), 
517                       here_ch_C ()));
518                     return CHORDMODIFIER_PITCH;
519                 }
520         }
521
522         yylval.scm = ly_str02scm (str.ch_C());
523         return STRING;
524 }
525
526 bool
527 My_lily_lexer::note_state_b () const
528 {
529         return YY_START == notes;
530 }
531
532 bool
533 My_lily_lexer::chord_state_b () const
534 {
535         return YY_START == chords;
536 }
537
538 bool
539 My_lily_lexer::lyric_state_b () const
540 {
541         return YY_START == lyrics;
542 }
543
544 /*
545  urg, belong to String(_convert)
546  and should be generalised 
547  */
548 void
549 strip_leading_white (String&s)
550 {
551         int i=0;
552         for (;  i < s.length_i (); i++) 
553                 if (!isspace (s[i]))
554                         break;
555
556         s = s.nomid_str (0, i);
557 }
558
559 void
560 strip_trailing_white (String&s)
561 {
562         int i=s.length_i ();    
563         while (i--) 
564                 if (!isspace (s[i]))
565                         break;
566
567         s = s.left_str (i+1);
568 }
569
570
571
572
573 bool
574 valid_version_b (String s)
575 {
576   Mudela_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
577   Mudela_version ver (s);
578   if (!((ver >= oldest_version) && (ver <= current)))
579         {       
580                 error (_f ("incorrect mudela version: %s (%s, %s)", ver.str (), oldest_version.str (), current.str ()));
581                 if (!version_ignore_global_b)
582                         return false;
583     }
584   return true;
585 }
586