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)
36 for (p1=tmp->str; (p=strchr(p1, '\\')) != 0; p1 = p+1)
40 case '\\': p[1] = '\\'; break;
41 case 'b': p[1] = '\b'; break;
42 case 'f': p[1] = '\f'; break;
43 case 'n': p[1] = '\n'; break;
44 case 'r': p[1] = '\r'; break;
45 case 't': p[1] = '\t'; break;
53 static void store(enum type_t type, int line, int column, char*text, int length)
59 token.column = column;
60 //printf("->%d(%s) %s\n", type, type_names[type], text);fflush(stdout);
65 string_set2(&tmp, "", 0);
66 token.text = (char*)mem_putstring(&strings, tmp);
69 string_set2(&tmp, text+1, length-2);
71 token.text = (char*)mem_putstring(&strings, tmp);
76 string_set2(&tmp, text, length);
79 token.text = (char*)mem_put(&strings, prefix, strlen(prefix));
80 mem_putstring(&strings, tmp);
82 token.text = (char*)mem_putstring(&strings, tmp);
87 string_set2(&tmp, text, length-1);
88 token.text = (char*)mem_putstring(&strings, tmp);
91 string_set2(&tmp, text+1, length-1);
92 token.text = (char*)mem_putstring(&strings, tmp);
95 char*x = &text[length-1];
96 if(x[-1] == '-' || x[-1] == '+')
98 do{x--;} while(*x==32 || *x==10 || *x==13 || *x=='\t');
100 string_set2(&tmp, text, x-text);
101 token.text = (char*)mem_putstring(&strings, tmp);
102 /*char*y,*x = strchr(text, '=');
105 do{y--;} while(*y==32 || *y==10 || *y==13 || *y=='\t');
106 do{x++;} while(*x==32 || *x==10 || *x==13 || *x=='\t');
107 token.text1 = (char*)put(&strings, text, y-text + 1, 1);
108 token.text2 = (char*)put(&strings, x, length-(x-text), 1);*/
112 mem_put(&tokens, &token, sizeof(struct token_t));
116 #define c() {count(yytext, yyleng, YY_START);}
117 #define s(type) {store(type, line, column, yytext, yyleng);}
123 NAME [a-zA-Z_./](-*[a-zA-Z0-9_./])*
124 TWIP ([0-9]+(\.([0-9]([05])?)?)?)
125 NUMBER [0-9]+(\.[0-9]*)?
127 STRING (\\.|[^\\"\n])*
129 RVALUE \"{STRING}\"|([^ \n\r\t]+)
133 <BINARY>\] {c();BEGIN(0);}
136 {TWIP}/[ \n\r\t] {s(TWIP);c();BEGIN(0);}
137 {NUMBER}/[ \n\r\t] {s(NUMBER);c();BEGIN(0);}
139 [ \t\r]#[^\n]*\n {c();}
140 \"{STRING}\" {s(STRING);c();BEGIN(0);}
141 \"{STRING}$ {c();printf("unterminated string in line %d: %s\n", line, yytext);exit(1);yyterminate();}
142 {NAME}{S}*\+= {s(ASSIGNMENT);prefix="<plus>";c();BEGIN(R);}
143 {NAME}{S}*-= {s(ASSIGNMENT);prefix="<minus>";c();BEGIN(R);}
144 {NAME}{S}*= {s(ASSIGNMENT);c();BEGIN(R);}
145 <R>{ /* values which appear only on the right-hand side of assignments, like: x=50% */
146 [^ \n\t\r]* {s(IDENTIFIER);c();BEGIN(0);}
148 \.{NAME} {s(COMMAND);c();}
149 {NAME}{S}*: {s(LABEL);c();}
150 {NAME} {s(IDENTIFIER);c();}
151 "[" {c();BEGIN(BINARY);}
154 printf("Syntax error in line %d, %d: %s", line, column, yytext);
158 if(c=='\n' || c==EOF)
162 if(c1>='0' && c1<='9')
163 printf(" (identifiers must not start with a digit)");
168 <<EOF>> {c();s(END);yyterminate();}
176 void freeTokens(struct token_t*file)
182 struct token_t* generateTokens(char*filename)
184 FILE*fi = fopen(filename, "rb");
186 struct token_t*result;
189 printf("Couldn't find file %s\n", filename);
196 mem_put(&strings, &t, 1); //hack- make all valid strings start at position >0
202 yy_delete_buffer(yy_current_buffer);
204 result = (struct token_t*)tokens.buffer;
205 num = tokens.pos/sizeof(struct token_t);
207 for(t=0;t<tokens.pos/sizeof(struct token_t);t++) {
209 result[t].text += (int)strings.buffer;