cleanup and bugfixing on domain parser
This commit is contained in:
parent
4f1e9ad22d
commit
587143eec1
3 changed files with 77 additions and 9 deletions
|
|
@ -1,4 +1,5 @@
|
|||
#include "glue.h"
|
||||
#include "../src/internal.h" // for h_carray_*
|
||||
|
||||
|
||||
// The action equivalent of h_ignore.
|
||||
|
|
@ -31,10 +32,68 @@ const HParsedToken *act_index0(const HParseResult *p)
|
|||
return act_index(0, p);
|
||||
}
|
||||
|
||||
HParsedToken *h_make_token(HArena *arena, HTokenType type, void *value)
|
||||
void h_seq_snoc(HParsedToken *xs, const HParsedToken *x)
|
||||
{
|
||||
assert(xs != NULL);
|
||||
assert(xs->token_type == TT_SEQUENCE);
|
||||
|
||||
h_carray_append(xs->seq, (void *)x);
|
||||
}
|
||||
|
||||
void h_seq_append(HParsedToken *xs, const HParsedToken *ys)
|
||||
{
|
||||
assert(xs != NULL);
|
||||
assert(xs->token_type == TT_SEQUENCE);
|
||||
assert(ys != NULL);
|
||||
assert(ys->token_type == TT_SEQUENCE);
|
||||
|
||||
for(size_t i; i<ys->seq->used; i++)
|
||||
h_carray_append(xs->seq, ys->seq->elements[i]);
|
||||
}
|
||||
|
||||
// Flatten nested sequences. Always returns a sequence.
|
||||
// If input element is not a sequence, returns it as a singleton sequence.
|
||||
const HParsedToken *h_token_flatten(HArena *arena, const HParsedToken *p)
|
||||
{
|
||||
assert(p != NULL);
|
||||
|
||||
HParsedToken *ret = h_make_token_seq(arena);
|
||||
switch(p->token_type) {
|
||||
case TT_SEQUENCE:
|
||||
// Flatten and append all.
|
||||
for(size_t i; i<p->seq->used; i++) {
|
||||
h_seq_append(ret, h_token_flatten(arena, h_seq_index(p, i)));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// Make singleton sequence.
|
||||
h_seq_snoc(ret, p);
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Action version of h_token_flatten.
|
||||
const HParsedToken *act_flatten(const HParseResult *p) {
|
||||
return h_token_flatten(p->arena, p->ast);
|
||||
}
|
||||
|
||||
HParsedToken *h_make_token_(HArena *arena, HTokenType type)
|
||||
{
|
||||
HParsedToken *ret = h_arena_malloc(arena, sizeof(HParsedToken));
|
||||
ret->token_type = type;
|
||||
return ret;
|
||||
}
|
||||
|
||||
HParsedToken *h_make_token_seq(HArena *arena)
|
||||
{
|
||||
return h_make_token_(arena, TT_SEQUENCE);
|
||||
}
|
||||
|
||||
HParsedToken *h_make_token(HArena *arena, HTokenType type, void *value)
|
||||
{
|
||||
HParsedToken *ret = h_make_token_(arena, type);
|
||||
ret->user = value;
|
||||
return ret;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue