| | 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 |
891 |
vcc_ErrToken(const struct vcc *tl, const struct token *t) |
45 |
|
{ |
46 |
|
|
47 |
891 |
if (t->tok == EOI) |
48 |
0 |
VSB_cat(tl->sb, "end of input"); |
49 |
891 |
else if (t->tok == CSRC) |
50 |
0 |
VSB_cat(tl->sb, "C{ ... }C"); |
51 |
|
else |
52 |
891 |
VSB_printf(tl->sb, "'%.*s'", PF(t)); |
53 |
891 |
} |
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 |
17496 |
vcc_iline(const struct token *t, const char **ll, int tail) |
70 |
|
{ |
71 |
|
const char *p, *b, *x; |
72 |
|
|
73 |
17496 |
b = t->src->b; |
74 |
17496 |
if (ll != NULL) |
75 |
17496 |
*ll = b; |
76 |
17496 |
x = tail ? t->e - 1 : t->b; |
77 |
7947342 |
for (p = b; p < x; p++) { |
78 |
7929846 |
if (*p == '\n') { |
79 |
218511 |
if (ll != NULL) |
80 |
218511 |
*ll = p + 1; |
81 |
218511 |
} |
82 |
7929846 |
} |
83 |
17496 |
} |
84 |
|
|
85 |
|
/*-------------------------------------------------------------------- |
86 |
|
* Find and print src+line+pos of this token |
87 |
|
*/ |
88 |
|
|
89 |
|
static void |
90 |
2747709 |
vcc_icoord(struct vsb *vsb, const struct token *t, int tail) |
91 |
|
{ |
92 |
|
unsigned lin, pos; |
93 |
|
const char *p, *b, *x; |
94 |
|
|
95 |
2747709 |
lin = 1; |
96 |
2747709 |
pos = 0; |
97 |
2747709 |
b = t->src->b; |
98 |
2747709 |
x = tail ? t->e - 1 : t->b; |
99 |
10636901271 |
for (p = b; p < x; p++) { |
100 |
10634153562 |
if (*p == '\n') { |
101 |
440689086 |
lin++; |
102 |
440689086 |
pos = 0; |
103 |
10634153562 |
} else if (*p == '\t') { |
104 |
215820666 |
pos &= ~7; |
105 |
215820666 |
pos += 8; |
106 |
215820666 |
} else |
107 |
9977643810 |
pos++; |
108 |
10634153562 |
} |
109 |
2747709 |
VSB_cat(vsb, "("); |
110 |
2747709 |
if (tail < 2) |
111 |
2744334 |
VSB_printf(vsb, "'%s' Line %u ", t->src->name, lin); |
112 |
2747709 |
VSB_printf(vsb, "Pos %u)", pos + 1); |
113 |
2747709 |
} |
114 |
|
|
115 |
|
/*--------------------------------------------------------------------*/ |
116 |
|
|
117 |
|
void |
118 |
2730213 |
vcc_Coord(const struct vcc *tl, struct vsb *vsb, const struct token *t) |
119 |
|
{ |
120 |
|
|
121 |
2730213 |
if (t == NULL) |
122 |
2730213 |
t = tl->t; |
123 |
2730213 |
vcc_icoord(vsb, t, 0); |
124 |
2730213 |
} |
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 |
14121 |
vcc_quoteline(const struct vcc *tl, const char *l, const char *le) |
133 |
|
{ |
134 |
|
const char *p; |
135 |
|
unsigned x, y; |
136 |
|
|
137 |
14121 |
x = y = 0; |
138 |
480380 |
for (p = l; p < le && *p != '\n'; p++) { |
139 |
466259 |
if (*p == '\t') { |
140 |
18657 |
y &= ~7; |
141 |
18657 |
y += 8; |
142 |
167913 |
while (x < y) { |
143 |
149256 |
VSB_putc(tl->sb, ' '); |
144 |
149256 |
x++; |
145 |
|
} |
146 |
18657 |
} else { |
147 |
447602 |
x++; |
148 |
447602 |
y++; |
149 |
447602 |
VSB_putc(tl->sb, *p); |
150 |
|
} |
151 |
466259 |
} |
152 |
14121 |
VSB_putc(tl->sb, '\n'); |
153 |
14121 |
} |
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 |
14121 |
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 |
14121 |
x = y = 0; |
169 |
480380 |
for (p = l; p < le && *p != '\n'; p++) { |
170 |
466259 |
if (p >= b && p < e) |
171 |
110190 |
c = '#'; |
172 |
|
else |
173 |
356069 |
c = '-'; |
174 |
|
|
175 |
466259 |
if (*p == '\t') { |
176 |
18657 |
y &= ~7; |
177 |
18657 |
y += 8; |
178 |
18657 |
} else |
179 |
447602 |
y++; |
180 |
1063117 |
while (x < y) { |
181 |
596858 |
VSB_putc(tl->sb, c); |
182 |
596858 |
x++; |
183 |
|
} |
184 |
466259 |
} |
185 |
14121 |
VSB_putc(tl->sb, '\n'); |
186 |
14121 |
} |
187 |
|
|
188 |
|
void |
189 |
3753 |
vcc_Warn(struct vcc *tl) |
190 |
|
{ |
191 |
|
|
192 |
3753 |
AN(tl); |
193 |
3753 |
AN(tl->err); |
194 |
3753 |
VSB_cat(tl->sb, "(That was just a warning)\n"); |
195 |
3753 |
tl->err = 0; |
196 |
3753 |
} |
197 |
|
|
198 |
|
/*--------------------------------------------------------------------*/ |
199 |
|
/* XXX: should take first+last token */ |
200 |
|
|
201 |
|
void |
202 |
3429 |
vcc_ErrWhere2(struct vcc *tl, const struct token *t, const struct token *t2) |
203 |
|
{ |
204 |
|
const char *l1, *l2, *l3; |
205 |
|
|
206 |
3429 |
if (t == NULL) { |
207 |
27 |
vcc_ErrWhere(tl, t2); |
208 |
27 |
return; |
209 |
|
} |
210 |
3402 |
vcc_iline(t, &l1, 0); |
211 |
3402 |
t2 = VTAILQ_PREV(t2, tokenhead, list); |
212 |
3402 |
vcc_iline(t2, &l2, 1); |
213 |
|
|
214 |
|
|
215 |
3402 |
if (l1 == l2) { |
216 |
3375 |
vcc_icoord(tl->sb, t, 0); |
217 |
3375 |
VSB_cat(tl->sb, " -- "); |
218 |
3375 |
vcc_icoord(tl->sb, t2, 2); |
219 |
3375 |
VSB_putc(tl->sb, '\n'); |
220 |
|
/* Two tokens on same line */ |
221 |
3375 |
vcc_quoteline(tl, l1, t->src->e); |
222 |
3375 |
vcc_markline(tl, l1, t->src->e, t->b, t2->e); |
223 |
3375 |
} else { |
224 |
|
/* Two tokens different lines */ |
225 |
27 |
l3 = strchr(l1, '\n'); |
226 |
27 |
AN(l3); |
227 |
|
/* XXX: t had better be before t2 */ |
228 |
27 |
vcc_icoord(tl->sb, t, 0); |
229 |
27 |
if (l3 + 1 == l2) { |
230 |
0 |
VSB_cat(tl->sb, " -- "); |
231 |
0 |
vcc_icoord(tl->sb, t2, 1); |
232 |
0 |
} |
233 |
27 |
VSB_putc(tl->sb, '\n'); |
234 |
27 |
vcc_quoteline(tl, l1, t->src->e); |
235 |
27 |
vcc_markline(tl, l1, t->src->e, t->b, t2->e); |
236 |
27 |
if (l3 + 1 != l2) { |
237 |
27 |
VSB_cat(tl->sb, "[...]\n"); |
238 |
27 |
vcc_icoord(tl->sb, t2, 1); |
239 |
27 |
VSB_putc(tl->sb, '\n'); |
240 |
27 |
} |
241 |
27 |
vcc_quoteline(tl, l2, t->src->e); |
242 |
27 |
vcc_markline(tl, l2, t->src->e, t->b, t2->e); |
243 |
|
} |
244 |
3402 |
VSB_putc(tl->sb, '\n'); |
245 |
3402 |
tl->err = 1; |
246 |
3429 |
} |
247 |
|
|
248 |
|
void |
249 |
10692 |
vcc_ErrWhere(struct vcc *tl, const struct token *t) |
250 |
|
{ |
251 |
|
const char *l1; |
252 |
|
|
253 |
10692 |
vcc_iline(t, &l1, 0); |
254 |
10692 |
vcc_icoord(tl->sb, t, 0); |
255 |
10692 |
VSB_putc(tl->sb, '\n'); |
256 |
10692 |
vcc_quoteline(tl, l1, t->src->e); |
257 |
10692 |
vcc_markline(tl, l1, t->src->e, t->b, t->e); |
258 |
10692 |
VSB_putc(tl->sb, '\n'); |
259 |
10692 |
tl->err = 1; |
260 |
10692 |
} |
261 |
|
|
262 |
|
/*--------------------------------------------------------------------*/ |
263 |
|
|
264 |
|
struct token * |
265 |
90052236 |
vcc_PeekTokenFrom(const struct vcc *tl, const struct token *t) |
266 |
|
{ |
267 |
|
struct token *t2; |
268 |
|
|
269 |
90052236 |
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); |
270 |
90052236 |
AN(t); |
271 |
90052236 |
assert(t->tok != EOI); |
272 |
90052236 |
t2 = VTAILQ_NEXT(t, list); |
273 |
90052236 |
AN(t2); |
274 |
90052236 |
return (t2); |
275 |
|
} |
276 |
|
|
277 |
|
struct token * |
278 |
6500952 |
vcc_PeekToken(const struct vcc *tl) |
279 |
|
{ |
280 |
|
|
281 |
6500952 |
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); |
282 |
6500952 |
return (vcc_PeekTokenFrom(tl, tl->t)); |
283 |
|
} |
284 |
|
|
285 |
|
void |
286 |
34084530 |
vcc_NextToken(struct vcc *tl) |
287 |
|
{ |
288 |
|
|
289 |
34084530 |
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); |
290 |
34084530 |
tl->t = vcc_PeekTokenFrom(tl, tl->t); |
291 |
34084530 |
} |
292 |
|
|
293 |
|
void |
294 |
23792751 |
vcc__Expect(struct vcc *tl, unsigned tok, unsigned line) |
295 |
|
{ |
296 |
23792751 |
if (tl->t->tok == tok) |
297 |
23792373 |
return; |
298 |
378 |
VSB_printf(tl->sb, "Expected %s got ", vcl_tnames[tok]); |
299 |
378 |
vcc_ErrToken(tl, tl->t); |
300 |
378 |
VSB_printf(tl->sb, "\n(program line %u), at\n", line); |
301 |
378 |
vcc_ErrWhere(tl, tl->t); |
302 |
23792751 |
} |
303 |
|
|
304 |
|
/*-------------------------------------------------------------------- |
305 |
|
* Compare ID token to string, return true of match |
306 |
|
*/ |
307 |
|
|
308 |
|
int |
309 |
60207300 |
vcc_IdIs(const struct token *t, const char *p) |
310 |
|
{ |
311 |
|
const char *q; |
312 |
|
|
313 |
60207300 |
assert(t->tok == ID); |
314 |
81821421 |
for (q = t->b; q < t->e && *p != '\0'; p++, q++) |
315 |
76825638 |
if (*q != *p) |
316 |
55211517 |
return (0); |
317 |
4995783 |
if (q != t->e || *p != '\0') |
318 |
5562 |
return (0); |
319 |
4990221 |
return (1); |
320 |
60207300 |
} |
321 |
|
|
322 |
|
/*-------------------------------------------------------------------- |
323 |
|
* Check that we have a Varnish identifier |
324 |
|
*/ |
325 |
|
|
326 |
|
void |
327 |
864 |
vcc_PrintTokens(const struct vcc *tl, |
328 |
|
const struct token *tb, const struct token *te) |
329 |
|
{ |
330 |
|
|
331 |
864 |
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); |
332 |
864 |
AN(tb); |
333 |
864 |
AN(te); |
334 |
2916 |
while (tb != te) { |
335 |
2052 |
VSB_printf(tl->sb, "%.*s", PF(tb)); |
336 |
2052 |
tb = vcc_PeekTokenFrom(tl, tb); |
337 |
2052 |
AN(tb); |
338 |
|
} |
339 |
864 |
} |
340 |
|
|
341 |
|
void |
342 |
2781621 |
vcc_ExpectVid(struct vcc *tl, const char *what) |
343 |
|
{ |
344 |
2781621 |
const char *bad = NULL; |
345 |
|
struct token *t2; |
346 |
|
|
347 |
2781621 |
ExpectErr(tl, ID); |
348 |
2781594 |
ERRCHK(tl); |
349 |
|
|
350 |
2781594 |
t2 = vcc_PeekToken(tl); |
351 |
2781594 |
AN(t2); |
352 |
2781675 |
while (t2->tok == '.') { |
353 |
81 |
bad = "."; |
354 |
81 |
t2 = vcc_PeekTokenFrom(tl, t2); |
355 |
81 |
AN(t2); |
356 |
81 |
if (t2->tok != ID) |
357 |
0 |
break; |
358 |
81 |
t2 = vcc_PeekTokenFrom(tl, t2); |
359 |
81 |
AN(t2); |
360 |
|
} |
361 |
2781594 |
if (bad == NULL) |
362 |
2781513 |
bad = VCT_invalid_name(tl->t->b, tl->t->e); |
363 |
2781594 |
if (bad != NULL) { |
364 |
81 |
VSB_printf(tl->sb, "Name of %s, '", what); |
365 |
81 |
vcc_PrintTokens(tl, tl->t, t2); |
366 |
162 |
VSB_printf(tl->sb, |
367 |
81 |
"', contains illegal character '%c'\n", *bad); |
368 |
81 |
vcc_ErrWhere2(tl, tl->t, t2); |
369 |
81 |
return; |
370 |
|
} |
371 |
2781621 |
} |
372 |
|
|
373 |
|
/*-------------------------------------------------------------------- |
374 |
|
* Decode a string |
375 |
|
*/ |
376 |
|
|
377 |
|
static void |
378 |
3013470 |
vcc_decstr(struct vcc *tl, unsigned sep) |
379 |
|
{ |
380 |
|
char *q; |
381 |
|
unsigned int l; |
382 |
|
|
383 |
3013470 |
assert(tl->t->tok == CSTR); |
384 |
3013470 |
l = pdiff(tl->t->b + sep, tl->t->e - sep); |
385 |
3013470 |
tl->t->dec = TlAlloc(tl, l + 1); |
386 |
3013470 |
AN(tl->t->dec); |
387 |
3013470 |
q = tl->t->dec; |
388 |
3013470 |
memcpy(q, tl->t->b + sep, l); |
389 |
3013470 |
q[l] = '\0'; |
390 |
3013470 |
} |
391 |
|
|
392 |
|
/*-------------------------------------------------------------------- |
393 |
|
* Add a token to the token list. |
394 |
|
*/ |
395 |
|
|
396 |
|
static void |
397 |
64712574 |
vcc_addtoken(struct vcc *tl, unsigned tok, |
398 |
|
struct source *sp, const char *b, const char *e) |
399 |
|
{ |
400 |
|
struct token *t; |
401 |
|
|
402 |
64712574 |
t = TlAlloc(tl, sizeof *t); |
403 |
64712574 |
assert(t != NULL); |
404 |
64712574 |
t->tok = tok; |
405 |
64712574 |
t->b = b; |
406 |
64712574 |
t->e = e; |
407 |
64712574 |
t->src = sp; |
408 |
64712574 |
VTAILQ_INSERT_TAIL(&sp->src_tokens, t, src_list); |
409 |
64712574 |
tl->t = t; |
410 |
64712574 |
} |
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 |
64591344 |
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 |
316127394 |
for (dd = delim_defs; dd->name != NULL; dd++) |
442 |
254550276 |
if (!strncmp(p, dd->b, dd->len)) |
443 |
3014226 |
break; |
444 |
|
|
445 |
64591344 |
if (dd->name == NULL) |
446 |
61577118 |
return (0); |
447 |
|
|
448 |
3014226 |
q = strstr(p + dd->len, dd->e); |
449 |
3014226 |
if (q != NULL && !dd->crlf) { |
450 |
2212110 |
r = strpbrk(p + dd->len, "\r\n"); |
451 |
2212110 |
if (r != NULL && r < q) |
452 |
0 |
q = NULL; |
453 |
2212110 |
} |
454 |
|
|
455 |
3014226 |
if (q == NULL) { |
456 |
108 |
vcc_addtoken(tl, EOI, sp, p, p + dd->len); |
457 |
108 |
VSB_printf(tl->sb, "Unterminated %s, starting at\n", dd->name); |
458 |
108 |
vcc_ErrWhere(tl, tl->t); |
459 |
108 |
return (0); |
460 |
|
} |
461 |
|
|
462 |
3014118 |
assert(q < sp->e); |
463 |
3014118 |
vcc_addtoken(tl, dd->tok, sp, p, q + dd->len); |
464 |
3014118 |
if (dd->tok == CSTR) |
465 |
3013470 |
vcc_decstr(tl, dd->len); |
466 |
3014118 |
*qp = q + dd->len; |
467 |
3014118 |
return (1); |
468 |
64591344 |
} |
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 |
800874 |
vcc_lex_number(struct vcc *tl, struct source *sp, const char *p) |
477 |
|
{ |
478 |
|
const char *q, *r; |
479 |
|
char *s; |
480 |
|
|
481 |
2601477 |
for (q = p; q < sp->e; q++) |
482 |
2601477 |
if (!vct_isdigit(*q)) |
483 |
800874 |
break; |
484 |
800874 |
if (*q != '.') { |
485 |
676836 |
vcc_addtoken(tl, CNUM, sp, p, q); |
486 |
676836 |
if (q - p > 15) { |
487 |
27 |
VSB_cat(tl->sb, "Too many digits for integer.\n"); |
488 |
27 |
vcc_ErrWhere(tl, tl->t); |
489 |
27 |
return (NULL); |
490 |
|
} |
491 |
676809 |
tl->t->num = strtod(p, &s); |
492 |
676809 |
assert(s == tl->t->e); |
493 |
676809 |
return (q); |
494 |
|
} |
495 |
124038 |
r = ++q; |
496 |
248805 |
for (; r < sp->e; r++) |
497 |
248805 |
if (!vct_isdigit(*r)) |
498 |
124038 |
break; |
499 |
124038 |
vcc_addtoken(tl, FNUM, sp, p, r); |
500 |
124038 |
if (q - p > 13 || r - q > 3) { |
501 |
81 |
VSB_cat(tl->sb, "Too many digits for real.\n"); |
502 |
81 |
vcc_ErrWhere(tl, tl->t); |
503 |
81 |
return (NULL); |
504 |
|
} |
505 |
123957 |
if (*tl->t->e == 'e' || *tl->t->e == 'E') { |
506 |
27 |
VSB_printf(tl->sb, "Unexpected character '%c'.\n", *tl->t->e); |
507 |
27 |
tl->t->e++; |
508 |
27 |
vcc_ErrWhere(tl, tl->t); |
509 |
27 |
return (NULL); |
510 |
|
} |
511 |
123930 |
tl->t->num = strtod(p, &s); |
512 |
123930 |
assert(s == tl->t->e); |
513 |
123930 |
return (r); |
514 |
800874 |
} |
515 |
|
|
516 |
|
/*-------------------------------------------------------------------- |
517 |
|
* Lexical analysis and token generation |
518 |
|
*/ |
519 |
|
|
520 |
|
void |
521 |
121419 |
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 |
130563900 |
for (p = sp->b; p < sp->e; ) { |
529 |
|
|
530 |
|
/* Skip any whitespace */ |
531 |
130443102 |
if (vct_isspace(*p)) { |
532 |
61604064 |
p++; |
533 |
61604064 |
continue; |
534 |
|
} |
535 |
|
|
536 |
|
/* Skip '#.*\n' comments */ |
537 |
68839038 |
if (*p == '#') { |
538 |
194557842 |
while (p < sp->e && *p != '\n') |
539 |
190310985 |
p++; |
540 |
4246857 |
continue; |
541 |
|
} |
542 |
|
|
543 |
|
/* Skip C-style comments */ |
544 |
64592181 |
if (*p == '/' && p[1] == '*') { |
545 |
1782 |
for (q = p + 2; q < sp->e; q++) { |
546 |
1755 |
if (*q == '/' && q[1] == '*') { |
547 |
27 |
VSB_cat(tl->sb, |
548 |
|
"/* ... */ comment contains /*\n"); |
549 |
27 |
vcc_addtoken(tl, EOI, sp, p, p + 2); |
550 |
27 |
vcc_ErrWhere(tl, tl->t); |
551 |
27 |
vcc_addtoken(tl, EOI, sp, q, q + 2); |
552 |
27 |
vcc_ErrWhere(tl, tl->t); |
553 |
27 |
return; |
554 |
|
} |
555 |
1728 |
if (*q == '*' && q[1] == '/') { |
556 |
54 |
p = q + 2; |
557 |
54 |
break; |
558 |
|
} |
559 |
1674 |
} |
560 |
81 |
if (q < sp->e) |
561 |
54 |
continue; |
562 |
27 |
vcc_addtoken(tl, EOI, sp, p, p + 2); |
563 |
27 |
VSB_cat(tl->sb, |
564 |
|
"Unterminated /* ... */ comment, starting at\n"); |
565 |
27 |
vcc_ErrWhere(tl, tl->t); |
566 |
27 |
return; |
567 |
|
} |
568 |
|
|
569 |
|
/* Skip C++-style comments */ |
570 |
64592073 |
if (*p == '/' && p[1] == '/') { |
571 |
9234 |
while (p < sp->e && *p != '\n') |
572 |
8856 |
p++; |
573 |
378 |
continue; |
574 |
|
} |
575 |
|
|
576 |
|
/* Recognize BLOB (= SF-binary) */ |
577 |
64591695 |
if (*p == ':') { |
578 |
351 |
vsb = VSB_new_auto(); |
579 |
351 |
AN(vsb); |
580 |
351 |
q = sp->e; |
581 |
351 |
q -= (q - (p + 1)) % 4; |
582 |
351 |
assert(q > p); |
583 |
351 |
r = VENC_Decode_Base64(vsb, p + 1, q); |
584 |
351 |
if (r == NULL) { |
585 |
54 |
vcc_addtoken(tl, CBLOB, sp, p, q + 1); |
586 |
54 |
VSB_cat(tl->sb, |
587 |
|
"Missing colon at end of BLOB:\n"); |
588 |
54 |
vcc_ErrWhere(tl, tl->t); |
589 |
54 |
VSB_destroy(&vsb); |
590 |
54 |
return; |
591 |
|
} |
592 |
297 |
vcc_addtoken(tl, CBLOB, sp, p, r + 1); |
593 |
297 |
if (*r == ':' && ((r - p) % 4) != 1) { |
594 |
81 |
VSB_cat(tl->sb, |
595 |
|
"BLOB must have n*4 base64 characters\n"); |
596 |
81 |
vcc_ErrWhere(tl, tl->t); |
597 |
81 |
VSB_destroy(&vsb); |
598 |
81 |
return; |
599 |
|
} |
600 |
216 |
if (*r == '=') { |
601 |
81 |
VSB_cat(tl->sb, |
602 |
|
"Wrong padding ('=') in BLOB:\n"); |
603 |
81 |
vcc_ErrWhere(tl, tl->t); |
604 |
81 |
VSB_destroy(&vsb); |
605 |
81 |
return; |
606 |
|
} |
607 |
135 |
if (*r != ':') { |
608 |
54 |
VSB_cat(tl->sb, "Illegal BLOB character:\n"); |
609 |
54 |
vcc_ErrWhere(tl, tl->t); |
610 |
54 |
VSB_destroy(&vsb); |
611 |
54 |
return; |
612 |
|
} |
613 |
81 |
r++; |
614 |
81 |
AZ(VSB_finish(vsb)); |
615 |
|
|
616 |
81 |
bprintf(namebuf, "blob_%u", tl->unique++); |
617 |
162 |
Fh(tl, 0, |
618 |
|
"\nstatic const unsigned char %s_data[%zd] = {\n", |
619 |
81 |
namebuf, VSB_len(vsb)); |
620 |
2214 |
for (u = 0; u < VSB_len(vsb); u++) { |
621 |
2133 |
Fh(tl, 0, "\t0x%02x,", VSB_data(vsb)[u] & 0xff); |
622 |
2133 |
if ((u & 7) == 7) |
623 |
243 |
Fh(tl, 0, "\n"); |
624 |
2133 |
} |
625 |
81 |
if ((u & 7) != 7) |
626 |
81 |
Fh(tl, 0, "\n"); |
627 |
81 |
Fh(tl, 0, "};\n"); |
628 |
162 |
Fh(tl, 0, |
629 |
|
"\nstatic const struct vrt_blob %s[1] = {{\n", |
630 |
81 |
namebuf); |
631 |
81 |
Fh(tl, 0, "\t.len =\t%zd,\n", VSB_len(vsb)); |
632 |
81 |
Fh(tl, 0, "\t.blob =\t%s_data,\n", namebuf); |
633 |
81 |
Fh(tl, 0, "}};\n"); |
634 |
81 |
REPLACE(tl->t->dec, namebuf); |
635 |
81 |
VSB_destroy(&vsb); |
636 |
81 |
p = r; |
637 |
81 |
continue; |
638 |
|
} |
639 |
|
|
640 |
|
/* Match delimited tokens */ |
641 |
64591344 |
if (vcc_delim_token(tl, sp, p, &q) != 0) { |
642 |
3014118 |
p = q; |
643 |
3014118 |
continue; |
644 |
|
} |
645 |
61577226 |
ERRCHK(tl); |
646 |
|
|
647 |
|
/* Match for the fixed tokens (see generate.py) */ |
648 |
61577118 |
u = vcl_fixed_token(p, &q); |
649 |
61577118 |
if (u != 0) { |
650 |
33373890 |
vcc_addtoken(tl, u, sp, p, q); |
651 |
33373890 |
p = q; |
652 |
33373890 |
continue; |
653 |
|
} |
654 |
|
|
655 |
|
/* Match Identifiers */ |
656 |
28203228 |
if (vct_isident1(*p)) { |
657 |
210632076 |
for (q = p; q < sp->e; q++) |
658 |
210632076 |
if (!vct_isident(*q)) |
659 |
27402300 |
break; |
660 |
27402300 |
vcc_addtoken(tl, ID, sp, p, q); |
661 |
27402300 |
p = q; |
662 |
27402300 |
continue; |
663 |
|
} |
664 |
|
|
665 |
|
/* Match numbers { [0-9]+ } */ |
666 |
800928 |
if (vct_isdigit(*p)) { |
667 |
800874 |
p = vcc_lex_number(tl, sp, p); |
668 |
800874 |
if (p == NULL) |
669 |
135 |
return; |
670 |
800739 |
continue; |
671 |
|
} |
672 |
54 |
vcc_addtoken(tl, EOI, sp, p, p + 1); |
673 |
54 |
VSB_cat(tl->sb, "Syntax error at\n"); |
674 |
54 |
vcc_ErrWhere(tl, tl->t); |
675 |
54 |
return; |
676 |
|
} |
677 |
120798 |
vcc_addtoken(tl, EOI, sp, sp->e, sp->e); |
678 |
121419 |
} |