]> git.donarmstrong.com Git - lilypond.git/blob - lily/lexer.ll
release: 1.0.8
[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 "parseconstruct.hh"
35 #include "main.hh"
36 #include "musical-request.hh"
37 #include "identifier.hh"
38 void strip_trailing_white (String&);
39 void strip_leading_white (String&);
40
41 #define start_quote()   \
42         yy_push_state (quote);\
43         yylval.string = new String
44
45 #define yylval (*(YYSTYPE*)lexval_l)
46
47 #define YY_USER_ACTION  add_lexed_char (YYLeng ());
48 /*
49
50 LYRICS          ({AA}|{TEX})[^0-9 \t\n\f]*
51
52 */
53
54 %}
55
56 %option c++
57 %option noyywrap
58 %option nodefault
59 %option debug
60 %option yyclass="My_lily_lexer"
61 %option stack
62 %option never-interactive 
63 %option warn
64
65 %x incl
66 %x lyrics
67 %x notes
68 %x quote
69 %x longcomment
70
71
72 A               [a-zA-Z]
73 AA              {A}|_
74 N               [0-9]
75 AN              {AA}|{N}
76 PUNCT           [?!:']
77 ACCENT          \\[`'"^]
78 NATIONAL  [\001-\006\021-\027\031\036\200-\377]
79 TEX             {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
80 WORD            {A}{AN}*
81 ALPHAWORD       {A}+
82 DIGIT           {N}
83 UNSIGNED        {N}+
84 INT             -?{UNSIGNED}
85 REAL            ({INT}\.{N}*)|(-?\.{N}+)
86 KEYWORD         \\{WORD}
87 WHITE           [ \n\t\f\r]
88 HORIZONTALWHITE         [ \t]
89 BLACK           [^ \n\t\f\r]
90 RESTNAME        [rs]
91 NOTECOMMAND     \\{A}+
92 ANDREWLYRICS            ([^ \n\t\f\\{}<>0-9";.|]|\\[^a-zA-Z\]\[<>])[^0-9 \t\n\f]
93 LYRICS          ({AA}|{TEX})[^0-9 \t\n\f]*
94 ESCAPED         [nt\\'"]
95 PLET            \\\[
96 TELP            \\\]
97
98 %%
99
100
101 <*>\r           {
102         // windows-suck-suck-suck
103 }
104
105 <notes,incl,INITIAL,lyrics>{
106   "%{"  {
107         yy_push_state (longcomment);
108   }
109   %[^{\n].*\n   {
110   }
111   %[^{\n]       { // backup rule
112   }
113   %\n   {
114   }
115   %[^{\n].*     {
116   }
117   {WHITE}+      {
118
119   }
120 }
121
122 <longcomment>{
123         [^\%]*          {
124         }
125         \%*[^}%]*               {
126
127         }
128         "%"+"}"         {
129                 yy_pop_state ();
130         }
131         <<EOF>>         {
132                 LexerError (_ ("EOF found inside a comment").ch_C ());
133                 if (! close_input ()) 
134                   yyterminate (); // can't move this, since it actually rets a YY_NULL
135         }
136 }
137
138
139 <notes,INITIAL,lyrics>\\maininput           {
140         start_main_input ();
141 }
142
143 <notes,INITIAL,lyrics>\\include           {
144         yy_push_state (incl);
145 }
146 <incl>\"[^"]*\";?   { /* got the include file name */
147         String s (YYText ()+1);
148         s = s.left_str (s.index_last_i ('"'));
149         DOUT << "#include `" << s << "\'\n";
150         new_input (s,source_global_l);
151         yy_pop_state ();
152 }
153 <incl>\\{BLACK}*;?{WHITE} { /* got the include identifier */
154         String s = YYText () + 1;
155         strip_trailing_white (s);
156         if (s.length_i () && (s[s.length_i () - 1] == ';'))
157           s = s.left_str (s.length_i () - 1);
158         DOUT << "#include `\\" << s << "'\n";
159         Identifier * id = lookup_identifier (s);
160         if (id) 
161           {
162             String* s_p = id->access_String ();
163             DOUT << "#include `" << *s_p << "\'\n";
164             new_input (*s_p, source_global_l);
165             delete s_p;
166             yy_pop_state ();
167           }
168         else
169           {
170             String msg (_f ("undefined identifier: `%s\'", s ));        
171             LexerError (msg.ch_C ());
172           }
173 }
174 <incl>\"[^"]*   { // backup rule
175         cerr << _ ("missing end quote") << endl;
176         exit (1);
177 }
178 <notes>{RESTNAME}       {
179         const char *s = YYText ();
180         yylval.string = new String (s); 
181         DOUT << "rest:"<< yylval.string;
182         return RESTNAME;
183 }
184 <notes>R                {
185         return MEASURES;
186 }
187 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE}       {
188         String s=YYText () + 2;
189         s=s.left_str (s.length_i () - 1);
190         return scan_escaped_word (s); 
191 }
192 <INITIAL,lyrics,notes>\${BLACK}*{WHITE}         {
193         String s=YYText () + 1;
194         s=s.left_str (s.length_i () - 1);
195         return scan_bare_word (s);
196 }
197 <INITIAL,lyrics,notes>\\\${BLACK}*              { // backup rule
198         cerr << _ ("white expected") << endl;
199         exit (1);
200 }
201 <INITIAL,lyrics,notes>\${BLACK}*                { // backup rule
202         cerr << _ ("white expected") << endl;
203         exit (1);
204 }
205 <notes>{
206
207         {ALPHAWORD}     {
208                 return scan_bare_word (YYText ());
209
210         }
211
212         {NOTECOMMAND}   {
213                 return scan_escaped_word (YYText () + 1); 
214         }
215
216         {DIGIT}         {
217                 yylval.i = String_convert::dec2_i (String (YYText ()));
218                 return DIGIT;
219         }
220
221         {UNSIGNED}              {
222                 yylval.i = String_convert::dec2_i (String (YYText ()));
223                 return UNSIGNED;
224         }
225
226         \" {
227                 start_quote ();
228         }
229 }
230
231 \"              {
232         start_quote ();
233 }
234 <quote>{
235         \\{ESCAPED}     {
236                 *yylval.string += to_str (escaped_char(YYText()[1]));
237         }
238         [^\\"]+ {
239                 *yylval.string += YYText ();
240         }
241         \"      {
242                 DOUT << "quoted string: `" << *yylval.string << "'\n";
243                 yy_pop_state ();
244                 return STRING;
245         }
246         .       {
247                 *yylval.string += YYText ();
248         }
249 }
250
251 <lyrics>{
252
253         \" {
254                 start_quote ();
255         }
256         {UNSIGNED}              {
257                 yylval.i = String_convert::dec2_i (String (YYText ()));
258                 return UNSIGNED;
259         }
260         {NOTECOMMAND}   {
261                 return scan_escaped_word (YYText () + 1);
262         }
263         {LYRICS} {
264                 /* ugr. This sux. */
265                 String s (YYText ()); 
266                 int i = 0;
267                 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
268                         *(s.ch_l () + i) = ' ';
269                 if ((i=s.index_i ("\\,")) != -1)   // change "\," to TeX's "\c "
270                         {
271                         *(s.ch_l () + i + 1) = 'c';
272                         s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
273                         }
274                 yylval.string = new String (s);
275                 DOUT << "lyric : `" << s << "'\n";
276                 return STRING;
277         }
278         . {
279                 return yylval.c = YYText ()[0];
280         }
281 }
282
283 <<EOF>> {
284         DOUT << "<<eof>>";
285
286         if (! close_input ()) { 
287           yyterminate (); // can't move this, since it actually rets a YY_NULL
288         }
289 }
290 {WORD}  {
291         return scan_bare_word (YYText ());
292 }
293 {KEYWORD}       {
294         return scan_escaped_word (YYText () + 1);
295 }
296 {REAL}          {
297         Real r;
298         int cnv=sscanf (YYText (), "%lf", &r);
299         assert (cnv == 1);
300         DOUT  << "REAL" << r<<'\n';
301         yylval.real = r;
302         return REAL;
303 }
304
305 {UNSIGNED}      {
306         yylval.i = String_convert::dec2_i (String (YYText ()));
307         return UNSIGNED;
308 }
309
310 [{}]    {
311
312         DOUT << "parens\n";
313         return YYText ()[0];
314 }
315 [*:=]           {
316         char c = YYText ()[0];
317         DOUT << "misc char" <<c<<"\n";
318         return c;
319 }
320
321 <lyrics,notes>{PLET}    {
322         return yylval.i = PLET;
323 }
324
325 <lyrics,notes>{TELP}    {
326         return yylval.i = TELP;
327 }
328
329 <INITIAL,notes>.        {
330         return yylval.c = YYText ()[0];
331 }
332
333 <INITIAL,lyrics,notes>\\. {
334     char c= YYText ()[1];
335     yylval.c = c;
336     switch (c) {
337     case '>':
338         return E_BIGGER;
339     case '<':
340         return E_SMALLER;
341     case '!':
342         return E_EXCLAMATION;
343     default:
344         return E_CHAR;
345     }
346 }
347
348 <*>.            {
349         String msg = _f ("illegal character: `%c\'", YYText ()[0]);
350         LexerError (msg.ch_C ());
351         return YYText ()[0];
352 }
353
354 %%
355
356 void
357 My_lily_lexer::push_note_state ()
358 {
359         yy_push_state (notes);
360 }
361
362 void
363 My_lily_lexer::push_lyric_state ()
364 {
365         yy_push_state (lyrics);
366 }
367 void
368 My_lily_lexer::pop_state ()
369 {
370         yy_pop_state ();
371 }
372
373 int
374 My_lily_lexer::scan_escaped_word (String str)
375 {       
376         DOUT << "\\word: `" << str<<"'\n";
377         int l = lookup_keyword (str);
378         if (l != -1) {
379                 DOUT << "(keyword)\n";
380                 return l;
381         }
382         Identifier * id = lookup_identifier (str);
383         if (id) {
384                 DOUT << "(identifier)\n";
385                 yylval.id = id;
386                 return id->token_code_i_;
387         }
388         if (YYSTATE != notes) {
389                 if (notename_b (str))
390                         {
391                         yylval.pitch = new Musical_pitch (lookup_pitch (str));
392                         yylval.pitch->set_spot (Input (source_file_l (), 
393                           here_ch_C ()));
394                         return NOTENAME_PITCH;
395                         }
396         }
397         if (check_debug)
398                 print_declarations (true);
399         String msg (_f ("unknown escaped string: `\\%s\'", str));       
400         LexerError (msg.ch_C ());
401         DOUT << "(string)";
402         String *sp = new String (str);
403         yylval.string=sp;
404         return STRING;
405 }
406
407 int
408 My_lily_lexer::scan_bare_word (String str)
409 {
410         DOUT << "word: `" << str<< "'\n";       
411         if (YYSTATE == notes){
412                 if (notename_b (str)) {
413                     DOUT << "(notename)\n";
414                     yylval.pitch = new Musical_pitch (lookup_pitch (str));
415                     yylval.pitch->set_spot (Input (source_file_l (), 
416                       here_ch_C ()));
417                     return NOTENAME_PITCH;
418                 }
419         }
420
421         yylval.string=new String (str);
422         return STRING;
423 }
424
425 bool
426 My_lily_lexer::note_state_b () const
427 {
428         return YY_START == notes;
429 }
430
431 bool
432 My_lily_lexer::lyric_state_b () const
433 {
434         return YY_START == lyrics;
435 }
436
437 /*
438  urg, belong to String(_convert)
439  and should be generalised 
440  */
441 void
442 strip_leading_white (String&s)
443 {
444         int i=0;
445         for (;  i < s.length_i (); i++) 
446                 if (!isspace (s[i]))
447                         break;
448
449         s = s.nomid_str (0, i);
450 }
451
452 void
453 strip_trailing_white (String&s)
454 {
455         int i=s.length_i ();    
456         while (i--) 
457                 if (!isspace (s[i]))
458                         break;
459
460         s = s.left_str (i+1);
461 }
462