-
Notifications
You must be signed in to change notification settings - Fork 7
/
token.c
287 lines (265 loc) · 4.36 KB
/
token.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include "token.h"
#include <stdlib.h>
#include "str.h"
int tokens_init = 0;
void initTokens()
{
if(tokens_init) return;
//for brevety
const int *ls = query_operation_token_oo_lvls;
char ***lls = &query_operation_tokens_of_oo_lvl[0];
int c = 0;
int si = 0;
int ci = 0;
for(c = 0; c < QUERY_NUM_OO_LEVELS; c++)
{
while(ls[ci] == c) ci++;
lls[ls[si]] = calloc(((ci-si)+1)*sizeof(const char *),1);
for(int j = 0; j < ci-si; j++)
lls[ls[si]][j] = (char *)query_operation_tokens[si+j];
lls[ls[si]][ci-si] = 0;
si = ci;
}
tokens_init = 1;
}
const char *query_operation_tokens[] =
{
"or",
"and",
"not",
"=",
"!=",
"<",
"<=",
">=",
">",
"-",
"+",
"/",
"*",
"%",
"SIN",
"COS",
"TAN",
"ABS",
"INT",
""
};
const int query_operation_token_oo_lvls[] = //manually keep in sync!
{
0, //"or",
1, //"and",
2, //"not",
3, //"=",
3, //"!=",
4, //"<",
4, //"<=",
4, //">=",
4, //">",
5, //"-",
5, //"+",
6, //"/",
6, //"*",
7, //"%",
8, //"SIN",
8, //"COS",
8, //"TAN",
8, //"ABS",
8, //"INT",
0 //""
};
char **query_operation_tokens_of_oo_lvl[QUERY_NUM_OO_LEVELS];
const char *query_property_tokens[] =
{
"row",
"col",
"r",
"g",
"b",
"a",
""
};
int isTokenType(char *t, char **type)
{
int i = 0;
while(type[i] != 0)
{
if(cmpLower(t,type[i]) == 0) return 1;
i++;
}
return 0;
}
int isTokenLevel(char *t, int l)
{
return isTokenType(t, query_operation_tokens_of_oo_lvl[l]);
}
int readToken(char *s, int offset, char *buff)
{
int buff_i = 0;
int str_i = 0;
char c;
c = '0'; //something other than null
while(c != '\0') //blow past leading whitespace
{
c = s[offset+str_i];
switch(c)
{
//whitespace
case ' ':
case '\t':
case '\n':
str_i++;
break;
default:
c = '\0';
break;
}
}
c = '0'; //something other than null
while(c != '\0')
{
c = s[offset+str_i];
switch(c)
{
//EOS
case '\0':
{
buff[buff_i] = '\0';
c = '\0';
}
break;
//whitespace
case ' ':
case '\t':
case '\n':
{
buff[buff_i] = '\0';
c = '\0';
}
break;
//delimeter
case ';':
case '.':
case ',':
case '(':
case ')':
case '[':
case ']':
case '=':
case '+':
case '-':
case '*':
case '/':
case '%':
{
if(buff_i == 0)
{
buff[buff_i++] = c;
str_i++;
}
buff[buff_i] = '\0';
c = '\0';
}
break;
//ambiguous delimeter
case '!':
case '>':
case '<':
{
if(buff_i == 0)
{
buff[buff_i++] = c;
str_i++;
if(s[offset+str_i] == '=')
{
buff[buff_i++] = '=';
str_i++;
}
}
buff[buff_i] = '\0';
c = '\0';
}
break;
default:
buff[buff_i++] = c;
str_i++;
break;
}
}
c = '0'; //some non-null
while(c != '\0') //blow past ending whitespace
{
c = s[offset+str_i];
switch(c)
{
//whitespace
case ' ':
case '\t':
case '\n':
str_i++;
break;
default:
c = '\0';
break;
}
}
return str_i;
}
int closingParen(char *q, int s, int e)
{
tokinit;
char open_t[2] = {'\0','\0'};
char close_t[2] = {'\0','\0'};
tok;
if(teq("("))
{
open_t[0] = '(';
close_t[0] = ')';
}
else if(teq("["))
{
open_t[0] = '[';
close_t[0] = ']';
}
else return -1;//err("Expected '('");
int n_parens = 1;
while(n_parens > 0 && o < e)
{
commit;
tok;
if(teq(open_t)) n_parens++;
if(teq(close_t)) n_parens--;
}
return o-s;
}
int tokenAfterParenExpress(char *q, int s, int e)
{
tokinit;
l = closingParen(q,o,e);
commit;
tok;
if(!(teq(")")) && !(teq("]"))) return -1;//err("Expected ')'");
commit;
return o-s;
}
int lastTokenLevelInRange(char *q, int s, int e, int level)
{
tokinit;
int last = -1;
while(o < e)
{
tok;
if(teq("(") || teq("["))
{
l = tokenAfterParenExpress(q,o,e);
commit;
}
else
{
if(isTokenType(token, query_operation_tokens_of_oo_lvl[level]))
last = o;
commit;
}
}
return last-s;
}