further merging of pesco's and aegis' changes

This commit is contained in:
Meredith L. Patterson 2013-05-23 02:16:52 -07:00
parent 1c7e9947a4
commit 3978266651
5 changed files with 71 additions and 83 deletions

View file

@ -1,12 +1,8 @@
#include <assert.h>
#include "glue.h"
#include "../src/internal.h" // for h_carray_*
// The action equivalent of h_ignore.
const HParsedToken *h_act_ignore(const HParseResult *p)
{
return NULL;
}
#include "hammer.h"
#include "internal.h" // for h_carray_*
#include "parsers/parser_internal.h"
// Helper to build HAction's that pick one index out of a sequence.
const HParsedToken *h_act_index(int i, const HParseResult *p)
@ -27,9 +23,57 @@ const HParsedToken *h_act_index(int i, const HParseResult *p)
return tok->seq->elements[i];
}
// Action version of h_seq_flatten.
const HParsedToken *h_act_first(const HParseResult *p) {
assert(p->ast);
assert(p->ast->token_type == TT_SEQUENCE);
assert(p->ast->seq->used > 0);
return p->ast->seq->elements[0];
}
const HParsedToken *h_act_second(const HParseResult *p) {
assert(p->ast);
assert(p->ast->token_type == TT_SEQUENCE);
assert(p->ast->seq->used > 0);
return p->ast->seq->elements[1];
}
const HParsedToken *h_act_last(const HParseResult *p) {
assert(p->ast);
assert(p->ast->token_type == TT_SEQUENCE);
assert(p->ast->seq->used > 0);
return p->ast->seq->elements[p->ast->seq->used-1];
}
static void act_flatten_(HCountedArray *seq, const HParsedToken *tok) {
if(tok == NULL) {
return;
} else if(tok->token_type == TT_SEQUENCE) {
size_t i;
for(i=0; i<tok->seq->used; i++)
act_flatten_(seq, tok->seq->elements[i]);
} else {
h_carray_append(seq, (HParsedToken *)tok);
}
}
const HParsedToken *h_act_flatten(const HParseResult *p) {
return h_seq_flatten(p->arena, p->ast);
HCountedArray *seq = h_carray_new(p->arena);
act_flatten_(seq, p->ast);
HParsedToken *res = a_new_(p->arena, HParsedToken, 1);
res->token_type = TT_SEQUENCE;
res->seq = seq;
res->index = p->ast->index;
res->bit_offset = p->ast->bit_offset;
return res;
}
const HParsedToken *h_act_ignore(const HParseResult *p) {
return NULL;
}
// Low-level helper for the h_make family.