action() works. Will finish DNS tomorrow.

This commit is contained in:
Meredith L. Patterson 2012-05-22 03:57:27 +02:00
parent e2af24fe80
commit f921ece53f
3 changed files with 33 additions and 13 deletions

View file

@ -344,8 +344,10 @@ typedef struct {
static parse_result_t* parse_action(void *env, parse_state_t *state) {
parse_action_t *a = (parse_action_t*)env;
if (a->p && a->action) {
parsed_token_t *tok = a->action(do_parse(a->p, state));
return make_result(state, tok);
parse_result_t *tmp = do_parse(a->p, state);
//parsed_token_t *tok = a->action(do_parse(a->p, state));
const parsed_token_t *tok = a->action(tmp);
return make_result(state, (parsed_token_t*)tok);
} else // either the parser's missing or the action's missing
return NULL;
}
@ -1046,21 +1048,35 @@ static void test_whitespace(void) {
#include <ctype.h>
parsed_token_t* upcase(parse_result_t *p) {
const parsed_token_t* upcase(parse_result_t *p) {
switch(p->ast->token_type) {
case TT_SEQUENCE:
for (size_t i=0; i<p->ast->seq->used; ++i) {
upcase((parse_result_t*)p->ast->seq->elements[i]);
return (parsed_token_t*)p->ast;
{
parsed_token_t *ret = a_new_(p->arena, parsed_token_t, 1);
counted_array_t *seq = carray_new_sized(p->arena, p->ast->seq->used);
ret->token_type = TT_SEQUENCE;
for (size_t i=0; i<p->ast->seq->used; ++i) {
if (TT_UINT == ((parsed_token_t*)p->ast->seq->elements[i])->token_type) {
parsed_token_t *tmp = a_new_(p->arena, parsed_token_t, 1);
tmp->token_type = TT_UINT;
tmp->uint = toupper(((parsed_token_t*)p->ast->seq->elements[i])->uint);
carray_append(seq, tmp);
} else {
carray_append(seq, p->ast->seq->elements[i]);
}
}
ret->seq = seq;
return (const parsed_token_t*)ret;
}
case TT_UINT:
{
parsed_token_t *ret = (parsed_token_t*)p->ast;
ret->uint = toupper(ret->uint);
return ret;
parsed_token_t *ret = a_new_(p->arena, parsed_token_t, 1);
ret->token_type = TT_UINT;
ret->uint = toupper(p->ast->uint);
return (const parsed_token_t*)ret;
}
default:
return (parsed_token_t*)p->ast;
return p->ast;
}
}
@ -1074,8 +1090,8 @@ static void test_action(void) {
NULL),
upcase);
g_check_parse_ok(action_, "ab", 2, "(u0x41, u0x42)");
g_check_parse_ok(action_, "AB", 2, "(u0x41, u0x42)");
g_check_parse_ok(action_, "ab", 2, "(u0x41 u0x42)");
g_check_parse_ok(action_, "AB", 2, "(u0x41 u0x42)");
}
static void test_not_in(void) {

View file

@ -87,7 +87,7 @@ typedef struct parse_result {
* say, structs) and stuff values for them into the void* in the
* tagged union in parsed_token_t.
*/
typedef parsed_token_t* (*action_t)(parse_result_t *p);
typedef const parsed_token_t* (*action_t)(parse_result_t *p);
/**
* Type of a boolean attribute-checking function, used in the

View file

@ -94,6 +94,10 @@ static inline void append_buf_c(struct result_buf *buf, char v) {
static void unamb_sub(const parsed_token_t* tok, struct result_buf *buf) {
char* tmpbuf;
int len;
if (!tok) {
append_buf(buf, "NULL", 4);
return;
}
switch (tok->token_type) {
case TT_NONE:
append_buf(buf, "null", 4);