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"};
19 static void count(char*text, int len, int condition)
32 static char*prefix = 0;
34 static char utf8buf[16];
35 static char* getUTF8(unsigned int charnum)
37 memset(utf8buf, 0, sizeof(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);
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);
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);
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);
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);
78 fprintf(stderr, "Illegal character: 0x%08x\n", charnum);
83 static void unescapeString(string_t * tmp)
86 /* fixme - this routine expects the string to be
89 for (p1=tmp->str; (p=strchr(p1, '\\')); p1 = p+1)
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;
105 while(strchr("0123456789abcdefABCDEF", p[nr])) {
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;
115 memcpy(p, utf8, new); // do not copy the terminating zero
121 tmp->len -= (nr-new);
124 char*to=p+new,*from=p+nr;
134 static void store(enum type_t type, int line, int column, char*text, int length)
136 struct token_t token;
140 token.column = column;
141 //printf("->%d(%s) %s\n", type, type_names[type], text);fflush(stdout);
146 string_set2(&tmp, "", 0);
147 token.text = (char*)mem_putstring(&strings, tmp);
150 string_set2(&tmp, text+1, length-2);
151 unescapeString(&tmp);
152 token.text = (char*)mem_putstring(&strings, tmp);
157 string_set2(&tmp, text, length);
160 token.text = (char*)mem_put(&strings, prefix, strlen(prefix));
161 mem_putstring(&strings, tmp);
163 token.text = (char*)mem_putstring(&strings, tmp);
168 string_set2(&tmp, text+1/*:*/, length-5/*.end*/);
169 token.text = (char*)mem_putstring(&strings, tmp);
172 string_set2(&tmp, text+1, length-1);
173 token.text = (char*)mem_putstring(&strings, tmp);
176 char*x = &text[length-1];
177 if(x[-1] == '-' || x[-1] == '+')
179 do{x--;} while(*x==32 || *x==10 || *x==13 || *x=='\t');
181 string_set2(&tmp, text, x-text);
182 token.text = (char*)mem_putstring(&strings, tmp);
183 /*char*y,*x = strchr(text, '=');
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);*/
193 mem_put(&tokens, &token, sizeof(struct token_t));
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;
203 void handleInclude(char*text, int len)
206 while(len >=1 && (text[0] == ' ' || text[0] == '\t')) {
209 while(len >= 1 && (text[len-1] == ' ' || text[len-1] == '\n')) {
212 if(len >= 2 && text[0] == '"' && text[len-1] == '"') {
216 if(include_stack_ptr >= MAX_INCLUDE_DEPTH) {
217 fprintf( stderr, "Includes nested too deeply" );
220 include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
221 line_stack[include_stack_ptr] = line;
222 column_stack[include_stack_ptr] = column;
224 yyin = fopen(text, "rb");
226 fprintf(stderr, "Couldn't open %s\n", text);
230 yy_create_buffer( yyin, YY_BUF_SIZE ) );
234 #define c() {count(yytext, yyleng, YY_START);}
235 #define s(type) {store(type, line, column, yytext, yyleng);}
241 NAME [a-zA-Z_./](-*[a-zA-Z0-9_./])*
242 TWIP (-?[0-9]+(\.([0-9]([05])?)?)?)
243 NUMBER -?[0-9]+(\.[0-9]*)?
245 STRING (\\.|[^\\"\n])*
247 RVALUE \"{STRING}\"|([^ \n\r\t]+)
251 <BINARY>\] {c();BEGIN(0);}
254 {TWIP}/[ \n\r\t] {s(TWIP);c();BEGIN(0);}
255 {NUMBER}/[ \n\r\t] {s(NUMBER);c();BEGIN(0);}
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);}
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);}
272 . {char c,c1=yytext[0];
273 printf("Syntax error in line %d, %d: %s", line, column, yytext);
276 if(c=='\n' || c==EOF)
280 if(c1>='0' && c1<='9')
281 printf(" (identifiers must not start with a digit)");
287 if ( --include_stack_ptr < 0 ) {
291 yy_delete_buffer( YY_CURRENT_BUFFER );
293 include_stack[include_stack_ptr] );
294 column = column_stack[include_stack_ptr];
295 line = line_stack[include_stack_ptr];
306 void freeTokens(struct token_t*file)
312 struct token_t* generateTokens(char*filename)
314 FILE*fi = fopen(filename, "rb");
316 struct token_t*result;
319 printf("Couldn't find file %s\n", filename);
326 mem_put(&strings, &t, 1); //hack- make all valid strings start at position >0
332 yy_delete_buffer(yy_current_buffer);
334 result = (struct token_t*)tokens.buffer;
335 num = tokens.pos/sizeof(struct token_t);
337 for(t=0;t<tokens.pos/sizeof(struct token_t);t++) {
339 result[t].text += (int)strings.buffer;