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