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