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 c() {count(yytext, yyleng, YY_START);}
120 #define s(type) {store(type, line, column, yytext, yyleng);}
126 NAME [a-zA-Z_./](-*[a-zA-Z0-9_./])*
127 TWIP ([0-9]+(\.([0-9]([05])?)?)?)
128 NUMBER [0-9]+(\.[0-9]*)?
130 STRING (\\.|[^\\"\n])*
132 RVALUE \"{STRING}\"|([^ \n\r\t]+)
136 <BINARY>\] {c();BEGIN(0);}
139 {TWIP}/[ \n\r\t] {s(TWIP);c();BEGIN(0);}
140 {NUMBER}/[ \n\r\t] {s(NUMBER);c();BEGIN(0);}
142 [ \t\r]#[^\n]*\n {c();}
143 \"{STRING}\" {s(STRING);c();BEGIN(0);}
144 \"{STRING}$ {c();printf("unterminated string in line %d: %s\n", line, yytext);exit(1);yyterminate();}
145 {NAME}{S}*\+= {s(ASSIGNMENT);prefix="<plus>";c();BEGIN(R);}
146 {NAME}{S}*-= {s(ASSIGNMENT);prefix="<minus>";c();BEGIN(R);}
147 {NAME}{S}*= {s(ASSIGNMENT);c();BEGIN(R);}
148 <R>{ /* values which appear only on the right-hand side of assignments, like: x=50% */
149 [^ \n\t\r]* {s(IDENTIFIER);c();BEGIN(0);}
151 \.{NAME} {s(COMMAND);c();}
152 {NAME}{S}*: {s(LABEL);c();}
153 {NAME} {s(IDENTIFIER);c();}
154 "[" {c();BEGIN(BINARY);}
157 printf("Syntax error in line %d, %d: %s", line, column, yytext);
161 if(c=='\n' || c==EOF)
165 if(c1>='0' && c1<='9')
166 printf(" (identifiers must not start with a digit)");
171 <<EOF>> {c();s(END);yyterminate();}
179 void freeTokens(struct token_t*file)
185 struct token_t* generateTokens(char*filename)
187 FILE*fi = fopen(filename, "rb");
189 struct token_t*result;
192 printf("Couldn't find file %s\n", filename);
199 mem_put(&strings, &t, 1); //hack- make all valid strings start at position >0
205 yy_delete_buffer(yy_current_buffer);
207 result = (struct token_t*)tokens.buffer;
208 num = tokens.pos/sizeof(struct token_t);
210 for(t=0;t<tokens.pos/sizeof(struct token_t);t++) {
212 result[t].text += (int)strings.buffer;