8 #include "blocks/error.h"
9 #include "swf5compiler.tab.h" /* defines token types */
13 static const char *lexBuffer = NULL;
14 static int lexBufferLen = 0;
16 static int sLineNumber = 0;
17 static char szLine[1024];
18 static char msgbufs[2][1024] = { {0}, {0} }, *msgline = {0};
19 static int column = 0;
21 static void comment();
22 static void comment1();
24 static void countline();
25 static void warning(char *msg);
27 #define YY_INPUT(buf,result,max_size) result=lexBufferInput(buf, max_size)
29 /* thanks to the prolific and brilliant Raff: */
30 static int lexBufferInput(char *buf, int max_size)
32 int l = lexBufferLen > max_size ? max_size : lexBufferLen;
34 if (lexBufferLen <= 0)
37 memcpy(buf, lexBuffer, l);
43 /* very inefficient method of unescaping strings */
44 static void unescape(char *buf)
48 for (p1=buf; (p=strchr(p1, '\\')) != 0; p1 = p+1) {
51 case 'b' : p[1] = '\b'; break;
52 case 'f' : p[1] = '\f'; break;
53 case 'n' : p[1] = '\n'; break;
54 case 'r' : p[1] = '\r'; break;
55 case 't' : p[1] = '\t'; break;
57 case 'u' : warning("unsupported escape sequence");
63 void swf5ParseInit(const char *script, int debug)
71 lexBufferLen = strlen(script);
82 // forward declaration needed by the following function
85 #define YY_PROTO(proto) proto
87 #define YY_PROTO(proto) ()
90 static void yyunput YY_PROTO(( int c, char *buf_ptr ));
92 void do_unput5(const char c) { unput(c); }
96 ID [a-zA-Z_][a-zA-Z0-9_]*
100 0x[0-9a-fA-F]+ { count(); swf5lval.intVal = strtoul(yytext, NULL, 0);
102 0[0-7]+ { count(); swf5lval.intVal = strtoul(yytext, NULL, 0);
104 {DIGIT}+ { count(); swf5lval.intVal = atoi(yytext);
106 {DIGIT}+"."{DIGIT}* { count(); swf5lval.doubleVal = atof(yytext);
108 true { count(); swf5lval.intVal = 1;
110 false { count(); swf5lval.intVal = 0;
112 null { count(); return NULLVAL; }
113 break { count(); return BREAK; }
114 continue { count(); return CONTINUE; }
115 function { count(); return FUNCTION; }
116 else { count(); return ELSE; }
117 switch { count(); return SWITCH; }
118 case { count(); return CASE; }
119 default { count(); return DEFAULT; }
120 for { count(); return FOR; }
121 in { count(); return IN; }
122 if { count(); return IF; }
123 while { count(); return WHILE; }
124 do { count(); return DO; }
125 var { count(); return VAR; }
126 new { count(); return NEW; }
127 delete { count(); return DELETE; }
128 return { count(); return RETURN; }
129 with { count(); return WITH; }
130 asm { count(); BEGIN(asm); return ASM; }
131 eval { count(); return EVAL; }
132 typeof { count(); return TYPEOF; }
133 instanceof { count(); return INSTANCEOF; }
135 /* legacy functions */
136 random { count(); return RANDOM; }
137 getTimer { count(); return GETTIMER; }
138 length { count(); return LENGTH; }
139 concat { count(); return CONCAT; }
140 substr { count(); return SUBSTR; }
141 trace { count(); return TRACE; }
142 int { count(); return INT; }
143 ord { count(); return ORD; }
144 chr { count(); return CHR; }
145 getURL { count(); return GETURL; }
146 getURL1 { count(); return GETURL1; }
147 nextFrame { count(); return NEXTFRAME; }
148 prevFrame { count(); return PREVFRAME; }
149 play { count(); return PLAY; }
150 stop { count(); return STOP; }
151 toggleQuality { count(); return TOGGLEQUALITY; }
152 stopSounds { count(); return STOPSOUNDS; }
153 callFrame { count(); return CALLFRAME; }
154 gotoFrame { count(); return GOTOFRAME; }
155 setTarget { count(); return SETTARGET; }
156 loadVariables { count(); return LOADVARIABLES; }
157 loadMovie { count(); return LOADMOVIE; }
158 loadVariablesNum { count(); return LOADVARIABLESNUM; }
159 loadMovieNum { count(); return LOADMOVIENUM; }
160 duplicateMovieClip { count(); return DUPLICATEMOVIECLIP; }
161 removeMovieClip { count(); return REMOVEMOVIECLIP; }
165 dup { count(); return DUP; }
166 swap { count(); return SWAP; }
167 pop { count(); return POP; }
168 push { count(); return PUSH; }
169 setregister { count(); return SETREGISTER; }
170 callfunction { count(); return CALLFUNCTION; }
171 callmethod { count(); return CALLMETHOD; }
172 and { count(); return AND; }
173 or { count(); return OR; }
174 xor { count(); return XOR; }
175 modulo { count(); return MODULO; }
176 add { count(); return ADD; }
177 newadd { count(); return ADD; }
178 lessthan { count(); return LESSTHAN; }
179 newlessthan { count(); return LESSTHAN; }
180 equals { count(); return EQUALS; }
181 newequals { count(); return EQUALS; }
182 inc { count(); return INC; }
183 dec { count(); return DEC; }
184 enumerate { count(); return ENUMERATE; }
185 initobject { count(); return INITOBJECT; }
186 initarray { count(); return INITARRAY; }
187 getmember { count(); return GETMEMBER; }
188 setmember { count(); return SETMEMBER; }
189 shiftleft { count(); return SHIFTLEFT; }
190 shiftright { count(); return SHIFTRIGHT; }
191 shiftright2 { count(); return SHIFTRIGHT2; }
192 varequals { count(); return VAREQUALS; }
193 oldadd { count(); return OLDADD; }
194 subtract { count(); return SUBTRACT; }
195 multiply { count(); return MULTIPLY; }
196 divide { count(); return DIVIDE; }
197 oldequals { count(); return OLDEQUALS; }
198 oldlessthan { count(); return OLDLESSTHAN; }
199 logicaland { count(); return LOGICALAND; }
200 logicalor { count(); return LOGICALOR; }
201 not { count(); return NOT; }
202 stringeq { count(); return STRINGEQ; }
203 stringlength { count(); return STRINGLENGTH; }
204 substring { count(); return SUBSTRING; }
205 getvariable { count(); return GETVARIABLE; }
206 setvariable { count(); return SETVARIABLE; }
207 settargetexpr { count(); return SETTARGETEXPRESSION; }
208 startdrag { count(); return STARTDRAG; }
209 stopdrag { count(); return STOPDRAG; }
210 stringlessthan { count(); return STRINGLESSTHAN; }
211 mblength { count(); return MBLENGTH; }
212 mbsubstring { count(); return MBSUBSTRING; }
213 mbord { count(); return MBORD; }
214 mbchr { count(); return MBCHR; }
215 branch { count(); return BRANCHALWAYS; }
216 branchalways { count(); return BRANCHALWAYS; }
217 branchiftrue { count(); return BRANCHIFTRUE; }
218 post { count(); return POST; }
219 get { count(); return GET; }
220 end { count(); return END; }
223 r\:{DIGIT}+ { count(); swf5lval.str = strdup(yytext+2);
227 {ID} { count(); swf5lval.str = strdup(yytext);
230 \"(\\.|[^\\"])*\" { count(); swf5lval.str = strdup(yytext+1);
231 swf5lval.str[strlen(swf5lval.str)-1]=0;
232 unescape(swf5lval.str);
235 \'(\\.|[^\\'])*\' { count(); swf5lval.str = strdup(yytext+1);
236 swf5lval.str[strlen(swf5lval.str)-1]=0;
237 unescape(swf5lval.str);
240 \"(\\.|[^\\"])*$ { count(); swf5lval.str = strdup("");
241 warning("Unterminated string!");
244 \'(\\.|[^\\'])*$ { count(); swf5lval.str = strdup("");
245 warning("Unterminated string!");
248 "/*" { count(); comment(); }
249 "//" { count(); comment1(); }
250 [ \t\v\f] { count(); }
252 "++" { count(); return INCR; }
253 "--" { count(); return DECR; }
254 "<=" { count(); return LE; }
255 ">=" { count(); return GE; }
256 "==" { count(); return EQ; }
257 "!=" { count(); return NE; }
258 "&&" { count(); return LAN; }
259 "||" { count(); return LOR; }
260 "*=" { count(); return MEQ; }
261 "/=" { count(); return DEQ; }
262 "+=" { count(); return IEQ; }
263 "-=" { count(); return SEQ; }
264 "&=" { count(); return AEQ; }
265 "|=" { count(); return OEQ; }
266 "<<" { count(); return SHL; }
267 ">>" { count(); return SHR; }
268 ">>>" { count(); return SHR2; }
269 "<<=" { count(); return SHLEQ; }
270 ">>=" { count(); return SHREQ; }
271 ">>>=" { count(); return SHR2EQ; }
273 "<" { count(); return '<'; }
274 ">" { count(); return '>'; }
275 ";" { count(); return ';'; }
276 "=" { count(); return '='; }
277 "+" { count(); return '+'; }
278 "-" { count(); return '-'; }
279 "&" { count(); return '&'; }
280 "|" { count(); return '|'; }
281 "^" { count(); return '^'; }
282 "*" { count(); return '*'; }
283 "/" { count(); return '/'; }
284 "%" { count(); return '%'; }
285 "!" { count(); return '!'; }
286 "(" { count(); return '('; }
287 ")" { count(); return ')'; }
288 "[" { count(); return '['; }
289 "]" { count(); return ']'; }
290 "{" { count(); return '{'; }
291 "}" { count(); BEGIN(0); return '}'; }
292 "," { count(); return ','; }
293 "." { count(); return '.'; }
294 "?" { count(); return '?'; }
295 ":" { count(); return ':'; }
296 "~" { count(); return '~'; }
298 \r?\n { count(); strcpy(szLine, yytext + 1);
299 countline(); yyless(1); }
301 . SWF_error("Unrecognized character: %s\n", yytext);
304 static int getinput() {
317 static void countline()
324 msgline = msgbufs[sLineNumber & 1];
327 static int LineNumber(void)
329 return (sLineNumber + 1);
332 static int ColumnNumber(void)
337 static char *LineText(void)
343 static void comment()
345 // Handle block comments
350 // We have the start of a comment so look skip everything up to the
351 // end of the comment character
352 while ((c = getinput()) != '*' && c != EOF)
359 // keep the line number in synch
362 // start the output (matches the algorithim in the lexx above)
366 if (swf5debug) putchar(c);
369 // is this the end of comment character
370 if ((c1 = getinput()) != '/' && c != EOF)
372 // false start as this was no end of comment
377 // write out the start of the end of comment
379 if (swf5debug) putchar(c);
381 // write out the end of the end of comment
383 if (swf5debug) putchar(c1);
386 static void comment1()
388 // Handle comment of type 1 (ie '//')
392 // this is a line comment
393 while ((c = getinput()) != '\n' && c != EOF)
395 if (swf5debug) putchar(c);
403 // keep the line number in synch
406 if (swf5debug) putchar(c);
412 static void count(void)
416 // Count the characters to maintain the current column position
417 if (yytext[0] == '\n')
419 if (swf5debug) printf("\n");
423 if (swf5debug) printf("%s", yytext);
425 for(n=0; n<yyleng; ++n, ++column)
428 msgline[column] = yytext[n];
431 //-- keep writing the stuff to standard output
436 static void printprog()
439 SWF_warn("\n%s", msgbufs[(sLineNumber-1)&1]);
444 SWF_warn("\n%s", msgline);
447 static void warning(char *msg)
449 // print a warning message
451 SWF_warn("\n%*s", ColumnNumber(), "^");
452 SWF_warn("\nLine %4.4d: Reason: '%s' \n", LineNumber(), msg);
455 void swf5error(char *msg)
460 SWF_error("\n%s\n%*s\nLine %i: Reason: '%s'\n",
461 LineText(), ColumnNumber(), "^", LineNumber(), msg);
465 SWF_error("\nLine %d: Reason: 'Unexpected EOF found while looking for input.'\n", LineNumber());