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