| | vinyl-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 |
136 |
vcc_ErrToken(const struct vcc *tl, const struct token *t) |
| 45 |
|
{ |
| 46 |
|
|
| 47 |
136 |
if (t->tok == EOI) |
| 48 |
0 |
VSB_cat(tl->sb, "end of input"); |
| 49 |
136 |
else if (t->tok == CSRC) |
| 50 |
0 |
VSB_cat(tl->sb, "C{ ... }C"); |
| 51 |
|
else |
| 52 |
136 |
VSB_printf(tl->sb, "'%.*s'", PF(t)); |
| 53 |
136 |
} |
| 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 |
2804 |
vcc_iline(const struct token *t, const char **ll, int tail) |
| 70 |
|
{ |
| 71 |
|
const char *p, *b, *x; |
| 72 |
|
|
| 73 |
2804 |
b = t->src->b; |
| 74 |
2804 |
if (ll != NULL) |
| 75 |
2804 |
*ll = b; |
| 76 |
2804 |
x = tail ? t->e - 1 : t->b; |
| 77 |
1106876 |
for (p = b; p < x; p++) { |
| 78 |
1104072 |
if (*p == '\n') { |
| 79 |
34080 |
if (ll != NULL) |
| 80 |
34080 |
*ll = p + 1; |
| 81 |
34080 |
} |
| 82 |
1104072 |
} |
| 83 |
2804 |
} |
| 84 |
|
|
| 85 |
|
/*-------------------------------------------------------------------- |
| 86 |
|
* Find and print src+line+pos of this token |
| 87 |
|
*/ |
| 88 |
|
|
| 89 |
|
static void |
| 90 |
516496 |
vcc_icoord(struct vsb *vsb, const struct token *t, int tail) |
| 91 |
|
{ |
| 92 |
|
unsigned lin, pos; |
| 93 |
|
const char *p, *b, *x; |
| 94 |
|
|
| 95 |
516496 |
lin = 1; |
| 96 |
516496 |
pos = 0; |
| 97 |
516496 |
b = t->src->b; |
| 98 |
516496 |
x = tail ? t->e - 1 : t->b; |
| 99 |
2741205059 |
for (p = b; p < x; p++) { |
| 100 |
2740688563 |
if (*p == '\n') { |
| 101 |
106083012 |
lin++; |
| 102 |
106083012 |
pos = 0; |
| 103 |
2740688563 |
} else if (*p == '\t') { |
| 104 |
49983540 |
pos &= ~7; |
| 105 |
49983540 |
pos += 8; |
| 106 |
49983540 |
} else |
| 107 |
2584622011 |
pos++; |
| 108 |
2740688563 |
} |
| 109 |
516496 |
VSB_cat(vsb, "("); |
| 110 |
516496 |
if (tail < 2) |
| 111 |
515988 |
VSB_printf(vsb, "'%s' Line %u ", t->src->name, lin); |
| 112 |
516496 |
VSB_printf(vsb, "Pos %u)", pos + 1); |
| 113 |
516496 |
} |
| 114 |
|
|
| 115 |
|
/*--------------------------------------------------------------------*/ |
| 116 |
|
|
| 117 |
|
void |
| 118 |
513692 |
vcc_Coord(const struct vcc *tl, struct vsb *vsb, const struct token *t) |
| 119 |
|
{ |
| 120 |
|
|
| 121 |
513692 |
if (t == NULL) |
| 122 |
513692 |
t = tl->t; |
| 123 |
513692 |
vcc_icoord(vsb, t, 0); |
| 124 |
513692 |
} |
| 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 |
2296 |
vcc_quoteline(const struct vcc *tl, const char *l, const char *le) |
| 133 |
|
{ |
| 134 |
|
const char *p; |
| 135 |
|
unsigned x, y; |
| 136 |
|
|
| 137 |
2296 |
x = y = 0; |
| 138 |
73370 |
for (p = l; p < le && *p != '\n'; p++) { |
| 139 |
71074 |
if (*p == '\t') { |
| 140 |
3112 |
y &= ~7; |
| 141 |
3112 |
y += 8; |
| 142 |
27976 |
while (x < y) { |
| 143 |
24864 |
VSB_putc(tl->sb, ' '); |
| 144 |
24864 |
x++; |
| 145 |
|
} |
| 146 |
3112 |
} else { |
| 147 |
67962 |
x++; |
| 148 |
67962 |
y++; |
| 149 |
67962 |
VSB_putc(tl->sb, *p); |
| 150 |
|
} |
| 151 |
71074 |
} |
| 152 |
2296 |
VSB_putc(tl->sb, '\n'); |
| 153 |
2296 |
} |
| 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 |
2296 |
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 |
2296 |
x = y = 0; |
| 169 |
73370 |
for (p = l; p < le && *p != '\n'; p++) { |
| 170 |
71074 |
if (p >= b && p < e) |
| 171 |
18277 |
c = '#'; |
| 172 |
|
else |
| 173 |
52797 |
c = '-'; |
| 174 |
|
|
| 175 |
71074 |
if (*p == '\t') { |
| 176 |
3112 |
y &= ~7; |
| 177 |
3112 |
y += 8; |
| 178 |
3112 |
} else |
| 179 |
67962 |
y++; |
| 180 |
163900 |
while (x < y) { |
| 181 |
92826 |
VSB_putc(tl->sb, c); |
| 182 |
92826 |
x++; |
| 183 |
|
} |
| 184 |
71074 |
} |
| 185 |
2296 |
VSB_putc(tl->sb, '\n'); |
| 186 |
2296 |
} |
| 187 |
|
|
| 188 |
|
void |
| 189 |
636 |
vcc_Warn(struct vcc *tl) |
| 190 |
|
{ |
| 191 |
|
|
| 192 |
636 |
AN(tl); |
| 193 |
636 |
AN(tl->err); |
| 194 |
636 |
VSB_cat(tl->sb, "(That was just a warning)\n"); |
| 195 |
636 |
tl->err = 0; |
| 196 |
636 |
} |
| 197 |
|
|
| 198 |
|
/*--------------------------------------------------------------------*/ |
| 199 |
|
/* XXX: should take first+last token */ |
| 200 |
|
|
| 201 |
|
void |
| 202 |
516 |
vcc_ErrWhere2(struct vcc *tl, const struct token *t, const struct token *t2) |
| 203 |
|
{ |
| 204 |
|
const char *l1, *l2, *l3; |
| 205 |
|
|
| 206 |
516 |
if (t == NULL) { |
| 207 |
4 |
vcc_ErrWhere(tl, t2); |
| 208 |
4 |
return; |
| 209 |
|
} |
| 210 |
512 |
vcc_iline(t, &l1, 0); |
| 211 |
512 |
t2 = VTAILQ_PREV(t2, tokenhead, list); |
| 212 |
512 |
vcc_iline(t2, &l2, 1); |
| 213 |
|
|
| 214 |
|
|
| 215 |
512 |
if (l1 == l2) { |
| 216 |
508 |
vcc_icoord(tl->sb, t, 0); |
| 217 |
508 |
VSB_cat(tl->sb, " -- "); |
| 218 |
508 |
vcc_icoord(tl->sb, t2, 2); |
| 219 |
508 |
VSB_putc(tl->sb, '\n'); |
| 220 |
|
/* Two tokens on same line */ |
| 221 |
508 |
vcc_quoteline(tl, l1, t->src->e); |
| 222 |
508 |
vcc_markline(tl, l1, t->src->e, t->b, t2->e); |
| 223 |
508 |
} else { |
| 224 |
|
/* Two tokens different lines */ |
| 225 |
4 |
l3 = strchr(l1, '\n'); |
| 226 |
4 |
AN(l3); |
| 227 |
|
/* XXX: t had better be before t2 */ |
| 228 |
4 |
vcc_icoord(tl->sb, t, 0); |
| 229 |
4 |
if (l3 + 1 == l2) { |
| 230 |
0 |
VSB_cat(tl->sb, " -- "); |
| 231 |
0 |
vcc_icoord(tl->sb, t2, 1); |
| 232 |
0 |
} |
| 233 |
4 |
VSB_putc(tl->sb, '\n'); |
| 234 |
4 |
vcc_quoteline(tl, l1, t->src->e); |
| 235 |
4 |
vcc_markline(tl, l1, t->src->e, t->b, t2->e); |
| 236 |
4 |
if (l3 + 1 != l2) { |
| 237 |
4 |
VSB_cat(tl->sb, "[...]\n"); |
| 238 |
4 |
vcc_icoord(tl->sb, t2, 1); |
| 239 |
4 |
VSB_putc(tl->sb, '\n'); |
| 240 |
4 |
} |
| 241 |
4 |
vcc_quoteline(tl, l2, t->src->e); |
| 242 |
4 |
vcc_markline(tl, l2, t->src->e, t->b, t2->e); |
| 243 |
|
} |
| 244 |
512 |
VSB_putc(tl->sb, '\n'); |
| 245 |
512 |
tl->err = 1; |
| 246 |
516 |
} |
| 247 |
|
|
| 248 |
|
void |
| 249 |
1780 |
vcc_ErrWhere(struct vcc *tl, const struct token *t) |
| 250 |
|
{ |
| 251 |
|
const char *l1; |
| 252 |
|
|
| 253 |
1780 |
vcc_iline(t, &l1, 0); |
| 254 |
1780 |
vcc_icoord(tl->sb, t, 0); |
| 255 |
1780 |
VSB_putc(tl->sb, '\n'); |
| 256 |
1780 |
vcc_quoteline(tl, l1, t->src->e); |
| 257 |
1780 |
vcc_markline(tl, l1, t->src->e, t->b, t->e); |
| 258 |
1780 |
VSB_putc(tl->sb, '\n'); |
| 259 |
1780 |
tl->err = 1; |
| 260 |
1780 |
} |
| 261 |
|
|
| 262 |
|
/*--------------------------------------------------------------------*/ |
| 263 |
|
|
| 264 |
|
struct token * |
| 265 |
17298024 |
vcc_PeekTokenFrom(const struct vcc *tl, const struct token *t) |
| 266 |
|
{ |
| 267 |
|
struct token *t2; |
| 268 |
|
|
| 269 |
17298024 |
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); |
| 270 |
17298024 |
AN(t); |
| 271 |
17298024 |
assert(t->tok != EOI); |
| 272 |
17298024 |
t2 = VTAILQ_NEXT(t, list); |
| 273 |
17298024 |
AN(t2); |
| 274 |
17298024 |
return (t2); |
| 275 |
|
} |
| 276 |
|
|
| 277 |
|
struct token * |
| 278 |
1238452 |
vcc_PeekToken(const struct vcc *tl) |
| 279 |
|
{ |
| 280 |
|
|
| 281 |
1238452 |
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); |
| 282 |
1238452 |
return (vcc_PeekTokenFrom(tl, tl->t)); |
| 283 |
|
} |
| 284 |
|
|
| 285 |
|
void |
| 286 |
6594272 |
vcc_NextToken(struct vcc *tl) |
| 287 |
|
{ |
| 288 |
|
|
| 289 |
6594272 |
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); |
| 290 |
6594272 |
tl->t = vcc_PeekTokenFrom(tl, tl->t); |
| 291 |
6594272 |
} |
| 292 |
|
|
| 293 |
|
void |
| 294 |
4579708 |
vcc__Expect(struct vcc *tl, unsigned tok, unsigned line) |
| 295 |
|
{ |
| 296 |
4579708 |
if (tl->t->tok == tok) |
| 297 |
4579648 |
return; |
| 298 |
60 |
VSB_printf(tl->sb, "Expected %s got ", vcl_tnames[tok]); |
| 299 |
60 |
vcc_ErrToken(tl, tl->t); |
| 300 |
60 |
VSB_printf(tl->sb, "\n(program line %u), at\n", line); |
| 301 |
60 |
vcc_ErrWhere(tl, tl->t); |
| 302 |
4579708 |
} |
| 303 |
|
|
| 304 |
|
/*-------------------------------------------------------------------- |
| 305 |
|
* Compare ID token to string, return true of match |
| 306 |
|
*/ |
| 307 |
|
|
| 308 |
|
int |
| 309 |
12169236 |
vcc_IdIs(const struct token *t, const char *p) |
| 310 |
|
{ |
| 311 |
|
const char *q; |
| 312 |
|
|
| 313 |
12169236 |
assert(t->tok == ID); |
| 314 |
16174180 |
for (q = t->b; q < t->e && *p != '\0'; p++, q++) |
| 315 |
15256236 |
if (*q != *p) |
| 316 |
11251292 |
return (0); |
| 317 |
917944 |
if (q != t->e || *p != '\0') |
| 318 |
840 |
return (0); |
| 319 |
917104 |
return (1); |
| 320 |
12169236 |
} |
| 321 |
|
|
| 322 |
|
/*-------------------------------------------------------------------- |
| 323 |
|
* Check that we have a Vinyl identifier |
| 324 |
|
*/ |
| 325 |
|
|
| 326 |
|
void |
| 327 |
128 |
vcc_PrintTokens(const struct vcc *tl, |
| 328 |
|
const struct token *tb, const struct token *te) |
| 329 |
|
{ |
| 330 |
|
|
| 331 |
128 |
CHECK_OBJ_NOTNULL(tl, VCC_MAGIC); |
| 332 |
128 |
AN(tb); |
| 333 |
128 |
AN(te); |
| 334 |
424 |
while (tb != te) { |
| 335 |
296 |
VSB_printf(tl->sb, "%.*s", PF(tb)); |
| 336 |
296 |
tb = vcc_PeekTokenFrom(tl, tb); |
| 337 |
296 |
AN(tb); |
| 338 |
|
} |
| 339 |
128 |
} |
| 340 |
|
|
| 341 |
|
void |
| 342 |
521756 |
vcc_ExpectVid(struct vcc *tl, const char *what) |
| 343 |
|
{ |
| 344 |
521756 |
const char *bad = NULL; |
| 345 |
|
struct token *t2; |
| 346 |
|
|
| 347 |
521756 |
ExpectErr(tl, ID); |
| 348 |
521752 |
ERRCHK(tl); |
| 349 |
|
|
| 350 |
521752 |
t2 = vcc_PeekToken(tl); |
| 351 |
521752 |
AN(t2); |
| 352 |
521764 |
while (t2->tok == '.') { |
| 353 |
12 |
bad = "."; |
| 354 |
12 |
t2 = vcc_PeekTokenFrom(tl, t2); |
| 355 |
12 |
AN(t2); |
| 356 |
12 |
if (t2->tok != ID) |
| 357 |
0 |
break; |
| 358 |
12 |
t2 = vcc_PeekTokenFrom(tl, t2); |
| 359 |
12 |
AN(t2); |
| 360 |
|
} |
| 361 |
521752 |
if (bad == NULL) |
| 362 |
521740 |
bad = VCT_invalid_name(tl->t->b, tl->t->e); |
| 363 |
521752 |
if (bad != NULL) { |
| 364 |
12 |
VSB_printf(tl->sb, "Name of %s, '", what); |
| 365 |
12 |
vcc_PrintTokens(tl, tl->t, t2); |
| 366 |
24 |
VSB_printf(tl->sb, |
| 367 |
12 |
"', contains illegal character '%c'\n", *bad); |
| 368 |
12 |
vcc_ErrWhere2(tl, tl->t, t2); |
| 369 |
12 |
return; |
| 370 |
|
} |
| 371 |
521756 |
} |
| 372 |
|
|
| 373 |
|
/*-------------------------------------------------------------------- |
| 374 |
|
* Decode a string |
| 375 |
|
*/ |
| 376 |
|
|
| 377 |
|
static void |
| 378 |
588536 |
vcc_decstr(struct vcc *tl, unsigned sep) |
| 379 |
|
{ |
| 380 |
|
char *q; |
| 381 |
|
unsigned int l; |
| 382 |
|
|
| 383 |
588536 |
assert(tl->t->tok == CSTR); |
| 384 |
588536 |
l = pdiff(tl->t->b + sep, tl->t->e - sep); |
| 385 |
588536 |
tl->t->dec = TlAlloc(tl, l + 1); |
| 386 |
588536 |
AN(tl->t->dec); |
| 387 |
588536 |
q = tl->t->dec; |
| 388 |
588536 |
memcpy(q, tl->t->b + sep, l); |
| 389 |
588536 |
q[l] = '\0'; |
| 390 |
588536 |
} |
| 391 |
|
|
| 392 |
|
/*-------------------------------------------------------------------- |
| 393 |
|
* Add a token to the token list. |
| 394 |
|
*/ |
| 395 |
|
|
| 396 |
|
static void |
| 397 |
12446496 |
vcc_addtoken(struct vcc *tl, unsigned tok, |
| 398 |
|
struct source *sp, const char *b, const char *e) |
| 399 |
|
{ |
| 400 |
|
struct token *t; |
| 401 |
|
|
| 402 |
12446496 |
t = TlAlloc(tl, sizeof *t); |
| 403 |
12446496 |
assert(t != NULL); |
| 404 |
12446496 |
t->tok = tok; |
| 405 |
12446496 |
t->b = b; |
| 406 |
12446496 |
t->e = e; |
| 407 |
12446496 |
t->src = sp; |
| 408 |
12446496 |
VTAILQ_INSERT_TAIL(&sp->src_tokens, t, src_list); |
| 409 |
12446496 |
tl->t = t; |
| 410 |
12446496 |
} |
| 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 |
12427392 |
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 |
60833416 |
for (dd = delim_defs; dd->name != NULL; dd++) |
| 442 |
48994672 |
if (!strncmp(p, dd->b, dd->len)) |
| 443 |
588648 |
break; |
| 444 |
|
|
| 445 |
12427392 |
if (dd->name == NULL) |
| 446 |
11838744 |
return (0); |
| 447 |
|
|
| 448 |
588648 |
q = strstr(p + dd->len, dd->e); |
| 449 |
588648 |
if (q != NULL && !dd->crlf) { |
| 450 |
462216 |
r = strpbrk(p + dd->len, "\r\n"); |
| 451 |
462216 |
if (r != NULL && r < q) |
| 452 |
0 |
q = NULL; |
| 453 |
462216 |
} |
| 454 |
|
|
| 455 |
588648 |
if (q == NULL) { |
| 456 |
16 |
vcc_addtoken(tl, EOI, sp, p, p + dd->len); |
| 457 |
16 |
VSB_printf(tl->sb, "Unterminated %s, starting at\n", dd->name); |
| 458 |
16 |
vcc_ErrWhere(tl, tl->t); |
| 459 |
16 |
return (0); |
| 460 |
|
} |
| 461 |
|
|
| 462 |
588632 |
assert(q < sp->e); |
| 463 |
588632 |
vcc_addtoken(tl, dd->tok, sp, p, q + dd->len); |
| 464 |
588632 |
if (dd->tok == CSTR) |
| 465 |
588536 |
vcc_decstr(tl, dd->len); |
| 466 |
588632 |
*qp = q + dd->len; |
| 467 |
588632 |
return (1); |
| 468 |
12427392 |
} |
| 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 int |
| 476 |
176808 |
vcc_lex_number_token_valid(struct vcc *tl) |
| 477 |
|
{ |
| 478 |
176808 |
if (*tl->t->e == 'e' || *tl->t->e == 'E') { |
| 479 |
8 |
VSB_printf(tl->sb, "Unexpected character '%c'.\n", *tl->t->e); |
| 480 |
8 |
tl->t->e++; |
| 481 |
8 |
vcc_ErrWhere(tl, tl->t); |
| 482 |
8 |
return (0); |
| 483 |
|
} |
| 484 |
176800 |
return (1); |
| 485 |
176808 |
} |
| 486 |
|
|
| 487 |
|
static const char * |
| 488 |
176824 |
vcc_lex_number(struct vcc *tl, struct source *sp, const char *p) |
| 489 |
|
{ |
| 490 |
|
const char *q, *r; |
| 491 |
|
char *s; |
| 492 |
|
|
| 493 |
612436 |
for (q = p; q < sp->e; q++) |
| 494 |
612436 |
if (!vct_isdigit(*q)) |
| 495 |
176824 |
break; |
| 496 |
176824 |
if (*q != '.') { |
| 497 |
157204 |
vcc_addtoken(tl, CNUM, sp, p, q); |
| 498 |
157204 |
if (q - p > 15) { |
| 499 |
4 |
VSB_cat(tl->sb, "Too many digits for integer.\n"); |
| 500 |
4 |
vcc_ErrWhere(tl, tl->t); |
| 501 |
4 |
return (NULL); |
| 502 |
|
} |
| 503 |
157200 |
if (! vcc_lex_number_token_valid(tl)) |
| 504 |
4 |
return (NULL); |
| 505 |
157196 |
tl->t->num = strtod(p, &s); |
| 506 |
157196 |
assert(s == tl->t->e); |
| 507 |
157196 |
return (q); |
| 508 |
|
} |
| 509 |
19620 |
r = ++q; |
| 510 |
39456 |
for (; r < sp->e; r++) |
| 511 |
39456 |
if (!vct_isdigit(*r)) |
| 512 |
19620 |
break; |
| 513 |
19620 |
vcc_addtoken(tl, FNUM, sp, p, r); |
| 514 |
19620 |
if (q - p > 13 || r - q > 3) { |
| 515 |
12 |
VSB_cat(tl->sb, "Too many digits for real.\n"); |
| 516 |
12 |
vcc_ErrWhere(tl, tl->t); |
| 517 |
12 |
return (NULL); |
| 518 |
|
} |
| 519 |
19608 |
if (! vcc_lex_number_token_valid(tl)) |
| 520 |
4 |
return (NULL); |
| 521 |
19604 |
tl->t->num = strtod(p, &s); |
| 522 |
19604 |
assert(s == tl->t->e); |
| 523 |
19604 |
return (r); |
| 524 |
176824 |
} |
| 525 |
|
|
| 526 |
|
/*-------------------------------------------------------------------- |
| 527 |
|
* Lexical analysis and token generation |
| 528 |
|
*/ |
| 529 |
|
|
| 530 |
|
void |
| 531 |
19132 |
vcc_Lexer(struct vcc *tl, struct source *sp) |
| 532 |
|
{ |
| 533 |
|
const char *p, *q, *r; |
| 534 |
|
unsigned u; |
| 535 |
|
struct vsb *vsb; |
| 536 |
|
char namebuf[40]; |
| 537 |
|
|
| 538 |
25350392 |
for (p = sp->b; p < sp->e; ) { |
| 539 |
|
|
| 540 |
|
/* Skip any whitespace */ |
| 541 |
25331356 |
if (vct_isspace(*p)) { |
| 542 |
11919004 |
p++; |
| 543 |
11919004 |
continue; |
| 544 |
|
} |
| 545 |
|
|
| 546 |
|
/* Skip '#.*\n' comments */ |
| 547 |
13412352 |
if (*p == '#') { |
| 548 |
46160368 |
while (p < sp->e && *p != '\n') |
| 549 |
45175548 |
p++; |
| 550 |
984820 |
continue; |
| 551 |
|
} |
| 552 |
|
|
| 553 |
|
/* Skip C-style comments */ |
| 554 |
12427532 |
if (*p == '/' && p[1] == '*') { |
| 555 |
264 |
for (q = p + 2; q < sp->e; q++) { |
| 556 |
260 |
if (*q == '/' && q[1] == '*') { |
| 557 |
4 |
VSB_cat(tl->sb, |
| 558 |
|
"/* ... */ comment contains /*\n"); |
| 559 |
4 |
vcc_addtoken(tl, EOI, sp, p, p + 2); |
| 560 |
4 |
vcc_ErrWhere(tl, tl->t); |
| 561 |
4 |
vcc_addtoken(tl, EOI, sp, q, q + 2); |
| 562 |
4 |
vcc_ErrWhere(tl, tl->t); |
| 563 |
4 |
return; |
| 564 |
|
} |
| 565 |
256 |
if (*q == '*' && q[1] == '/') { |
| 566 |
8 |
p = q + 2; |
| 567 |
8 |
break; |
| 568 |
|
} |
| 569 |
248 |
} |
| 570 |
12 |
if (q < sp->e) |
| 571 |
8 |
continue; |
| 572 |
4 |
vcc_addtoken(tl, EOI, sp, p, p + 2); |
| 573 |
4 |
VSB_cat(tl->sb, |
| 574 |
|
"Unterminated /* ... */ comment, starting at\n"); |
| 575 |
4 |
vcc_ErrWhere(tl, tl->t); |
| 576 |
4 |
return; |
| 577 |
|
} |
| 578 |
|
|
| 579 |
|
/* Skip C++-style comments */ |
| 580 |
12427516 |
if (*p == '/' && p[1] == '/') { |
| 581 |
2012 |
while (p < sp->e && *p != '\n') |
| 582 |
1944 |
p++; |
| 583 |
68 |
continue; |
| 584 |
|
} |
| 585 |
|
|
| 586 |
|
/* Recognize BLOB (= SF-binary) */ |
| 587 |
12427448 |
if (*p == ':') { |
| 588 |
56 |
vsb = VSB_new_auto(); |
| 589 |
56 |
AN(vsb); |
| 590 |
56 |
q = sp->e; |
| 591 |
56 |
q -= (q - (p + 1)) % 4; |
| 592 |
56 |
assert(q > p); |
| 593 |
56 |
r = VENC_Decode_Base64(vsb, p + 1, q); |
| 594 |
56 |
if (r == NULL) { |
| 595 |
8 |
vcc_addtoken(tl, CBLOB, sp, p, q + 1); |
| 596 |
8 |
VSB_cat(tl->sb, |
| 597 |
|
"Missing colon at end of BLOB:\n"); |
| 598 |
8 |
vcc_ErrWhere(tl, tl->t); |
| 599 |
8 |
VSB_destroy(&vsb); |
| 600 |
8 |
return; |
| 601 |
|
} |
| 602 |
48 |
vcc_addtoken(tl, CBLOB, sp, p, r + 1); |
| 603 |
48 |
if (*r == ':' && ((r - p) % 4) != 1) { |
| 604 |
12 |
VSB_cat(tl->sb, |
| 605 |
|
"BLOB must have n*4 base64 characters\n"); |
| 606 |
12 |
vcc_ErrWhere(tl, tl->t); |
| 607 |
12 |
VSB_destroy(&vsb); |
| 608 |
12 |
return; |
| 609 |
|
} |
| 610 |
36 |
if (*r == '=') { |
| 611 |
12 |
VSB_cat(tl->sb, |
| 612 |
|
"Wrong padding ('=') in BLOB:\n"); |
| 613 |
12 |
vcc_ErrWhere(tl, tl->t); |
| 614 |
12 |
VSB_destroy(&vsb); |
| 615 |
12 |
return; |
| 616 |
|
} |
| 617 |
24 |
if (*r != ':') { |
| 618 |
8 |
VSB_cat(tl->sb, "Illegal BLOB character:\n"); |
| 619 |
8 |
vcc_ErrWhere(tl, tl->t); |
| 620 |
8 |
VSB_destroy(&vsb); |
| 621 |
8 |
return; |
| 622 |
|
} |
| 623 |
16 |
r++; |
| 624 |
16 |
AZ(VSB_finish(vsb)); |
| 625 |
|
|
| 626 |
16 |
bprintf(namebuf, "blob_%u", tl->unique++); |
| 627 |
32 |
Fh(tl, 0, |
| 628 |
|
"\nstatic const unsigned char %s_data[%zd] = {\n", |
| 629 |
16 |
namebuf, VSB_len(vsb)); |
| 630 |
524 |
for (u = 0; u < VSB_len(vsb); u++) { |
| 631 |
508 |
Fh(tl, 0, "\t0x%02x,", VSB_data(vsb)[u] & 0xff); |
| 632 |
508 |
if ((u & 7) == 7) |
| 633 |
60 |
Fh(tl, 0, "\n"); |
| 634 |
508 |
} |
| 635 |
16 |
if ((u & 7) != 7) |
| 636 |
16 |
Fh(tl, 0, "\n"); |
| 637 |
16 |
Fh(tl, 0, "};\n"); |
| 638 |
32 |
Fh(tl, 0, |
| 639 |
|
"\nstatic const struct vrt_blob %s[1] = {{\n", |
| 640 |
16 |
namebuf); |
| 641 |
16 |
Fh(tl, 0, "\t.magic =\tVRT_BLOB_MAGIC,\n"); |
| 642 |
16 |
Fh(tl, 0, "\t.len =\t%zd,\n", VSB_len(vsb)); |
| 643 |
16 |
Fh(tl, 0, "\t.blob =\t%s_data,\n", namebuf); |
| 644 |
16 |
Fh(tl, 0, "}};\n"); |
| 645 |
16 |
REPLACE(tl->t->dec, namebuf); |
| 646 |
16 |
VSB_destroy(&vsb); |
| 647 |
16 |
p = r; |
| 648 |
16 |
continue; |
| 649 |
|
} |
| 650 |
|
|
| 651 |
|
/* Match delimited tokens */ |
| 652 |
12427392 |
if (vcc_delim_token(tl, sp, p, &q) != 0) { |
| 653 |
588632 |
p = q; |
| 654 |
588632 |
continue; |
| 655 |
|
} |
| 656 |
11838760 |
ERRCHK(tl); |
| 657 |
|
|
| 658 |
|
/* Match for the fixed tokens (see generate.py) */ |
| 659 |
11838744 |
u = vcl_fixed_token(p, &q); |
| 660 |
11838744 |
if (u != 0) { |
| 661 |
6471660 |
vcc_addtoken(tl, u, sp, p, q); |
| 662 |
6471660 |
p = q; |
| 663 |
6471660 |
continue; |
| 664 |
|
} |
| 665 |
|
|
| 666 |
|
/* Match Identifiers */ |
| 667 |
5367084 |
if (vct_isident1(*p)) { |
| 668 |
40584388 |
for (q = p; q < sp->e; q++) |
| 669 |
40584388 |
if (!vct_isident(*q)) |
| 670 |
5190252 |
break; |
| 671 |
5190252 |
vcc_addtoken(tl, ID, sp, p, q); |
| 672 |
5190252 |
p = q; |
| 673 |
5190252 |
continue; |
| 674 |
|
} |
| 675 |
|
|
| 676 |
|
/* Match numbers { [0-9]+ } */ |
| 677 |
176832 |
if (vct_isdigit(*p)) { |
| 678 |
176824 |
p = vcc_lex_number(tl, sp, p); |
| 679 |
176824 |
if (p == NULL) |
| 680 |
24 |
return; |
| 681 |
176800 |
continue; |
| 682 |
|
} |
| 683 |
8 |
vcc_addtoken(tl, EOI, sp, p, p + 1); |
| 684 |
8 |
VSB_cat(tl->sb, "Syntax error at\n"); |
| 685 |
8 |
vcc_ErrWhere(tl, tl->t); |
| 686 |
8 |
return; |
| 687 |
|
} |
| 688 |
19036 |
vcc_addtoken(tl, EOI, sp, sp->e, sp->e); |
| 689 |
19132 |
} |