]> git.donarmstrong.com Git - lilypond.git/blob - lily/lexer.l
release: 0.1.7
[lilypond.git] / lily / lexer.l
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@stack.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
26 #include "string.hh"
27 #include "string-convert.hh"
28 #include "my-lily-lexer.hh"
29 #include "varray.hh"
30 #include "parser.hh"
31 #include "debug.hh"
32 #include "parseconstruct.hh"
33 #include "main.hh"
34 #include "identifier.hh"
35
36 #define start_quote()   \
37         yy_push_state(quote);\
38         yylval.string = new String
39
40 #define yylval (*(YYSTYPE*)lexval_l)
41
42 #define YY_USER_ACTION  add_lexed_char(YYLeng());
43 %}
44
45 %option c++
46 %option noyywrap
47 %option nodefault
48 %option debug
49 %option yyclass="My_lily_lexer"
50 %option stack
51 %option never-interactive 
52 %option warn
53
54 %x incl
55 %x lyrics
56 %x notes
57 %x quote
58 %x longcomment
59
60
61 A               [a-zA-Z]
62 AA              {A}|_
63 N               [0-9]
64 AN              {AA}|{N}
65 PUNCT           [?!,.:;']
66 ACCENT          \\[`'"^]
67 NATIONAL        [\241-\377]
68 TEX             {AA}|-|{PUNCT}|{ACCENT}|{NATIONAL}
69
70 WORD            {A}{AN}*
71 ALPHAWORD       {A}+
72 INT             -?{N}+
73 REAL            ({INT}\.{N}*)|(-?\.{N}+)
74 KEYWORD         \\{WORD}
75 WHITE           [ \n\t\f]
76 BLACK           [^ \n\t\f]
77 RESTNAME        [rs]
78 NOTECOMMAND     \\{A}+
79 LYRICS          ({AA}|{NATIONAL})[^0-9 \t\n\f]*
80
81 %%
82
83
84 <notes,incl,INITIAL,lyrics>{
85   "%{"  {
86         yy_push_state(longcomment);
87   }
88   %[^{\n].*\n   {
89   }
90   %[^{\n]       { // backup rule
91   }
92   %\n   {
93   }
94   %[^{\n].*     {
95   }
96   {WHITE}+      {
97         
98   }
99 }
100
101 <longcomment>{
102         [^\%]*          {
103         }
104         \%*[^}%]*               {
105
106         }
107         "%"+"}"         {
108                 yy_pop_state();
109         }
110 }
111 <longcomment><<EOF>> {
112         LexerError("EOF found inside a comment");
113         if (! close_input()) { 
114           yyterminate(); // can't move this, since it actually rets a YY_NULL
115         }
116 }
117 <notes,INITIAL,lyrics>\\include           {
118         yy_push_state(incl);
119 }
120 <incl>\"[^"]*\"   { /* got the include file name */
121         String s (YYText()+1);
122         s = s.left_str(s.length_i()-1);
123         mtor << "#include `" << s << "\'\n";
124         new_input(s,source_l_g);
125         yy_pop_state();
126 }
127 <incl>\"[^"]*   { // backup rule
128         cerr << "missing end quote" << endl;
129         exit( 1 );
130 }
131 <notes>{RESTNAME}       {
132         const char *s = YYText();
133         yylval.string = new String (s); 
134         mtor << "rest:"<< yylval.string;
135         return RESTNAME;
136 }
137 <INITIAL,lyrics,notes>\\\${BLACK}*{WHITE}       {
138         String s=YYText() + 2;
139         s=s.left_str(s.length_i() - 1);
140         return scan_escaped_word(s);
141 }
142 <INITIAL,lyrics,notes>\${BLACK}*{WHITE}         {
143         String s=YYText() + 1;
144         s=s.left_str(s.length_i() - 1);
145         return scan_bare_word(s);
146 }
147 <INITIAL,lyrics,notes>\\\${BLACK}*              { // backup rule
148         cerr << "white expected" << endl;
149         exit( 1 );
150 }
151 <INITIAL,lyrics,notes>\${BLACK}*                { // backup rule
152         cerr << "white expected" << endl;
153         exit( 1 );
154 }
155 <notes>{
156         {ALPHAWORD}/\'  {
157                 post_quotes_b_ = true;
158                 return scan_bare_word(YYText());
159         }
160         \'+             {
161                 yylval.i = YYLeng();
162                 if (post_quotes_b_) {
163                         post_quotes_b_ = false;
164                         return POST_QUOTES;
165                 } else
166                         return PRE_QUOTES;
167         }
168         {ALPHAWORD}     {
169                 return scan_bare_word(YYText());
170
171         }
172
173         {NOTECOMMAND}   {
174                 return scan_escaped_word(YYText()+1);
175         }
176
177         {INT}           {
178                 yylval.i = String_convert::dec2_i( String( YYText() ) );
179                 return INT;
180         }
181
182         \" {
183                 start_quote();
184         }
185 }
186
187 \"              {
188         start_quote();
189 }
190 <quote>{
191         \\\\    {
192                 *yylval.string += '\\';
193         }
194         \\\"    {
195                 *yylval.string +='\"';
196         }
197         [^"]+   {
198                 *yylval.string += YYText();
199         }
200         \"      {
201                 mtor << "quoted string: `" << *yylval.string << "'\n";
202                 yy_pop_state();
203                 return STRING;
204         }
205 }
206
207 <lyrics>{
208
209         \" {
210                 start_quote();
211         }
212         {INT}           {
213                 yylval.i = String_convert::dec2_i( String( YYText() ) );
214                 return INT;
215         }
216         {NOTECOMMAND}   {
217                 return scan_escaped_word(YYText()+1);
218         }
219         {LYRICS} {
220                 /* ugr. This sux. */
221                 String s (YYText()); 
222                 int i = 0;
223                 while ((i=s.index_i("_")) != -1) // change word binding "_" to " "
224                         *(s.ch_l() + i) = ' ';
225                 if ((i=s.index_i("\\,")) != -1)   // change "\," to TeX's "\c "
226                         {
227                         *(s.ch_l() + i + 1) = 'c';
228                         s = s.left_str(i+2) + " " + s.right_str(s.length_i()-i-2);
229                         }
230                 yylval.string = new String(s);
231                 mtor << "lyric : `" << s << "'\n";
232                 return STRING;
233         }
234         . {
235                 return yylval.c = YYText()[0];
236         }
237 }
238
239 <<EOF>> {
240         mtor << "<<eof>>";
241
242         if (! close_input()) { 
243           yyterminate(); // can't move this, since it actually rets a YY_NULL
244         }
245 }
246 {WORD}  {
247         return scan_bare_word(YYText());
248 }
249 {KEYWORD}       {
250         return scan_escaped_word(YYText()+1);
251 }
252 {REAL}          {
253         Real r;
254         int cnv=sscanf (YYText(), "%lf", &r);
255         assert(cnv == 1);
256         mtor  << "REAL" << r<<'\n';
257         yylval.real = r;
258         return REAL;
259 }
260
261 {INT}   {
262         yylval.i = String_convert::dec2_i( String( YYText() ) );
263         return INT;
264 }
265
266 [{}]    {
267
268         mtor << "parens\n";
269         return YYText()[0];
270 }
271 [*:=]           {
272         char c = YYText()[0];
273         mtor << "misc char" <<c<<"\n";
274         return c;
275 }
276
277 <INITIAL,notes>.        {
278         return yylval.c = YYText()[0];
279 }
280
281 <INITIAL,lyrics,notes>\\. {
282     char c= YYText()[1];
283     yylval.c = c;
284     switch (c) {
285     case '>':
286         return E_BIGGER;
287     case '<':
288         return E_SMALLER;
289     case '!':
290         return E_EXCLAMATION;
291     default:
292         return E_CHAR;
293     }
294 }
295
296 <*>.            {
297         LexerError( String( "illegal character: " ) +String( YYText()[0] ));
298         return YYText()[0];
299 }
300
301 %%
302
303 void
304 My_lily_lexer::push_note_state()
305 {
306         yy_push_state(notes);
307 }
308
309 void
310 My_lily_lexer::push_lyric_state()
311 {
312         yy_push_state(lyrics);
313 }
314 void
315 My_lily_lexer::pop_state()
316 {
317         yy_pop_state();
318 }
319
320 int
321 My_lily_lexer::scan_escaped_word(String str)
322 {       
323         mtor << "\\word: `" << str<<"'\n";
324         int l = lookup_keyword(str);
325         if (l != -1) {
326                 mtor << "(keyword)\n";
327                 return l;
328         }
329         Identifier * id = lookup_identifier(str);
330         if (id) {
331                 mtor << "(identifier)\n";
332                 yylval.id = id;
333                 return id->token_code_i_;
334         }
335         if ( YYSTATE != notes ) {
336                 Melodic_req * mel_l = lookup_melodic_req_l(str);
337                 if (mel_l) {
338                     mtor << "(notename)\n";
339                     yylval.melreq = mel_l;
340                     return NOTENAME_ID;
341                 }
342         }
343         LexerError( "Unknown escaped string: `" + str + "'");   
344         mtor << "(string)";
345         String *sp = new String( str);
346         yylval.string=sp;
347         return STRING;
348 }
349
350 int
351 My_lily_lexer::scan_bare_word(String str)
352 {
353         mtor << "word: `" << str<< "'\n";       
354         if (YYSTATE == notes){
355                 Melodic_req * mel_l = lookup_melodic_req_l(str);
356                 if (mel_l) {
357                     mtor << "(notename)\n";
358                     yylval.melreq = mel_l;
359                     return NOTENAME_ID;
360                 }
361         }
362
363         yylval.string=new String( str );
364         return STRING;
365 }
366
367 bool
368 My_lily_lexer::note_state_b() const
369 {
370         return YY_START == notes;
371 }
372
373 bool
374 My_lily_lexer::lyric_state_b() const
375 {
376         return YY_START == lyrics;
377 }