]> git.donarmstrong.com Git - lilypond.git/blob - lily/lexer.ll
patch::: 1.1.5.jcn2: extender
[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 LYRICS          ({AA}|{TEX})[^0-9 \t\n\f]*
93 ESCAPED         [nt\\'"]
94 PLET            \\\[
95 TELP            \\\]
96 EXTENDER        [_][_]
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_l = id->access_content_String (false);
163             DOUT << "#include `" << *s_l << "\'\n";
164             new_input (*s_l, source_global_l);
165
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                 if (s == "__")
267                         return yylval.i = EXTENDER;
268                 int i = 0;
269                 while ((i=s.index_i ("_")) != -1) // change word binding "_" to " "
270                         *(s.ch_l () + i) = ' ';
271                 if ((i=s.index_i ("\\,")) != -1)   // change "\," to TeX's "\c "
272                         {
273                         *(s.ch_l () + i + 1) = 'c';
274                         s = s.left_str (i+2) + " " + s.right_str (s.length_i ()-i-2);
275                         }
276                 yylval.string = new String (s);
277                 DOUT << "lyric : `" << s << "'\n";
278                 return STRING;
279         }
280         . {
281                 return yylval.c = YYText ()[0];
282         }
283 }
284
285 <<EOF>> {
286         DOUT << "<<eof>>";
287
288         if (! close_input ()) { 
289           yyterminate (); // can't move this, since it actually rets a YY_NULL
290         }
291 }
292
293
294 {WORD}  {
295         return scan_bare_word (YYText ());
296 }
297 {KEYWORD}       {
298         return scan_escaped_word (YYText () + 1);
299 }
300 {REAL}          {
301         Real r;
302         int cnv=sscanf (YYText (), "%lf", &r);
303         assert (cnv == 1);
304         DOUT  << "REAL" << r<<'\n';
305         yylval.real = r;
306         return REAL;
307 }
308
309 {UNSIGNED}      {
310         yylval.i = String_convert::dec2_i (String (YYText ()));
311         return UNSIGNED;
312 }
313
314 [{}]    {
315
316         DOUT << "parens\n";
317         return YYText ()[0];
318 }
319 [*:=]           {
320         char c = YYText ()[0];
321         DOUT << "misc char" <<c<<"\n";
322         return c;
323 }
324
325 <lyrics,notes>{PLET}    {
326         return yylval.i = PLET;
327 }
328
329 <lyrics,notes>{TELP}    {
330         return yylval.i = TELP;
331 }
332
333 <INITIAL,notes>.        {
334         return yylval.c = YYText ()[0];
335 }
336
337 <INITIAL,lyrics,notes>\\. {
338     char c= YYText ()[1];
339     yylval.c = c;
340     switch (c) {
341     case '>':
342         return E_BIGGER;
343     case '<':
344         return E_SMALLER;
345     case '!':
346         return E_EXCLAMATION;
347     default:
348         return E_CHAR;
349     }
350 }
351
352 <*>.            {
353         String msg = _f ("illegal character: `%c\'", YYText ()[0]);
354         LexerError (msg.ch_C ());
355         return YYText ()[0];
356 }
357
358 %%
359
360 void
361 My_lily_lexer::push_note_state ()
362 {
363         yy_push_state (notes);
364 }
365
366 void
367 My_lily_lexer::push_lyric_state ()
368 {
369         yy_push_state (lyrics);
370 }
371 void
372 My_lily_lexer::pop_state ()
373 {
374         yy_pop_state ();
375 }
376
377 int
378 My_lily_lexer::scan_escaped_word (String str)
379 {       
380         DOUT << "\\word: `" << str<<"'\n";
381         int l = lookup_keyword (str);
382         if (l != -1) {
383                 DOUT << "(keyword)\n";
384                 return l;
385         }
386         Identifier * id = lookup_identifier (str);
387         if (id) {
388                 DOUT << "(identifier)\n";
389                 yylval.id = id;
390                 return id->token_code_i_;
391         }
392         if (YYSTATE != notes) {
393                 if (notename_b (str))
394                         {
395                         yylval.pitch = new Musical_pitch (lookup_pitch (str));
396                         yylval.pitch->set_spot (Input (source_file_l (), 
397                           here_ch_C ()));
398                         return NOTENAME_PITCH;
399                         }
400         }
401         if (check_debug)
402                 print_declarations (true);
403         String msg (_f ("unknown escaped string: `\\%s\'", str));       
404         LexerError (msg.ch_C ());
405         DOUT << "(string)";
406         String *sp = new String (str);
407         yylval.string=sp;
408         return STRING;
409 }
410
411 int
412 My_lily_lexer::scan_bare_word (String str)
413 {
414         DOUT << "word: `" << str<< "'\n";       
415         if (YYSTATE == notes){
416                 if (notename_b (str)) {
417                     DOUT << "(notename)\n";
418                     yylval.pitch = new Musical_pitch (lookup_pitch (str));
419                     yylval.pitch->set_spot (Input (source_file_l (), 
420                       here_ch_C ()));
421                     return NOTENAME_PITCH;
422                 }
423         }
424
425         yylval.string=new String (str);
426         return STRING;
427 }
428
429 bool
430 My_lily_lexer::note_state_b () const
431 {
432         return YY_START == notes;
433 }
434
435 bool
436 My_lily_lexer::lyric_state_b () const
437 {
438         return YY_START == lyrics;
439 }
440
441 /*
442  urg, belong to String(_convert)
443  and should be generalised 
444  */
445 void
446 strip_leading_white (String&s)
447 {
448         int i=0;
449         for (;  i < s.length_i (); i++) 
450                 if (!isspace (s[i]))
451                         break;
452
453         s = s.nomid_str (0, i);
454 }
455
456 void
457 strip_trailing_white (String&s)
458 {
459         int i=s.length_i ();    
460         while (i--) 
461                 if (!isspace (s[i]))
462                         break;
463
464         s = s.left_str (i+1);
465 }
466