added '*=' token
[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 static void countlines(char*text, int len) {
35     int t;
36     for(t=0;t<len;t++) {
37         if(text[t]=='\n') {
38             current_line++;
39             current_column=0;
40         } else {
41             current_column++;
42         }
43     }
44 }
45
46 static int verbose = 1;
47 static void dbg(const char*format, ...)
48 {
49     char buf[1024];
50     int l;
51     va_list arglist;
52     if(!verbose)
53         return;
54     va_start(arglist, format);
55     vsprintf(buf, format, arglist);
56     va_end(arglist);
57     l = strlen(buf);
58     while(l && buf[l-1]=='\n') {
59         buf[l-1] = 0;
60         l--;
61     }
62     printf("(tokenizer) ");
63     printf("%s\n", buf);
64     fflush(stdout);
65 }
66
67 void syntaxerror(const char*format, ...)
68 {
69     char buf[1024];
70     int l;
71     va_list arglist;
72     if(!verbose)
73         return;
74     va_start(arglist, format);
75     vsprintf(buf, format, arglist);
76     va_end(arglist);
77     fprintf(stderr, "%s:%d:%d: error: %s\n", current_filename_short, current_line, current_column, buf);
78     fflush(stderr);
79     exit(1);
80 }
81 void warning(const char*format, ...)
82 {
83     char buf[1024];
84     int l;
85     va_list arglist;
86     if(!verbose)
87         return;
88     va_start(arglist, format);
89     vsprintf(buf, format, arglist);
90     va_end(arglist);
91     fprintf(stderr, "%s:%d:%d: warning: %s\n", current_filename_short, current_line, current_column, buf);
92     fflush(stderr);
93 }
94
95
96 #ifndef YY_CURRENT_BUFFER
97 #define YY_CURRENT_BUFFER yy_current_buffer
98 #endif
99
100 void handleInclude(char*text, int len, char quotes)
101 {
102     char*filename = 0;
103     if(quotes) {
104         char*p1 = strchr(text, '"');
105         char*p2 = strrchr(text, '"');
106         if(!p1 || !p2 || p1==p2) {
107             syntaxerror("Invalid include in line %d\n", current_line);
108         }
109         *p2 = 0;
110         filename = strdup(p1+1);
111     } else {
112         int i1=0,i2=len;
113         // find start
114         while(!strchr(" \n\r\t", text[i1])) i1++;
115         // strip
116         while(strchr(" \n\r\t", text[i1])) i1++;
117         while(strchr(" \n\r\t", text[i2-1])) i2--;
118         if(i2!=len) text[i2]=0;
119         filename = strdup(&text[i1]);
120     }
121     
122     char*fullfilename = enter_file(filename, 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(INITIAL); keep context
130 }
131
132 static void handleString(char*s, int len)
133 {
134     if(s[0]=='"') {
135         if(s[len-1]!='"') syntaxerror("String doesn't end with '\"'");
136         s++;len-=2;
137     }
138     else if(s[0]=='\'') {
139         if(s[len-1]!='\'') syntaxerror("String doesn't end with '\"'");
140         s++;len-=2;
141     }
142     else syntaxerror("String incorrectly terminated");
143     s[len] = 0;
144     avm2_lval.string = s;
145 }
146
147
148 char start_of_expression;
149
150 static inline int m(int type)
151 {
152     char*s = malloc(yyleng+1);
153     memcpy(s, yytext, yyleng);
154     s[yyleng]=0;
155
156     NEW(token_t,t);
157     t->type = type;
158     t->text = s;
159     avm2_lval.token = t;
160     return type;
161 }
162
163 static char numberbuf[64];
164 static inline int handlenumber()
165 {
166     if(yyleng>sizeof(numberbuf)-1)
167         syntaxerror("decimal number overflow");
168
169     char*s = numberbuf;
170     memcpy(s, yytext, yyleng);
171     s[yyleng]=0;
172
173     int t;
174     char is_float=0;
175     for(t=0;t<yyleng;t++) {
176         if(yytext[t]=='.') {
177             if(is_float)
178                 syntaxerror("Invalid number");
179             is_float=1;
180         } else if(!strchr("-0123456789", yytext[t])) {
181             syntaxerror("Invalid number");
182         }
183     }
184     if(is_float) {
185         avm2_lval.number_float = atof(s);
186         return T_FLOAT;
187     } 
188     char l = (yytext[0]=='-');
189
190     char*max = l?"1073741824":"2147483647";
191     if(yyleng-l>10)
192         syntaxerror("integer overflow");
193     if(yyleng-l==10) {
194         int t;
195         for(t=0;t<yyleng-l;t++) {
196             if(yytext[l+t]>max[t])
197                 syntaxerror("integer overflow %s > %s", s+l,max);
198             else if(yytext[l+t]<max[t])
199                 break;
200         }
201     }
202     if(yytext[0]=='-') {
203         int v = atoi(s);
204         avm2_lval.number_int = v;
205         if(v>-128)
206             return T_BYTE;
207         else if(v>=-32768)
208             return T_SHORT;
209         else
210             return T_INT;
211     } else {
212         unsigned int v = 0;
213         for(t=0;t<yyleng;t++) {
214             v*=10;
215             v+=yytext[t]-'0';
216         }
217         avm2_lval.number_uint = v;
218         if(v<128)
219             return T_BYTE;
220         else if(v<32768)
221             return T_SHORT;
222         else
223             return T_UINT;
224     }
225 }
226
227 void initialize_scanner();
228 #define YY_USER_INIT initialize_scanner();
229
230 #define c() {countlines(yytext, yyleng);}
231
232 %}
233
234 %s REGEXPOK
235 %s BEGINNING
236
237 NAME     [a-zA-Z_][a-zA-Z0-9_\\]*
238
239 NUMBER   -?[0-9]+(\.[0-9]*)?
240
241 STRING   ["](\\[\x00-\xff]|[^\\"\n])*["]|['](\\[\x00-\xff]|[^\\'\n])*[']
242 S        [ \n\r\t]
243 MULTILINE_COMMENT [/][*]+([*][^/]|[^/*]|[^*][/]|[\x00-\x1f])*[*]+[/]
244 SINGLELINE_COMMENT \/\/[^\n]*\n
245 REGEXP   [/]([^/\n]|\\[/])*[/][a-zA-Z]*
246 %%
247
248
249 {SINGLELINE_COMMENT}         {c(); /* single line comment */}
250 {MULTILINE_COMMENT}          {c(); /* multi line comment */}
251 [/][*]                       {syntaxerror("syntax error: unterminated comment", yytext);}
252
253 ^include{S}+{STRING}{S}*/\n    {c();handleInclude(yytext, yyleng, 1);}
254 ^include{S}+[^" \t\r\n][\x20-\xff]*{S}*/\n    {c();handleInclude(yytext, yyleng, 0);}
255 {STRING}                     {c(); BEGIN(INITIAL);handleString(yytext, yyleng);return T_STRING;}
256
257 <BEGINNING,REGEXPOK>{
258 {REGEXP}                     {c(); BEGIN(INITIAL);return m(T_REGEXP);} 
259 }
260
261 \xef\xbb\xbf                 {/* utf 8 bom */}
262 {S}                          {c();}
263
264 {NUMBER}                     {c(); BEGIN(INITIAL);return handlenumber();}
265
266 3rr0r                        {/* for debugging: generates a tokenizer-level error */
267                               syntaxerror("3rr0r");}
268
269 [&][&]                       {c();BEGIN(REGEXPOK);return m(T_ANDAND);}
270 [|][|]                       {c();BEGIN(REGEXPOK);return m(T_OROR);}
271 [!][=]                       {c();BEGIN(REGEXPOK);return m(T_NE);}
272 [=][=][=]                    {c();BEGIN(REGEXPOK);return m(T_EQEQEQ);}
273 [=][=]                       {c();BEGIN(REGEXPOK);return m(T_EQEQ);}
274 [>][=]                       {c();return m(T_GE);}
275 [<][=]                       {c();return m(T_LE);}
276 [-][-]                       {c();BEGIN(INITIAL);return m(T_MINUSMINUS);}
277 [+][+]                       {c();BEGIN(INITIAL);return m(T_PLUSPLUS);}
278 [+][=]                       {c();return m(T_PLUSBY);}
279 [-][=]                       {c();return m(T_MINUSBY);}
280 [/][=]                       {c();return m(T_DIVBY);}
281 [%][=]                       {c();return m(T_MODBY);}
282 [*][=]                       {c();return m(T_MULBY);}
283 [>][>][=]                    {c();return m(T_SHRBY);}
284 [<][<][=]                    {c();return m(T_SHLBY);}
285 [>][>][>][=]                 {c();return m(T_USHRBY);}
286 [<][<]                       {c();return m(T_SHL);}
287 [>][>][>]                    {c();return m(T_USHR);}
288 [>][>]                       {c();return m(T_SHR);}
289 \.\.\.                       {c();return m(T_DOTDOTDOT);}
290 \.\.                         {c();return m(T_DOTDOT);}
291 \.                           {c();return m('.');}
292 ::                           {c();return m(T_COLONCOLON);}
293 :                            {c();return m(':');}
294 implements                   {c();return m(KW_IMPLEMENTS);}
295 interface                    {c();return m(KW_INTERFACE);}
296 namespace                    {c();return m(KW_NAMESPACE);}
297 protected                    {c();return m(KW_PROTECTED);}
298 override                     {c();return m(KW_OVERRIDE);}
299 internal                     {c();return m(KW_INTERNAL);}
300 function                     {c();return m(KW_FUNCTION);}
301 package                      {c();return m(KW_PACKAGE);}
302 private                      {c();return m(KW_PRIVATE);}
303 Boolean                      {c();return m(KW_BOOLEAN);}
304 dynamic                      {c();return m(KW_DYNAMIC);}
305 extends                      {c();return m(KW_EXTENDS);}
306 return                       {c();return m(KW_RETURN);}
307 public                       {c();return m(KW_PUBLIC);}
308 native                       {c();return m(KW_NATIVE);}
309 static                       {c();return m(KW_STATIC);}
310 import                       {c();return m(KW_IMPORT);}
311 Number                       {c();return m(KW_NUMBER);}
312 while                        {c();return m(KW_WHILE);}
313 class                        {c();return m(KW_CLASS);}
314 const                        {c();return m(KW_CONST);}
315 final                        {c();return m(KW_FINAL);}
316 false                        {c();return m(KW_FALSE);}
317 break                        {c();return m(KW_BREAK);}
318 true                         {c();return m(KW_TRUE);}
319 uint                         {c();return m(KW_UINT);}
320 null                         {c();return m(KW_NULL);}
321 else                         {c();return m(KW_ELSE);}
322 use                          {c();return m(KW_USE);}
323 int                          {c();return m(KW_INT);}
324 new                          {c();return m(KW_NEW);}
325 get                          {c();return m(KW_GET);}
326 for                          {c();return m(KW_FOR);}
327 set                          {c();return m(KW_SET);}
328 var                          {c();return m(KW_VAR);}
329 is                           {c();return m(KW_IS) ;}
330 if                           {c();return m(KW_IF) ;}
331 as                           {c();return m(KW_AS);}
332 {NAME}                       {c();BEGIN(INITIAL);return m(T_IDENTIFIER);}
333
334 [+-\/*^~@$!%&\(=\[\]\{\}|?:;,.<>] {c();BEGIN(REGEXPOK);return m(yytext[0]);}
335 [\)\]]                            {c();BEGIN(INITIAL);return m(yytext[0]);}
336
337 .                            {char c1=yytext[0];
338                               char buf[128];
339                               buf[0] = yytext[0];
340                               int t;
341                               for(t=1;t<128;t++) {
342                                   char c = buf[t]=input();
343                                   if(c=='\n' || c==EOF)  {
344                                       buf[t] = 0;
345                                       break;
346                                   }
347                               }
348                               if(c1>='0' && c1<='9')
349                                   syntaxerror("syntax error: %s (identifiers must not start with a digit)");
350                               else
351                                   syntaxerror("syntax error: %s", buf);
352                               printf("\n");
353                               exit(1);
354                               yyterminate();
355                              }
356 <<EOF>>                      {c();
357                               void*b = leave_file();
358                               if (!b) {
359                                  yyterminate();
360                                  yy_delete_buffer(YY_CURRENT_BUFFER);
361                                  return m(T_EOF);
362                               } else {
363                                   yy_delete_buffer(YY_CURRENT_BUFFER);
364                                   yy_switch_to_buffer(b);
365                               }
366                              }
367
368 %%
369
370 int yywrap()
371 {
372     return 1;
373 }
374
375 static char mbuf[256];
376 char*token2string(token_t*t)
377 {
378     int nr=t->type;
379     if(nr==T_STRING)     return "<string>";
380     else if(nr==T_INT)     return "<int>";
381     else if(nr==T_UINT)     return "<uint>";
382     else if(nr==T_FLOAT)     return "<float>";
383     else if(nr==T_REGEXP)     return "REGEXP";
384     else if(nr==T_EOF)        return "***END***";
385     else if(nr==T_GE)         return ">=";
386     else if(nr==T_LE)         return "<=";
387     else if(nr==T_MINUSMINUS) return "--";
388     else if(nr==T_PLUSPLUS)   return "++";
389     else if(nr==KW_IMPLEMENTS) return "implements";
390     else if(nr==KW_INTERFACE)  return "interface";
391     else if(nr==KW_NAMESPACE)  return "namespace";
392     else if(nr==KW_PROTECTED)  return "protected";
393     else if(nr==KW_OVERRIDE)   return "override";
394     else if(nr==KW_INTERNAL)   return "internal";
395     else if(nr==KW_FUNCTION)   return "function";
396     else if(nr==KW_PACKAGE)    return "package";
397     else if(nr==KW_PRIVATE)    return "private";
398     else if(nr==KW_BOOLEAN)    return "Boolean";
399     else if(nr==KW_DYNAMIC)    return "dynamic";
400     else if(nr==KW_EXTENDS)    return "extends";
401     else if(nr==KW_PUBLIC)     return "public";
402     else if(nr==KW_NATIVE)     return "native";
403     else if(nr==KW_STATIC)     return "static";
404     else if(nr==KW_IMPORT)     return "import";
405     else if(nr==KW_NUMBER)     return "number";
406     else if(nr==KW_CLASS)      return "class";
407     else if(nr==KW_CONST)      return "const";
408     else if(nr==KW_FINAL)      return "final";
409     else if(nr==KW_FALSE)      return "False";
410     else if(nr==KW_TRUE)       return "True";
411     else if(nr==KW_UINT)       return "uint";
412     else if(nr==KW_NULL)       return "null";
413     else if(nr==KW_ELSE)       return "else";
414     else if(nr==KW_USE)        return "use";
415     else if(nr==KW_INT)        return "int";
416     else if(nr==KW_NEW)        return "new";
417     else if(nr==KW_GET)        return "get";
418     else if(nr==KW_FOR)        return "for";
419     else if(nr==KW_SET)        return "set";
420     else if(nr==KW_VAR)        return "var";
421     else if(nr==KW_IS)         return "is";
422     else if(nr==KW_AS)         return "as";
423     else if(nr==T_IDENTIFIER) {
424         if(strlen(t->text)>sizeof(mbuf)-1)
425             return "ID(...)";
426         sprintf(mbuf, "ID(%s)", t->text);
427         return mbuf;
428     } else {
429         sprintf(mbuf, "%d", nr);
430         return mbuf;
431     }
432 }
433
434 void initialize_scanner()
435 {
436     BEGIN(BEGINNING);
437 }
438