move api additions to glue.[ch]
This commit is contained in:
parent
4b30ebdb77
commit
3412541370
6 changed files with 106 additions and 78 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
65
examples/glue.c
Normal file
65
examples/glue.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
36
examples/glue.h
Normal file
36
examples/glue.h
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#ifndef HAMMER_EXAMPLES_GLUE__H
|
||||
#define HAMMER_EXAMPLES_GLUE__H
|
||||
|
||||
#include <assert.h>
|
||||
#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
|
||||
Loading…
Add table
Add a link
Reference in a new issue