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