From 341254137052b4a33e151bde3c2036d8e2a63c42 Mon Sep 17 00:00:00 2001 From: "Sven M. Hallberg" Date: Wed, 16 Jan 2013 15:05:04 +0100 Subject: [PATCH] move api additions to glue.[ch] --- examples/Makefile | 5 ++- examples/dns.c | 76 +------------------------------------------ examples/dns_common.c | 1 + examples/dns_common.h | 1 + examples/glue.c | 65 ++++++++++++++++++++++++++++++++++++ examples/glue.h | 36 ++++++++++++++++++++ 6 files changed, 106 insertions(+), 78 deletions(-) create mode 100644 examples/glue.c create mode 100644 examples/glue.h diff --git a/examples/Makefile b/examples/Makefile index 6a054ca..786af44 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -15,14 +15,13 @@ LDFLAGS += $(pkg-config --libs glib-2.0) all: dns base64 dns: LDFLAGS:=-L../src -lhammer $(LDFLAGS) -dns: dns.o rr.o dns_common.o +dns: dns.o rr.o dns_common.o glue.o $(call hush, "Linking $@") $(CC) -o $@ $^ $(LDFLAGS) dns.o: ../src/hammer.h dns_common.h - rr.o: ../src/hammer.h rr.h dns_common.h - dns_common.o: ../src/hammer.h dns_common.h +glue.o: ../src/hammer.h glue.h base64: LDFLAGS:=-L../src -lhammer $(LDFLAGS) base64: base64.o diff --git a/examples/dns.c b/examples/dns.c index d786d59..b513a51 100644 --- a/examples/dns.c +++ b/examples/dns.c @@ -13,89 +13,15 @@ /// -// API Additions +// Semantic Actions and Validations /// -#define H_RULE(rule, def) const HParser *rule = def -#define H_ARULE(rule, def) const HParser *rule = h_action(def, act_ ## rule) - -// The action equivalent of h_ignore. -const HParsedToken *act_ignore(const HParseResult *p) -{ - return NULL; -} - -// Helper to build HAction's that pick one index out of a sequence. -const HParsedToken *act_index(int i, const HParseResult *p) -{ - if(!p) return NULL; - - const HParsedToken *tok = p->ast; - - if(!tok || tok->token_type != TT_SEQUENCE) - return NULL; - - const HCountedArray *seq = tok->seq; - size_t n = seq->used; - - if(i<0 || (size_t)i>=n) - return NULL; - else - return tok->seq->elements[i]; -} - -const HParsedToken *act_index0(const HParseResult *p) -{ - return act_index(0, p); -} - -HParsedToken *h_make_token(HArena *arena, HTokenType type, void *value) { - HParsedToken *ret = h_arena_malloc(arena, sizeof(HParsedToken)); - ret->token_type = type; - ret->user = value; - return ret; -} - -#define H_MAKE(TYP) \ - ((TYP ## _t *) h_arena_malloc(p->arena, sizeof(TYP ## _t))) - -#define H_MAKE_TOKEN(TYP, VAL) \ - h_make_token(p->arena, TT_ ## TYP, VAL) - -HParsedToken *h_carray_index(const HCountedArray *a, size_t i) { - assert(i < a->used); - return a->elements[i]; -} - -HParsedToken *h_seq_index(const HParsedToken *p, size_t i) { - assert(p->token_type == TT_SEQUENCE); - return h_carray_index(p->seq, i); -} - -void *h_seq_index_user(HTokenType type, const HParsedToken *p, size_t i) { - HParsedToken *elem = h_seq_index(p, i); - assert(elem->token_type == (HTokenType)type); - return elem->user; -} - -#define H_SEQ_INDEX(TYP, SEQ, IDX) \ - ((TYP ## _t *) h_seq_index_user(TT_ ## TYP, SEQ, IDX)) - -#define H_FIELD(TYP, IDX) \ - H_SEQ_INDEX(TYP, p->ast, IDX) - - bool is_zero(HParseResult *p) { if (TT_UINT != p->ast->token_type) return false; return (0 == p->ast->uint); } - -/// -// Semantic Actions -/// - /** * Every DNS message should have QDCOUNT entries in the question * section, and ANCOUNT+NSCOUNT+ARCOUNT resource records. diff --git a/examples/dns_common.c b/examples/dns_common.c index 3d349f1..5bd2374 100644 --- a/examples/dns_common.c +++ b/examples/dns_common.c @@ -13,6 +13,7 @@ bool validate_label(HParseResult *p) { return (64 > p->ast->seq->used); } + const HParser* init_domain() { static const HParser *domain = NULL; if (domain) diff --git a/examples/dns_common.h b/examples/dns_common.h index 41d73f0..2d796f8 100644 --- a/examples/dns_common.h +++ b/examples/dns_common.h @@ -2,6 +2,7 @@ #define HAMMER_DNS_COMMON__H #include "../src/hammer.h" +#include "glue.h" const HParser* init_domain(); const HParser* init_character_string(); diff --git a/examples/glue.c b/examples/glue.c new file mode 100644 index 0000000..a438b17 --- /dev/null +++ b/examples/glue.c @@ -0,0 +1,65 @@ +#include "glue.h" + + +// The action equivalent of h_ignore. +const HParsedToken *act_ignore(const HParseResult *p) +{ + return NULL; +} + +// Helper to build HAction's that pick one index out of a sequence. +const HParsedToken *act_index(int i, const HParseResult *p) +{ + if(!p) return NULL; + + const HParsedToken *tok = p->ast; + + if(!tok || tok->token_type != TT_SEQUENCE) + return NULL; + + const HCountedArray *seq = tok->seq; + size_t n = seq->used; + + if(i<0 || (size_t)i>=n) + return NULL; + else + return tok->seq->elements[i]; +} + +const HParsedToken *act_index0(const HParseResult *p) +{ + return act_index(0, p); +} + +HParsedToken *h_make_token(HArena *arena, HTokenType type, void *value) +{ + HParsedToken *ret = h_arena_malloc(arena, sizeof(HParsedToken)); + ret->token_type = type; + ret->user = value; + return ret; +} + +#define H_MAKE(TYP) \ + ((TYP ## _t *) h_arena_malloc(p->arena, sizeof(TYP ## _t))) + +#define H_MAKE_TOKEN(TYP, VAL) \ + h_make_token(p->arena, TT_ ## TYP, VAL) + +HParsedToken *h_carray_index(const HCountedArray *a, size_t i) +{ + assert(i < a->used); + return a->elements[i]; +} + +HParsedToken *h_seq_index(const HParsedToken *p, size_t i) +{ + assert(p->token_type == TT_SEQUENCE); + return h_carray_index(p->seq, i); +} + +void *h_seq_index_user(HTokenType type, const HParsedToken *p, size_t i) +{ + HParsedToken *elem = h_seq_index(p, i); + assert(elem->token_type == (HTokenType)type); + return elem->user; +} diff --git a/examples/glue.h b/examples/glue.h new file mode 100644 index 0000000..d8776b3 --- /dev/null +++ b/examples/glue.h @@ -0,0 +1,36 @@ +#ifndef HAMMER_EXAMPLES_GLUE__H +#define HAMMER_EXAMPLES_GLUE__H + +#include +#include "../src/hammer.h" + +/// +// API Additions +/// + +#define H_RULE(rule, def) const HParser *rule = def +#define H_ARULE(rule, def) const HParser *rule = h_action(def, act_ ## rule) + +const HParsedToken *act_ignore(const HParseResult *p); +const HParsedToken *act_index(int i, const HParseResult *p); +const HParsedToken *act_index0(const HParseResult *p); + +HParsedToken *h_make_token(HArena *arena, HTokenType type, void *value); + +#define H_MAKE(TYP) \ + ((TYP ## _t *) h_arena_malloc(p->arena, sizeof(TYP ## _t))) + +#define H_MAKE_TOKEN(TYP, VAL) \ + h_make_token(p->arena, TT_ ## TYP, VAL) + +HParsedToken *h_carray_index(const HCountedArray *a, size_t i); +HParsedToken *h_seq_index(const HParsedToken *p, size_t i); +void *h_seq_index_user(HTokenType type, const HParsedToken *p, size_t i); + +#define H_SEQ_INDEX(TYP, SEQ, IDX) \ + ((TYP ## _t *) h_seq_index_user(TT_ ## TYP, SEQ, IDX)) + +#define H_FIELD(TYP, IDX) \ + H_SEQ_INDEX(TYP, p->ast, IDX) + +#endif