rework dns example syntax into H_RULE style

This commit is contained in:
Sven M. Hallberg 2013-01-14 21:25:46 +01:00
parent 40ab87b4fb
commit 2ff51ab9c3

View file

@ -10,6 +10,10 @@
#define false 0 #define false 0
#define true 1 #define true 1
#define H_RULE(rule, def) const HParser *rule = def
#define H_ARULE(rule, def) const HParser *rule = h_action(def, act_ ## rule)
bool is_zero(HParseResult *p) { bool is_zero(HParseResult *p) {
if (TT_UINT != p->ast->token_type) if (TT_UINT != p->ast->token_type)
return false; return false;
@ -283,7 +287,7 @@ void set_rr(struct dns_rr rr, HCountedArray *rdata) {
} }
} }
const HParsedToken* pack_dns_struct(const HParseResult *p) { const HParsedToken* act_dns_message(const HParseResult *p) {
h_pprint(stdout, p->ast, 0, 2); h_pprint(stdout, p->ast, 0, 2);
HParsedToken *ret = h_arena_malloc(p->arena, sizeof(HParsedToken)); HParsedToken *ret = h_arena_malloc(p->arena, sizeof(HParsedToken));
ret->token_type = TT_DNS_MESSAGE; ret->token_type = TT_DNS_MESSAGE;
@ -360,66 +364,65 @@ const HParsedToken* pack_dns_struct(const HParseResult *p) {
return ret; return ret;
} }
// The action equivalent of h_ignore.
const HParsedToken *act_ignore(const HParseResult *p)
{
return NULL;
}
#define act_dns_hdzero act_ignore
const HParser* init_parser() { const HParser* init_parser() {
static HParser *dns_message = NULL; static const HParser *ret = NULL;
if (dns_message) if (ret)
return dns_message; return ret;
const HParser *domain = init_domain(); H_RULE (domain, init_domain());
H_ARULE(dns_hdzero, h_attr_bool(h_bits(3, false), is_zero));
const HParser *dns_header = h_sequence(h_bits(16, false), // ID H_RULE (dns_header, h_sequence(h_bits(16, false), // ID
h_bits(1, false), // QR h_bits(1, false), // QR
h_bits(4, false), // opcode h_bits(4, false), // opcode
h_bits(1, false), // AA h_bits(1, false), // AA
h_bits(1, false), // TC h_bits(1, false), // TC
h_bits(1, false), // RD h_bits(1, false), // RD
h_bits(1, false), // RA h_bits(1, false), // RA
h_ignore(h_attr_bool(h_bits(3, false), is_zero)), // Z dns_hdzero, // Z
h_bits(4, false), // RCODE h_bits(4, false), // RCODE
h_uint16(), // QDCOUNT h_uint16(), // QDCOUNT
h_uint16(), // ANCOUNT h_uint16(), // ANCOUNT
h_uint16(), // NSCOUNT h_uint16(), // NSCOUNT
h_uint16(), // ARCOUNT h_uint16(), // ARCOUNT
NULL); NULL));
H_RULE (type, h_int_range(h_uint16(), 1, 16));
const HParser *type = h_int_range(h_uint16(), 1, 16); H_RULE (qtype, h_choice(type,
const HParser *qtype = h_choice(type,
h_int_range(h_uint16(), 252, 255), h_int_range(h_uint16(), 252, 255),
NULL); NULL));
H_RULE (class, h_int_range(h_uint16(), 1, 4));
const HParser *class = h_int_range(h_uint16(), 1, 4); H_RULE (qclass, h_choice(class,
const HParser *qclass = h_choice(class,
h_int_range(h_uint16(), 255, 255), h_int_range(h_uint16(), 255, 255),
NULL); NULL));
H_RULE (dns_question, h_sequence(h_sequence(h_many1(h_length_value(h_int_range(h_uint8(), 1, 255),
const HParser *dns_question = h_sequence(h_sequence(h_many1(h_length_value(h_int_range(h_uint8(), 1, 255),
h_uint8())), h_uint8())),
h_ch('\x00'), h_ch('\x00'),
NULL), // QNAME NULL), // QNAME
qtype, // QTYPE qtype, // QTYPE
qclass, // QCLASS qclass, // QCLASS
NULL); NULL));
H_RULE (dns_rr, h_sequence(domain, // NAME
const HParser *dns_rr = h_sequence(domain, // NAME
type, // TYPE type, // TYPE
class, // CLASS class, // CLASS
h_uint32(), // TTL h_uint32(), // TTL
h_length_value(h_uint16(), h_uint8()), // RDLENGTH+RDATA h_length_value(h_uint16(), h_uint8()), // RDLENGTH+RDATA
NULL); NULL));
H_ARULE(dns_message, h_attr_bool(h_sequence(dns_header,
dns_message = (HParser*)h_action(h_attr_bool(h_sequence(dns_header,
h_many(dns_question), h_many(dns_question),
h_many(dns_rr), h_many(dns_rr),
h_end_p(), h_end_p(),
NULL), NULL),
validate_dns), validate_dns));
pack_dns_struct);
return dns_message; ret = dns_message;
return ret;
} }
int start_listening() { int start_listening() {