]> git.donarmstrong.com Git - lilypond.git/blob - lily/lexer.ll
release: 1.3.107
[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 "lilypond-input-version.hh"
42 #include "translator-def.hh"
43
44 /*
45 RH 7 fix (?)
46 */
47 #define isatty HORRIBLEKLUDGE
48
49 void strip_trailing_white (String&);
50 void strip_leading_white (String&);
51
52
53 bool
54 valid_version_b (String s);
55
56
57
58 #define start_quote()   \
59         yy_push_state (quote);\
60         yylval.string = new String
61
62 #define yylval (*(YYSTYPE*)lexval_l)
63
64 #define YY_USER_ACTION  add_lexed_char (YYLeng ());
65 /*
66
67 LYRICS          ({AA}|{TEX})[^0-9 \t\n\f]*
68
69 */
70
71 %}
72
73 %option c++
74 %option noyywrap
75 %option nodefault
76 %option debug
77 %option yyclass="My_lily_lexer"
78 %option stack
79 %option never-interactive 
80 %option warn
81
82 %x version
83 %x chords
84 %x incl
85 %x lyrics
86 %x notes
87 %x quote
88 %x longcomment
89
90
91 A               [a-zA-Z]
92 AA              {A}|_
93 N               [0-9]
94 AN              {AA}|{N}
95 PUNCT           [?!:']
96 ACCENT          \\[`'"^]
97 NATIONAL  [\001-\006\021-\027\031\036\200-\377]
98 TEX             {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
99 WORD            {A}{AN}*
100 ALPHAWORD       {A}+
101 DIGIT           {N}
102 UNSIGNED        {N}+
103 INT             -?{UNSIGNED}
104 REAL            ({INT}\.{N}*)|(-?\.{N}+)
105 KEYWORD         \\{WORD}
106 WHITE           [ \n\t\f\r]
107 HORIZONTALWHITE         [ \t]
108 BLACK           [^ \n\t\f\r]
109 RESTNAME        [rs]
110 NOTECOMMAND     \\{A}+
111 LYRICS          ({AA}|{TEX})[^0-9 \t\n\f]*
112 ESCAPED         [nt\\'"]
113 EXTENDER        __
114 HYPHEN          --
115 %%
116
117
118 <*>\r           {
119         // windows-suck-suck-suck
120 }
121
122 <INITIAL,chords,incl,lyrics,notes>{
123   "%{"  {
124         yy_push_state (longcomment);
125   }
126   %[^{\n].*\n   {
127   }
128   %[^{\n]       { // backup rule
129   }
130   %\n   {
131   }
132   %[^{\n].*     {
133   }
134   {WHITE}+      {
135
136   }
137 }
138
139 <INITIAL,chords,lyrics,notes>\\version{WHITE}*  {
140         yy_push_state (version);
141 }
142 <version>\"[^"]*\";?   { /* got the include file name */
143         String s (YYText ()+1);
144         s = s.left_str (s.index_last_i ('"'));
145         DEBUG_OUT << "#version `" << s << "'\n";
146         yy_pop_state ();
147         if (!valid_version_b (s))
148                 return INVALID;
149 }
150 <version>.      {
151         LexerError ("No quoted string found after \\version");
152         yy_pop_state ();
153 }
154 <longcomment>{
155         [^\%]*          {
156         }
157         \%*[^}%]*               {
158
159         }
160         "%"+"}"         {
161                 yy_pop_state ();
162         }
163         <<EOF>>         {
164                 LexerError (_ ("EOF found inside a comment").ch_C ());
165                 if (! close_input ()) 
166                   yyterminate (); // can't move this, since it actually rets a YY_NULL
167         }
168 }
169
170
171 <INITIAL,chords,lyrics,notes>\\maininput           {
172         if (!main_input_b_)
173         {
174                 start_main_input ();
175                 main_input_b_ = true;
176         }
177         else
178                 error (_ ("\\maininput disallowed outside init files"));
179 }
180
181 <INITIAL,chords,lyrics,notes>\\include           {
182         yy_push_state (incl);
183 }
184 <incl>\"[^"]*\";?   { /* got the include file name */
185         String s (YYText ()+1);
186         s = s.left_str (s.index_last_i ('"'));
187         DEBUG_OUT << "#include `" << s << "'\n";
188         new_input (s,source_global_l);
189         yy_pop_state ();
190 }
191 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
192         String s = YYText () + 1;
193         strip_trailing_white (s);
194         if (s.length_i () && (s[s.length_i () - 1] == ';'))
195           s = s.left_str (s.length_i () - 1);
196         DEBUG_OUT << "#include `\\" << s << "'\n";
197         SCM sid = lookup_identifier (s);
198         if (gh_string_p (sid)) {
199                 new_input (ly_scm2string (sid), source_global_l);
200                 yy_pop_state ();
201         } else { 
202             String msg (_f ("wrong or 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                 yylval.scm =  SCM_EOL;
243                 return SCM_T;
244         }
245         yylval.scm = ly_parse_scm (s, &n);
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         DEBUG_OUT << "parens\n";
405         return YYText ()[0];
406 }
407 [*:=]           {
408         char c = YYText ()[0];
409         DEBUG_OUT << "misc char" <<c<<"\n";
410         return c;
411 }
412
413 <INITIAL,notes>.        {
414         return YYText ()[0];
415 }
416
417 <INITIAL,lyrics,notes>\\. {
418     char c= YYText ()[1];
419
420     switch (c) {
421     case '>':
422         return E_BIGGER;
423     case '<':
424         return E_SMALLER;
425     case '!':
426         return E_EXCLAMATION;
427     default:
428         return E_CHAR;
429     }
430 }
431
432 <*>.            {
433         String msg = _f ("invalid character: `%c'", YYText ()[0]);
434         LexerError (msg.ch_C ());
435         return YYText ()[0];
436 }
437
438 %%
439
440 void
441 My_lily_lexer::push_note_state ()
442 {
443         yy_push_state (notes);
444 }
445
446 void
447 My_lily_lexer::push_chord_state ()
448 {
449         yy_push_state (chords);
450 }
451
452 void
453 My_lily_lexer::push_lyric_state ()
454 {
455         yy_push_state (lyrics);
456 }
457
458 void
459 My_lily_lexer::pop_state ()
460 {
461         yy_pop_state ();
462 }
463
464 int
465 My_lily_lexer::scan_escaped_word (String str)
466 {
467         // use more SCM for this.
468
469         SCM sym = ly_symbol2scm (str.ch_C());
470
471         int l = lookup_keyword (str);
472         if (l != -1) {
473                 return l;
474         }
475         SCM sid = lookup_identifier (str);
476         if (gh_string_p (sid)) {
477                 yylval.scm = sid; 
478                 return STRING_IDENTIFIER;
479         } else if (gh_number_p (sid)) {
480                 yylval.scm = sid;
481                 return NUMBER_IDENTIFIER;
482         } else if (unsmob_translator_def (sid)) {
483                 yylval.scm = sid;
484                 return TRANSLATOR_IDENTIFIER;
485         } else if (Music * mus =unsmob_music (sid)) {
486                 yylval.scm = sid;
487                 
488                 return dynamic_cast<Request*> (mus) ? REQUEST_IDENTIFIER : MUSIC_IDENTIFIER;
489         } else if (unsmob_duration (sid)) {
490                 yylval.scm = sid;
491                 return DURATION_IDENTIFIER;
492         }
493
494
495
496
497         Identifier * id = unsmob_identifier (sid);
498         if (id) {
499                 yylval.id = id;
500                 return id->token_code_i_;
501         } else if (sid != SCM_UNDEFINED) {
502                 yylval.scm = sid;
503                 return SCM_IDENTIFIER;
504         }
505
506         if ((YYSTATE != notes) && (YYSTATE != chords)) {
507                 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
508                 
509                 if (gh_pair_p (pitch))
510                 {
511                         yylval.scm = gh_cdr (pitch);
512                         return NOTENAME_PITCH;
513                 }
514         }
515         String msg (_f ("unknown escaped string: `\\%s'", str));        
516         LexerError (msg.ch_C ());
517
518         yylval.scm = ly_str02scm(str.ch_C());
519
520         return STRING;
521 }
522
523 int
524 My_lily_lexer::scan_bare_word (String str)
525 {
526         SCM sym = ly_symbol2scm (str.ch_C ());
527         if ((YYSTATE == notes) || (YYSTATE == chords)) {
528                 SCM pitch = scm_hashq_get_handle (pitchname_tab_, sym);
529                 if (gh_pair_p (pitch)) {
530                     yylval.scm = gh_cdr (pitch);
531                     return (YYSTATE == notes) ? NOTENAME_PITCH : TONICNAME_PITCH;
532                 } else if ((pitch = scm_hashq_get_handle (chordmodifier_tab_, sym))!= SCM_BOOL_F)
533                 {
534                     yylval.scm = gh_cdr (pitch);
535                     return CHORDMODIFIER_PITCH;
536                 }
537         }
538
539         yylval.scm = ly_str02scm (str.ch_C());
540         return STRING;
541 }
542
543 bool
544 My_lily_lexer::note_state_b () const
545 {
546         return YY_START == notes;
547 }
548
549 bool
550 My_lily_lexer::chord_state_b () const
551 {
552         return YY_START == chords;
553 }
554
555 bool
556 My_lily_lexer::lyric_state_b () const
557 {
558         return YY_START == lyrics;
559 }
560
561 /*
562  urg, belong to String(_convert)
563  and should be generalised 
564  */
565 void
566 strip_leading_white (String&s)
567 {
568         int i=0;
569         for (;  i < s.length_i (); i++) 
570                 if (!isspace (s[i]))
571                         break;
572
573         s = s.nomid_str (0, i);
574 }
575
576 void
577 strip_trailing_white (String&s)
578 {
579         int i=s.length_i ();    
580         while (i--) 
581                 if (!isspace (s[i]))
582                         break;
583
584         s = s.left_str (i+1);
585 }
586
587
588
589
590 bool
591 valid_version_b (String s)
592 {
593   Lilypond_version current ( MAJOR_VERSION "." MINOR_VERSION "." PATCH_LEVEL );
594   Lilypond_version ver (s);
595   if (!((ver >= oldest_version) && (ver <= current)))
596         {       
597                 non_fatal_error (_f ("incorrect lilypond version: %s (%s, %s)", ver.str (), oldest_version.str (), current.str ()));
598                 non_fatal_error (_("Consider converting the input with the convert-ly script")); 
599                 return false;
600     }
601   return true;
602 }
603