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