| | varnish-cache/lib/libvcc/vcc_token.c |
0 |
|
/*- |
1 |
|
* Copyright (c) 2006 Verdens Gang AS |
2 |
|
* Copyright (c) 2006-2011 Varnish Software AS |
3 |
|
* All rights reserved. |
4 |
|
* |
5 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
6 |
|
* |
7 |
|
* SPDX-License-Identifier: BSD-2-Clause |
8 |
|
* |
9 |
|
* Redistribution and use in source and binary forms, with or without |
10 |
|
* modification, are permitted provided that the following conditions |
11 |
|
* are met: |
12 |
|
* 1. Redistributions of source code must retain the above copyright |
13 |
|
* notice, this list of conditions and the following disclaimer. |
14 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
15 |
|
* notice, this list of conditions and the following disclaimer in the |
16 |
|
* documentation and/or other materials provided with the distribution. |
17 |
|
* |
18 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
19 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
22 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
24 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
25 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
26 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
27 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
28 |
|
* SUCH DAMAGE. |
29 |
|
*/ |
30 |
|
|
31 |
|
#include "config.h" |
32 |
|
|
33 |
|
#include <stdlib.h> |
34 |
|
#include <string.h> |
35 |
|
|
36 |
|
#include "vcc_compile.h" |
37 |
|
|
38 |
|
#include "venc.h" |
39 |
|
#include "vct.h" |
40 |
|
|
41 |
|
/*--------------------------------------------------------------------*/ |
42 |
|
|
43 |
|
void |
44 |
1320 |
vcc_ErrToken(const struct vcc *tl, const struct token *t) |
45 |
|
{ |
46 |
|
|
47 |
1320 |
if (t->tok == EOI) |
48 |
0 |
VSB_cat(tl->sb, "end of input"); |
49 |
1320 |
else if (t->tok == CSRC) |
50 |
0 |
VSB_cat(tl->sb, "C{ ... }C"); |
51 |
|
else |
52 |
1320 |
VSB_printf(tl->sb, "'%.*s'", PF(t)); |
53 |
1320 |
} |
54 |
|
|
55 |
|
void |
56 |
0 |
vcc__ErrInternal(struct vcc *tl, const char *func, unsigned line) |
57 |
|
{ |
58 |
|
|
59 |
0 |
VSB_printf(tl->sb, "VCL compiler internal error at %s():%u\n", |
60 |
0 |
func, line); |
61 |
0 |
tl->err = 1; |
62 |
0 |
} |
63 |
|
|
64 |
|
/*-------------------------------------------------------------------- |
65 |
|
* Find start of source-line of token |
66 |
|
*/ |
67 |
|
|
68 |
|
static void |
69 |
25920 |
vcc_iline(const struct token *t, const char **ll, int tail) |
70 |
|
{ |
71 |
|
const char *p, *b, *x; |
72 |
|
|
73 |
25920 |
b = t->src->b; |
74 |
25920 |
if (ll != NULL) |
75 |
25920 |
*ll = b; |
76 |
25920 |
x = tail ? t->e - 1 : t->b; |
77 |
11773840 |
for (p = b; p < x; p++) { |
78 |
11747920 |
if (*p == '\n') { |
79 |
323720 |
if (ll != NULL) |
80 |
323720 |
*ll = p + 1; |
81 |
323720 |
} |
82 |
11747920 |
} |
83 |
25920 |
} |
84 |
|
|
85 |
|
/*-------------------------------------------------------------------- |
86 |
|
* Find and print src+line+pos of this token |
87 |
|
*/ |
88 |
|
|
89 |
|
static void |
90 |
4070680 |
vcc_icoord(struct vsb *vsb, const struct token *t, int tail) |
91 |
|
{ |
92 |
|
unsigned lin, pos; |
93 |
|
const char *p, *b, *x; |
94 |
|
|
95 |
4070680 |
lin = 1; |
96 |
4070680 |
pos = 0; |
97 |
4070680 |
b = t->src->b; |
98 |
4070680 |
x = tail ? t->e - 1 : t->b; |
99 |
15758372190 |
for (p = b; p < x; p++) { |
100 |
15754301510 |
if (*p == '\n') { |
101 |
652872720 |
lin++; |
102 |
652872720 |
pos = 0; |
103 |
15754301510 |
} else if (*p == '\t') { |
104 |
319734320 |
pos &= ~7; |
105 |
319734320 |
pos += 8; |
106 |
319734320 |
} else |
107 |
14781694470 |
pos++; |
108 |
15754301510 |
} |
109 |
4070680 |
VSB_cat(vsb, "("); |
110 |
4070680 |
if (tail < 2) |
111 |
4065680 |
VSB_printf(vsb, "'%s' Line %u ", t->src->name, lin); |
112 |
4070680 |
VSB_printf(vsb, "Pos %u)", pos + 1); |
113 |
4070680 |
} |
114 |
|
|
115 |
|
/*--------------------------------------------------------------------*/ |
116 |
|
|
117 |
|
void |
118 |
4044760 |
vcc_Coord(const struct vcc *tl, struct vsb *vsb, const struct token *t) |
119 |
|
{ |
120 |
|
|
121 |
4044760 |
if (t == NULL) |
122 |
4044760 |
t = tl->t; |
123 |
4044760 |
vcc_icoord(vsb, t, 0); |
124 |
4044760 |
} |
125 |
|
|
126 |
|
/*-------------------------------------------------------------------- |
127 |
|
* Output one line of source code, starting at 'l' and ending at the |
128 |
|
* first NL or 'le'. |
129 |
|
*/ |
130 |
|
|
131 |
|
static void |
132 |
20920 |
vcc_quoteline(const struct vcc *tl, const char *l, const char *le) |
133 |
|
{ |
134 |
|
const char *p; |
135 |
|
unsigned x, y; |
136 |
|
|
137 |
20920 |
x = y = 0; |
138 |
711674 |
for (p = l; p < le && *p != '\n'; p++) { |
139 |
690754 |
if (*p == '\t') { |
140 |
27640 |
y &= ~7; |
141 |
27640 |
y += 8; |
142 |
248760 |
while (x < y) { |
143 |
221120 |
VSB_putc(tl->sb, ' '); |
144 |
221120 |
x++; |
145 |
|
} |
146 |
27640 |
} else { |
147 |
663114 |
x++; |
148 |
663114 |
y++; |
149 |
663114 |
VSB_putc(tl->sb, *p); |
150 |
|
} |
151 |
690754 |
} |
152 |
20920 |
VSB_putc(tl->sb, '\n'); |
153 |
20920 |
} |
154 |
|
|
155 |
|
/*-------------------------------------------------------------------- |
156 |
|
* Output a marker line for a sourceline starting at 'l' and ending at |
157 |
|
* the first NL or 'le'. Characters between 'b' and 'e' are marked. |
158 |
|
*/ |
159 |
|
|
160 |
|
static void |
161 |
20920 |
vcc_markline(const struct vcc *tl, const char *l, const char *le, |
162 |
|
const char *b, const char *e) |
163 |
|
{ |
164 |
|
const char *p; |
165 |
|
unsigned x, y; |
166 |
|
char c; |
167 |
|
|
168 |
20920 |
x = y = 0; |
169 |
711674 |
for (p = l; p < le && *p != '\n'; p++) { |
170 |
690754 |
if (p >= b && p < e) |
171 |
163247 |
c = '#'; |
172 |
|
else |
173 |
527507 |
c = '-'; |
174 |
|
|
175 |
690754 |
if (*p == '\t') { |
176 |
27640 |
y &= ~7; |
177 |
27640 |
y += 8; |
178 |
27640 |
} else |
179 |
663114 |
y++; |
180 |
1574988 |
while (x < y) { |
181 |
884234 |
VSB_putc(tl->sb, c); |
182 |
884234 |
x++; |
183 |
|
} |
184 |
690754 |
} |
185 |
20920 |
VSB_putc(tl->sb, '\n'); |
186 |
20920 |
} |
187 |
|
|
188 |
|
void |
189 |
5560 |
vcc_Warn(struct vcc *tl) |
190 |
|
{ |
191 |
|
|
192 |
5560 |
AN(tl); |
193 |
5560 |
AN(tl->err); |
194 |
5560 |
VSB_cat(tl->sb, "(That was just a warning)\n"); |
195 |
5560 |
tl->err = 0; |
196 |
5560 |
} |
197 |
|
|
198 |
|
/*--------------------------------------------------------------------*/ |
199 |
|
/* XXX: should take first+last token */ |
200 |
|
|
201 |
|
void |
202 |
5080 |
vcc_ErrWhere2(struct vcc *tl, const struct token *t, const struct token *t2) |
203 |
|
{ |
204 |
|
const char *l1, *l2, *l3; |
205 |
|
|
206 |
5080 |
if (t == NULL) { |
207 |
40 |
vcc_ErrWhere(tl, t2); |
208 |
40 |
return; |
209 |
|
} |
210 |
5040 |
vcc_iline(t, &l1, 0); |
211 |
5040 |
t2 = VTAILQ_PREV(t2, tokenhead, list); |
212 |
5040 |
vcc_iline(t2, &l2, 1); |
213 |
|
|
214 |
|
|
215 |
5040 |
if (l1 == l2) { |
216 |
5000 |
vcc_icoord(tl->sb, t, 0); |
217 |
5000 |
VSB_cat(tl->sb, " -- "); |
218 |
5000 |
vcc_icoord(tl->sb, t2, 2); |
219 |
5000 |
VSB_putc(tl->sb, '\n'); |
220 |
|
/* Two tokens on same line */ |
221 |
5000 |
vcc_quoteline(tl, l1, t->src->e); |
222 |
5000 |
vcc_markline(tl, l1, t->src->e, t->b, t2->e); |
223 |
5000 |
} else { |
224 |
|
/* Two tokens different lines */ |
225 |
40 |
l3 = strchr(l1, '\n'); |
226 |
40 |
AN(l3); |
227 |
|
/* XXX: t had better be before t2 */ |
228 |
40 |
vcc_icoord(tl->sb, t, 0); |
229 |
40 |
if (l3 + 1 == l2) { |
230 |
0 |
VSB_cat(tl->sb, " -- "); |
231 |
0 |
vcc_icoord(tl->sb, t2, 1); |
232 |
0 |
} |
233 |
40 |
VSB_putc(tl->sb, '\n'); |
234 |
40 |
vcc_quoteline(tl, l1, t->src->e); |
235 |
40 |
vcc_markline(tl, l1, t->src->e, t->b, t2->e); |
236 |
40 |
if (l3 + 1 != l2) { |
237 |
40 |
VSB_cat(tl->sb, "[...]\n"); |
238 |
40 |
vcc_icoord(tl->sb, t2, 1); |
239 |
40 |
VSB_putc(tl->sb, '\n'); |
240 |
40 |
} |
241 |
40 |
vcc_quoteline(tl, l2, t->src->e); |
242 |
40 |
vcc_markline(tl, l2, t->src->e, t->b, t2->e); |
243 |
|
} |
244 |
5040 |
VSB_putc(tl->sb, '\n'); |
245 |
5040 |
tl->err = 1; |
246 |
5080 |
} |
247 |
|
|
248 |
|
void |
249 |
15840 |
vcc_ErrWhere(struct vcc *tl, const struct token *t) |
250 |
|
{ |
251 |
|
const char *l1; |
252 |
|
|
253 |
15840 |
vcc_iline(t, &l1, 0); |
254 |
15840 |
vcc_icoord(tl->sb, t, 0); |
255 |
15840 |
VSB_putc(tl->sb, '\n'); |
256 |
15840 |
vcc_quoteline(tl, l1, t->src->e); |
257 |
15840 |
vcc_markline(tl, l1, t->src->e, t->b, t->e); |
258 |
15840 |
VSB_putc(tl->sb, '\n'); |
259 |
15840 |
tl->err = 1; |
260 |
15840 |
} |
261 |
|
|
262 |
|
/*--------------------------------------------------------------------*/ |
263 |
|
|
264 |
|
struct token * |
265 |
133410720 |
vcc_PeekTokenFrom(const struct vcc *tl, const struct token *t) |
266 |
|
{ |
267 |
|
struct token *t2; |
268 |
|
|
269 |
133410720 |
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); |
270 |
133410720 |
AN(t); |
271 |
133410720 |
assert(t->tok != EOI); |
272 |
133410720 |
t2 = VTAILQ_NEXT(t, list); |
273 |
133410720 |
AN(t2); |
274 |
133410720 |
return (t2); |
275 |
|
} |
276 |
|
|
277 |
|
struct token * |
278 |
9631040 |
vcc_PeekToken(const struct vcc *tl) |
279 |
|
{ |
280 |
|
|
281 |
9631040 |
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); |
282 |
9631040 |
return (vcc_PeekTokenFrom(tl, tl->t)); |
283 |
|
} |
284 |
|
|
285 |
|
void |
286 |
50495600 |
vcc_NextToken(struct vcc *tl) |
287 |
|
{ |
288 |
|
|
289 |
50495600 |
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); |
290 |
50495600 |
tl->t = vcc_PeekTokenFrom(tl, tl->t); |
291 |
50495600 |
} |
292 |
|
|
293 |
|
void |
294 |
35248520 |
vcc__Expect(struct vcc *tl, unsigned tok, unsigned line) |
295 |
|
{ |
296 |
35248520 |
if (tl->t->tok == tok) |
297 |
35247960 |
return; |
298 |
560 |
VSB_printf(tl->sb, "Expected %s got ", vcl_tnames[tok]); |
299 |
560 |
vcc_ErrToken(tl, tl->t); |
300 |
560 |
VSB_printf(tl->sb, "\n(program line %u), at\n", line); |
301 |
560 |
vcc_ErrWhere(tl, tl->t); |
302 |
35248520 |
} |
303 |
|
|
304 |
|
/*-------------------------------------------------------------------- |
305 |
|
* Compare ID token to string, return true of match |
306 |
|
*/ |
307 |
|
|
308 |
|
int |
309 |
89196000 |
vcc_IdIs(const struct token *t, const char *p) |
310 |
|
{ |
311 |
|
const char *q; |
312 |
|
|
313 |
89196000 |
assert(t->tok == ID); |
314 |
121216920 |
for (q = t->b; q < t->e && *p != '\0'; p++, q++) |
315 |
113815760 |
if (*q != *p) |
316 |
81794840 |
return (0); |
317 |
7401160 |
if (q != t->e || *p != '\0') |
318 |
8240 |
return (0); |
319 |
7392920 |
return (1); |
320 |
89196000 |
} |
321 |
|
|
322 |
|
/*-------------------------------------------------------------------- |
323 |
|
* Check that we have a Varnish identifier |
324 |
|
*/ |
325 |
|
|
326 |
|
void |
327 |
1280 |
vcc_PrintTokens(const struct vcc *tl, |
328 |
|
const struct token *tb, const struct token *te) |
329 |
|
{ |
330 |
|
|
331 |
1280 |
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); |
332 |
1280 |
AN(tb); |
333 |
1280 |
AN(te); |
334 |
4320 |
while (tb != te) { |
335 |
3040 |
VSB_printf(tl->sb, "%.*s", PF(tb)); |
336 |
3040 |
tb = vcc_PeekTokenFrom(tl, tb); |
337 |
3040 |
AN(tb); |
338 |
|
} |
339 |
1280 |
} |
340 |
|
|
341 |
|
void |
342 |
4120920 |
vcc_ExpectVid(struct vcc *tl, const char *what) |
343 |
|
{ |
344 |
4120920 |
const char *bad = NULL; |
345 |
|
struct token *t2; |
346 |
|
|
347 |
4120920 |
ExpectErr(tl, ID); |
348 |
4120880 |
ERRCHK(tl); |
349 |
|
|
350 |
4120880 |
t2 = vcc_PeekToken(tl); |
351 |
4120880 |
AN(t2); |
352 |
4121000 |
while (t2->tok == '.') { |
353 |
120 |
bad = "."; |
354 |
120 |
t2 = vcc_PeekTokenFrom(tl, t2); |
355 |
120 |
AN(t2); |
356 |
120 |
if (t2->tok != ID) |
357 |
0 |
break; |
358 |
120 |
t2 = vcc_PeekTokenFrom(tl, t2); |
359 |
120 |
AN(t2); |
360 |
|
} |
361 |
4120880 |
if (bad == NULL) |
362 |
4120760 |
bad = VCT_invalid_name(tl->t->b, tl->t->e); |
363 |
4120880 |
if (bad != NULL) { |
364 |
120 |
VSB_printf(tl->sb, "Name of %s, '", what); |
365 |
120 |
vcc_PrintTokens(tl, tl->t, t2); |
366 |
240 |
VSB_printf(tl->sb, |
367 |
120 |
"', contains illegal character '%c'\n", *bad); |
368 |
120 |
vcc_ErrWhere2(tl, tl->t, t2); |
369 |
120 |
return; |
370 |
|
} |
371 |
4120920 |
} |
372 |
|
|
373 |
|
/*-------------------------------------------------------------------- |
374 |
|
* Decode a string |
375 |
|
*/ |
376 |
|
|
377 |
|
static void |
378 |
4464400 |
vcc_decstr(struct vcc *tl, unsigned sep) |
379 |
|
{ |
380 |
|
char *q; |
381 |
|
unsigned int l; |
382 |
|
|
383 |
4464400 |
assert(tl->t->tok == CSTR); |
384 |
4464400 |
l = pdiff(tl->t->b + sep, tl->t->e - sep); |
385 |
4464400 |
tl->t->dec = TlAlloc(tl, l + 1); |
386 |
4464400 |
AN(tl->t->dec); |
387 |
4464400 |
q = tl->t->dec; |
388 |
4464400 |
memcpy(q, tl->t->b + sep, l); |
389 |
4464400 |
q[l] = '\0'; |
390 |
4464400 |
} |
391 |
|
|
392 |
|
/*-------------------------------------------------------------------- |
393 |
|
* Add a token to the token list. |
394 |
|
*/ |
395 |
|
|
396 |
|
static void |
397 |
95870480 |
vcc_addtoken(struct vcc *tl, unsigned tok, |
398 |
|
struct source *sp, const char *b, const char *e) |
399 |
|
{ |
400 |
|
struct token *t; |
401 |
|
|
402 |
95870480 |
t = TlAlloc(tl, sizeof *t); |
403 |
95870480 |
assert(t != NULL); |
404 |
95870480 |
t->tok = tok; |
405 |
95870480 |
t->b = b; |
406 |
95870480 |
t->e = e; |
407 |
95870480 |
t->src = sp; |
408 |
95870480 |
VTAILQ_INSERT_TAIL(&sp->src_tokens, t, src_list); |
409 |
95870480 |
tl->t = t; |
410 |
95870480 |
} |
411 |
|
|
412 |
|
/*-------------------------------------------------------------------- |
413 |
|
* Find a delimited token |
414 |
|
*/ |
415 |
|
|
416 |
|
static const struct delim_def { |
417 |
|
const char *name; |
418 |
|
const char *b; |
419 |
|
const char *e; |
420 |
|
unsigned len; /* NB: must be the same for both delimiters */ |
421 |
|
unsigned crlf; |
422 |
|
unsigned tok; |
423 |
|
} delim_defs[] = { |
424 |
|
#define DELIM_DEF(nm, l, r, c, t) \ |
425 |
|
{ nm, l, r, sizeof (l) - 1, c, t } |
426 |
|
DELIM_DEF("long-string", "\"\"\"", "\"\"\"", 1, CSTR), /* """...""" */ |
427 |
|
DELIM_DEF("long-string", "{\"", "\"}", 1, CSTR), /* {"..."} */ |
428 |
|
DELIM_DEF("string", "\"", "\"", 0, CSTR), /* "..." */ |
429 |
|
DELIM_DEF("inline C source", "C{", "}C", 1, CSRC), /* C{...}C */ |
430 |
|
#undef DELIM_DEF |
431 |
|
{ NULL } |
432 |
|
}; |
433 |
|
|
434 |
|
static unsigned |
435 |
95690880 |
vcc_delim_token(struct vcc *tl, struct source *sp, const char *p, |
436 |
|
const char **qp) |
437 |
|
{ |
438 |
|
const struct delim_def *dd; |
439 |
|
const char *q, *r; |
440 |
|
|
441 |
468336880 |
for (dd = delim_defs; dd->name != NULL; dd++) |
442 |
377111520 |
if (!strncmp(p, dd->b, dd->len)) |
443 |
4465520 |
break; |
444 |
|
|
445 |
95690880 |
if (dd->name == NULL) |
446 |
91225360 |
return (0); |
447 |
|
|
448 |
4465520 |
q = strstr(p + dd->len, dd->e); |
449 |
4465520 |
if (q != NULL && !dd->crlf) { |
450 |
3277200 |
r = strpbrk(p + dd->len, "\r\n"); |
451 |
3277200 |
if (r != NULL && r < q) |
452 |
0 |
q = NULL; |
453 |
3277200 |
} |
454 |
|
|
455 |
4465520 |
if (q == NULL) { |
456 |
160 |
vcc_addtoken(tl, EOI, sp, p, p + dd->len); |
457 |
160 |
VSB_printf(tl->sb, "Unterminated %s, starting at\n", dd->name); |
458 |
160 |
vcc_ErrWhere(tl, tl->t); |
459 |
160 |
return (0); |
460 |
|
} |
461 |
|
|
462 |
4465360 |
assert(q < sp->e); |
463 |
4465360 |
vcc_addtoken(tl, dd->tok, sp, p, q + dd->len); |
464 |
4465360 |
if (dd->tok == CSTR) |
465 |
4464400 |
vcc_decstr(tl, dd->len); |
466 |
4465360 |
*qp = q + dd->len; |
467 |
4465360 |
return (1); |
468 |
95690880 |
} |
469 |
|
|
470 |
|
/*-------------------------------------------------------------------- |
471 |
|
* Lex a number, either CNUM or FNUM. |
472 |
|
* We enforce the RFC8941 restrictions on number of digits here. |
473 |
|
*/ |
474 |
|
|
475 |
|
static const char * |
476 |
1186480 |
vcc_lex_number(struct vcc *tl, struct source *sp, const char *p) |
477 |
|
{ |
478 |
|
const char *q, *r; |
479 |
|
char *s; |
480 |
|
|
481 |
3854040 |
for (q = p; q < sp->e; q++) |
482 |
3854040 |
if (!vct_isdigit(*q)) |
483 |
1186480 |
break; |
484 |
1186480 |
if (*q != '.') { |
485 |
1002720 |
vcc_addtoken(tl, CNUM, sp, p, q); |
486 |
1002720 |
if (q - p > 15) { |
487 |
40 |
VSB_cat(tl->sb, "Too many digits for integer.\n"); |
488 |
40 |
vcc_ErrWhere(tl, tl->t); |
489 |
40 |
return (NULL); |
490 |
|
} |
491 |
1002680 |
tl->t->num = strtod(p, &s); |
492 |
1002680 |
assert(s == tl->t->e); |
493 |
1002680 |
return (q); |
494 |
|
} |
495 |
183760 |
r = ++q; |
496 |
368600 |
for (; r < sp->e; r++) |
497 |
368600 |
if (!vct_isdigit(*r)) |
498 |
183760 |
break; |
499 |
183760 |
vcc_addtoken(tl, FNUM, sp, p, r); |
500 |
183760 |
if (q - p > 13 || r - q > 3) { |
501 |
120 |
VSB_cat(tl->sb, "Too many digits for real.\n"); |
502 |
120 |
vcc_ErrWhere(tl, tl->t); |
503 |
120 |
return (NULL); |
504 |
|
} |
505 |
183640 |
if (*tl->t->e == 'e' || *tl->t->e == 'E') { |
506 |
40 |
VSB_printf(tl->sb, "Unexpected character '%c'.\n", *tl->t->e); |
507 |
40 |
tl->t->e++; |
508 |
40 |
vcc_ErrWhere(tl, tl->t); |
509 |
40 |
return (NULL); |
510 |
|
} |
511 |
183600 |
tl->t->num = strtod(p, &s); |
512 |
183600 |
assert(s == tl->t->e); |
513 |
183600 |
return (r); |
514 |
1186480 |
} |
515 |
|
|
516 |
|
/*-------------------------------------------------------------------- |
517 |
|
* Lexical analysis and token generation |
518 |
|
*/ |
519 |
|
|
520 |
|
void |
521 |
179880 |
vcc_Lexer(struct vcc *tl, struct source *sp) |
522 |
|
{ |
523 |
|
const char *p, *q, *r; |
524 |
|
unsigned u; |
525 |
|
struct vsb *vsb; |
526 |
|
char namebuf[40]; |
527 |
|
|
528 |
193428000 |
for (p = sp->b; p < sp->e; ) { |
529 |
|
|
530 |
|
/* Skip any whitespace */ |
531 |
193249040 |
if (vct_isspace(*p)) { |
532 |
91265280 |
p++; |
533 |
91265280 |
continue; |
534 |
|
} |
535 |
|
|
536 |
|
/* Skip '#.*\n' comments */ |
537 |
101983760 |
if (*p == '#') { |
538 |
288233840 |
while (p < sp->e && *p != '\n') |
539 |
281942200 |
p++; |
540 |
6291640 |
continue; |
541 |
|
} |
542 |
|
|
543 |
|
/* Skip C-style comments */ |
544 |
95692120 |
if (*p == '/' && p[1] == '*') { |
545 |
2640 |
for (q = p + 2; q < sp->e; q++) { |
546 |
2600 |
if (*q == '/' && q[1] == '*') { |
547 |
40 |
VSB_cat(tl->sb, |
548 |
|
"/* ... */ comment contains /*\n"); |
549 |
40 |
vcc_addtoken(tl, EOI, sp, p, p + 2); |
550 |
40 |
vcc_ErrWhere(tl, tl->t); |
551 |
40 |
vcc_addtoken(tl, EOI, sp, q, q + 2); |
552 |
40 |
vcc_ErrWhere(tl, tl->t); |
553 |
40 |
return; |
554 |
|
} |
555 |
2560 |
if (*q == '*' && q[1] == '/') { |
556 |
80 |
p = q + 2; |
557 |
80 |
break; |
558 |
|
} |
559 |
2480 |
} |
560 |
120 |
if (q < sp->e) |
561 |
80 |
continue; |
562 |
40 |
vcc_addtoken(tl, EOI, sp, p, p + 2); |
563 |
40 |
VSB_cat(tl->sb, |
564 |
|
"Unterminated /* ... */ comment, starting at\n"); |
565 |
40 |
vcc_ErrWhere(tl, tl->t); |
566 |
40 |
return; |
567 |
|
} |
568 |
|
|
569 |
|
/* Skip C++-style comments */ |
570 |
95691960 |
if (*p == '/' && p[1] == '/') { |
571 |
13680 |
while (p < sp->e && *p != '\n') |
572 |
13120 |
p++; |
573 |
560 |
continue; |
574 |
|
} |
575 |
|
|
576 |
|
/* Recognize BLOB (= SF-binary) */ |
577 |
95691400 |
if (*p == ':') { |
578 |
520 |
vsb = VSB_new_auto(); |
579 |
520 |
AN(vsb); |
580 |
520 |
q = sp->e; |
581 |
520 |
q -= (q - (p + 1)) % 4; |
582 |
520 |
assert(q > p); |
583 |
520 |
r = VENC_Decode_Base64(vsb, p + 1, q); |
584 |
520 |
if (r == NULL) { |
585 |
80 |
vcc_addtoken(tl, CBLOB, sp, p, q + 1); |
586 |
80 |
VSB_cat(tl->sb, |
587 |
|
"Missing colon at end of BLOB:\n"); |
588 |
80 |
vcc_ErrWhere(tl, tl->t); |
589 |
80 |
VSB_destroy(&vsb); |
590 |
80 |
return; |
591 |
|
} |
592 |
440 |
vcc_addtoken(tl, CBLOB, sp, p, r + 1); |
593 |
440 |
if (*r == ':' && ((r - p) % 4) != 1) { |
594 |
120 |
VSB_cat(tl->sb, |
595 |
|
"BLOB must have n*4 base64 characters\n"); |
596 |
120 |
vcc_ErrWhere(tl, tl->t); |
597 |
120 |
VSB_destroy(&vsb); |
598 |
120 |
return; |
599 |
|
} |
600 |
320 |
if (*r == '=') { |
601 |
120 |
VSB_cat(tl->sb, |
602 |
|
"Wrong padding ('=') in BLOB:\n"); |
603 |
120 |
vcc_ErrWhere(tl, tl->t); |
604 |
120 |
VSB_destroy(&vsb); |
605 |
120 |
return; |
606 |
|
} |
607 |
200 |
if (*r != ':') { |
608 |
80 |
VSB_cat(tl->sb, "Illegal BLOB character:\n"); |
609 |
80 |
vcc_ErrWhere(tl, tl->t); |
610 |
80 |
VSB_destroy(&vsb); |
611 |
80 |
return; |
612 |
|
} |
613 |
120 |
r++; |
614 |
120 |
AZ(VSB_finish(vsb)); |
615 |
|
|
616 |
120 |
bprintf(namebuf, "blob_%u", tl->unique++); |
617 |
240 |
Fh(tl, 0, |
618 |
|
"\nstatic const unsigned char %s_data[%zd] = {\n", |
619 |
120 |
namebuf, VSB_len(vsb)); |
620 |
3280 |
for (u = 0; u < VSB_len(vsb); u++) { |
621 |
3160 |
Fh(tl, 0, "\t0x%02x,", VSB_data(vsb)[u] & 0xff); |
622 |
3160 |
if ((u & 7) == 7) |
623 |
360 |
Fh(tl, 0, "\n"); |
624 |
3160 |
} |
625 |
120 |
if ((u & 7) != 7) |
626 |
120 |
Fh(tl, 0, "\n"); |
627 |
120 |
Fh(tl, 0, "};\n"); |
628 |
240 |
Fh(tl, 0, |
629 |
|
"\nstatic const struct vrt_blob %s[1] = {{\n", |
630 |
120 |
namebuf); |
631 |
120 |
Fh(tl, 0, "\t.len =\t%zd,\n", VSB_len(vsb)); |
632 |
120 |
Fh(tl, 0, "\t.blob =\t%s_data,\n", namebuf); |
633 |
120 |
Fh(tl, 0, "}};\n"); |
634 |
120 |
REPLACE(tl->t->dec, namebuf); |
635 |
120 |
VSB_destroy(&vsb); |
636 |
120 |
p = r; |
637 |
120 |
continue; |
638 |
|
} |
639 |
|
|
640 |
|
/* Match delimited tokens */ |
641 |
95690880 |
if (vcc_delim_token(tl, sp, p, &q) != 0) { |
642 |
4465360 |
p = q; |
643 |
4465360 |
continue; |
644 |
|
} |
645 |
91225520 |
ERRCHK(tl); |
646 |
|
|
647 |
|
/* Match for the fixed tokens (see generate.py) */ |
648 |
91225360 |
u = vcl_fixed_token(p, &q); |
649 |
91225360 |
if (u != 0) { |
650 |
49442800 |
vcc_addtoken(tl, u, sp, p, q); |
651 |
49442800 |
p = q; |
652 |
49442800 |
continue; |
653 |
|
} |
654 |
|
|
655 |
|
/* Match Identifiers */ |
656 |
41782560 |
if (vct_isident1(*p)) { |
657 |
312047520 |
for (q = p; q < sp->e; q++) |
658 |
312047520 |
if (!vct_isident(*q)) |
659 |
40596000 |
break; |
660 |
40596000 |
vcc_addtoken(tl, ID, sp, p, q); |
661 |
40596000 |
p = q; |
662 |
40596000 |
continue; |
663 |
|
} |
664 |
|
|
665 |
|
/* Match numbers { [0-9]+ } */ |
666 |
1186560 |
if (vct_isdigit(*p)) { |
667 |
1186480 |
p = vcc_lex_number(tl, sp, p); |
668 |
1186480 |
if (p == NULL) |
669 |
200 |
return; |
670 |
1186280 |
continue; |
671 |
|
} |
672 |
80 |
vcc_addtoken(tl, EOI, sp, p, p + 1); |
673 |
80 |
VSB_cat(tl->sb, "Syntax error at\n"); |
674 |
80 |
vcc_ErrWhere(tl, tl->t); |
675 |
80 |
return; |
676 |
|
} |
677 |
178960 |
vcc_addtoken(tl, EOI, sp, sp->e, sp->e); |
678 |
179880 |
} |