7752bd524e688a6c4fba10882f4445eea7cbb131
[swftools.git] / lib / as3 / tokenizer.lex
1 /* tokenizer.lex
2
3    Routines for compiling Flash2 AVM2 ABC Actionscript
4
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7
8    Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
9  
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
23 %{
24
25
26 #include <string.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include "../utf8.h"
31 #include "tokenizer.h"
32 #include "files.h"
33
34 int as3_pass = 0;
35 int as3_verbosity = 1;
36 unsigned int as3_tokencount = 0;
37
38 void as3_error(const char*format, ...)
39 {
40     char buf[1024];
41     int l;
42     va_list arglist;
43     if(as3_verbosity<0)
44         exit(1);
45     va_start(arglist, format);
46     vsprintf(buf, format, arglist);
47     va_end(arglist);
48     fprintf(stderr, "%s:%d:%d: error: %s\n", current_filename_short, current_line, current_column, buf);
49     fflush(stderr);
50     exit(1);
51 }
52 void as3_warning(const char*format, ...)
53 {
54     char buf[1024];
55     int l;
56     va_list arglist;
57     if(as3_verbosity<1)
58         return;
59     va_start(arglist, format);
60     vsprintf(buf, format, arglist);
61     va_end(arglist);
62     fprintf(stderr, "%s:%d:%d: warning: %s\n", current_filename_short, current_line, current_column, buf);
63     fflush(stderr);
64 }
65 void as3_softwarning(const char*format, ...)
66 {
67     char buf[1024];
68     int l;
69     va_list arglist;
70     if(as3_verbosity<2)
71         return;
72     va_start(arglist, format);
73     vsprintf(buf, format, arglist);
74     va_end(arglist);
75     fprintf(stderr, "%s:%d:%d: warning: %s\n", current_filename_short, current_line, current_column, buf);
76     fflush(stderr);
77 }
78 static void dbg(const char*format, ...)
79 {
80     char buf[1024];
81     int l;
82     va_list arglist;
83     if(as3_verbosity<3)
84         return;
85     va_start(arglist, format);
86     vsprintf(buf, format, arglist);
87     va_end(arglist);
88     l = strlen(buf);
89     while(l && buf[l-1]=='\n') {
90         buf[l-1] = 0;
91         l--;
92     }
93     printf("(tokenizer) ");
94     printf("%s\n", buf);
95     fflush(stdout);
96 }
97
98 #ifndef YY_CURRENT_BUFFER
99 #define YY_CURRENT_BUFFER yy_current_buffer
100 #endif
101
102 static void*as3_buffer=0;
103 static int as3_buffer_pos=0;
104 static int as3_buffer_len=0;
105 void as3_file_input(FILE*fi)
106 {
107     as3_in = fi;
108     as3_buffer = 0;
109 }
110 void as3_buffer_input(void*buffer, int len)
111 {
112     if(!buffer)
113         syntaxerror("trying to parse zero bytearray");
114     as3_buffer = buffer;
115     as3_buffer_len = len;
116     as3_buffer_pos = 0;
117     as3_in = 0;
118 }
119
120 #define YY_INPUT(buf,result,max_size) { \
121   if(!as3_buffer) { \
122       errno=0; \
123       while((result = fread(buf, 1, max_size, as3_in))==0 && ferror(as3_in)) \
124       { if(errno != EINTR) {YY_FATAL_ERROR("input in flex scanner failed"); break;} \
125         errno=0; clearerr(as3_in); \
126       } \
127   } else { \
128       int to_read = max_size; \
129       if(to_read + as3_buffer_pos > as3_buffer_len) \
130           to_read = as3_buffer_len - as3_buffer_pos; \
131       memcpy(buf, as3_buffer+as3_buffer_pos, to_read); \
132       as3_buffer_pos += to_read; \
133       result=to_read; \
134   } \
135 }
136
137 void handleInclude(char*text, int len, char quotes)
138 {
139     char*filename = 0;
140     if(quotes) {
141         char*p1 = strchr(text, '"');
142         char*p2 = strrchr(text, '"');
143         if(!p1 || !p2 || p1==p2) {
144             syntaxerror("Invalid include in line %d\n", current_line);
145         }
146         *p2 = 0;
147         filename = strdup(p1+1);
148     } else {
149         int i1=0,i2=len;
150         // find start
151         while(!strchr(" \n\r\t", text[i1])) i1++;
152         // strip
153         while(strchr(" \n\r\t", text[i1])) i1++;
154         while(strchr(" \n\r\t", text[i2-1])) i2--;
155         if(i2!=len) text[i2]=0;
156         filename = strdup(&text[i1]);
157     }
158     
159     char*fullfilename = find_file(filename, 1);
160     enter_file2(filename, fullfilename, YY_CURRENT_BUFFER);
161     yyin = fopen(fullfilename, "rb");
162     if (!yyin) {
163         syntaxerror("Couldn't open include file \"%s\"\n", fullfilename);
164     }
165
166     yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );
167     //BEGIN(INITIAL); keep context
168 }
169
170 static int do_unescape(const char*s, const char*end, char*n) 
171 {
172     char*o = n;
173     int len=0;
174     while(s<end) {
175         if(*s!='\\') {
176             if(o) o[len] = *s;len++;
177             s++;
178             continue;
179         }
180         s++; //skip past '\'
181         if(s==end) syntaxerror("invalid \\ at end of string");
182
183         /* handle the various line endings (mac, dos, unix) */
184         if(*s=='\r') { 
185             s++; 
186             if(s==end) break;
187             if(*s=='\n') 
188                 s++;
189             continue;
190         }
191         if(*s=='\n')  {
192             s++;
193             continue;
194         }
195         switch(*s) {
196             case '\\': if(o) o[len] = '\\';s++;len++; break;
197             case '"': if(o) o[len] = '"';s++;len++; break;
198             case '\'': if(o) o[len] = '\'';s++;len++; break;
199             case 'b': if(o) o[len] = '\b';s++;len++; break;
200             case 'f': if(o) o[len] = '\f';s++;len++; break;
201             case 'n': if(o) o[len] = '\n';s++;len++; break;
202             case 'r': if(o) o[len] = '\r';s++;len++; break;
203             case 't': if(o) o[len] = '\t';s++;len++; break;
204             case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': {
205                 unsigned int num=0;
206                 int nr = 0;
207                 while(strchr("01234567", *s) && nr<3 && s<end) {
208                     num <<= 3;
209                     num |= *s-'0';
210                     nr++;
211                     s++;
212                 }
213                 if(num>256) 
214                     syntaxerror("octal number out of range (0-255): %d", num);
215                 if(o) o[len] = num;len++;
216                 continue;
217             }
218             case 'x': case 'u': {
219                 int max=2;
220                 char bracket = 0;
221                 char unicode = 0;
222                 if(*s == 'u') {
223                     max = 6;
224                     unicode = 1;
225                 }
226                 s++;
227                 if(s==end) syntaxerror("invalid \\u or \\x at end of string");
228                 if(*s == '{')  {
229                     s++;
230                     if(s==end) syntaxerror("invalid \\u{ at end of string");
231                     bracket=1;
232                 }
233                 unsigned int num=0;
234                 int nr = 0;
235                 while(strchr("0123456789abcdefABCDEF", *s) && (bracket || nr < max) && s<end) {
236                     num <<= 4;
237                     if(*s>='0' && *s<='9') num |= *s - '0';
238                     if(*s>='a' && *s<='f') num |= *s - 'a' + 10;
239                     if(*s>='A' && *s<='F') num |= *s - 'A' + 10;
240                     nr++;
241                     s++;
242                 }
243                 if(bracket) {
244                     if(*s=='}' && s<end) {
245                         s++;
246                     } else {
247                         syntaxerror("missing terminating '}'");
248                     }
249                 }
250                 if(unicode) {
251                     char*utf8 = getUTF8(num);
252                     while(*utf8) {
253                         if(o) o[len] = *utf8;utf8++;len++;
254                     }
255                 } else {
256                     if(num>256) 
257                         syntaxerror("byte out of range (0-255): %d", num);
258                     if(o) o[len] = num;len++;
259                 }
260                 break;
261             }
262             default: {
263                 if(o) {
264                     o[len+0] = '\\';
265                     o[len+1] = *s;
266                 }
267                 s++;
268                 len+=2;
269                 break;
270             }
271         }
272     }
273     if(o) o[len]=0;
274     return len;
275 }
276
277 static string_t string_unescape(const char*in, int l)
278 {
279     const char*s = in;
280     const char*end = &in[l];
281
282     int len = do_unescape(s, end, 0);
283     char*n = (char*)malloc(len+1);
284     do_unescape(s, end, n);
285     string_t out = string_new(n, len);
286     return out; 
287 }
288
289 static void handleCData(char*s, int len)
290 {
291     a3_lval.str.str = s+9;    // <![CDATA[
292     a3_lval.str.len = len-9-3;// ]]>
293     a3_lval.str.str = strdup_n(a3_lval.str.str, a3_lval.str.len);
294 }
295
296 static void handleString(char*s, int len)
297 {
298     if(s[0]=='"') {
299         if(s[len-1]!='"') syntaxerror("String doesn't end with '\"'");
300         s++;len-=2;
301     }
302     else if(s[0]=='\'') {
303         if(s[len-1]!='\'') syntaxerror("String doesn't end with '\"'");
304         s++;len-=2;
305     }
306     else syntaxerror("String incorrectly terminated");
307     
308     a3_lval.str = string_unescape(s, len);
309 }
310
311
312 char start_of_expression;
313
314 static inline int m(int type)
315 {
316     a3_lval.token = type;
317     return type;
318 }
319
320
321 static char numberbuf[64];
322 static char*nrbuf()
323 {
324     if(yyleng>sizeof(numberbuf)-1)
325         syntaxerror("decimal number overflow");
326     char*s = numberbuf;
327     memcpy(s, yytext, yyleng);
328     s[yyleng]=0;
329     return s;
330 }
331
332 static inline int setint(int v)
333 {
334     a3_lval.number_int = v;
335     if(v>-128)
336         return T_BYTE;
337     else if(v>=-32768)
338         return T_SHORT;
339     else
340         return T_INT;
341 }
342 static inline int setuint(unsigned int v)
343 {
344     a3_lval.number_uint = v;
345     if(v<128)
346         return T_BYTE;
347     else if(v<32768)
348         return T_SHORT;
349     else
350         return T_UINT;
351 }
352 static inline int setfloat(double v)
353 {
354     a3_lval.number_float = v;
355     return T_FLOAT;
356 }
357
358 static inline int handlefloat()
359 {
360     char*s = nrbuf();
361     a3_lval.number_float = atof(s);
362     return T_FLOAT;
363 }
364
365 static inline int handleint()
366 {
367     char*s = nrbuf();
368     char l = (yytext[0]=='-');
369
370     char*max = l?"1073741824":"2147483647";
371     if(yyleng-l>10) {
372         as3_softwarning("integer overflow: %s (converted to Number)", s);
373         return handlefloat();
374     }
375     if(yyleng-l==10) {
376         int t;
377         for(t=0;t<yyleng-l;t++) {
378             if(yytext[l+t]>max[t]) {
379                 as3_softwarning("integer overflow: %s (converted to Number)", s);
380                 return handlefloat();
381             }
382             else if(yytext[l+t]<max[t])
383                 break;
384         }
385     }
386     if(yytext[0]=='-') {
387         int v = atoi(s);
388         return setint(v);
389     } else {
390         unsigned int v = 0;
391         int t;
392         for(t=0;t<yyleng;t++) {
393             v*=10;
394             v+=yytext[t]-'0';
395         }
396         return setuint(v);
397     }
398 }
399
400 static inline int handlehexfloat()
401 {
402     char l = (yytext[0]=='-')+2;
403     double d=0;
404     char dot=0;
405     double base=1;
406     int t;
407     for(t=l;t<yyleng;t++) {
408         char c = yytext[t];
409         if(c=='.') {
410             dot=1;
411             continue;
412         }
413         if(!dot) {
414             d*=16;
415         } else {
416             base*=1/16.0;
417         }
418         if(c>='0' && c<='9')
419             d+=(c&15)*base;
420         else if((c>='a' && c<='f') || (c>='A' && c<='F'))
421             d+=((c&0x0f)+9)*base;
422     }
423     return setfloat(d);
424 }
425 static inline int handlehex()
426 {
427     char l = (yytext[0]=='-')+2;
428     int len = yyleng;
429
430     if(len-l>8) {
431         char*s = nrbuf();
432         syntaxerror("integer overflow %s", s);
433     }
434
435     int t;
436     unsigned int v = 0;
437     for(t=l;t<len;t++) {
438         v<<=4;
439         char c = yytext[t];
440         if(c>='0' && c<='9')
441             v|=(c&15);
442         else if((c>='a' && c<='f') || (c>='A' && c<='F'))
443             v|=(c&0x0f)+9;
444     }
445     if(l && v>1073741824) {
446         char*s = nrbuf();
447         as3_softwarning("signed integer overflow: %s (converted to Number)", s);
448         return setfloat(v);
449     }
450     if(!l && v>2147483647) {
451         char*s = nrbuf();
452         as3_softwarning("unsigned integer overflow: %s (converted to Number)", s);
453         return setfloat(v);
454     }
455
456     if(l==3) {
457         return setint(-(int)v);
458     } else {
459         return setuint(v);
460     }
461 }
462
463 void handleLabel(char*text, int len)
464 {
465     int t;
466     for(t=len-1;t>=0;--t) {
467         if(text[t]!=' ' &&
468            text[t]!=':')
469             break;
470     }
471     char*s = malloc(t+1);
472     memcpy(s, yytext, t);
473     s[t]=0;
474     a3_lval.id = s;
475 }
476
477 static int handleregexp()
478 {
479     char*s = malloc(yyleng);
480     int len=yyleng-1;
481     memcpy(s, yytext+1, len);
482     s[len] = 0;
483     int t;
484     for(t=len;t>=0;--t) {
485         if(s[t]=='/') {
486             s[t] = 0;
487             break;
488         }
489     }
490     a3_lval.regexp.pattern = s;
491     if(t==len) {
492         a3_lval.regexp.options = 0;
493     } else {
494         a3_lval.regexp.options = s+t+1;
495     }
496     return T_REGEXP;
497 }
498
499 void initialize_scanner();
500 #define YY_USER_INIT initialize_scanner();
501
502 /* count the number of lines+columns consumed by this token */
503 static inline void l() {
504     int t;
505     for(t=0;t<yyleng;t++) {
506         if(yytext[t]=='\n') {
507             current_line++;
508             current_column=0;
509         } else {
510             current_column++;
511         }
512     }
513 }
514 /* count the number of columns consumed by this token */
515 static inline void c() {
516     current_column+=yyleng;
517 }
518
519 static trie_t*namespaces = 0;
520 void tokenizer_register_namespace(const char*id)
521 {
522     trie_put(&namespaces, id);
523 }
524 void tokenizer_unregister_namespace(const char*id)
525 {
526     trie_remove(namespaces, id);
527 }
528 static inline tokenizer_is_namespace(const char*id)
529 {
530     return trie_lookup(namespaces, id);
531 }
532
533 static inline int handleIdentifier()
534 {
535     char*s = malloc(yyleng+1);
536     memcpy(s, yytext, yyleng);
537     s[yyleng]=0;
538     a3_lval.id = s;
539     if(tokenizer_is_namespace(s)) 
540         return T_NAMESPACE;
541     else
542         return T_IDENTIFIER;
543 }
544
545
546 //Boolean                      {c();return m(KW_BOOLEAN);}
547 //int                          {c();return m(KW_INT);}
548 //uint                         {c();return m(KW_UINT);}
549 //Number                       {c();return m(KW_NUMBER);}
550
551 %}
552
553 %s REGEXPOK
554 %s BEGINNING
555
556 NAME     [a-zA-Z_][a-zA-Z0-9_\\]*
557 _        [^a-zA-Z0-9_\\]
558
559 HEXINT    0x[a-zA-Z0-9]+
560 HEXFLOAT  0x[a-zA-Z0-9]*\.[a-zA-Z0-9]*
561 INT       [0-9]+
562 FLOAT     ([0-9]+(\.[0-9]*)?|\.[0-9]+)(e[0-9]+)?
563
564 HEXWITHSIGN [+-]?({HEXINT})
565 HEXFLOATWITHSIGN [+-]?({HEXFLOAT})
566 INTWITHSIGN [+-]?({INT})
567 FLOATWITHSIGN [+-]?({FLOAT})
568
569 CDATA    <!\[CDATA\[([^]]|\][^]]|\]\][^>])*\]*\]\]\>
570 STRING   ["](\\[\x00-\xff]|[^\\"\n])*["]|['](\\[\x00-\xff]|[^\\'\n])*[']
571 S        [ \n\r\t]
572 MULTILINE_COMMENT [/][*]+([*][^/]|[^/*]|[^*][/]|[\x00-\x1f])*[*]+[/]
573 SINGLELINE_COMMENT \/\/[^\n\r]*[\n\r]
574 REGEXP   [/]([^/\n]|\\[/])*[/][a-zA-Z]*
575 %%
576
577
578 {SINGLELINE_COMMENT}         {l(); /* single line comment */}
579 {MULTILINE_COMMENT}          {l(); /* multi line comment */}
580 [/][*]                       {syntaxerror("syntax error: unterminated comment", yytext);}
581
582 ^include{S}+{STRING}{S}*/\n    {l();handleInclude(yytext, yyleng, 1);}
583 ^include{S}+[^" \t\r\n][\x20-\xff]*{S}*/\n    {l();handleInclude(yytext, yyleng, 0);}
584 {STRING}                     {l(); BEGIN(INITIAL);handleString(yytext, yyleng);return T_STRING;}
585 {CDATA}                      {l(); BEGIN(INITIAL);handleCData(yytext, yyleng);return T_STRING;}
586
587 <BEGINNING,REGEXPOK>{
588 {REGEXP}                     {c(); BEGIN(INITIAL);return handleregexp();} 
589 {HEXWITHSIGN}/{_}            {c(); BEGIN(INITIAL);return handlehex();}
590 {HEXFLOATWITHSIGN}/{_}       {c(); BEGIN(INITIAL);return handlehexfloat();}
591 {INTWITHSIGN}/{_}            {c(); BEGIN(INITIAL);return handleint();}
592 {FLOATWITHSIGN}/{_}          {c(); BEGIN(INITIAL);return handlefloat();}
593 }
594
595 <REGEXPOK>[\{]               {c(); BEGIN(REGEXPOK);return m(T_DICTSTART);}
596 [\{]                         {c(); BEGIN(INITIAL); return m('{');}
597
598 \xef\xbb\xbf                 {/* utf 8 bom */}
599 {S}                          {l();}
600
601 {HEXINT}/{_}                 {c(); BEGIN(INITIAL);return handlehex();}
602 {HEXFLOAT}/{_}               {c(); BEGIN(INITIAL);return handlehexfloat();}
603 {INT}/{_}                    {c(); BEGIN(INITIAL);return handleint();}
604 {FLOAT}/{_}                  {c(); BEGIN(INITIAL);return handlefloat();}
605
606 3rr0r                        {/* for debugging: generates a tokenizer-level error */
607                               syntaxerror("3rr0r");}
608
609 {NAME}{S}*:{S}*for/{_}       {l();BEGIN(INITIAL);handleLabel(yytext, yyleng-3);return T_FOR;}
610 {NAME}{S}*:{S}*do/{_}        {l();BEGIN(INITIAL);handleLabel(yytext, yyleng-2);return T_DO;}
611 {NAME}{S}*:{S}*while/{_}     {l();BEGIN(INITIAL);handleLabel(yytext, yyleng-5);return T_WHILE;}
612 {NAME}{S}*:{S}*switch/{_}    {l();BEGIN(INITIAL);handleLabel(yytext, yyleng-6);return T_SWITCH;}
613 for                          {c();BEGIN(INITIAL);a3_lval.id="";return T_FOR;}
614 do                           {c();BEGIN(INITIAL);a3_lval.id="";return T_DO;}
615 while                        {c();BEGIN(INITIAL);a3_lval.id="";return T_WHILE;}
616 switch                       {c();BEGIN(INITIAL);a3_lval.id="";return T_SWITCH;}
617
618 [&][&]                       {c();BEGIN(REGEXPOK);return m(T_ANDAND);}
619 [|][|]                       {c();BEGIN(REGEXPOK);return m(T_OROR);}
620 [!][=]                       {c();BEGIN(REGEXPOK);return m(T_NE);}
621 [!][=][=]                    {c();BEGIN(REGEXPOK);return m(T_NEE);}
622 [=][=][=]                    {c();BEGIN(REGEXPOK);return m(T_EQEQEQ);}
623 [=][=]                       {c();BEGIN(REGEXPOK);return m(T_EQEQ);}
624 [>][=]                       {c();BEGIN(REGEXPOK);return m(T_GE);}
625 [<][=]                       {c();BEGIN(REGEXPOK);return m(T_LE);}
626 [-][-]                       {c();BEGIN(INITIAL);return m(T_MINUSMINUS);}
627 [+][+]                       {c();BEGIN(INITIAL);return m(T_PLUSPLUS);}
628 [+][=]                       {c();BEGIN(REGEXPOK);return m(T_PLUSBY);}
629 [\^][=]                      {c();BEGIN(REGEXPOK);return m(T_XORBY);}
630 [-][=]                       {c();BEGIN(REGEXPOK);return m(T_MINUSBY);}
631 [/][=]                       {c();BEGIN(REGEXPOK);return m(T_DIVBY);}
632 [%][=]                       {c();BEGIN(REGEXPOK);return m(T_MODBY);}
633 [*][=]                       {c();BEGIN(REGEXPOK);return m(T_MULBY);}
634 [|][=]                       {c();BEGIN(REGEXPOK);return m(T_ORBY);}
635 [>][>][=]                    {c();BEGIN(REGEXPOK);return m(T_SHRBY);}
636 [<][<][=]                    {c();BEGIN(REGEXPOK);return m(T_SHLBY);}
637 [>][>][>][=]                 {c();BEGIN(REGEXPOK);return m(T_USHRBY);}
638 [<][<]                       {c();BEGIN(REGEXPOK);return m(T_SHL);}
639 [>][>][>]                    {c();BEGIN(REGEXPOK);return m(T_USHR);}
640 [>][>]                       {c();BEGIN(REGEXPOK);return m(T_SHR);}
641 \.\.\.                       {c();BEGIN(REGEXPOK);return m(T_DOTDOTDOT);}
642 \.\.                         {c();BEGIN(REGEXPOK);return m(T_DOTDOT);}
643 \.                           {c();BEGIN(REGEXPOK);return m('.');}
644 ::                           {c();BEGIN(REGEXPOK);return m(T_COLONCOLON);}
645 :                            {c();BEGIN(REGEXPOK);return m(':');}
646 instanceof                   {c();BEGIN(REGEXPOK);return m(KW_INSTANCEOF);}
647 implements                   {c();BEGIN(REGEXPOK);return m(KW_IMPLEMENTS);}
648 interface                    {c();BEGIN(INITIAL);return m(KW_INTERFACE);}
649 namespace                    {c();BEGIN(INITIAL);return m(KW_NAMESPACE);}
650 protected                    {c();BEGIN(INITIAL);return m(KW_PROTECTED);}
651 undefined                    {c();BEGIN(INITIAL);return m(KW_UNDEFINED);}
652 continue                     {c();BEGIN(INITIAL);return m(KW_CONTINUE);}
653 override                     {c();BEGIN(INITIAL);return m(KW_OVERRIDE);}
654 internal                     {c();BEGIN(INITIAL);return m(KW_INTERNAL);}
655 function                     {c();BEGIN(INITIAL);return m(KW_FUNCTION);}
656 finally                      {c();BEGIN(INITIAL);return m(KW_FINALLY);}
657 default                      {c();BEGIN(INITIAL);return m(KW_DEFAULT);}
658 package                      {c();BEGIN(INITIAL);return m(KW_PACKAGE);}
659 private                      {c();BEGIN(INITIAL);return m(KW_PRIVATE);}
660 dynamic                      {c();BEGIN(INITIAL);return m(KW_DYNAMIC);}
661 extends                      {c();BEGIN(INITIAL);return m(KW_EXTENDS);}
662 delete                       {c();BEGIN(REGEXPOK);return m(KW_DELETE);}
663 return                       {c();BEGIN(REGEXPOK);return m(KW_RETURN);}
664 public                       {c();BEGIN(INITIAL);return m(KW_PUBLIC);}
665 native                       {c();BEGIN(INITIAL);return m(KW_NATIVE);}
666 static                       {c();BEGIN(INITIAL);return m(KW_STATIC);}
667 import                       {c();BEGIN(REGEXPOK);return m(KW_IMPORT);}
668 typeof                       {c();BEGIN(REGEXPOK);return m(KW_TYPEOF);}
669 throw                        {c();BEGIN(REGEXPOK);return m(KW_THROW);}
670 class                        {c();BEGIN(INITIAL);return m(KW_CLASS);}
671 const                        {c();BEGIN(INITIAL);return m(KW_CONST);}
672 catch                        {c();BEGIN(INITIAL);return m(KW_CATCH);}
673 final                        {c();BEGIN(INITIAL);return m(KW_FINAL);}
674 false                        {c();BEGIN(INITIAL);return m(KW_FALSE);}
675 break                        {c();BEGIN(INITIAL);return m(KW_BREAK);}
676 super                        {c();BEGIN(INITIAL);return m(KW_SUPER);}
677 each                         {c();BEGIN(INITIAL);return m(KW_EACH);}
678 void                         {c();BEGIN(INITIAL);return m(KW_VOID);}
679 true                         {c();BEGIN(INITIAL);return m(KW_TRUE);}
680 null                         {c();BEGIN(INITIAL);return m(KW_NULL);}
681 else                         {c();BEGIN(INITIAL);return m(KW_ELSE);}
682 case                         {c();BEGIN(REGEXPOK);return m(KW_CASE);}
683 with                         {c();BEGIN(REGEXPOK);return m(KW_WITH);}
684 use                          {c();BEGIN(REGEXPOK);return m(KW_USE);}
685 new                          {c();BEGIN(REGEXPOK);return m(KW_NEW);}
686 get                          {c();BEGIN(INITIAL);return m(KW_GET);}
687 set                          {c();BEGIN(INITIAL);return m(KW_SET);}
688 var                          {c();BEGIN(INITIAL);return m(KW_VAR);}
689 try                          {c();BEGIN(INITIAL);return m(KW_TRY);}
690 is                           {c();BEGIN(REGEXPOK);return m(KW_IS) ;}
691 in                           {c();BEGIN(REGEXPOK);return m(KW_IN) ;}
692 if                           {c();BEGIN(INITIAL);return m(KW_IF) ;}
693 as                           {c();BEGIN(REGEXPOK);return m(KW_AS);}
694 $?{NAME}                       {c();BEGIN(INITIAL);return handleIdentifier();}
695
696 [\]\}*]                       {c();BEGIN(INITIAL);return m(yytext[0]);}
697 [+-\/^~@$!%&\(=\[|?:;,<>]   {c();BEGIN(REGEXPOK);return m(yytext[0]);}
698 [\)\]]                           {c();BEGIN(INITIAL);return m(yytext[0]);}
699
700 .                            {/* ERROR */
701                               char c1=yytext[0];
702                               char buf[128];
703                               buf[0] = yytext[0];
704                               int t;
705                               for(t=1;t<128;t++) {
706                                   char c = buf[t]=input();
707                                   if(c=='\n' || c==EOF)  {
708                                       buf[t] = 0;
709                                       break;
710                                   }
711                               }
712                               if(c1>='0' && c1<='9')
713                                   syntaxerror("syntax error: %s (identifiers must not start with a digit)");
714                               else
715                                   syntaxerror("syntax error: %s", buf);
716                               printf("\n");
717                               exit(1);
718                               yyterminate();
719                              }
720 <<EOF>>                      {l();
721                               void*b = leave_file();
722                               if (!b) {
723                                  yyterminate();
724                                  yy_delete_buffer(YY_CURRENT_BUFFER);
725                                  return m(T_EOF);
726                               } else {
727                                   yy_delete_buffer(YY_CURRENT_BUFFER);
728                                   yy_switch_to_buffer(b);
729                               }
730                              }
731
732 %%
733
734 int yywrap()
735 {
736     return 1;
737 }
738
739 static char mbuf[256];
740 char*token2string(enum yytokentype nr, YYSTYPE v)
741 {
742     if(nr==T_STRING) {
743         char*s = malloc(v.str.len+10);
744         strcpy(s, "<string>");
745         memcpy(s+8, v.str.str, v.str.len);
746         sprintf(s+8+v.str.len, " (%d bytes)", v.str.len);
747         return s;
748     }
749     else if(nr==T_REGEXP) {
750         char*s = malloc(strlen(v.regexp.pattern)+10);
751         sprintf(s, "<regexp>%s", v.regexp.pattern);
752         return s;
753     }
754     else if(nr==T_IDENTIFIER) {
755         char*s = malloc(strlen(v.id)+10);
756         sprintf(s, "<ID>%s", v.id);
757         return s;
758     }
759     else if(nr==T_INT)     return "<int>";
760     else if(nr==T_UINT)     return "<uint>";
761     else if(nr==T_BYTE)     return "<byte>";
762     else if(nr==T_FLOAT)     return "<float>";
763     else if(nr==T_EOF)        return "***END***";
764     else if(nr==T_GE)         return ">=";
765     else if(nr==T_LE)         return "<=";
766     else if(nr==T_MINUSMINUS) return "--";
767     else if(nr==T_PLUSPLUS)   return "++";
768     else if(nr==KW_IMPLEMENTS) return "implements";
769     else if(nr==KW_INTERFACE)  return "interface";
770     else if(nr==KW_NAMESPACE)  return "namespace";
771     else if(nr==KW_PROTECTED)  return "protected";
772     else if(nr==KW_OVERRIDE)   return "override";
773     else if(nr==KW_INTERNAL)   return "internal";
774     else if(nr==KW_FUNCTION)   return "function";
775     else if(nr==KW_PACKAGE)    return "package";
776     else if(nr==KW_PRIVATE)    return "private";
777     else if(nr==KW_BOOLEAN)    return "Boolean";
778     else if(nr==KW_DYNAMIC)    return "dynamic";
779     else if(nr==KW_EXTENDS)    return "extends";
780     else if(nr==KW_PUBLIC)     return "public";
781     else if(nr==KW_NATIVE)     return "native";
782     else if(nr==KW_STATIC)     return "static";
783     else if(nr==KW_IMPORT)     return "import";
784     else if(nr==KW_NUMBER)     return "number";
785     else if(nr==KW_CLASS)      return "class";
786     else if(nr==KW_CONST)      return "const";
787     else if(nr==KW_FINAL)      return "final";
788     else if(nr==KW_FALSE)      return "False";
789     else if(nr==KW_TRUE)       return "True";
790     else if(nr==KW_UINT)       return "uint";
791     else if(nr==KW_NULL)       return "null";
792     else if(nr==KW_ELSE)       return "else";
793     else if(nr==KW_USE)        return "use";
794     else if(nr==KW_INT)        return "int";
795     else if(nr==KW_NEW)        return "new";
796     else if(nr==KW_GET)        return "get";
797     else if(nr==KW_SET)        return "set";
798     else if(nr==KW_VAR)        return "var";
799     else if(nr==KW_IS)         return "is";
800     else if(nr==KW_AS)         return "as";
801     else {
802         sprintf(mbuf, "%d", nr);
803         return mbuf;
804     }
805 }
806
807 void initialize_scanner()
808 {
809     BEGIN(BEGINNING);
810 }
811
812