Commit fa8942f6 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Add a very rudimentary symbol table, and use it to find our

variables.



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@5013 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 2fe55590
......@@ -22,6 +22,7 @@ libvcl_la_SOURCES = \
vcc_fixed_token.c \
vcc_obj.c \
vcc_string.c \
vcc_symb.c \
vcc_token.c \
vcc_var.c \
vcc_xref.c
......
......@@ -463,6 +463,7 @@ vcc_NewVcc(const struct vcc *tl0)
REPLACE(tl->vcl_dir, tl0->vcl_dir);
tl->vars = tl0->vars;
}
VTAILQ_INIT(&tl->symbols);
VTAILQ_INIT(&tl->hosts);
VTAILQ_INIT(&tl->membits);
VTAILQ_INIT(&tl->tokens);
......@@ -504,6 +505,7 @@ vcc_DestroyTokenList(struct vcc *tl, char *ret)
{
struct membit *mb;
struct source *sp;
struct symbol *sym;
int i;
while (!VTAILQ_EMPTY(&tl->membits)) {
......@@ -518,6 +520,12 @@ vcc_DestroyTokenList(struct vcc *tl, char *ret)
vcc_destroy_source(sp);
}
while (!VTAILQ_EMPTY(&tl->symbols)) {
sym = VTAILQ_FIRST(&tl->symbols);
VTAILQ_REMOVE(&tl->symbols, sym, list);
FREE_OBJ(sym);
}
vsb_delete(tl->fh);
vsb_delete(tl->fc);
vsb_delete(tl->fi);
......@@ -537,12 +545,21 @@ static char *
vcc_CompileSource(const struct vcc *tl0, struct vsb *sb, struct source *sp)
{
struct vcc *tl;
struct symbol *sym;
const struct var *v;
char *of;
int i;
tl = vcc_NewVcc(tl0);
tl->sb = sb;
for (v = tl->vars; v->name != NULL; v++) {
sym = VCC_AddSymbol(tl, v->name);
sym->var = v;
if (v->fmt == HEADER)
sym->wildcard = 1;
}
vcl_output_lang_h(tl->fh);
Fh(tl, 0, "\n/* ---===### VCC generated below here ###===---*/\n");
Fh(tl, 0, "\nextern const struct VCL_conf VCL_conf;\n");
......@@ -685,7 +702,9 @@ VCC_New(void)
struct vcc *tl;
tl = vcc_NewVcc(NULL);
tl->vars = vcc_vars;
return (tl);
}
......
......@@ -62,6 +62,16 @@ struct token {
char *dec;
};
struct symbol {
unsigned magic;
#define SYMBOL_MAGIC 0x3368c9fb
VTAILQ_ENTRY(symbol) list;
char *name;
unsigned nlen;
unsigned wildcard;
const struct var *var;
};
VTAILQ_HEAD(tokenhead, token);
struct vcc {
......@@ -74,6 +84,7 @@ struct vcc {
char *vmod_dir;
const struct var *vars;
VTAILQ_HEAD(, symbol) symbols;
/* Instance section */
struct tokenhead tokens;
......@@ -225,6 +236,11 @@ char *vcc_regexp(struct vcc *tl);
int vcc_StringVal(struct vcc *tl);
void vcc_ExpectedStringval(struct vcc *tl);
/* vcc_symbol */
struct symbol *VCC_AddSymbol(struct vcc *tl, const char *name);
const struct symbol *VCC_FindSymbol(const struct vcc *tl,
const struct token *t);
/* vcc_token.c */
void vcc_Coord(const struct vcc *tl, struct vsb *vsb,
const struct token *t);
......
/*-
* Copyright (c) 2010 Linpro AS
* All rights reserved.
*
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "config.h"
#include "svnid.h"
SVNID("$Id$")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vsb.h"
#include "vcc_priv.h"
#include "vcc_compile.h"
#include "libvarnish.h"
/*--------------------------------------------------------------------*/
struct symbol *
VCC_AddSymbol(struct vcc *tl, const char *name)
{
struct symbol *sym;
VTAILQ_FOREACH(sym, &tl->symbols, list) {
if (strcmp(name, sym->name))
continue;
printf("%s <> %s\n", name, sym->name);
WRONG("name collision");
}
ALLOC_OBJ(sym, SYMBOL_MAGIC);
REPLACE(sym->name, name);
sym->nlen = strlen(name);
VTAILQ_INSERT_TAIL(&tl->symbols, sym, list);
return (sym);
}
const struct symbol *
VCC_FindSymbol(const struct vcc *tl, const struct token *t)
{
struct symbol *sym;
assert(t->tok == ID);
VTAILQ_FOREACH(sym, &tl->symbols, list) {
if (!sym->wildcard) {
if (vcc_IdIs(t, sym->name))
return (sym);
continue;
}
if (t->e - t->b <= sym->nlen)
continue;
if (!memcmp(sym->name, t->b, sym->nlen))
return (sym);
}
return (NULL);
}
......@@ -91,15 +91,14 @@ vcc_FindVar(struct vcc *tl, const struct token *t, int wr_access,
const char *use)
{
const struct var *v;
const struct symbol *sym;
AN(tl->vars);
for (v = tl->vars; v->name != NULL; v++) {
if (v->fmt == HEADER && (t->e - t->b) <= v->len)
continue;
if (v->fmt != HEADER && t->e - t->b != v->len)
continue;
if (memcmp(t->b, v->name, v->len))
continue;
sym = VCC_FindSymbol(tl, t);
if (sym != NULL) {
v = sym->var;
AN(v);
if (wr_access && v->l_methods == 0) {
vsb_printf(tl->sb, "Variable ");
vcc_ErrToken(tl, t);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment