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 store(enum type_t type, int line, int column, char*text, int length)
38 token.column = column;
39 //printf("->%d(%s) %s\n", type, type_names[type], text);fflush(stdout);
44 string_set2(&tmp, "", 0);
45 token.text = (char*)mem_putstring(&strings, tmp);
48 string_set2(&tmp, text+1, length-2);
49 token.text = (char*)mem_putstring(&strings, tmp);
54 string_set2(&tmp, text, length);
57 token.text = (char*)mem_put(&strings, prefix, strlen(prefix));
58 mem_putstring(&strings, tmp);
60 token.text = (char*)mem_putstring(&strings, tmp);
65 string_set2(&tmp, text, length-1);
66 token.text = (char*)mem_putstring(&strings, tmp);
69 string_set2(&tmp, text+1, length-1);
70 token.text = (char*)mem_putstring(&strings, tmp);
73 char*x = &text[length-1];
74 if(x[-1] == '-' || x[-1] == '+')
76 do{x--;} while(*x==32 || *x==10 || *x==13 || *x=='\t');
78 string_set2(&tmp, text, x-text);
79 token.text = (char*)mem_putstring(&strings, tmp);
80 /*char*y,*x = strchr(text, '=');
83 do{y--;} while(*y==32 || *y==10 || *y==13 || *y=='\t');
84 do{x++;} while(*x==32 || *x==10 || *x==13 || *x=='\t');
85 token.text1 = (char*)put(&strings, text, y-text + 1, 1);
86 token.text2 = (char*)put(&strings, x, length-(x-text), 1);*/
90 mem_put(&tokens, &token, sizeof(struct token_t));
94 #define c() {count(yytext, yyleng, YY_START);}
95 #define s(type) {store(type, line, column, yytext, yyleng);}
101 NAME [a-zA-Z_./](-*[a-zA-Z0-9_./])*
102 TWIP ([0-9]+(\.([0-9]([05])?)?)?)
103 NUMBER [0-9]+(\.[0-9]*)?
105 STRING (\\.|[^\\"\n])*
107 RVALUE \"{STRING}\"|([^ \n\r\t]+)
111 <BINARY>\] {c();BEGIN(0);}
114 {TWIP}/[ \n\r\t] {s(TWIP);c();BEGIN(0);}
115 {NUMBER}/[ \n\r\t] {s(NUMBER);c();BEGIN(0);}
117 [ \t\r]#[^\n]*\n {c();}
118 \"{STRING}\" {s(STRING);c();BEGIN(0);}
119 \"{STRING}$ {c();printf("unterminated string in line %d: %s\n", line, yytext);exit(1);yyterminate();}
120 {NAME}{S}*\+= {s(ASSIGNMENT);prefix="<plus>";c();BEGIN(R);}
121 {NAME}{S}*-= {s(ASSIGNMENT);prefix="<minus>";c();BEGIN(R);}
122 {NAME}{S}*= {s(ASSIGNMENT);c();BEGIN(R);}
123 <R>{ /* values which appear only on the right-hand side of assignments, like: x=50% */
124 [^ \n\t\r]* {s(IDENTIFIER);c();BEGIN(0);}
126 \.{NAME} {s(COMMAND);c();}
127 {NAME}{S}*: {s(LABEL);c();}
128 {NAME} {s(IDENTIFIER);c();}
129 "[" {c();BEGIN(BINARY);}
132 printf("Syntax error in line %d, %d: %s", line, column, yytext);
136 if(c=='\n' || c==EOF)
140 if(c1>='0' && c1<='9')
141 printf(" (identifiers must not start with a digit)");
146 <<EOF>> {c();s(END);yyterminate();}
154 void freeTokens(struct token_t*file)
160 struct token_t* generateTokens(char*filename)
162 FILE*fi = fopen(filename, "rb");
164 struct token_t*result;
167 printf("Couldn't find file %s\n", filename);
174 mem_put(&strings, &t, 1); //hack- make all valid strings start at position >0
180 yy_delete_buffer(yy_current_buffer);
182 result = (struct token_t*)tokens.buffer;
183 num = tokens.pos/sizeof(struct token_t);
185 for(t=0;t<tokens.pos/sizeof(struct token_t);t++) {
187 result[t].text += (int)strings.buffer;