7 //RVALUE {NUMBER}|{PERCENT}|{NAME}|\"{STRING}\"|{DIM}
8 //<a>. {printf("<a>%s\n", yytext);}
9 // %x: exclusive, %s: inclusive
10 char*type_names[] = {"twip","number","command","string","assignment","identifier","label","end"};
17 static void count(char*text, int len, int condition)
30 static char*prefix = 0;
32 static void unescapeString(string_t * tmp)
35 /* fixme - this routine expects the string to be
38 for (p1=tmp->str; (p=strchr(p1, '\\')); p1 = p+1)
42 case '\\': p[1] = '\\'; tmp->len--; break;
43 case '"': p[1] = '"'; tmp->len--; break;
44 case 'b': p[1] = '\b'; tmp->len--; break;
45 case 'f': p[1] = '\f'; tmp->len--; break;
46 case 'n': p[1] = '\n'; tmp->len--; break;
47 case 'r': p[1] = '\r'; tmp->len--; break;
48 case 't': p[1] = '\t'; tmp->len--; break;
56 static void store(enum type_t type, int line, int column, char*text, int length)
62 token.column = column;
63 //printf("->%d(%s) %s\n", type, type_names[type], text);fflush(stdout);
68 string_set2(&tmp, "", 0);
69 token.text = (char*)mem_putstring(&strings, tmp);
72 string_set2(&tmp, text+1, length-2);
74 token.text = (char*)mem_putstring(&strings, tmp);
79 string_set2(&tmp, text, length);
82 token.text = (char*)mem_put(&strings, prefix, strlen(prefix));
83 mem_putstring(&strings, tmp);
85 token.text = (char*)mem_putstring(&strings, tmp);
90 string_set2(&tmp, text, length-1);
91 token.text = (char*)mem_putstring(&strings, tmp);
94 string_set2(&tmp, text+1, length-1);
95 token.text = (char*)mem_putstring(&strings, tmp);
98 char*x = &text[length-1];
99 if(x[-1] == '-' || x[-1] == '+')
101 do{x--;} while(*x==32 || *x==10 || *x==13 || *x=='\t');
103 string_set2(&tmp, text, x-text);
104 token.text = (char*)mem_putstring(&strings, tmp);
105 /*char*y,*x = strchr(text, '=');
108 do{y--;} while(*y==32 || *y==10 || *y==13 || *y=='\t');
109 do{x++;} while(*x==32 || *x==10 || *x==13 || *x=='\t');
110 token.text1 = (char*)put(&strings, text, y-text + 1, 1);
111 token.text2 = (char*)put(&strings, x, length-(x-text), 1);*/
115 mem_put(&tokens, &token, sizeof(struct token_t));
119 #define MAX_INCLUDE_DEPTH 16
120 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
121 int include_stack_ptr = 0;
123 void handleInclude(char*text, int len)
126 while(len >=1 && (text[0] == ' ' || text[0] == '\t')) {
129 while(len >= 1 && (text[len-1] == ' ' || text[len-1] == '\n')) {
132 if(len >= 2 && text[0] == '"' && text[len-1] == '"') {
136 if(include_stack_ptr >= MAX_INCLUDE_DEPTH) {
137 fprintf( stderr, "Includes nested too deeply" );
140 include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
141 yyin = fopen(text, "rb");
143 fprintf(stderr, "Couldn't open %s\n", text);
147 yy_create_buffer( yyin, YY_BUF_SIZE ) );
151 #define c() {count(yytext, yyleng, YY_START);}
152 #define s(type) {store(type, line, column, yytext, yyleng);}
158 NAME [a-zA-Z_./](-*[a-zA-Z0-9_./])*
159 TWIP ([0-9]+(\.([0-9]([05])?)?)?)
160 NUMBER [0-9]+(\.[0-9]*)?
162 STRING (\\.|[^\\"\n])*
164 RVALUE \"{STRING}\"|([^ \n\r\t]+)
168 <BINARY>\] {c();BEGIN(0);}
171 {TWIP}/[ \n\r\t] {s(TWIP);c();BEGIN(0);}
172 {NUMBER}/[ \n\r\t] {s(NUMBER);c();BEGIN(0);}
174 [ \t\r]#[^\n]*\n {c();}
175 \"{STRING}\" {s(STRING);c();BEGIN(0);}
176 \"{STRING}$ {c();printf("unterminated string in line %d: %s\n", line, yytext);exit(1);yyterminate();}
177 {NAME}{S}*\+= {s(ASSIGNMENT);prefix="<plus>";c();BEGIN(R);}
178 {NAME}{S}*-= {s(ASSIGNMENT);prefix="<minus>";c();BEGIN(R);}
179 {NAME}{S}*= {s(ASSIGNMENT);c();BEGIN(R);}
180 <R>{ /* values which appear only on the right-hand side of assignments, like: x=50% */
181 [^ \n\t\r]* {s(IDENTIFIER);c();BEGIN(0);}
183 \.include{S}.*\n {handleInclude(yytext, yyleng);}
184 \.{NAME} {s(COMMAND);c();}
185 {NAME}{S}*: {s(LABEL);c();}
186 {NAME} {s(IDENTIFIER);c();}
187 "[" {c();BEGIN(BINARY);}
189 . {char c,c1=yytext[0];
190 printf("Syntax error in line %d, %d: %s", line, column, yytext);
193 if(c=='\n' || c==EOF)
197 if(c1>='0' && c1<='9')
198 printf(" (identifiers must not start with a digit)");
204 if ( --include_stack_ptr < 0 ) {
208 yy_delete_buffer( YY_CURRENT_BUFFER );
210 include_stack[include_stack_ptr] );
221 void freeTokens(struct token_t*file)
227 struct token_t* generateTokens(char*filename)
229 FILE*fi = fopen(filename, "rb");
231 struct token_t*result;
234 printf("Couldn't find file %s\n", filename);
241 mem_put(&strings, &t, 1); //hack- make all valid strings start at position >0
247 yy_delete_buffer(yy_current_buffer);
249 result = (struct token_t*)tokens.buffer;
250 num = tokens.pos/sizeof(struct token_t);
252 for(t=0;t<tokens.pos/sizeof(struct token_t);t++) {
254 result[t].text += (int)strings.buffer;