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 void unescapeString(string_t * tmp)
37 /* fixme - this routine expects the string to be
40 for (p1=tmp->str; (p=strchr(p1, '\\')); p1 = p+1)
44 case '\\': p[1] = '\\'; tmp->len--; break;
45 case '"': p[1] = '"'; tmp->len--; break;
46 case 'b': p[1] = '\b'; tmp->len--; break;
47 case 'f': p[1] = '\f'; tmp->len--; break;
48 case 'n': p[1] = '\n'; tmp->len--; break;
49 case 'r': p[1] = '\r'; tmp->len--; break;
50 case 't': p[1] = '\t'; tmp->len--; break;
58 static void store(enum type_t type, int line, int column, char*text, int length)
64 token.column = column;
65 //printf("->%d(%s) %s\n", type, type_names[type], text);fflush(stdout);
70 string_set2(&tmp, "", 0);
71 token.text = (char*)mem_putstring(&strings, tmp);
74 string_set2(&tmp, text+1, length-2);
76 token.text = (char*)mem_putstring(&strings, tmp);
81 string_set2(&tmp, text, length);
84 token.text = (char*)mem_put(&strings, prefix, strlen(prefix));
85 mem_putstring(&strings, tmp);
87 token.text = (char*)mem_putstring(&strings, tmp);
92 string_set2(&tmp, text+1/*:*/, length-5/*.end*/);
93 token.text = (char*)mem_putstring(&strings, tmp);
96 string_set2(&tmp, text+1, length-1);
97 token.text = (char*)mem_putstring(&strings, tmp);
100 char*x = &text[length-1];
101 if(x[-1] == '-' || x[-1] == '+')
103 do{x--;} while(*x==32 || *x==10 || *x==13 || *x=='\t');
105 string_set2(&tmp, text, x-text);
106 token.text = (char*)mem_putstring(&strings, tmp);
107 /*char*y,*x = strchr(text, '=');
110 do{y--;} while(*y==32 || *y==10 || *y==13 || *y=='\t');
111 do{x++;} while(*x==32 || *x==10 || *x==13 || *x=='\t');
112 token.text1 = (char*)put(&strings, text, y-text + 1, 1);
113 token.text2 = (char*)put(&strings, x, length-(x-text), 1);*/
117 mem_put(&tokens, &token, sizeof(struct token_t));
121 #define MAX_INCLUDE_DEPTH 16
122 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
123 int line_stack[MAX_INCLUDE_DEPTH];
124 int column_stack[MAX_INCLUDE_DEPTH];
125 int include_stack_ptr = 0;
127 void handleInclude(char*text, int len)
130 while(len >=1 && (text[0] == ' ' || text[0] == '\t')) {
133 while(len >= 1 && (text[len-1] == ' ' || text[len-1] == '\n')) {
136 if(len >= 2 && text[0] == '"' && text[len-1] == '"') {
140 if(include_stack_ptr >= MAX_INCLUDE_DEPTH) {
141 fprintf( stderr, "Includes nested too deeply" );
144 include_stack[include_stack_ptr] = YY_CURRENT_BUFFER;
145 line_stack[include_stack_ptr] = line;
146 column_stack[include_stack_ptr] = column;
148 yyin = fopen(text, "rb");
150 fprintf(stderr, "Couldn't open %s\n", text);
154 yy_create_buffer( yyin, YY_BUF_SIZE ) );
158 #define c() {count(yytext, yyleng, YY_START);}
159 #define s(type) {store(type, line, column, yytext, yyleng);}
165 NAME [a-zA-Z_./](-*[a-zA-Z0-9_./])*
166 TWIP (-?[0-9]+(\.([0-9]([05])?)?)?)
167 NUMBER -?[0-9]+(\.[0-9]*)?
169 STRING (\\.|[^\\"\n])*
171 RVALUE \"{STRING}\"|([^ \n\r\t]+)
175 <BINARY>\] {c();BEGIN(0);}
178 {TWIP}/[ \n\r\t] {s(TWIP);c();BEGIN(0);}
179 {NUMBER}/[ \n\r\t] {s(NUMBER);c();BEGIN(0);}
181 [ \t\r]#[^\n]*\n {c();}
182 \"{STRING}\" {s(STRING);c();BEGIN(0);}
183 \"{STRING}$ {c();printf("unterminated string in line %d: %s\n", line, yytext);exit(1);yyterminate();}
184 {NAME}{S}*\+= {s(ASSIGNMENT);prefix="<plus>";c();BEGIN(R);}
185 {NAME}{S}*-= {s(ASSIGNMENT);prefix="<minus>";c();BEGIN(R);}
186 {NAME}{S}*= {s(ASSIGNMENT);c();BEGIN(R);}
187 <R>{ /* values which appear only on the right-hand side of assignments, like: x=50% */
188 [^ :\n\t\r]* {s(IDENTIFIER);c();BEGIN(0);}
190 \.include{S}.*\n {handleInclude(yytext, yyleng);}
191 \.{NAME} {s(COMMAND);c();}
192 :([^.]|\.[^e]|\.e[^n]|\.en[^d]|[ \n\r\t])*\.end {s(RAWDATA);c();}
193 {NAME} {s(IDENTIFIER);c();}
194 "[" {c();BEGIN(BINARY);}
196 . {char c,c1=yytext[0];
197 printf("Syntax error in line %d, %d: %s", line, column, yytext);
200 if(c=='\n' || c==EOF)
204 if(c1>='0' && c1<='9')
205 printf(" (identifiers must not start with a digit)");
211 if ( --include_stack_ptr < 0 ) {
215 yy_delete_buffer( YY_CURRENT_BUFFER );
217 include_stack[include_stack_ptr] );
218 column = column_stack[include_stack_ptr];
219 line = line_stack[include_stack_ptr];
230 void freeTokens(struct token_t*file)
236 struct token_t* generateTokens(char*filename)
238 FILE*fi = fopen(filename, "rb");
240 struct token_t*result;
243 printf("Couldn't find file %s\n", filename);
250 mem_put(&strings, &t, 1); //hack- make all valid strings start at position >0
256 yy_delete_buffer(yy_current_buffer);
258 result = (struct token_t*)tokens.buffer;
259 num = tokens.pos/sizeof(struct token_t);
261 for(t=0;t<tokens.pos/sizeof(struct token_t);t++) {
263 result[t].text += (int)strings.buffer;