Regex all works now! (Merge branch 'master' of https://github.com/thequux/hammer)
This commit is contained in:
commit
cb6e7f4229
27 changed files with 245 additions and 150 deletions
|
|
@ -53,7 +53,7 @@ void* h_rvm_run__m(HAllocator *mm__, HRVMProg *prog, const uint8_t* input, size_
|
|||
HRVMTrace **heads_p = a_new(HRVMTrace*, prog->length),
|
||||
**heads_n = a_new(HRVMTrace*, prog->length);
|
||||
|
||||
HRVMTrace *ret_trace;
|
||||
HRVMTrace *ret_trace = NULL;
|
||||
|
||||
uint8_t *insn_seen = a_new(uint8_t, prog->length); // 0 -> not seen, 1->processed, 2->queued
|
||||
HRVMThread *ip_queue = a_new(HRVMThread, prog->length);
|
||||
|
|
@ -61,6 +61,7 @@ void* h_rvm_run__m(HAllocator *mm__, HRVMProg *prog, const uint8_t* input, size_
|
|||
|
||||
|
||||
|
||||
|
||||
|
||||
#define THREAD ip_queue[ipq_top-1]
|
||||
#define PUSH_SVM(op_, arg_) do { \
|
||||
|
|
@ -102,15 +103,18 @@ void* h_rvm_run__m(HAllocator *mm__, HRVMProg *prog, const uint8_t* input, size_
|
|||
uint8_t hi, lo;
|
||||
uint16_t arg;
|
||||
while(ipq_top > 0) {
|
||||
if (insn_seen[THREAD.ip] == 1)
|
||||
if (insn_seen[THREAD.ip] == 1) {
|
||||
ipq_top--; // Kill thread.
|
||||
continue;
|
||||
}
|
||||
insn_seen[THREAD.ip] = 1;
|
||||
arg = prog->insns[THREAD.ip].arg;
|
||||
switch(prog->insns[THREAD.ip].op) {
|
||||
case RVM_ACCEPT:
|
||||
PUSH_SVM(SVM_ACCEPT, 0);
|
||||
ret_trace = THREAD.trace;
|
||||
goto run_trace;
|
||||
ipq_top--;
|
||||
goto next_insn;
|
||||
case RVM_MATCH:
|
||||
hi = (arg >> 8) & 0xff;
|
||||
lo = arg & 0xff;
|
||||
|
|
@ -163,10 +167,12 @@ void* h_rvm_run__m(HAllocator *mm__, HRVMProg *prog, const uint8_t* input, size_
|
|||
}
|
||||
// No accept was reached.
|
||||
match_fail:
|
||||
h_delete_arena(arena);
|
||||
return NULL;
|
||||
if (ret_trace == NULL) {
|
||||
// No match found; definite failure.
|
||||
h_delete_arena(arena);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
run_trace:
|
||||
// Invert the direction of the trace linked list.
|
||||
|
||||
ret_trace = invert_trace(ret_trace);
|
||||
|
|
@ -213,8 +219,9 @@ HParseResult *run_trace(HAllocator *mm__, HRVMProg *orig_prog, HRVMTrace *trace,
|
|||
case SVM_ACTION:
|
||||
// Action should modify stack appropriately
|
||||
if (!orig_prog->actions[cur->arg].action(arena, &ctx, orig_prog->actions[cur->arg].env)) {
|
||||
|
||||
// action failed... abort somehow
|
||||
// TODO: Actually abort
|
||||
goto fail;
|
||||
}
|
||||
break;
|
||||
case SVM_CAPTURE:
|
||||
|
|
@ -243,7 +250,7 @@ HParseResult *run_trace(HAllocator *mm__, HRVMProg *orig_prog, HRVMTrace *trace,
|
|||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
fail:
|
||||
h_delete_arena(arena);
|
||||
return NULL;
|
||||
}
|
||||
|
|
@ -294,7 +301,7 @@ void h_rvm_patch_arg(HRVMProg *prog, uint16_t ip, uint16_t new_val) {
|
|||
|
||||
size_t h_svm_count_to_mark(HSVMContext *ctx) {
|
||||
size_t ctm;
|
||||
for (ctm = 0; ctm < ctx->stack_count-1; ctm++) {
|
||||
for (ctm = 0; ctm < ctx->stack_count; ctm++) {
|
||||
if (ctx->stack[ctx->stack_count - 1 - ctm]->token_type == TT_MARK)
|
||||
return ctm;
|
||||
}
|
||||
|
|
@ -308,20 +315,20 @@ bool h_svm_action_make_sequence(HArena *arena, HSVMContext *ctx, void* env) {
|
|||
HParsedToken *res = ctx->stack[ctx->stack_count - 1 - n_items];
|
||||
assert (res->token_type == TT_MARK);
|
||||
res->token_type = TT_SEQUENCE;
|
||||
|
||||
|
||||
HCountedArray *ret_carray = h_carray_new_sized(arena, n_items);
|
||||
res->seq = ret_carray;
|
||||
// res index and bit offset are the same as the mark.
|
||||
for (size_t i = 0; i < n_items; i++) {
|
||||
ret_carray->elements[i] = ctx->stack[ctx->stack_count - n_items + i];
|
||||
}
|
||||
ret_carray->used = n_items;
|
||||
ctx->stack_count -= n_items;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool h_svm_action_clear_to_mark(HArena *arena, HSVMContext *ctx, void* env) {
|
||||
while (ctx->stack_count > 0) {
|
||||
if (ctx->stack[--ctx->stack_count]->token_type == TT_MARK)
|
||||
while (ctx->stack_count > 0) { if (ctx->stack[--ctx->stack_count]->token_type == TT_MARK)
|
||||
return true;
|
||||
}
|
||||
return false; // no mark found.
|
||||
|
|
|
|||
|
|
@ -67,12 +67,12 @@ void dump_rvm_prog(HRVMProg *prog) {
|
|||
if (high < low)
|
||||
printf("NONE\n");
|
||||
else {
|
||||
if (low >= 0x32 && low <= 0x7e)
|
||||
if (low >= 0x20 && low <= 0x7e)
|
||||
printf("%02hhx ('%c')", low, low);
|
||||
else
|
||||
printf("%02hhx", low);
|
||||
|
||||
if (high >= 0x32 && high <= 0x7e)
|
||||
if (high >= 0x20 && high <= 0x7e)
|
||||
printf(" - %02hhx ('%c')\n", high, high);
|
||||
else
|
||||
printf(" - %02hhx\n", high);
|
||||
|
|
|
|||
|
|
@ -9,7 +9,8 @@
|
|||
|
||||
HCountedArray *h_carray_new_sized(HArena * arena, size_t size) {
|
||||
HCountedArray *ret = h_arena_malloc(arena, sizeof(HCountedArray));
|
||||
assert(size > 0);
|
||||
if (size == 0)
|
||||
size = 1;
|
||||
ret->used = 0;
|
||||
ret->capacity = size;
|
||||
ret->arena = arena;
|
||||
|
|
|
|||
12
src/glue.c
12
src/glue.c
|
|
@ -5,7 +5,7 @@
|
|||
#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)
|
||||
HParsedToken *h_act_index(int i, const HParseResult *p)
|
||||
{
|
||||
if(!p) return NULL;
|
||||
|
||||
|
|
@ -23,7 +23,7 @@ const HParsedToken *h_act_index(int i, const HParseResult *p)
|
|||
return tok->seq->elements[i];
|
||||
}
|
||||
|
||||
const HParsedToken *h_act_first(const HParseResult *p) {
|
||||
HParsedToken *h_act_first(const HParseResult *p) {
|
||||
assert(p->ast);
|
||||
assert(p->ast->token_type == TT_SEQUENCE);
|
||||
assert(p->ast->seq->used > 0);
|
||||
|
|
@ -31,7 +31,7 @@ const HParsedToken *h_act_first(const HParseResult *p) {
|
|||
return p->ast->seq->elements[0];
|
||||
}
|
||||
|
||||
const HParsedToken *h_act_second(const HParseResult *p) {
|
||||
HParsedToken *h_act_second(const HParseResult *p) {
|
||||
assert(p->ast);
|
||||
assert(p->ast->token_type == TT_SEQUENCE);
|
||||
assert(p->ast->seq->used > 0);
|
||||
|
|
@ -39,7 +39,7 @@ const HParsedToken *h_act_second(const HParseResult *p) {
|
|||
return p->ast->seq->elements[1];
|
||||
}
|
||||
|
||||
const HParsedToken *h_act_last(const HParseResult *p) {
|
||||
HParsedToken *h_act_last(const HParseResult *p) {
|
||||
assert(p->ast);
|
||||
assert(p->ast->token_type == TT_SEQUENCE);
|
||||
assert(p->ast->seq->used > 0);
|
||||
|
|
@ -59,7 +59,7 @@ static void act_flatten_(HCountedArray *seq, const HParsedToken *tok) {
|
|||
}
|
||||
}
|
||||
|
||||
const HParsedToken *h_act_flatten(const HParseResult *p) {
|
||||
HParsedToken *h_act_flatten(const HParseResult *p) {
|
||||
HCountedArray *seq = h_carray_new(p->arena);
|
||||
|
||||
act_flatten_(seq, p->ast);
|
||||
|
|
@ -72,7 +72,7 @@ const HParsedToken *h_act_flatten(const HParseResult *p) {
|
|||
return res;
|
||||
}
|
||||
|
||||
const HParsedToken *h_act_ignore(const HParseResult *p) {
|
||||
HParsedToken *h_act_ignore(const HParseResult *p) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
|
|||
24
src/glue.h
24
src/glue.h
|
|
@ -55,13 +55,13 @@
|
|||
//
|
||||
|
||||
|
||||
#define H_RULE(rule, def) const HParser *rule = def
|
||||
#define H_ARULE(rule, def) const HParser *rule = h_action(def, act_ ## rule)
|
||||
#define H_VRULE(rule, def) const HParser *rule = \
|
||||
#define H_RULE(rule, def) HParser *rule = def
|
||||
#define H_ARULE(rule, def) HParser *rule = h_action(def, act_ ## rule)
|
||||
#define H_VRULE(rule, def) HParser *rule = \
|
||||
h_attr_bool(def, validate_ ## rule)
|
||||
#define H_VARULE(rule, def) const HParser *rule = \
|
||||
#define H_VARULE(rule, def) HParser *rule = \
|
||||
h_attr_bool(h_action(def, act_ ## rule), validate_ ## rule)
|
||||
#define H_AVRULE(rule, def) const HParser *rule = \
|
||||
#define H_AVRULE(rule, def) HParser *rule = \
|
||||
h_action(h_attr_bool(def, validate_ ## rule), act_ ## rule)
|
||||
|
||||
|
||||
|
|
@ -88,17 +88,17 @@
|
|||
// action such as h_act_index.
|
||||
//
|
||||
|
||||
const HParsedToken *h_act_index(int i, const HParseResult *p);
|
||||
const HParsedToken *h_act_first(const HParseResult *p);
|
||||
const HParsedToken *h_act_second(const HParseResult *p);
|
||||
const HParsedToken *h_act_last(const HParseResult *p);
|
||||
const HParsedToken *h_act_flatten(const HParseResult *p);
|
||||
const HParsedToken *h_act_ignore(const HParseResult *p);
|
||||
HParsedToken *h_act_index(int i, const HParseResult *p);
|
||||
HParsedToken *h_act_first(const HParseResult *p);
|
||||
HParsedToken *h_act_second(const HParseResult *p);
|
||||
HParsedToken *h_act_last(const HParseResult *p);
|
||||
HParsedToken *h_act_flatten(const HParseResult *p);
|
||||
HParsedToken *h_act_ignore(const HParseResult *p);
|
||||
|
||||
// Define 'myaction' as a specialization of 'paction' by supplying the leading
|
||||
// parameters.
|
||||
#define H_ACT_APPLY(myaction, paction, ...) \
|
||||
const HParsedToken *myaction(const HParseResult *p) { \
|
||||
HParsedToken *myaction(const HParseResult *p) { \
|
||||
return paction(__VA_ARGS__, p); \
|
||||
}
|
||||
|
||||
|
|
|
|||
16
src/hammer.h
16
src/hammer.h
|
|
@ -111,7 +111,7 @@ typedef struct HBitWriter_ HBitWriter;
|
|||
* say, structs) and stuff values for them into the void* in the
|
||||
* tagged union in HParsedToken.
|
||||
*/
|
||||
typedef const HParsedToken* (*HAction)(const HParseResult *p);
|
||||
typedef HParsedToken* (*HAction)(const HParseResult *p);
|
||||
|
||||
/**
|
||||
* Type of a boolean attribute-checking function, used in the
|
||||
|
|
@ -370,7 +370,7 @@ HAMMER_FN_DECL_NOARG(HParser*, h_nothing_p);
|
|||
*
|
||||
* Result token type: TT_SEQUENCE
|
||||
*/
|
||||
HAMMER_FN_DECL_VARARGS_ATTR(__attribute__((sentinel)), HParser*, h_sequence, const HParser* p);
|
||||
HAMMER_FN_DECL_VARARGS_ATTR(__attribute__((sentinel)), HParser*, h_sequence, HParser* p);
|
||||
|
||||
/**
|
||||
* Given an array of parsers, p_array, apply each parser in order. The
|
||||
|
|
@ -379,7 +379,7 @@ HAMMER_FN_DECL_VARARGS_ATTR(__attribute__((sentinel)), HParser*, h_sequence, con
|
|||
*
|
||||
* Result token type: The type of the first successful parser's result.
|
||||
*/
|
||||
HAMMER_FN_DECL_VARARGS_ATTR(__attribute__((sentinel)), HParser*, h_choice, const HParser* p);
|
||||
HAMMER_FN_DECL_VARARGS_ATTR(__attribute__((sentinel)), HParser*, h_choice, HParser* p);
|
||||
|
||||
/**
|
||||
* Given two parsers, p1 and p2, this parser succeeds in the following
|
||||
|
|
@ -605,11 +605,11 @@ void h_bit_writer_free(HBitWriter* w);
|
|||
|
||||
// General-purpose actions for use with h_action
|
||||
// XXX to be consolidated with glue.h when merged upstream
|
||||
const HParsedToken *h_act_first(const HParseResult *p);
|
||||
const HParsedToken *h_act_second(const HParseResult *p);
|
||||
const HParsedToken *h_act_last(const HParseResult *p);
|
||||
const HParsedToken *h_act_flatten(const HParseResult *p);
|
||||
const HParsedToken *h_act_ignore(const HParseResult *p);
|
||||
HParsedToken *h_act_first(const HParseResult *p);
|
||||
HParsedToken *h_act_second(const HParseResult *p);
|
||||
HParsedToken *h_act_last(const HParseResult *p);
|
||||
HParsedToken *h_act_flatten(const HParseResult *p);
|
||||
HParsedToken *h_act_ignore(const HParseResult *p);
|
||||
|
||||
// {{{ Benchmark functions
|
||||
HAMMER_FN_DECL(HBenchmarkResults *, h_benchmark, HParser* parser, HParserTestcase* testcases);
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
#include <assert.h>
|
||||
#include "parser_internal.h"
|
||||
|
||||
typedef struct {
|
||||
|
|
@ -45,9 +46,35 @@ static bool action_isValidCF(void *env) {
|
|||
return a->p->vtable->isValidCF(a->p->env);
|
||||
}
|
||||
|
||||
static bool h_svm_action_action(HArena *arena, HSVMContext *ctx, void* arg) {
|
||||
HParseResult res;
|
||||
HAction action = arg;
|
||||
assert(ctx->stack_count >= 1);
|
||||
if (ctx->stack[ctx->stack_count-1]->token_type != TT_MARK) {
|
||||
assert(ctx->stack_count >= 2 && ctx->stack[ctx->stack_count-2]->token_type == TT_MARK);
|
||||
res.ast = ctx->stack[ctx->stack_count-2] = ctx->stack[ctx->stack_count-1];
|
||||
ctx->stack_count--;
|
||||
// mark replaced.
|
||||
} else {
|
||||
res.ast = NULL;
|
||||
}
|
||||
res.arena = arena;
|
||||
|
||||
HParsedToken *tok = action(&res);
|
||||
if (tok != NULL)
|
||||
ctx->stack[ctx->stack_count-1] = tok;
|
||||
else
|
||||
ctx->stack_count--;
|
||||
return true; // action can't fail
|
||||
}
|
||||
|
||||
static bool action_ctrvm(HRVMProg *prog, void* env) {
|
||||
HParseAction *a = (HParseAction*)env;
|
||||
return a->p->vtable->compile_to_rvm(prog, a->p->env);
|
||||
h_rvm_insert_insn(prog, RVM_PUSH, 0);
|
||||
if (!h_compile_regex(prog, a->p))
|
||||
return false;
|
||||
h_rvm_insert_insn(prog, RVM_ACTION, h_rvm_create_action(prog, h_svm_action_action, a->action));
|
||||
return true;
|
||||
}
|
||||
|
||||
static const HParserVtable action_vt = {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
#include <assert.h>
|
||||
#include "parser_internal.h"
|
||||
|
||||
typedef struct {
|
||||
|
|
@ -48,9 +49,30 @@ static HCFChoice* desugar_ab(HAllocator *mm__, void *env) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
static bool h_svm_action_attr_bool(HArena *arena, HSVMContext *ctx, void* arg) {
|
||||
HParseResult res;
|
||||
HPredicate pred = arg;
|
||||
assert(ctx->stack_count >= 1);
|
||||
if (ctx->stack[ctx->stack_count-1]->token_type != TT_MARK) {
|
||||
assert(ctx->stack_count >= 2 && ctx->stack[ctx->stack_count-2]->token_type == TT_MARK);
|
||||
ctx->stack_count--;
|
||||
res.ast = ctx->stack[ctx->stack_count-1] = ctx->stack[ctx->stack_count];
|
||||
// mark replaced.
|
||||
} else {
|
||||
ctx->stack_count--;
|
||||
res.ast = NULL;
|
||||
}
|
||||
res.arena = arena;
|
||||
return pred(&res);
|
||||
}
|
||||
|
||||
static bool ab_ctrvm(HRVMProg *prog, void *env) {
|
||||
HAttrBool *ab = (HAttrBool*)env;
|
||||
return h_compile_regex(prog, ab->p);
|
||||
h_rvm_insert_insn(prog, RVM_PUSH, 0);
|
||||
if (!h_compile_regex(prog, ab->p))
|
||||
return false;
|
||||
h_rvm_insert_insn(prog, RVM_ACTION, h_rvm_create_action(prog, h_svm_action_attr_bool, ab->pred));
|
||||
return true;
|
||||
}
|
||||
|
||||
static const HParserVtable attr_bool_vt = {
|
||||
|
|
|
|||
|
|
@ -45,10 +45,10 @@ static HParsedToken *reshape_bits(const HParseResult *p, bool signedp) {
|
|||
|
||||
return ret;
|
||||
}
|
||||
static const HParsedToken *reshape_bits_unsigned(const HParseResult *p) {
|
||||
static HParsedToken *reshape_bits_unsigned(const HParseResult *p) {
|
||||
return reshape_bits(p, false);
|
||||
}
|
||||
static const HParsedToken *reshape_bits_signed(const HParseResult *p) {
|
||||
static HParsedToken *reshape_bits_signed(const HParseResult *p) {
|
||||
return reshape_bits(p, true);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ static bool cs_ctrvm(HRVMProg *prog, void *env) {
|
|||
if (collecting) {
|
||||
collecting = false;
|
||||
uint16_t insn = h_rvm_insert_insn(prog, RVM_FORK, 0);
|
||||
h_rvm_insert_insn(prog, RVM_MATCH, range_start | i << 8);
|
||||
h_rvm_insert_insn(prog, RVM_MATCH, range_start | (i-1) << 8);
|
||||
h_rvm_insert_insn(prog, RVM_GOTO, 0);
|
||||
h_rvm_patch_arg(prog, insn, h_rvm_get_ip(prog));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
typedef struct {
|
||||
size_t len;
|
||||
const HParser **p_array;
|
||||
HParser **p_array;
|
||||
} HSequence;
|
||||
|
||||
|
||||
|
|
@ -58,16 +58,16 @@ static HCFChoice* desugar_choice(HAllocator *mm__, void *env) {
|
|||
static bool choice_ctrvm(HRVMProg *prog, void* env) {
|
||||
HSequence *s = (HSequence*)env;
|
||||
uint16_t gotos[s->len];
|
||||
uint16_t start = h_rvm_get_ip(prog);
|
||||
for (size_t i=0; i<s->len; ++i) {
|
||||
uint16_t insn = h_rvm_insert_insn(prog, RVM_FORK, 0);
|
||||
if (!h_compile_regex(prog, s->p_array[i]->env))
|
||||
if (!h_compile_regex(prog, s->p_array[i]))
|
||||
return false;
|
||||
gotos[i] = h_rvm_insert_insn(prog, RVM_GOTO, 0);
|
||||
gotos[i] = h_rvm_insert_insn(prog, RVM_GOTO, 65535);
|
||||
h_rvm_patch_arg(prog, insn, h_rvm_get_ip(prog));
|
||||
}
|
||||
uint16_t jump = h_rvm_insert_insn(prog, RVM_STEP, 0);
|
||||
for (size_t i=start; i<s->len; ++i) {
|
||||
h_rvm_insert_insn(prog, RVM_MATCH, 0x00FF); // fail.
|
||||
uint16_t jump = h_rvm_get_ip(prog);
|
||||
for (size_t i=0; i<s->len; ++i) {
|
||||
h_rvm_patch_arg(prog, gotos[i], jump);
|
||||
}
|
||||
return true;
|
||||
|
|
@ -81,7 +81,7 @@ static const HParserVtable choice_vt = {
|
|||
.compile_to_rvm = choice_ctrvm,
|
||||
};
|
||||
|
||||
HParser* h_choice(const HParser* p, ...) {
|
||||
HParser* h_choice(HParser* p, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, p);
|
||||
HParser* ret = h_choice__mv(&system_allocator, p, ap);
|
||||
|
|
@ -89,7 +89,7 @@ HParser* h_choice(const HParser* p, ...) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
HParser* h_choice__m(HAllocator* mm__, const HParser* p, ...) {
|
||||
HParser* h_choice__m(HAllocator* mm__, HParser* p, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, p);
|
||||
HParser* ret = h_choice__mv(mm__, p, ap);
|
||||
|
|
@ -97,28 +97,28 @@ HParser* h_choice__m(HAllocator* mm__, const HParser* p, ...) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
HParser* h_choice__v(const HParser* p, va_list ap) {
|
||||
HParser* h_choice__v(HParser* p, va_list ap) {
|
||||
return h_choice__mv(&system_allocator, p, ap);
|
||||
}
|
||||
|
||||
HParser* h_choice__mv(HAllocator* mm__, const HParser* p, va_list ap_) {
|
||||
HParser* h_choice__mv(HAllocator* mm__, HParser* p, va_list ap_) {
|
||||
va_list ap;
|
||||
size_t len = 0;
|
||||
HSequence *s = h_new(HSequence, 1);
|
||||
|
||||
const HParser *arg;
|
||||
HParser *arg;
|
||||
va_copy(ap, ap_);
|
||||
do {
|
||||
len++;
|
||||
arg = va_arg(ap, const HParser *);
|
||||
arg = va_arg(ap, HParser *);
|
||||
} while (arg);
|
||||
va_end(ap);
|
||||
s->p_array = h_new(const HParser *, len);
|
||||
s->p_array = h_new(HParser *, len);
|
||||
|
||||
va_copy(ap, ap_);
|
||||
s->p_array[0] = p;
|
||||
for (size_t i = 1; i < len; i++) {
|
||||
s->p_array[i] = va_arg(ap, const HParser *);
|
||||
s->p_array[i] = va_arg(ap, HParser *);
|
||||
} while (arg);
|
||||
va_end(ap);
|
||||
|
||||
|
|
@ -139,7 +139,7 @@ HParser* h_choice__ma(HAllocator* mm__, void *args[]) {
|
|||
} while(arg);
|
||||
|
||||
HSequence *s = h_new(HSequence, 1);
|
||||
s->p_array = h_new(const HParser *, len);
|
||||
s->p_array = h_new(HParser *, len);
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
s->p_array[i] = ((HParser **)args)[i];
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ static bool h_svm_action_pop(HArena *arena, HSVMContext *ctx, void* arg) {
|
|||
|
||||
static bool ignore_ctrvm(HRVMProg *prog, void *env) {
|
||||
HParser *p = (HParser*)env;
|
||||
h_compile_regex(prog, p->env);
|
||||
h_compile_regex(prog, p);
|
||||
h_rvm_insert_insn(prog, RVM_ACTION, h_rvm_create_action(prog, h_svm_action_pop, NULL));
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ static bool h_svm_action_ignoreseq(HArena *arena, HSVMContext *ctx, void* env) {
|
|||
// stack.
|
||||
assert(seq->len >= 1);
|
||||
for (int i = seq->len - 1; i>=0; i--) {
|
||||
if (i == (int)seq->which && ctx->stack[ctx->stack_count]->token_type != TT_MARK)
|
||||
if (i == (int)seq->which && ctx->stack[ctx->stack_count-1]->token_type != TT_MARK)
|
||||
save = ctx->stack[ctx->stack_count-1];
|
||||
// skip over everything up to and including the mark.
|
||||
while (ctx->stack[--ctx->stack_count]->token_type != TT_MARK)
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
#include <assert.h>
|
||||
#include "parser_internal.h"
|
||||
|
||||
// TODO: split this up.
|
||||
|
|
@ -14,7 +15,7 @@ static HParseResult *parse_many(void* env, HParseState *state) {
|
|||
HInputStream bak;
|
||||
while (env_->min_p || env_->count > count) {
|
||||
bak = state->input_stream;
|
||||
if (count > 0) {
|
||||
if (count > 0 && env_->sep != NULL) {
|
||||
HParseResult *sep = h_do_parse(env_->sep, state);
|
||||
if (!sep)
|
||||
goto err0;
|
||||
|
|
@ -47,13 +48,15 @@ static HParseResult *parse_many(void* env, HParseState *state) {
|
|||
static bool many_isValidRegular(void *env) {
|
||||
HRepeat *repeat = (HRepeat*)env;
|
||||
return (repeat->p->vtable->isValidRegular(repeat->p->env) &&
|
||||
repeat->sep->vtable->isValidRegular(repeat->sep->env));
|
||||
(repeat->sep == NULL ||
|
||||
repeat->sep->vtable->isValidRegular(repeat->sep->env)));
|
||||
}
|
||||
|
||||
static bool many_isValidCF(void *env) {
|
||||
HRepeat *repeat = (HRepeat*)env;
|
||||
return (repeat->p->vtable->isValidCF(repeat->p->env) &&
|
||||
repeat->sep->vtable->isValidCF(repeat->sep->env));
|
||||
(repeat->sep == NULL ||
|
||||
repeat->sep->vtable->isValidCF(repeat->sep->env)));
|
||||
}
|
||||
|
||||
static HCFChoice* desugar_many(HAllocator *mm__, void *env) {
|
||||
|
|
@ -70,7 +73,9 @@ static HCFChoice* desugar_many(HAllocator *mm__, void *env) {
|
|||
-> \epsilon
|
||||
*/
|
||||
|
||||
HCFChoice *sep = h_desugar(mm__, repeat->sep);
|
||||
HParser *epsilon = h_epsilon_p__m(mm__);
|
||||
|
||||
HCFChoice *sep = h_desugar(mm__, (repeat->sep != NULL) ? repeat->sep : epsilon);
|
||||
HCFChoice *a = h_desugar(mm__, repeat->p);
|
||||
HCFChoice *ma = h_new(HCFChoice, 1);
|
||||
HCFChoice *mar = h_new(HCFChoice, 1);
|
||||
|
|
@ -119,24 +124,56 @@ static HCFChoice* desugar_many(HAllocator *mm__, void *env) {
|
|||
|
||||
static bool many_ctrvm(HRVMProg *prog, void *env) {
|
||||
HRepeat *repeat = (HRepeat*)env;
|
||||
// FIXME: Implement clear_to_mark
|
||||
uint16_t clear_to_mark = h_rvm_create_action(prog, h_svm_action_clear_to_mark, NULL);
|
||||
h_rvm_insert_insn(prog, RVM_PUSH, 0);
|
||||
// TODO: implement min and max properly. Right now, it's always min==0, max==inf
|
||||
uint16_t insn = h_rvm_insert_insn(prog, RVM_FORK, 0);
|
||||
if (!h_compile_regex(prog, repeat->p))
|
||||
return false;
|
||||
if (repeat->sep != NULL) {
|
||||
h_rvm_insert_insn(prog, RVM_PUSH, 0);
|
||||
if (!h_compile_regex(prog, repeat->sep))
|
||||
return false;
|
||||
h_rvm_insert_insn(prog, RVM_ACTION, clear_to_mark);
|
||||
}
|
||||
h_rvm_insert_insn(prog, RVM_GOTO, insn);
|
||||
h_rvm_patch_arg(prog, insn, h_rvm_get_ip(prog));
|
||||
// TODO: implement min & max properly. Right now, it's always
|
||||
// max==inf, min={0,1}
|
||||
|
||||
h_rvm_insert_insn(prog, RVM_ACTION, h_rvm_create_action(prog, h_svm_action_make_sequence, NULL));
|
||||
return true;
|
||||
// Structure:
|
||||
// Min == 0:
|
||||
// FORK end // if Min == 0
|
||||
// GOTO mid
|
||||
// nxt: <SEP>
|
||||
// mid: <ELEM>
|
||||
// FORK nxt
|
||||
// end:
|
||||
|
||||
if (repeat->min_p) {
|
||||
h_rvm_insert_insn(prog, RVM_PUSH, 0);
|
||||
assert(repeat->count < 2); // TODO: The other cases should be supported later.
|
||||
uint16_t end_fork;
|
||||
if (repeat->count == 0)
|
||||
end_fork = h_rvm_insert_insn(prog, RVM_FORK, 0xFFFF);
|
||||
uint16_t goto_mid = h_rvm_insert_insn(prog, RVM_GOTO, 0xFFFF);
|
||||
uint16_t nxt = h_rvm_get_ip(prog);
|
||||
if (repeat->sep != NULL) {
|
||||
h_rvm_insert_insn(prog, RVM_PUSH, 0);
|
||||
if (!h_compile_regex(prog, repeat->sep))
|
||||
return false;
|
||||
h_rvm_insert_insn(prog, RVM_ACTION, clear_to_mark);
|
||||
}
|
||||
h_rvm_patch_arg(prog, goto_mid, h_rvm_get_ip(prog));
|
||||
if (!h_compile_regex(prog, repeat->p))
|
||||
return false;
|
||||
h_rvm_insert_insn(prog, RVM_FORK, nxt);
|
||||
h_rvm_patch_arg(prog, end_fork, h_rvm_get_ip(prog));
|
||||
|
||||
h_rvm_insert_insn(prog, RVM_ACTION, h_rvm_create_action(prog, h_svm_action_make_sequence, NULL));
|
||||
return true;
|
||||
} else {
|
||||
h_rvm_insert_insn(prog, RVM_PUSH, 0);
|
||||
for (size_t i = 0; i < repeat->count; i++) {
|
||||
if (repeat->sep != NULL && i != 0) {
|
||||
h_rvm_insert_insn(prog, RVM_PUSH, 0);
|
||||
if (!h_compile_regex(prog, repeat->sep))
|
||||
return false;
|
||||
h_rvm_insert_insn(prog, RVM_ACTION, clear_to_mark);
|
||||
}
|
||||
if (!h_compile_regex(prog, repeat->p))
|
||||
return false;
|
||||
}
|
||||
h_rvm_insert_insn(prog, RVM_ACTION, h_rvm_create_action(prog, h_svm_action_make_sequence, NULL));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static const HParserVtable many_vt = {
|
||||
|
|
@ -153,7 +190,7 @@ HParser* h_many(const HParser* p) {
|
|||
HParser* h_many__m(HAllocator* mm__, const HParser* p) {
|
||||
HRepeat *env = h_new(HRepeat, 1);
|
||||
env->p = p;
|
||||
env->sep = h_epsilon_p__m(mm__);
|
||||
env->sep = NULL;
|
||||
env->count = 0;
|
||||
env->min_p = true;
|
||||
return h_new_parser(mm__, &many_vt, env);
|
||||
|
|
@ -165,7 +202,7 @@ HParser* h_many1(const HParser* p) {
|
|||
HParser* h_many1__m(HAllocator* mm__, const HParser* p) {
|
||||
HRepeat *env = h_new(HRepeat, 1);
|
||||
env->p = p;
|
||||
env->sep = h_epsilon_p__m(mm__);
|
||||
env->sep = NULL;
|
||||
env->count = 1;
|
||||
env->min_p = true;
|
||||
return h_new_parser(mm__, &many_vt, env);
|
||||
|
|
@ -177,7 +214,7 @@ HParser* h_repeat_n(const HParser* p, const size_t n) {
|
|||
HParser* h_repeat_n__m(HAllocator* mm__, const HParser* p, const size_t n) {
|
||||
HRepeat *env = h_new(HRepeat, 1);
|
||||
env->p = p;
|
||||
env->sep = h_epsilon_p__m(mm__);
|
||||
env->sep = NULL;
|
||||
env->count = n;
|
||||
env->min_p = false;
|
||||
return h_new_parser(mm__, &many_vt, env);
|
||||
|
|
@ -222,7 +259,7 @@ static HParseResult* parse_length_value(void *env, HParseState *state) {
|
|||
// TODO: allocate this using public functions
|
||||
HRepeat repeat = {
|
||||
.p = lv->value,
|
||||
.sep = h_epsilon_p(),
|
||||
.sep = NULL,
|
||||
.count = len->ast->uint,
|
||||
.min_p = false
|
||||
};
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ static bool opt_isValidCF(void *env) {
|
|||
return p->vtable->isValidCF(p->env);
|
||||
}
|
||||
|
||||
static const HParsedToken* reshape_optional(const HParseResult *p) {
|
||||
static HParsedToken* reshape_optional(const HParseResult *p) {
|
||||
assert(p->ast);
|
||||
assert(p->ast->token_type == TT_SEQUENCE);
|
||||
assert(p->ast->seq->used > 0);
|
||||
|
|
@ -83,7 +83,7 @@ static bool opt_ctrvm(HRVMProg *prog, void* env) {
|
|||
h_rvm_insert_insn(prog, RVM_PUSH, 0);
|
||||
uint16_t insn = h_rvm_insert_insn(prog, RVM_FORK, 0);
|
||||
HParser *p = (HParser*) env;
|
||||
if (!h_compile_regex(prog, p->env))
|
||||
if (!h_compile_regex(prog, p))
|
||||
return false;
|
||||
h_rvm_patch_arg(prog, insn, h_rvm_get_ip(prog));
|
||||
h_rvm_insert_insn(prog, RVM_ACTION, h_rvm_create_action(prog, h_svm_action_optional, NULL));
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
typedef struct {
|
||||
size_t len;
|
||||
const HParser **p_array;
|
||||
HParser **p_array;
|
||||
} HSequence;
|
||||
|
||||
static HParseResult* parse_sequence(void *env, HParseState *state) {
|
||||
|
|
@ -43,7 +43,7 @@ static bool sequence_isValidCF(void *env) {
|
|||
return true;
|
||||
}
|
||||
|
||||
static const HParsedToken *reshape_sequence(const HParseResult *p) {
|
||||
static HParsedToken *reshape_sequence(const HParseResult *p) {
|
||||
assert(p->ast);
|
||||
assert(p->ast->token_type == TT_SEQUENCE);
|
||||
|
||||
|
|
@ -101,7 +101,7 @@ static const HParserVtable sequence_vt = {
|
|||
.compile_to_rvm = sequence_ctrvm,
|
||||
};
|
||||
|
||||
HParser* h_sequence(const HParser* p, ...) {
|
||||
HParser* h_sequence(HParser* p, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, p);
|
||||
HParser* ret = h_sequence__mv(&system_allocator, p, ap);
|
||||
|
|
@ -109,7 +109,7 @@ HParser* h_sequence(const HParser* p, ...) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
HParser* h_sequence__m(HAllocator* mm__, const HParser* p, ...) {
|
||||
HParser* h_sequence__m(HAllocator* mm__, HParser* p, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, p);
|
||||
HParser* ret = h_sequence__mv(mm__, p, ap);
|
||||
|
|
@ -117,27 +117,27 @@ HParser* h_sequence__m(HAllocator* mm__, const HParser* p, ...) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
HParser* h_sequence__v(const HParser* p, va_list ap) {
|
||||
HParser* h_sequence__v(HParser* p, va_list ap) {
|
||||
return h_sequence__mv(&system_allocator, p, ap);
|
||||
}
|
||||
|
||||
HParser* h_sequence__mv(HAllocator* mm__, const HParser *p, va_list ap_) {
|
||||
HParser* h_sequence__mv(HAllocator* mm__, HParser *p, va_list ap_) {
|
||||
va_list ap;
|
||||
size_t len = 0;
|
||||
const HParser *arg;
|
||||
va_copy(ap, ap_);
|
||||
do {
|
||||
len++;
|
||||
arg = va_arg(ap, const HParser *);
|
||||
arg = va_arg(ap, HParser *);
|
||||
} while (arg);
|
||||
va_end(ap);
|
||||
HSequence *s = h_new(HSequence, 1);
|
||||
s->p_array = h_new(const HParser *, len);
|
||||
s->p_array = h_new(HParser *, len);
|
||||
|
||||
va_copy(ap, ap_);
|
||||
s->p_array[0] = p;
|
||||
for (size_t i = 1; i < len; i++) {
|
||||
s->p_array[i] = va_arg(ap, const HParser *);
|
||||
s->p_array[i] = va_arg(ap, HParser *);
|
||||
} while (arg);
|
||||
va_end(ap);
|
||||
|
||||
|
|
@ -158,7 +158,7 @@ HParser* h_sequence__ma(HAllocator* mm__, void *args[]) {
|
|||
} while(arg);
|
||||
|
||||
HSequence *s = h_new(HSequence, 1);
|
||||
s->p_array = h_new(const HParser *, len);
|
||||
s->p_array = h_new(HParser *, len);
|
||||
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
s->p_array[i] = ((HParser **)args)[i];
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ static HParseResult* parse_token(void *env, HParseState *state) {
|
|||
}
|
||||
|
||||
|
||||
static const HParsedToken *reshape_token(const HParseResult *p) {
|
||||
static HParsedToken *reshape_token(const HParseResult *p) {
|
||||
// fetch sequence of uints from p
|
||||
assert(p->ast);
|
||||
assert(p->ast->token_type == TT_SEQUENCE);
|
||||
|
|
|
|||
|
|
@ -15,9 +15,9 @@ static void test_end(void) {
|
|||
}
|
||||
|
||||
static void test_example_1(void) {
|
||||
const HParser *c = h_many(h_ch('x'));
|
||||
const HParser *q = h_sequence(c, h_ch('y'), NULL);
|
||||
const HParser *p = h_choice(q, h_end_p(), NULL);
|
||||
HParser *c = h_many(h_ch('x'));
|
||||
HParser *q = h_sequence(c, h_ch('y'), NULL);
|
||||
HParser *p = h_choice(q, h_end_p(), NULL);
|
||||
HCFGrammar *g = h_cfgrammar(&system_allocator, p);
|
||||
|
||||
g_check_nonterminal(g, c);
|
||||
|
|
|
|||
|
|
@ -162,7 +162,7 @@ static void test_middle(gconstpointer backend) {
|
|||
|
||||
#include <ctype.h>
|
||||
|
||||
const HParsedToken* upcase(const HParseResult *p) {
|
||||
HParsedToken* upcase(const HParseResult *p) {
|
||||
switch(p->ast->token_type) {
|
||||
case TT_SEQUENCE:
|
||||
{
|
||||
|
|
@ -180,17 +180,17 @@ const HParsedToken* upcase(const HParseResult *p) {
|
|||
}
|
||||
}
|
||||
ret->seq = seq;
|
||||
return (const HParsedToken*)ret;
|
||||
return ret;
|
||||
}
|
||||
case TT_UINT:
|
||||
{
|
||||
HParsedToken *ret = a_new_(p->arena, HParsedToken, 1);
|
||||
ret->token_type = TT_UINT;
|
||||
ret->uint = toupper(p->ast->uint);
|
||||
return (const HParsedToken*)ret;
|
||||
return ret;
|
||||
}
|
||||
default:
|
||||
return p->ast;
|
||||
return (HParsedToken*)p->ast;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -526,6 +526,7 @@ void register_parser_tests(void) {
|
|||
g_test_add_data_func("/core/parser/regex/choice", GINT_TO_POINTER(PB_REGULAR), test_choice);
|
||||
g_test_add_data_func("/core/parser/regex/many", GINT_TO_POINTER(PB_REGULAR), test_many);
|
||||
g_test_add_data_func("/core/parser/regex/many1", GINT_TO_POINTER(PB_REGULAR), test_many1);
|
||||
g_test_add_data_func("/core/parser/regex/repeat_n", GINT_TO_POINTER(PB_REGULAR), test_repeat_n);
|
||||
g_test_add_data_func("/core/parser/regex/optional", GINT_TO_POINTER(PB_REGULAR), test_optional);
|
||||
g_test_add_data_func("/core/parser/regex/sepBy", GINT_TO_POINTER(PB_REGULAR), test_sepBy);
|
||||
g_test_add_data_func("/core/parser/regex/sepBy1", GINT_TO_POINTER(PB_REGULAR), test_sepBy1);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue