8 #include "../lib/utf8.h"
10 //RVALUE {NUMBER}|{PERCENT}|{NAME}|\"{STRING}\"|{DIM}
11 //<a>. {printf("<a>%s\n", yytext);}
12 // %x: exclusive, %s: inclusive
13 char*type_names[] = {"twip","number","command","string","assignment","identifier","label","end"};
20 static void count(char*text, int len, int condition)
33 static char*prefix = 0;
35 static void unescapeString(string_t * tmp)
38 /* fixme - this routine expects the string to be
41 for (p1=(char*)tmp->str; (p=strchr(p1, '\\')); p1 = p+1)
47 case '\\': p[0] = '\\'; break;
48 case '"': p[0] = '"'; break;
49 case 'b': p[0] = '\b'; break;
50 case 'f': p[0] = '\f'; break;
51 case 'n': p[0] = '\n'; break;
52 case 'r': p[0] = '\r'; break;
53 case 't': p[0] = '\t'; break;
62 bracket = 1;nr++;max++;
64 while(strchr("0123456789abcdefABCDEF", p[nr]) && (bracket || nr < max)) {
66 if(p[nr]>='0' && p[nr]<='9') num |= p[nr] - '0';
67 if(p[nr]>='a' && p[nr]<='f') num |= p[nr] - 'a' + 10;
68 if(p[nr]>='A' && p[nr]<='F') num |= p[nr] - 'A' + 10;
71 if(bracket && p[nr]=='}') {
77 memcpy(p, utf8, new); // do not copy the terminating zero
86 char*to=p+new,*from=p+nr;
96 static void store(enum type_t type, int line, int column, char*text, int length)
102 token.column = column;
103 //printf("->%d(%s) %s\n", type, type_names[type], text);fflush(stdout);
108 string_set2(&tmp, "", 0);
109 token.text = (char*)mem_putstring(&strings, tmp);
112 string_set2(&tmp, text+1, length-2);
113 unescapeString(&tmp);
114 token.text = (char*)mem_putstring(&strings, tmp);
119 string_set2(&tmp, text, length);
122 token.text = (char*)mem_put(&strings, prefix, strlen(prefix));
123 mem_putstring(&strings, tmp);
125 token.text = (char*)mem_putstring(&strings, tmp);
130 string_set2(&tmp, text+1/*:*/, length-5/*.end*/);
131 token.text = (char*)mem_putstring(&strings, tmp);
134 string_set2(&tmp, text+1, length-1);
135 token.text = (char*)mem_putstring(&strings, tmp);
138 char*x = &text[length-1];
139 if(x[-1] == '-' || x[-1] == '+')
141 do{x--;} while(*x==32 || *x==10 || *x==13 || *x=='\t');
143 string_set2(&tmp, text, x-text);
144 token.text = (char*)mem_putstring(&strings, tmp);
145 /*char*y,*x = strchr(text, '=');
148 do{y--;} while(*y==32 || *y==10 || *y==13 || *y=='\t');
149 do{x++;} while(*x==32 || *x==10 || *x==13 || *x=='\t');
150 token.text1 = (char*)put(&strings, text, y-text + 1, 1);
151 token.text2 = (char*)put(&strings, x, length-(x-text), 1);*/
155 mem_put(&tokens, &token, sizeof(struct token_t));
159 #define MAX_INCLUDE_DEPTH 16
160 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
161 int line_stack[MAX_INCLUDE_DEPTH];
162 int column_stack[MAX_INCLUDE_DEPTH];
163 int include_stack_ptr = 0;
165 void handleInclude(char*text, int len)
168 while(len >=1 && (text[0] == ' ' || text[0] == '\t')) {
172 (text[len-1] == ' ' ||
173 text[len-1] == '\r' ||
174 text[len-1] == '\n')) {
177 if(len >= 2 && text[0] == '"' && text[len-1] == '"') {
181 if(include_stack_ptr >= MAX_INCLUDE_DEPTH) {
182 fprintf( stderr, "Includes nested too deeply" );
185 include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
186 line_stack[include_stack_ptr] = line;
187 column_stack[include_stack_ptr] = column;
189 yyin = fopen(text, "rb");
191 fprintf(stderr, "Couldn't open %s\n", text);
194 yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );
204 #define c() {count(yytext, yyleng, YY_START);}
205 #define s(type) {store(type, line, column, yytext, yyleng);}
211 NAME [a-zA-Z_./](-*[a-zA-Z0-9_./])*
212 TWIP (-?[0-9]+(\.([0-9]([05])?)?)?)
213 NUMBER -?[0-9]+(\.[0-9]*)?
215 STRING (\\.|[^\\"\n])*
217 RVALUE \"{STRING}\"|([^ \n\r\t]+)
221 <BINARY>\] {c();BEGIN(0);}
224 {TWIP}/[ \n\r\t] {s(TWIP);c();BEGIN(0);}
225 {NUMBER}/[ \n\r\t] {s(NUMBER);c();BEGIN(0);}
227 [ \t\r]#[^\n]*\n {c();}
228 \"{STRING}\" {s(STRING);c();BEGIN(0);}
229 \"{STRING}$ {c();printf("unterminated string in line %d: %s\n", line, yytext);exit(1);yyterminate();}
230 {NAME}{S}*\+= {s(ASSIGNMENT);prefix="<plus>";c();BEGIN(R);}
231 {NAME}{S}*-= {s(ASSIGNMENT);prefix="<minus>";c();BEGIN(R);}
232 {NAME}{S}*= {s(ASSIGNMENT);c();BEGIN(R);}
233 <R>{ /* values which appear only on the right-hand side of assignments, like: x=50% */
234 [^ :\n\t\r]* {s(IDENTIFIER);c();BEGIN(0);}
236 \.include{S}.*\n {handleInclude(yytext, yyleng);}
237 \.{NAME} {s(COMMAND);c();}
238 :([^.]|\.[^e]|\.e[^n]|\.en[^d]|\.end[^ \n\r\t]|[ \n\r\t])*\.end {s(RAWDATA);c();}
239 {NAME} {s(IDENTIFIER);c();}
240 "[" {c();BEGIN(BINARY);}
242 . {char c,c1=yytext[0];
243 printf("Syntax error in line %d, %d: %s", line, column, yytext);
246 if(c=='\n' || c==EOF)
250 if(c1>='0' && c1<='9')
251 printf(" (identifiers must not start with a digit)");
257 if ( --include_stack_ptr < 0 ) {
261 yy_delete_buffer( YY_CURRENT_BUFFER );
263 include_stack[include_stack_ptr] );
264 column = column_stack[include_stack_ptr];
265 line = line_stack[include_stack_ptr];
276 void freeTokens(struct token_t*file)
282 struct token_t* generateTokens(char*filename)
286 struct token_t*result;
292 if(!strcmp(filename,"-"))
295 fi = fopen(filename, "rb");
298 printf("Couldn't find file %s\n", filename);
305 mem_put(&strings, &t, 1); //hack- make all valid strings start at position >0
311 #ifdef YY_CURRENT_BUFFER
312 // some newer flex versions require it like this:
313 yy_delete_buffer(YY_CURRENT_BUFFER);
315 yy_delete_buffer(yy_current_buffer);
318 result = (struct token_t*)tokens.buffer;
319 num = tokens.pos/sizeof(struct token_t);
321 for(t=0;t<tokens.pos/sizeof(struct token_t);t++) {
323 result[t].text += (int)strings.buffer;