keyframes are now every 20 frames.
[swftools.git] / src / parser.lex
1 %{
2
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include "../lib/q.h"
7 #include "parser.h"
8
9 //RVALUE         {NUMBER}|{PERCENT}|{NAME}|\"{STRING}\"|{DIM}
10 //<a>.                {printf("<a>%s\n", yytext);}
11 // %x: exclusive, %s: inclusive
12 char*type_names[] = {"twip","number","command","string","assignment","identifier","label","end"};
13 static int line=1;
14 static int column=1;
15
16 mem_t strings;
17 mem_t tokens;
18
19 static void count(char*text, int len, int condition)
20 {
21     int t;
22     for(t=0;t<len;t++) {
23         if(text[t]=='\n') {
24             line++;
25             column=1;
26         } else {
27             column++;
28         }
29     }
30 }
31
32 static char*prefix = 0;
33
34 static char utf8buf[16];
35 static char* getUTF8(unsigned int charnum)
36 {
37     memset(utf8buf, 0, sizeof(utf8buf));
38
39     if(charnum < 0x80) {
40         utf8buf[0] = charnum;
41         return utf8buf;
42     } else if(charnum <0x800) {
43         /* 0000 0080-0000 07FF   110xxxxx 10xxxxxx */
44         utf8buf[0] = 0xc0 | (charnum >> 6);
45         utf8buf[1] = 0x80 | (charnum & 0x3f);
46         return utf8buf;
47     } else if(charnum < 0x10000) {
48         /* 0000 0800-0000 FFFF   1110xxxx 10xxxxxx 10xxxxxx */
49         utf8buf[0] = 0xe0 | (charnum >> 12);
50         utf8buf[1] = 0x80 |((charnum >> 6)&0x3f);
51         utf8buf[2] = 0x80 |((charnum     )&0x3f);
52         return utf8buf;
53     } else if(charnum < 0x200000) {
54         /* 0001 0000-001F FFFF   11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */
55         utf8buf[0] = 0xf0 | (charnum >> 18);
56         utf8buf[1] = 0x80 |((charnum >> 12)&0x3f);
57         utf8buf[2] = 0x80 |((charnum >> 6 )&0x3f);
58         utf8buf[3] = 0x80 |((charnum      )&0x3f);
59         return utf8buf;
60     } else if(charnum < 0x4000000) {
61         /* 0020 0000-03FF FFFF   111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx */
62         utf8buf[0] = 0xf8 | (charnum >> 24);
63         utf8buf[1] = 0x80 |((charnum >> 18)&0x3f);
64         utf8buf[2] = 0x80 |((charnum >> 12)&0x3f);
65         utf8buf[3] = 0x80 |((charnum >> 6 )&0x3f);
66         utf8buf[4] = 0x80 |((charnum      )&0x3f);
67         return utf8buf;
68     } else if(charnum < 0x80000000) {
69         /* 0400 0000-7FFF FFFF   1111110x 10xxxxxx ... 10xxxxxx */
70         utf8buf[0] = 0xfc | (charnum >> 30);
71         utf8buf[1] = 0x80 |((charnum >> 24)&0x3f);
72         utf8buf[2] = 0x80 |((charnum >> 18)&0x3f);
73         utf8buf[3] = 0x80 |((charnum >> 12)&0x3f);
74         utf8buf[4] = 0x80 |((charnum >> 6 )&0x3f);
75         utf8buf[5] = 0x80 |((charnum      )&0x3f);
76         return utf8buf;
77     } else {
78         fprintf(stderr, "Illegal character: 0x%08x\n", charnum);
79         return utf8buf;
80     }
81 }
82
83 static void unescapeString(string_t * tmp)
84 {
85     char *p, *p1;
86     /* fixme - this routine expects the string to be
87        null-terminated */
88
89     for (p1=tmp->str; (p=strchr(p1, '\\')); p1 = p+1) 
90     {
91         int nr=2;
92         int new=1;
93         switch(p[1])
94         {
95             case '\\': p[0] = '\\'; break;
96             case '"': p[0] = '"'; break;
97             case 'b': p[0] = '\b'; break;
98             case 'f': p[0] = '\f'; break;
99             case 'n': p[0] = '\n'; break;
100             case 'r': p[0] = '\r'; break;
101             case 't': p[0] = '\t'; break;
102             case 'x':  {
103                 int num=0;
104                 char*utf8;
105                 while(strchr("0123456789abcdefABCDEF", p[nr])) {
106                     num <<= 4;
107                     if(p[nr]>='0' && p[nr]<='9') num |= p[nr] - '0';
108                     if(p[nr]>='a' && p[nr]<='f') num |= p[nr] - 'a' + 10;
109                     if(p[nr]>='A' && p[nr]<='F') num |= p[nr] - 'A' + 10;
110                     nr++;
111                 }
112                 utf8 = getUTF8(num);
113                 new = strlen(utf8);
114
115                 memcpy(p, utf8, new); // do not copy the terminating zero
116                 break;
117             }
118             default:
119                 continue;
120         }
121         tmp->len -= (nr-new); 
122         {
123             int t;
124             char*to=p+new,*from=p+nr;
125             while(*from) {
126                 *to = *from;
127                 to++;
128                 from++;
129             }
130         }
131     }
132 }
133
134 static void store(enum type_t type, int line, int column, char*text, int length)
135 {
136     struct token_t token;
137     string_t tmp;
138     token.type = type;
139     token.line = line;
140     token.column = column;
141     //printf("->%d(%s) %s\n", type, type_names[type], text);fflush(stdout);
142
143     token.text = 0;
144     switch(type) {
145         case END:
146             string_set2(&tmp, "", 0);
147             token.text = (char*)mem_putstring(&strings, tmp);
148         break;
149         case STRING:
150             string_set2(&tmp, text+1, length-2);
151             unescapeString(&tmp);
152             token.text = (char*)mem_putstring(&strings, tmp);
153         break;
154         case TWIP: 
155         case NUMBER: 
156         case IDENTIFIER:
157             string_set2(&tmp, text, length);
158             if(prefix) {
159                 //strcat
160                 token.text = (char*)mem_put(&strings, prefix, strlen(prefix));
161                 mem_putstring(&strings, tmp);
162             } else {
163                 token.text = (char*)mem_putstring(&strings, tmp);
164             }
165             prefix = 0;
166         break;
167         case RAWDATA:
168             string_set2(&tmp, text+1/*:*/, length-5/*.end*/);
169             token.text = (char*)mem_putstring(&strings, tmp);
170         break;
171         case COMMAND:
172             string_set2(&tmp, text+1, length-1);
173             token.text = (char*)mem_putstring(&strings, tmp);
174         break;
175         case ASSIGNMENT: {
176             char*x = &text[length-1];
177             if(x[-1] == '-' || x[-1] == '+')
178                 x--;
179             do{x--;} while(*x==32 || *x==10 || *x==13 || *x=='\t');
180             x++; //first space
181             string_set2(&tmp, text, x-text);
182             token.text = (char*)mem_putstring(&strings, tmp);
183             /*char*y,*x = strchr(text, '=');
184             if(!x) exit(1);
185             y=x;
186             do{y--;} while(*y==32 || *y==10 || *y==13 || *y=='\t');
187             do{x++;} while(*x==32 || *x==10 || *x==13 || *x=='\t');
188             token.text1 = (char*)put(&strings, text, y-text + 1, 1);
189             token.text2 = (char*)put(&strings, x, length-(x-text), 1);*/
190         } break;
191     }
192
193     mem_put(&tokens, &token, sizeof(struct token_t));
194     prefix = 0;
195 }
196
197 #define MAX_INCLUDE_DEPTH 16
198 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
199 int line_stack[MAX_INCLUDE_DEPTH];
200 int column_stack[MAX_INCLUDE_DEPTH];
201 int include_stack_ptr = 0;
202
203 void handleInclude(char*text, int len)
204 {
205     text+=9;len-=9;
206     while(len >=1 && (text[0] == ' ' || text[0] == '\t')) {
207         text++;len--;
208     }
209     while(len >= 1 && (text[len-1] == ' ' || text[len-1] == '\n')) {
210         len--;
211     }
212     if(len >= 2 && text[0] == '"' && text[len-1] == '"') {
213         text++; len-=2;
214     }
215     text[len] = 0;
216     if(include_stack_ptr >= MAX_INCLUDE_DEPTH) {
217         fprintf( stderr, "Includes nested too deeply" );
218         exit( 1 );
219     }
220     include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
221     line_stack[include_stack_ptr] = line;
222     column_stack[include_stack_ptr] = column;
223     include_stack_ptr++;
224     yyin = fopen(text, "rb");
225     if (!yyin) {
226         fprintf(stderr, "Couldn't open %s\n", text);
227         exit(1);
228     }
229     yy_switch_to_buffer(
230         yy_create_buffer( yyin, YY_BUF_SIZE ) );
231     BEGIN(INITIAL);
232 }
233
234 #define c() {count(yytext, yyleng, YY_START);}
235 #define s(type) {store(type, line, column, yytext, yyleng);}
236 %}
237
238 %s R
239 %x BINARY
240
241 NAME     [a-zA-Z_./](-*[a-zA-Z0-9_./])*
242 TWIP     (-?[0-9]+(\.([0-9]([05])?)?)?)
243 NUMBER   -?[0-9]+(\.[0-9]*)?
244 PERCENT  {NUMBER}%
245 STRING   (\\.|[^\\"\n])*
246 S        [ \n\r\t]
247 RVALUE   \"{STRING}\"|([^ \n\r\t]+)
248
249 %%
250
251 <BINARY>\] {c();BEGIN(0);}
252 <BINARY>.  {c();}
253 <BINARY>\n {c();}
254 {TWIP}/[ \n\r\t]            {s(TWIP);c();BEGIN(0);}
255 {NUMBER}/[ \n\r\t]          {s(NUMBER);c();BEGIN(0);}
256 ^#[^\n]*\n                  {c();}
257 [ \t\r]#[^\n]*\n            {c();}
258 \"{STRING}\"                {s(STRING);c();BEGIN(0);}
259 \"{STRING}$                 {c();printf("unterminated string in line %d: %s\n", line, yytext);exit(1);yyterminate();}
260 {NAME}{S}*\+=               {s(ASSIGNMENT);prefix="<plus>";c();BEGIN(R);}
261 {NAME}{S}*-=                {s(ASSIGNMENT);prefix="<minus>";c();BEGIN(R);}
262 {NAME}{S}*=                 {s(ASSIGNMENT);c();BEGIN(R);}
263 <R>{ /* values which appear only on the right-hand side of assignments, like: x=50% */
264     [^ :\n\t\r]*                    {s(IDENTIFIER);c();BEGIN(0);}
265 }
266 \.include{S}.*\n                    {handleInclude(yytext, yyleng);}
267 \.{NAME}                    {s(COMMAND);c();}
268 :([^.]|\.[^e]|\.e[^n]|\.en[^d]|\.end[^ \n\r\t]|[ \n\r\t])*\.end     {s(RAWDATA);c();}
269 {NAME}                      {s(IDENTIFIER);c();}
270 "["                         {c();BEGIN(BINARY);}
271 {S}                         {c();}
272 .                           {char c,c1=yytext[0];
273                              printf("Syntax error in line %d, %d: %s", line, column, yytext);
274                              while(1) {
275                                  c=input();
276                                  if(c=='\n' || c==EOF) 
277                                      break;
278                                 printf("%c", c);
279                              }
280                              if(c1>='0' && c1<='9')
281                                  printf(" (identifiers must not start with a digit)");
282                              printf("\n");
283                              exit(1);
284                              yyterminate();
285                             }
286 <<EOF>>                     {c();
287                              if ( --include_stack_ptr < 0 ) {
288                                 s(END);
289                                 yyterminate();
290                              } else {
291                                  yy_delete_buffer( YY_CURRENT_BUFFER );
292                                  yy_switch_to_buffer(
293                                       include_stack[include_stack_ptr] );
294                                  column = column_stack[include_stack_ptr];
295                                  line = line_stack[include_stack_ptr];
296                              }
297                             }
298
299 %%
300
301 int yywrap()
302 {
303     return 1;
304 }
305
306 void freeTokens(struct token_t*file)
307 {
308     mem_clear(&strings);
309     mem_clear(&tokens);
310 }
311
312 struct token_t* generateTokens(char*filename)
313 {
314     FILE*fi = fopen(filename, "rb");
315     int t;
316     struct token_t*result;
317     int num;
318     if(!fi) {
319         printf("Couldn't find file %s\n", filename);
320         return 0;
321     }
322     yyin = fi;
323
324     mem_init(&strings);
325     mem_init(&tokens);
326     mem_put(&strings, &t, 1); //hack- make all valid strings start at position >0
327
328     line=1;
329     column=1;
330
331     yylex();
332     yy_delete_buffer(yy_current_buffer);
333
334     result = (struct token_t*)tokens.buffer;
335     num = tokens.pos/sizeof(struct token_t);
336
337     for(t=0;t<tokens.pos/sizeof(struct token_t);t++) {
338         if(result[t].text)
339             result[t].text += (int)strings.buffer;
340     }
341
342     fclose(fi);
343     return result;
344 }
345