Renamed all the public functions
This commit is contained in:
parent
d7ba53b3b1
commit
0284c99f12
9 changed files with 237 additions and 227 deletions
|
|
@ -41,7 +41,7 @@ struct HArena_ {
|
|||
size_t wasted;
|
||||
};
|
||||
|
||||
HArena *new_arena(size_t block_size) {
|
||||
HArena *h_new_arena(size_t block_size) {
|
||||
if (block_size == 0)
|
||||
block_size = 4096;
|
||||
struct HArena_ *ret = g_new(struct HArena_, 1);
|
||||
|
|
@ -56,7 +56,7 @@ HArena *new_arena(size_t block_size) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
void* arena_malloc(HArena *arena, size_t size) {
|
||||
void* h_arena_malloc(HArena *arena, size_t size) {
|
||||
if (size <= arena->head->free) {
|
||||
// fast path..
|
||||
void* ret = arena->head->rest + arena->head->used;
|
||||
|
|
@ -87,7 +87,7 @@ void* arena_malloc(HArena *arena, size_t size) {
|
|||
}
|
||||
}
|
||||
|
||||
void delete_arena(HArena *arena) {
|
||||
void h_delete_arena(HArena *arena) {
|
||||
struct arena_link *link = arena->head;
|
||||
while (link) {
|
||||
struct arena_link *next = link->next;
|
||||
|
|
@ -100,7 +100,7 @@ void delete_arena(HArena *arena) {
|
|||
g_free(arena);
|
||||
}
|
||||
|
||||
void allocator_stats(HArena *arena, HArenaStats *stats) {
|
||||
void h_allocator_stats(HArena *arena, HArenaStats *stats) {
|
||||
stats->used = arena->used;
|
||||
stats->wasted = arena->wasted;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,16 +21,16 @@
|
|||
|
||||
typedef struct HArena_ HArena ; // hidden implementation
|
||||
|
||||
HArena *new_arena(size_t block_size); // pass 0 for default...
|
||||
void* arena_malloc(HArena *arena, size_t count) __attribute__(( malloc, alloc_size(2) ));
|
||||
void delete_arena(HArena *arena);
|
||||
HArena *h_new_arena(size_t block_size); // pass 0 for default...
|
||||
void* h_arena_malloc(HArena *arena, size_t count) __attribute__(( malloc, alloc_size(2) ));
|
||||
void h_delete_arena(HArena *arena);
|
||||
|
||||
typedef struct {
|
||||
size_t used;
|
||||
size_t wasted;
|
||||
} HArenaStats;
|
||||
|
||||
void allocator_stats(HArena *arena, HArenaStats *stats);
|
||||
void h_allocator_stats(HArena *arena, HArenaStats *stats);
|
||||
|
||||
|
||||
#endif // #ifndef LIB_ALLOCATOR__H__
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@
|
|||
#define LDB(range,i) (((i)>>LSB(range))&((1<<(MSB(range)-LSB(range)+1))-1))
|
||||
|
||||
|
||||
long long read_bits(HInputStream* state, int count, char signed_p) {
|
||||
long long h_read_bits(HInputStream* state, int count, char signed_p) {
|
||||
// BUG: Does not
|
||||
long long out = 0;
|
||||
int offset = 0;
|
||||
|
|
@ -123,44 +123,44 @@ long long read_bits(HInputStream* state, int count, char signed_p) {
|
|||
|
||||
static void test_bitreader_ints(void) {
|
||||
HInputStream is = MK_INPUT_STREAM("\xFF\xFF\xFF\xFE\x00\x00\x00\x00", 8, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN);
|
||||
g_check_cmplong(read_bits(&is, 64, true), ==, -0x200000000);
|
||||
g_check_cmplong(h_read_bits(&is, 64, true), ==, -0x200000000);
|
||||
}
|
||||
|
||||
static void test_bitreader_be(void) {
|
||||
HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN);
|
||||
g_check_cmpint(read_bits(&is, 3, false), ==, 0x03);
|
||||
g_check_cmpint(read_bits(&is, 8, false), ==, 0x52);
|
||||
g_check_cmpint(read_bits(&is, 5, false), ==, 0x1A);
|
||||
g_check_cmpint(h_read_bits(&is, 3, false), ==, 0x03);
|
||||
g_check_cmpint(h_read_bits(&is, 8, false), ==, 0x52);
|
||||
g_check_cmpint(h_read_bits(&is, 5, false), ==, 0x1A);
|
||||
}
|
||||
static void test_bitreader_le(void) {
|
||||
HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_LITTLE_ENDIAN | BYTE_LITTLE_ENDIAN);
|
||||
g_check_cmpint(read_bits(&is, 3, false), ==, 0x02);
|
||||
g_check_cmpint(read_bits(&is, 8, false), ==, 0x4D);
|
||||
g_check_cmpint(read_bits(&is, 5, false), ==, 0x0B);
|
||||
g_check_cmpint(h_read_bits(&is, 3, false), ==, 0x02);
|
||||
g_check_cmpint(h_read_bits(&is, 8, false), ==, 0x4D);
|
||||
g_check_cmpint(h_read_bits(&is, 5, false), ==, 0x0B);
|
||||
}
|
||||
|
||||
static void test_largebits_be(void) {
|
||||
HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN);
|
||||
g_check_cmpint(read_bits(&is, 11, false), ==, 0x352);
|
||||
g_check_cmpint(read_bits(&is, 5, false), ==, 0x1A);
|
||||
g_check_cmpint(h_read_bits(&is, 11, false), ==, 0x352);
|
||||
g_check_cmpint(h_read_bits(&is, 5, false), ==, 0x1A);
|
||||
}
|
||||
|
||||
static void test_largebits_le(void) {
|
||||
HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_LITTLE_ENDIAN | BYTE_LITTLE_ENDIAN);
|
||||
g_check_cmpint(read_bits(&is, 11, false), ==, 0x26A);
|
||||
g_check_cmpint(read_bits(&is, 5, false), ==, 0x0B);
|
||||
g_check_cmpint(h_read_bits(&is, 11, false), ==, 0x26A);
|
||||
g_check_cmpint(h_read_bits(&is, 5, false), ==, 0x0B);
|
||||
}
|
||||
|
||||
static void test_offset_largebits_be(void) {
|
||||
HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN);
|
||||
g_check_cmpint(read_bits(&is, 5, false), ==, 0xD);
|
||||
g_check_cmpint(read_bits(&is, 11, false), ==, 0x25A);
|
||||
g_check_cmpint(h_read_bits(&is, 5, false), ==, 0xD);
|
||||
g_check_cmpint(h_read_bits(&is, 11, false), ==, 0x25A);
|
||||
}
|
||||
|
||||
static void test_offset_largebits_le(void) {
|
||||
HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_LITTLE_ENDIAN | BYTE_LITTLE_ENDIAN);
|
||||
g_check_cmpint(read_bits(&is, 5, false), ==, 0xA);
|
||||
g_check_cmpint(read_bits(&is, 11, false), ==, 0x2D3);
|
||||
g_check_cmpint(h_read_bits(&is, 5, false), ==, 0xA);
|
||||
g_check_cmpint(h_read_bits(&is, 11, false), ==, 0x2D3);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -6,22 +6,22 @@
|
|||
// {{{ counted arrays
|
||||
|
||||
|
||||
HCountedArray *carray_new_sized(HArena * arena, size_t size) {
|
||||
HCountedArray *ret = arena_malloc(arena, sizeof(HCountedArray));
|
||||
HCountedArray *h_carray_new_sized(HArena * arena, size_t size) {
|
||||
HCountedArray *ret = h_arena_malloc(arena, sizeof(HCountedArray));
|
||||
assert(size > 0);
|
||||
ret->used = 0;
|
||||
ret->capacity = size;
|
||||
ret->arena = arena;
|
||||
ret->elements = arena_malloc(arena, sizeof(void*) * size);
|
||||
ret->elements = h_arena_malloc(arena, sizeof(void*) * size);
|
||||
return ret;
|
||||
}
|
||||
HCountedArray *carray_new(HArena * arena) {
|
||||
return carray_new_sized(arena, 4);
|
||||
HCountedArray *h_carray_new(HArena * arena) {
|
||||
return h_carray_new_sized(arena, 4);
|
||||
}
|
||||
|
||||
void carray_append(HCountedArray *array, void* item) {
|
||||
void h_carray_append(HCountedArray *array, void* item) {
|
||||
if (array->used >= array->capacity) {
|
||||
HParsedToken **elements = arena_malloc(array->arena, (array->capacity *= 2) * sizeof(HCountedArray*));
|
||||
HParsedToken **elements = h_arena_malloc(array->arena, (array->capacity *= 2) * sizeof(HCountedArray*));
|
||||
for (size_t i = 0; i < array->used; i++)
|
||||
elements[i] = array->elements[i];
|
||||
for (size_t i = array->used; i < array->capacity; i++)
|
||||
|
|
|
|||
256
src/hammer.c
256
src/hammer.c
|
|
@ -25,11 +25,11 @@
|
|||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
#define a_new_(arena, typ, count) ((typ*)arena_malloc((arena), sizeof(typ)*(count)))
|
||||
#define a_new_(arena, typ, count) ((typ*)h_arena_malloc((arena), sizeof(typ)*(count)))
|
||||
#define a_new(typ, count) a_new_(state->arena, typ, count)
|
||||
// we can create a_new0 if necessary. It would allocate some memory and immediately zero it out.
|
||||
|
||||
guint djbhash(const uint8_t *buf, size_t len) {
|
||||
static guint djbhash(const uint8_t *buf, size_t len) {
|
||||
guint hash = 5381;
|
||||
while (len--) {
|
||||
hash = hash * 33 + *buf++;
|
||||
|
|
@ -150,7 +150,7 @@ HParseResult* lr_answer(HParserCacheKey *k, HParseState *state, HLeftRec *growab
|
|||
}
|
||||
|
||||
/* Warth's recursion. Hi Alessandro! */
|
||||
HParseResult* do_parse(const HParser* parser, HParseState *state) {
|
||||
HParseResult* h_do_parse(const HParser* parser, HParseState *state) {
|
||||
HParserCacheKey *key = a_new(HParserCacheKey, 1);
|
||||
key->input_pos = state->input_stream; key->parser = parser;
|
||||
HParserCacheValue *m = recall(key, state);
|
||||
|
|
@ -215,7 +215,7 @@ HParseResult* do_parse(const HParser* parser, HParseState *state) {
|
|||
}
|
||||
|
||||
/* Helper function, since these lines appear in every parser */
|
||||
HParseResult* make_result(HParseState *state, HParsedToken *tok) {
|
||||
static HParseResult* make_result(HParseState *state, HParsedToken *tok) {
|
||||
HParseResult *ret = a_new(HParseResult, 1);
|
||||
ret->ast = tok;
|
||||
ret->arena = state->arena;
|
||||
|
|
@ -254,13 +254,13 @@ static HParseResult* parse_bits(void* env, HParseState *state) {
|
|||
HParsedToken *result = a_new(HParsedToken, 1);
|
||||
result->token_type = (env_->signedp ? TT_SINT : TT_UINT);
|
||||
if (env_->signedp)
|
||||
result->sint = read_bits(&state->input_stream, env_->length, true);
|
||||
result->sint = h_read_bits(&state->input_stream, env_->length, true);
|
||||
else
|
||||
result->uint = read_bits(&state->input_stream, env_->length, false);
|
||||
result->uint = h_read_bits(&state->input_stream, env_->length, false);
|
||||
return make_result(state, result);
|
||||
}
|
||||
|
||||
const HParser* bits(size_t len, bool sign) {
|
||||
const HParser* h_bits(size_t len, bool sign) {
|
||||
struct bits_env *env = g_new(struct bits_env, 1);
|
||||
env->length = len;
|
||||
env->signedp = sign;
|
||||
|
|
@ -271,8 +271,8 @@ const HParser* bits(size_t len, bool sign) {
|
|||
}
|
||||
|
||||
#define SIZED_BITS(name_pre, len, signedp) \
|
||||
const HParser* name_pre##len () { \
|
||||
return bits(len, signedp); \
|
||||
const HParser* h_##name_pre##len () { \
|
||||
return h_bits(len, signedp); \
|
||||
}
|
||||
SIZED_BITS(int, 8, true)
|
||||
SIZED_BITS(int, 16, true)
|
||||
|
|
@ -286,7 +286,7 @@ SIZED_BITS(uint, 64, false)
|
|||
static HParseResult* parse_token(void *env, HParseState *state) {
|
||||
HToken *t = (HToken*)env;
|
||||
for (int i=0; i<t->len; ++i) {
|
||||
uint8_t chr = (uint8_t)read_bits(&state->input_stream, 8, false);
|
||||
uint8_t chr = (uint8_t)h_read_bits(&state->input_stream, 8, false);
|
||||
if (t->str[i] != chr) {
|
||||
return NULL;
|
||||
}
|
||||
|
|
@ -296,7 +296,7 @@ static HParseResult* parse_token(void *env, HParseState *state) {
|
|||
return make_result(state, tok);
|
||||
}
|
||||
|
||||
const HParser* token(const uint8_t *str, const size_t len) {
|
||||
const HParser* h_token(const uint8_t *str, const size_t len) {
|
||||
HToken *t = g_new(HToken, 1);
|
||||
t->str = (uint8_t*)str, t->len = len;
|
||||
HParser *ret = g_new(HParser, 1);
|
||||
|
|
@ -306,7 +306,7 @@ const HParser* token(const uint8_t *str, const size_t len) {
|
|||
|
||||
static HParseResult* parse_ch(void* env, HParseState *state) {
|
||||
uint8_t c = (uint8_t)GPOINTER_TO_UINT(env);
|
||||
uint8_t r = (uint8_t)read_bits(&state->input_stream, 8, false);
|
||||
uint8_t r = (uint8_t)h_read_bits(&state->input_stream, 8, false);
|
||||
if (c == r) {
|
||||
HParsedToken *tok = a_new(HParsedToken, 1);
|
||||
tok->token_type = TT_UINT; tok->uint = r;
|
||||
|
|
@ -316,7 +316,7 @@ static HParseResult* parse_ch(void* env, HParseState *state) {
|
|||
}
|
||||
}
|
||||
|
||||
const HParser* ch(const uint8_t c) {
|
||||
const HParser* h_ch(const uint8_t c) {
|
||||
HParser *ret = g_new(HParser, 1);
|
||||
ret->fn = parse_ch; ret->env = GUINT_TO_POINTER(c);
|
||||
return (const HParser*)ret;
|
||||
|
|
@ -327,15 +327,15 @@ static HParseResult* parse_whitespace(void* env, HParseState *state) {
|
|||
HInputStream bak;
|
||||
do {
|
||||
bak = state->input_stream;
|
||||
c = read_bits(&state->input_stream, 8, false);
|
||||
c = h_read_bits(&state->input_stream, 8, false);
|
||||
if (state->input_stream.overrun)
|
||||
return NULL;
|
||||
} while (isspace(c));
|
||||
state->input_stream = bak;
|
||||
return do_parse((HParser*)env, state);
|
||||
return h_do_parse((HParser*)env, state);
|
||||
}
|
||||
|
||||
const HParser* whitespace(const HParser* p) {
|
||||
const HParser* h_whitespace(const HParser* p) {
|
||||
HParser *ret = g_new(HParser, 1);
|
||||
ret->fn = parse_whitespace;
|
||||
ret->env = (void*)p;
|
||||
|
|
@ -350,15 +350,15 @@ typedef struct {
|
|||
static HParseResult* parse_action(void *env, HParseState *state) {
|
||||
HParseAction *a = (HParseAction*)env;
|
||||
if (a->p && a->action) {
|
||||
HParseResult *tmp = do_parse(a->p, state);
|
||||
//HParsedToken *tok = a->action(do_parse(a->p, state));
|
||||
HParseResult *tmp = h_do_parse(a->p, state);
|
||||
//HParsedToken *tok = a->action(h_do_parse(a->p, state));
|
||||
const HParsedToken *tok = a->action(tmp);
|
||||
return make_result(state, (HParsedToken*)tok);
|
||||
} else // either the parser's missing or the action's missing
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const HParser* action(const HParser* p, const HAction a) {
|
||||
const HParser* h_action(const HParser* p, const HAction a) {
|
||||
HParser *res = g_new(HParser, 1);
|
||||
res->fn = parse_action;
|
||||
HParseAction *env = g_new(HParseAction, 1);
|
||||
|
|
@ -369,7 +369,7 @@ const HParser* action(const HParser* p, const HAction a) {
|
|||
}
|
||||
|
||||
static HParseResult* parse_charset(void *env, HParseState *state) {
|
||||
uint8_t in = read_bits(&state->input_stream, 8, false);
|
||||
uint8_t in = h_read_bits(&state->input_stream, 8, false);
|
||||
HCharset cs = (HCharset)env;
|
||||
|
||||
if (charset_isset(cs, in)) {
|
||||
|
|
@ -380,7 +380,7 @@ static HParseResult* parse_charset(void *env, HParseState *state) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
const HParser* ch_range(const uint8_t lower, const uint8_t upper) {
|
||||
const HParser* h_ch_range(const uint8_t lower, const uint8_t upper) {
|
||||
HParser *ret = g_new(HParser, 1);
|
||||
HCharset cs = new_charset();
|
||||
for (int i = 0; i < 256; i++)
|
||||
|
|
@ -397,7 +397,7 @@ typedef struct {
|
|||
|
||||
static HParseResult* parse_int_range(void *env, HParseState *state) {
|
||||
HRange *r_env = (HRange*)env;
|
||||
HParseResult *ret = do_parse(r_env->p, state);
|
||||
HParseResult *ret = h_do_parse(r_env->p, state);
|
||||
if (!ret || !ret->ast)
|
||||
return NULL;
|
||||
switch(ret->ast->token_type) {
|
||||
|
|
@ -416,7 +416,7 @@ static HParseResult* parse_int_range(void *env, HParseState *state) {
|
|||
}
|
||||
}
|
||||
|
||||
const HParser* int_range(const HParser *p, const int64_t lower, const int64_t upper) {
|
||||
const HParser* h_int_range(const HParser *p, const int64_t lower, const int64_t upper) {
|
||||
struct bits_env *b_env = p->env;
|
||||
// p must be an integer parser, which means it's using parse_bits
|
||||
assert_message(p->fn == parse_bits, "int_range requires an integer parser");
|
||||
|
|
@ -457,7 +457,7 @@ const HParser* int_range(const HParser *p, const int64_t lower, const int64_t up
|
|||
return ret;
|
||||
}
|
||||
|
||||
const HParser* not_in(const uint8_t *options, int count) {
|
||||
const HParser* h_not_in(const uint8_t *options, int count) {
|
||||
HParser *ret = g_new(HParser, 1);
|
||||
HCharset cs = new_charset();
|
||||
for (int i = 0; i < 256; i++)
|
||||
|
|
@ -479,7 +479,7 @@ static HParseResult* parse_end(void *env, HParseState *state) {
|
|||
}
|
||||
}
|
||||
|
||||
const HParser* end_p() {
|
||||
const HParser* h_end_p() {
|
||||
HParser *ret = g_new(HParser, 1);
|
||||
ret->fn = parse_end; ret->env = NULL;
|
||||
return (const HParser*)ret;
|
||||
|
|
@ -490,7 +490,7 @@ static HParseResult* parse_nothing() {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
const HParser* nothing_p() {
|
||||
const HParser* h_nothing_p() {
|
||||
HParser *ret = g_new(HParser, 1);
|
||||
ret->fn = parse_nothing; ret->env = NULL;
|
||||
return (const HParser*)ret;
|
||||
|
|
@ -503,15 +503,15 @@ typedef struct {
|
|||
|
||||
static HParseResult* parse_sequence(void *env, HParseState *state) {
|
||||
HSequence *s = (HSequence*)env;
|
||||
HCountedArray *seq = carray_new_sized(state->arena, (s->len > 0) ? s->len : 4);
|
||||
HCountedArray *seq = h_carray_new_sized(state->arena, (s->len > 0) ? s->len : 4);
|
||||
for (size_t i=0; i<s->len; ++i) {
|
||||
HParseResult *tmp = do_parse(s->p_array[i], state);
|
||||
HParseResult *tmp = h_do_parse(s->p_array[i], state);
|
||||
// if the interim parse fails, the whole thing fails
|
||||
if (NULL == tmp) {
|
||||
return NULL;
|
||||
} else {
|
||||
if (tmp->ast)
|
||||
carray_append(seq, (void*)tmp->ast);
|
||||
h_carray_append(seq, (void*)tmp->ast);
|
||||
}
|
||||
}
|
||||
HParsedToken *tok = a_new(HParsedToken, 1);
|
||||
|
|
@ -519,7 +519,7 @@ static HParseResult* parse_sequence(void *env, HParseState *state) {
|
|||
return make_result(state, tok);
|
||||
}
|
||||
|
||||
const HParser* sequence(const HParser *p, ...) {
|
||||
const HParser* h_sequence(const HParser *p, ...) {
|
||||
va_list ap;
|
||||
size_t len = 0;
|
||||
const HParser *arg;
|
||||
|
|
@ -551,7 +551,7 @@ static HParseResult* parse_choice(void *env, HParseState *state) {
|
|||
for (size_t i=0; i<s->len; ++i) {
|
||||
if (i != 0)
|
||||
state->input_stream = backup;
|
||||
HParseResult *tmp = do_parse(s->p_array[i], state);
|
||||
HParseResult *tmp = h_do_parse(s->p_array[i], state);
|
||||
if (NULL != tmp)
|
||||
return tmp;
|
||||
}
|
||||
|
|
@ -559,7 +559,7 @@ static HParseResult* parse_choice(void *env, HParseState *state) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
const HParser* choice(const HParser* p, ...) {
|
||||
const HParser* h_choice(const HParser* p, ...) {
|
||||
va_list ap;
|
||||
size_t len = 0;
|
||||
HSequence *s = g_new(HSequence, 1);
|
||||
|
|
@ -604,7 +604,7 @@ static HParseResult* parse_butnot(void *env, HParseState *state) {
|
|||
HTwoParsers *parsers = (HTwoParsers*)env;
|
||||
// cache the initial state of the input stream
|
||||
HInputStream start_state = state->input_stream;
|
||||
HParseResult *r1 = do_parse(parsers->p1, state);
|
||||
HParseResult *r1 = h_do_parse(parsers->p1, state);
|
||||
// if p1 failed, bail out early
|
||||
if (NULL == r1) {
|
||||
return NULL;
|
||||
|
|
@ -612,7 +612,7 @@ static HParseResult* parse_butnot(void *env, HParseState *state) {
|
|||
// cache the state after parse #1, since we might have to back up to it
|
||||
HInputStream after_p1_state = state->input_stream;
|
||||
state->input_stream = start_state;
|
||||
HParseResult *r2 = do_parse(parsers->p2, state);
|
||||
HParseResult *r2 = h_do_parse(parsers->p2, state);
|
||||
// TODO(mlp): I'm pretty sure the input stream state should be the post-p1 state in all cases
|
||||
state->input_stream = after_p1_state;
|
||||
// if p2 failed, restore post-p1 state and bail out early
|
||||
|
|
@ -629,7 +629,7 @@ static HParseResult* parse_butnot(void *env, HParseState *state) {
|
|||
}
|
||||
}
|
||||
|
||||
const HParser* butnot(const HParser* p1, const HParser* p2) {
|
||||
const HParser* h_butnot(const HParser* p1, const HParser* p2) {
|
||||
HTwoParsers *env = g_new(HTwoParsers, 1);
|
||||
env->p1 = p1; env->p2 = p2;
|
||||
HParser *ret = g_new(HParser, 1);
|
||||
|
|
@ -641,7 +641,7 @@ static HParseResult* parse_difference(void *env, HParseState *state) {
|
|||
HTwoParsers *parsers = (HTwoParsers*)env;
|
||||
// cache the initial state of the input stream
|
||||
HInputStream start_state = state->input_stream;
|
||||
HParseResult *r1 = do_parse(parsers->p1, state);
|
||||
HParseResult *r1 = h_do_parse(parsers->p1, state);
|
||||
// if p1 failed, bail out early
|
||||
if (NULL == r1) {
|
||||
return NULL;
|
||||
|
|
@ -649,7 +649,7 @@ static HParseResult* parse_difference(void *env, HParseState *state) {
|
|||
// cache the state after parse #1, since we might have to back up to it
|
||||
HInputStream after_p1_state = state->input_stream;
|
||||
state->input_stream = start_state;
|
||||
HParseResult *r2 = do_parse(parsers->p2, state);
|
||||
HParseResult *r2 = h_do_parse(parsers->p2, state);
|
||||
// TODO(mlp): I'm pretty sure the input stream state should be the post-p1 state in all cases
|
||||
state->input_stream = after_p1_state;
|
||||
// if p2 failed, restore post-p1 state and bail out early
|
||||
|
|
@ -666,7 +666,7 @@ static HParseResult* parse_difference(void *env, HParseState *state) {
|
|||
}
|
||||
}
|
||||
|
||||
const HParser* difference(const HParser* p1, const HParser* p2) {
|
||||
const HParser* h_difference(const HParser* p1, const HParser* p2) {
|
||||
HTwoParsers *env = g_new(HTwoParsers, 1);
|
||||
env->p1 = p1; env->p2 = p2;
|
||||
HParser *ret = g_new(HParser, 1);
|
||||
|
|
@ -678,11 +678,11 @@ static HParseResult* parse_xor(void *env, HParseState *state) {
|
|||
HTwoParsers *parsers = (HTwoParsers*)env;
|
||||
// cache the initial state of the input stream
|
||||
HInputStream start_state = state->input_stream;
|
||||
HParseResult *r1 = do_parse(parsers->p1, state);
|
||||
HParseResult *r1 = h_do_parse(parsers->p1, state);
|
||||
HInputStream after_p1_state = state->input_stream;
|
||||
// reset input stream, parse again
|
||||
state->input_stream = start_state;
|
||||
HParseResult *r2 = do_parse(parsers->p2, state);
|
||||
HParseResult *r2 = h_do_parse(parsers->p2, state);
|
||||
if (NULL == r1) {
|
||||
if (NULL != r2) {
|
||||
return r2;
|
||||
|
|
@ -699,7 +699,7 @@ static HParseResult* parse_xor(void *env, HParseState *state) {
|
|||
}
|
||||
}
|
||||
|
||||
const HParser* xor(const HParser* p1, const HParser* p2) {
|
||||
const HParser* h_xor(const HParser* p1, const HParser* p2) {
|
||||
HTwoParsers *env = g_new(HTwoParsers, 1);
|
||||
env->p1 = p1; env->p2 = p2;
|
||||
HParser *ret = g_new(HParser, 1);
|
||||
|
|
@ -715,21 +715,21 @@ typedef struct {
|
|||
|
||||
static HParseResult *parse_many(void* env, HParseState *state) {
|
||||
HRepeat *env_ = (HRepeat*) env;
|
||||
HCountedArray *seq = carray_new_sized(state->arena, (env_->count > 0 ? env_->count : 4));
|
||||
HCountedArray *seq = h_carray_new_sized(state->arena, (env_->count > 0 ? env_->count : 4));
|
||||
size_t count = 0;
|
||||
HInputStream bak;
|
||||
while (env_->min_p || env_->count > count) {
|
||||
bak = state->input_stream;
|
||||
if (count > 0) {
|
||||
HParseResult *sep = do_parse(env_->sep, state);
|
||||
HParseResult *sep = h_do_parse(env_->sep, state);
|
||||
if (!sep)
|
||||
goto err0;
|
||||
}
|
||||
HParseResult *elem = do_parse(env_->p, state);
|
||||
HParseResult *elem = h_do_parse(env_->p, state);
|
||||
if (!elem)
|
||||
goto err0;
|
||||
if (elem->ast)
|
||||
carray_append(seq, (void*)elem->ast);
|
||||
h_carray_append(seq, (void*)elem->ast);
|
||||
count++;
|
||||
}
|
||||
if (count < env_->count)
|
||||
|
|
@ -750,11 +750,11 @@ static HParseResult *parse_many(void* env, HParseState *state) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
const HParser* many(const HParser* p) {
|
||||
const HParser* h_many(const HParser* p) {
|
||||
HParser *res = g_new(HParser, 1);
|
||||
HRepeat *env = g_new(HRepeat, 1);
|
||||
env->p = p;
|
||||
env->sep = epsilon_p();
|
||||
env->sep = h_epsilon_p();
|
||||
env->count = 0;
|
||||
env->min_p = true;
|
||||
res->fn = parse_many;
|
||||
|
|
@ -762,11 +762,11 @@ const HParser* many(const HParser* p) {
|
|||
return res;
|
||||
}
|
||||
|
||||
const HParser* many1(const HParser* p) {
|
||||
const HParser* h_many1(const HParser* p) {
|
||||
HParser *res = g_new(HParser, 1);
|
||||
HRepeat *env = g_new(HRepeat, 1);
|
||||
env->p = p;
|
||||
env->sep = epsilon_p();
|
||||
env->sep = h_epsilon_p();
|
||||
env->count = 1;
|
||||
env->min_p = true;
|
||||
res->fn = parse_many;
|
||||
|
|
@ -774,11 +774,11 @@ const HParser* many1(const HParser* p) {
|
|||
return res;
|
||||
}
|
||||
|
||||
const HParser* repeat_n(const HParser* p, const size_t n) {
|
||||
const HParser* h_repeat_n(const HParser* p, const size_t n) {
|
||||
HParser *res = g_new(HParser, 1);
|
||||
HRepeat *env = g_new(HRepeat, 1);
|
||||
env->p = p;
|
||||
env->sep = epsilon_p();
|
||||
env->sep = h_epsilon_p();
|
||||
env->count = n;
|
||||
env->min_p = false;
|
||||
res->fn = parse_many;
|
||||
|
|
@ -787,7 +787,7 @@ const HParser* repeat_n(const HParser* p, const size_t n) {
|
|||
}
|
||||
|
||||
static HParseResult* parse_ignore(void* env, HParseState* state) {
|
||||
HParseResult *res0 = do_parse((HParser*)env, state);
|
||||
HParseResult *res0 = h_do_parse((HParser*)env, state);
|
||||
if (!res0)
|
||||
return NULL;
|
||||
HParseResult *res = a_new(HParseResult, 1);
|
||||
|
|
@ -795,7 +795,7 @@ static HParseResult* parse_ignore(void* env, HParseState* state) {
|
|||
res->arena = state->arena;
|
||||
return res;
|
||||
}
|
||||
const HParser* ignore(const HParser* p) {
|
||||
const HParser* h_ignore(const HParser* p) {
|
||||
HParser* ret = g_new(HParser, 1);
|
||||
ret->fn = parse_ignore;
|
||||
ret->env = (void*)p;
|
||||
|
|
@ -804,7 +804,7 @@ const HParser* ignore(const HParser* p) {
|
|||
|
||||
static HParseResult* parse_optional(void* env, HParseState* state) {
|
||||
HInputStream bak = state->input_stream;
|
||||
HParseResult *res0 = do_parse((HParser*)env, state);
|
||||
HParseResult *res0 = h_do_parse((HParser*)env, state);
|
||||
if (res0)
|
||||
return res0;
|
||||
state->input_stream = bak;
|
||||
|
|
@ -813,7 +813,7 @@ static HParseResult* parse_optional(void* env, HParseState* state) {
|
|||
return make_result(state, ast);
|
||||
}
|
||||
|
||||
const HParser* optional(const HParser* p) {
|
||||
const HParser* h_optional(const HParser* p) {
|
||||
assert_message(p->fn != parse_ignore, "Thou shalt ignore an option, rather than the other way 'round.");
|
||||
HParser *ret = g_new(HParser, 1);
|
||||
ret->fn = parse_optional;
|
||||
|
|
@ -821,7 +821,7 @@ const HParser* optional(const HParser* p) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
const HParser* sepBy(const HParser* p, const HParser* sep) {
|
||||
const HParser* h_sepBy(const HParser* p, const HParser* sep) {
|
||||
HParser *res = g_new(HParser, 1);
|
||||
HRepeat *env = g_new(HRepeat, 1);
|
||||
env->p = p;
|
||||
|
|
@ -833,7 +833,7 @@ const HParser* sepBy(const HParser* p, const HParser* sep) {
|
|||
return res;
|
||||
}
|
||||
|
||||
const HParser* sepBy1(const HParser* p, const HParser* sep) {
|
||||
const HParser* h_sepBy1(const HParser* p, const HParser* sep) {
|
||||
HParser *res = g_new(HParser, 1);
|
||||
HRepeat *env = g_new(HRepeat, 1);
|
||||
env->p = p;
|
||||
|
|
@ -853,7 +853,7 @@ static HParseResult* parse_epsilon(void* env, HParseState* state) {
|
|||
return res;
|
||||
}
|
||||
|
||||
const HParser* epsilon_p() {
|
||||
const HParser* h_epsilon_p() {
|
||||
HParser *res = g_new(HParser, 1);
|
||||
res->fn = parse_epsilon;
|
||||
res->env = NULL;
|
||||
|
|
@ -861,13 +861,13 @@ const HParser* epsilon_p() {
|
|||
}
|
||||
|
||||
static HParseResult* parse_indirect(void* env, HParseState* state) {
|
||||
return do_parse(env, state);
|
||||
return h_do_parse(env, state);
|
||||
}
|
||||
void bind_indirect(HParser* indirect, HParser* inner) {
|
||||
void h_bind_indirect(HParser* indirect, HParser* inner) {
|
||||
indirect->env = inner;
|
||||
}
|
||||
|
||||
HParser* indirect() {
|
||||
HParser* h_indirect() {
|
||||
HParser *res = g_new(HParser, 1);
|
||||
res->fn = parse_indirect;
|
||||
res->env = NULL;
|
||||
|
|
@ -881,7 +881,7 @@ typedef struct {
|
|||
|
||||
static HParseResult* parse_attr_bool(void *env, HParseState *state) {
|
||||
HAttrBool *a = (HAttrBool*)env;
|
||||
HParseResult *res = do_parse(a->p, state);
|
||||
HParseResult *res = h_do_parse(a->p, state);
|
||||
if (res && res->ast) {
|
||||
if (a->pred(res))
|
||||
return res;
|
||||
|
|
@ -891,7 +891,7 @@ static HParseResult* parse_attr_bool(void *env, HParseState *state) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
const HParser* attr_bool(const HParser* p, HPredicate pred) {
|
||||
const HParser* h_attr_bool(const HParser* p, HPredicate pred) {
|
||||
HParser *res = g_new(HParser, 1);
|
||||
res->fn = parse_attr_bool;
|
||||
HAttrBool *env = g_new(HAttrBool, 1);
|
||||
|
|
@ -908,7 +908,7 @@ typedef struct {
|
|||
|
||||
static HParseResult* parse_length_value(void *env, HParseState *state) {
|
||||
HLenVal *lv = (HLenVal*)env;
|
||||
HParseResult *len = do_parse(lv->length, state);
|
||||
HParseResult *len = h_do_parse(lv->length, state);
|
||||
if (!len)
|
||||
return NULL;
|
||||
if (len->ast->token_type != TT_UINT)
|
||||
|
|
@ -926,7 +926,7 @@ static HParseResult* parse_length_value(void *env, HParseState *state) {
|
|||
return parse_many(&repeat, state);
|
||||
}
|
||||
|
||||
const HParser* length_value(const HParser* length, const HParser* value) {
|
||||
const HParser* h_length_value(const HParser* length, const HParser* value) {
|
||||
HParser *res = g_new(HParser, 1);
|
||||
res->fn = parse_length_value;
|
||||
HLenVal *env = g_new(HLenVal, 1);
|
||||
|
|
@ -938,14 +938,14 @@ const HParser* length_value(const HParser* length, const HParser* value) {
|
|||
|
||||
static HParseResult *parse_and(void* env, HParseState* state) {
|
||||
HInputStream bak = state->input_stream;
|
||||
HParseResult *res = do_parse((HParser*)env, state);
|
||||
HParseResult *res = h_do_parse((HParser*)env, state);
|
||||
state->input_stream = bak;
|
||||
if (res)
|
||||
return make_result(state, NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const HParser* and(const HParser* p) {
|
||||
const HParser* h_and(const HParser* p) {
|
||||
// zero-width postive lookahead
|
||||
HParser *res = g_new(HParser, 1);
|
||||
res->env = (void*)p;
|
||||
|
|
@ -955,7 +955,7 @@ const HParser* and(const HParser* p) {
|
|||
|
||||
static HParseResult* parse_not(void* env, HParseState* state) {
|
||||
HInputStream bak = state->input_stream;
|
||||
if (do_parse((HParser*)env, state))
|
||||
if (h_do_parse((HParser*)env, state))
|
||||
return NULL;
|
||||
else {
|
||||
state->input_stream = bak;
|
||||
|
|
@ -963,7 +963,7 @@ static HParseResult* parse_not(void* env, HParseState* state) {
|
|||
}
|
||||
}
|
||||
|
||||
const HParser* not(const HParser* p) {
|
||||
const HParser* h_not(const HParser* p) {
|
||||
HParser *res = g_new(HParser, 1);
|
||||
res->fn = parse_not;
|
||||
res->env = (void*)p;
|
||||
|
|
@ -978,9 +978,9 @@ static gboolean cache_key_equal(gconstpointer key1, gconstpointer key2) {
|
|||
}
|
||||
|
||||
|
||||
HParseResult* parse(const HParser* parser, const uint8_t* input, size_t length) {
|
||||
HParseResult* h_parse(const HParser* parser, const uint8_t* input, size_t length) {
|
||||
// Set up a parse state...
|
||||
HArena * arena = new_arena(0);
|
||||
HArena * arena = h_new_arena(0);
|
||||
HParseState *parse_state = a_new_(arena, HParseState, 1);
|
||||
parse_state->cache = g_hash_table_new(cache_key_hash, // hash_func
|
||||
cache_key_equal);// key_equal_func
|
||||
|
|
@ -994,13 +994,13 @@ HParseResult* parse(const HParser* parser, const uint8_t* input, size_t length)
|
|||
parse_state->recursion_heads = g_hash_table_new(cache_key_hash,
|
||||
cache_key_equal);
|
||||
parse_state->arena = arena;
|
||||
HParseResult *res = do_parse(parser, parse_state);
|
||||
HParseResult *res = h_do_parse(parser, parse_state);
|
||||
g_queue_free(parse_state->lr_stack);
|
||||
g_hash_table_destroy(parse_state->recursion_heads);
|
||||
// tear down the parse state
|
||||
g_hash_table_destroy(parse_state->cache);
|
||||
if (!res)
|
||||
delete_arena(parse_state->arena);
|
||||
h_delete_arena(parse_state->arena);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
|
@ -1009,21 +1009,21 @@ HParseResult* parse(const HParser* parser, const uint8_t* input, size_t length)
|
|||
|
||||
#include "test_suite.h"
|
||||
static void test_token(void) {
|
||||
const HParser *token_ = token((const uint8_t*)"95\xa2", 3);
|
||||
const HParser *token_ = h_token((const uint8_t*)"95\xa2", 3);
|
||||
|
||||
g_check_parse_ok(token_, "95\xa2", 3, "<39.35.a2>");
|
||||
g_check_parse_failed(token_, "95", 2);
|
||||
}
|
||||
|
||||
static void test_ch(void) {
|
||||
const HParser *ch_ = ch(0xa2);
|
||||
const HParser *ch_ = h_ch(0xa2);
|
||||
|
||||
g_check_parse_ok(ch_, "\xa2", 1, "u0xa2");
|
||||
g_check_parse_failed(ch_, "\xa3", 1);
|
||||
}
|
||||
|
||||
static void test_ch_range(void) {
|
||||
const HParser *range_ = ch_range('a', 'c');
|
||||
const HParser *range_ = h_ch_range('a', 'c');
|
||||
|
||||
g_check_parse_ok(range_, "b", 1, "u0x62");
|
||||
g_check_parse_failed(range_, "d", 1);
|
||||
|
|
@ -1031,56 +1031,56 @@ static void test_ch_range(void) {
|
|||
|
||||
//@MARK_START
|
||||
static void test_int64(void) {
|
||||
const HParser *int64_ = int64();
|
||||
const HParser *int64_ = h_int64();
|
||||
|
||||
g_check_parse_ok(int64_, "\xff\xff\xff\xfe\x00\x00\x00\x00", 8, "s-0x200000000");
|
||||
g_check_parse_failed(int64_, "\xff\xff\xff\xfe\x00\x00\x00", 7);
|
||||
}
|
||||
|
||||
static void test_int32(void) {
|
||||
const HParser *int32_ = int32();
|
||||
const HParser *int32_ = h_int32();
|
||||
|
||||
g_check_parse_ok(int32_, "\xff\xfe\x00\x00", 4, "s-0x20000");
|
||||
g_check_parse_failed(int32_, "\xff\xfe\x00", 3);
|
||||
}
|
||||
|
||||
static void test_int16(void) {
|
||||
const HParser *int16_ = int16();
|
||||
const HParser *int16_ = h_int16();
|
||||
|
||||
g_check_parse_ok(int16_, "\xfe\x00", 2, "s-0x200");
|
||||
g_check_parse_failed(int16_, "\xfe", 1);
|
||||
}
|
||||
|
||||
static void test_int8(void) {
|
||||
const HParser *int8_ = int8();
|
||||
const HParser *int8_ = h_int8();
|
||||
|
||||
g_check_parse_ok(int8_, "\x88", 1, "s-0x78");
|
||||
g_check_parse_failed(int8_, "", 0);
|
||||
}
|
||||
|
||||
static void test_uint64(void) {
|
||||
const HParser *uint64_ = uint64();
|
||||
const HParser *uint64_ = h_uint64();
|
||||
|
||||
g_check_parse_ok(uint64_, "\x00\x00\x00\x02\x00\x00\x00\x00", 8, "u0x200000000");
|
||||
g_check_parse_failed(uint64_, "\x00\x00\x00\x02\x00\x00\x00", 7);
|
||||
}
|
||||
|
||||
static void test_uint32(void) {
|
||||
const HParser *uint32_ = uint32();
|
||||
const HParser *uint32_ = h_uint32();
|
||||
|
||||
g_check_parse_ok(uint32_, "\x00\x02\x00\x00", 4, "u0x20000");
|
||||
g_check_parse_failed(uint32_, "\x00\x02\x00", 3);
|
||||
}
|
||||
|
||||
static void test_uint16(void) {
|
||||
const HParser *uint16_ = uint16();
|
||||
const HParser *uint16_ = h_uint16();
|
||||
|
||||
g_check_parse_ok(uint16_, "\x02\x00", 2, "u0x200");
|
||||
g_check_parse_failed(uint16_, "\x02", 1);
|
||||
}
|
||||
|
||||
static void test_uint8(void) {
|
||||
const HParser *uint8_ = uint8();
|
||||
const HParser *uint8_ = h_uint8();
|
||||
|
||||
g_check_parse_ok(uint8_, "\x78", 1, "u0x78");
|
||||
g_check_parse_failed(uint8_, "", 0);
|
||||
|
|
@ -1088,7 +1088,7 @@ static void test_uint8(void) {
|
|||
//@MARK_END
|
||||
|
||||
static void test_int_range(void) {
|
||||
const HParser *int_range_ = int_range(uint8(), 3, 10);
|
||||
const HParser *int_range_ = h_int_range(h_uint8(), 3, 10);
|
||||
|
||||
g_check_parse_ok(int_range_, "\x05", 1, "u0x5");
|
||||
g_check_parse_failed(int_range_, "\xb", 1);
|
||||
|
|
@ -1096,14 +1096,14 @@ static void test_int_range(void) {
|
|||
|
||||
#if 0
|
||||
static void test_float64(void) {
|
||||
const HParser *float64_ = float64();
|
||||
const HParser *float64_ = h_float64();
|
||||
|
||||
g_check_parse_ok(float64_, "\x3f\xf0\x00\x00\x00\x00\x00\x00", 8, 1.0);
|
||||
g_check_parse_failed(float64_, "\x3f\xf0\x00\x00\x00\x00\x00", 7);
|
||||
}
|
||||
|
||||
static void test_float32(void) {
|
||||
const HParser *float32_ = float32();
|
||||
const HParser *float32_ = h_float32();
|
||||
|
||||
g_check_parse_ok(float32_, "\x3f\x80\x00\x00", 4, 1.0);
|
||||
g_check_parse_failed(float32_, "\x3f\x80\x00");
|
||||
|
|
@ -1112,7 +1112,7 @@ static void test_float32(void) {
|
|||
|
||||
|
||||
static void test_whitespace(void) {
|
||||
const HParser *whitespace_ = whitespace(ch('a'));
|
||||
const HParser *whitespace_ = h_whitespace(h_ch('a'));
|
||||
|
||||
g_check_parse_ok(whitespace_, "a", 1, "u0x61");
|
||||
g_check_parse_ok(whitespace_, " a", 2, "u0x61");
|
||||
|
|
@ -1128,16 +1128,16 @@ const HParsedToken* upcase(const HParseResult *p) {
|
|||
case TT_SEQUENCE:
|
||||
{
|
||||
HParsedToken *ret = a_new_(p->arena, HParsedToken, 1);
|
||||
HCountedArray *seq = carray_new_sized(p->arena, p->ast->seq->used);
|
||||
HCountedArray *seq = h_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 == ((HParsedToken*)p->ast->seq->elements[i])->token_type) {
|
||||
HParsedToken *tmp = a_new_(p->arena, HParsedToken, 1);
|
||||
tmp->token_type = TT_UINT;
|
||||
tmp->uint = toupper(((HParsedToken*)p->ast->seq->elements[i])->uint);
|
||||
carray_append(seq, tmp);
|
||||
h_carray_append(seq, tmp);
|
||||
} else {
|
||||
carray_append(seq, p->ast->seq->elements[i]);
|
||||
h_carray_append(seq, p->ast->seq->elements[i]);
|
||||
}
|
||||
}
|
||||
ret->seq = seq;
|
||||
|
|
@ -1156,14 +1156,14 @@ const HParsedToken* upcase(const HParseResult *p) {
|
|||
}
|
||||
|
||||
static void test_action(void) {
|
||||
const HParser *action_ = action(sequence(choice(ch('a'),
|
||||
ch('A'),
|
||||
NULL),
|
||||
choice(ch('b'),
|
||||
ch('B'),
|
||||
NULL),
|
||||
NULL),
|
||||
upcase);
|
||||
const HParser *action_ = h_action(h_sequence(h_choice(h_ch('a'),
|
||||
h_ch('A'),
|
||||
NULL),
|
||||
h_choice(h_ch('b'),
|
||||
h_ch('B'),
|
||||
NULL),
|
||||
NULL),
|
||||
upcase);
|
||||
|
||||
g_check_parse_ok(action_, "ab", 2, "(u0x41 u0x42)");
|
||||
g_check_parse_ok(action_, "AB", 2, "(u0x41 u0x42)");
|
||||
|
|
@ -1171,28 +1171,26 @@ static void test_action(void) {
|
|||
|
||||
static void test_not_in(void) {
|
||||
uint8_t options[3] = { 'a', 'b', 'c' };
|
||||
const HParser *not_in_ = not_in(options, 3);
|
||||
const HParser *not_in_ = h_not_in(options, 3);
|
||||
g_check_parse_ok(not_in_, "d", 1, "u0x64");
|
||||
g_check_parse_failed(not_in_, "a", 1);
|
||||
|
||||
}
|
||||
|
||||
static void test_end_p(void) {
|
||||
const HParser *end_p_ = sequence(ch('a'), end_p(), NULL);
|
||||
const HParser *end_p_ = h_sequence(h_ch('a'), h_end_p(), NULL);
|
||||
g_check_parse_ok(end_p_, "a", 1, "(u0x61)");
|
||||
g_check_parse_failed(end_p_, "aa", 2);
|
||||
}
|
||||
|
||||
static void test_nothing_p(void) {
|
||||
uint8_t test[1] = { 'a' };
|
||||
const HParser *nothing_p_ = nothing_p();
|
||||
HParseResult *ret = parse(nothing_p_, test, 1);
|
||||
g_check_failed(ret);
|
||||
const HParser *nothing_p_ = h_nothing_p();
|
||||
g_check_parse_failed(nothing_p_, "a", 1);
|
||||
}
|
||||
|
||||
static void test_sequence(void) {
|
||||
const HParser *sequence_1 = sequence(ch('a'), ch('b'), NULL);
|
||||
const HParser *sequence_2 = sequence(ch('a'), whitespace(ch('b')), NULL);
|
||||
const HParser *sequence_1 = h_sequence(h_ch('a'), h_ch('b'), NULL);
|
||||
const HParser *sequence_2 = h_sequence(h_ch('a'), h_whitespace(h_ch('b')), NULL);
|
||||
|
||||
g_check_parse_ok(sequence_1, "ab", 2, "(u0x61 u0x62)");
|
||||
g_check_parse_failed(sequence_1, "a", 1);
|
||||
|
|
@ -1203,7 +1201,7 @@ static void test_sequence(void) {
|
|||
}
|
||||
|
||||
static void test_choice(void) {
|
||||
const HParser *choice_ = choice(ch('a'), ch('b'), NULL);
|
||||
const HParser *choice_ = h_choice(h_ch('a'), h_ch('b'), NULL);
|
||||
|
||||
g_check_parse_ok(choice_, "a", 1, "u0x61");
|
||||
g_check_parse_ok(choice_, "b", 1, "u0x62");
|
||||
|
|
@ -1211,8 +1209,8 @@ static void test_choice(void) {
|
|||
}
|
||||
|
||||
static void test_butnot(void) {
|
||||
const HParser *butnot_1 = butnot(ch('a'), token((const uint8_t*)"ab", 2));
|
||||
const HParser *butnot_2 = butnot(ch_range('0', '9'), ch('6'));
|
||||
const HParser *butnot_1 = h_butnot(h_ch('a'), h_token((const uint8_t*)"ab", 2));
|
||||
const HParser *butnot_2 = h_butnot(h_ch_range('0', '9'), h_ch('6'));
|
||||
|
||||
g_check_parse_ok(butnot_1, "a", 1, "u0x61");
|
||||
g_check_parse_failed(butnot_1, "ab", 2);
|
||||
|
|
@ -1221,14 +1219,14 @@ static void test_butnot(void) {
|
|||
}
|
||||
|
||||
static void test_difference(void) {
|
||||
const HParser *difference_ = difference(token((const uint8_t*)"ab", 2), ch('a'));
|
||||
const HParser *difference_ = h_difference(h_token((const uint8_t*)"ab", 2), h_ch('a'));
|
||||
|
||||
g_check_parse_ok(difference_, "ab", 2, "<61.62>");
|
||||
g_check_parse_failed(difference_, "a", 1);
|
||||
}
|
||||
|
||||
static void test_xor(void) {
|
||||
const HParser *xor_ = xor(ch_range('0', '6'), ch_range('5', '9'));
|
||||
const HParser *xor_ = h_xor(h_ch_range('0', '6'), h_ch_range('5', '9'));
|
||||
|
||||
g_check_parse_ok(xor_, "0", 1, "u0x30");
|
||||
g_check_parse_ok(xor_, "9", 1, "u0x39");
|
||||
|
|
@ -1237,7 +1235,7 @@ static void test_xor(void) {
|
|||
}
|
||||
|
||||
static void test_many(void) {
|
||||
const HParser *many_ = many(choice(ch('a'), ch('b'), NULL));
|
||||
const HParser *many_ = h_many(h_choice(h_ch('a'), h_ch('b'), NULL));
|
||||
g_check_parse_ok(many_, "adef", 4, "(u0x61)");
|
||||
g_check_parse_ok(many_, "bdef", 4, "(u0x62)");
|
||||
g_check_parse_ok(many_, "aabbabadef", 10, "(u0x61 u0x61 u0x62 u0x62 u0x61 u0x62 u0x61)");
|
||||
|
|
@ -1245,7 +1243,7 @@ static void test_many(void) {
|
|||
}
|
||||
|
||||
static void test_many1(void) {
|
||||
const HParser *many1_ = many1(choice(ch('a'), ch('b'), NULL));
|
||||
const HParser *many1_ = h_many1(h_choice(h_ch('a'), h_ch('b'), NULL));
|
||||
|
||||
g_check_parse_ok(many1_, "adef", 4, "(u0x61)");
|
||||
g_check_parse_ok(many1_, "bdef", 4, "(u0x62)");
|
||||
|
|
@ -1254,7 +1252,7 @@ static void test_many1(void) {
|
|||
}
|
||||
|
||||
static void test_repeat_n(void) {
|
||||
const HParser *repeat_n_ = repeat_n(choice(ch('a'), ch('b'), NULL), 2);
|
||||
const HParser *repeat_n_ = h_repeat_n(h_choice(h_ch('a'), h_ch('b'), NULL), 2);
|
||||
|
||||
g_check_parse_failed(repeat_n_, "adef", 4);
|
||||
g_check_parse_ok(repeat_n_, "abdef", 5, "(u0x61 u0x62)");
|
||||
|
|
@ -1262,7 +1260,7 @@ static void test_repeat_n(void) {
|
|||
}
|
||||
|
||||
static void test_optional(void) {
|
||||
const HParser *optional_ = sequence(ch('a'), optional(choice(ch('b'), ch('c'), NULL)), ch('d'), NULL);
|
||||
const HParser *optional_ = h_sequence(h_ch('a'), h_optional(h_choice(h_ch('b'), h_ch('c'), NULL)), h_ch('d'), NULL);
|
||||
|
||||
g_check_parse_ok(optional_, "abd", 3, "(u0x61 u0x62 u0x64)");
|
||||
g_check_parse_ok(optional_, "acd", 3, "(u0x61 u0x63 u0x64)");
|
||||
|
|
@ -1273,14 +1271,14 @@ static void test_optional(void) {
|
|||
}
|
||||
|
||||
static void test_ignore(void) {
|
||||
const HParser *ignore_ = sequence(ch('a'), ignore(ch('b')), ch('c'), NULL);
|
||||
const HParser *ignore_ = h_sequence(h_ch('a'), h_ignore(h_ch('b')), h_ch('c'), NULL);
|
||||
|
||||
g_check_parse_ok(ignore_, "abc", 3, "(u0x61 u0x63)");
|
||||
g_check_parse_failed(ignore_, "ac", 2);
|
||||
}
|
||||
|
||||
static void test_sepBy1(void) {
|
||||
const HParser *sepBy1_ = sepBy1(choice(ch('1'), ch('2'), ch('3'), NULL), ch(','));
|
||||
const HParser *sepBy1_ = h_sepBy1(h_choice(h_ch('1'), h_ch('2'), h_ch('3'), NULL), h_ch(','));
|
||||
|
||||
g_check_parse_ok(sepBy1_, "1,2,3", 5, "(u0x31 u0x32 u0x33)");
|
||||
g_check_parse_ok(sepBy1_, "1,3,2", 5, "(u0x31 u0x33 u0x32)");
|
||||
|
|
@ -1289,9 +1287,9 @@ static void test_sepBy1(void) {
|
|||
}
|
||||
|
||||
static void test_epsilon_p(void) {
|
||||
const HParser *epsilon_p_1 = sequence(ch('a'), epsilon_p(), ch('b'), NULL);
|
||||
const HParser *epsilon_p_2 = sequence(epsilon_p(), ch('a'), NULL);
|
||||
const HParser *epsilon_p_3 = sequence(ch('a'), epsilon_p(), NULL);
|
||||
const HParser *epsilon_p_1 = h_sequence(h_ch('a'), h_epsilon_p(), h_ch('b'), NULL);
|
||||
const HParser *epsilon_p_2 = h_sequence(h_epsilon_p(), h_ch('a'), NULL);
|
||||
const HParser *epsilon_p_3 = h_sequence(h_ch('a'), h_epsilon_p(), NULL);
|
||||
|
||||
g_check_parse_ok(epsilon_p_1, "ab", 2, "(u0x61 u0x62)");
|
||||
g_check_parse_ok(epsilon_p_2, "a", 1, "(u0x61)");
|
||||
|
|
@ -1303,9 +1301,9 @@ static void test_attr_bool(void) {
|
|||
}
|
||||
|
||||
static void test_and(void) {
|
||||
const HParser *and_1 = sequence(and(ch('0')), ch('0'), NULL);
|
||||
const HParser *and_2 = sequence(and(ch('0')), ch('1'), NULL);
|
||||
const HParser *and_3 = sequence(ch('1'), and(ch('2')), NULL);
|
||||
const HParser *and_1 = h_sequence(h_and(h_ch('0')), h_ch('0'), NULL);
|
||||
const HParser *and_2 = h_sequence(h_and(h_ch('0')), h_ch('1'), NULL);
|
||||
const HParser *and_3 = h_sequence(h_ch('1'), h_and(h_ch('2')), NULL);
|
||||
|
||||
g_check_parse_ok(and_1, "0", 1, "(u0x30)");
|
||||
g_check_parse_failed(and_2, "0", 1);
|
||||
|
|
@ -1313,11 +1311,11 @@ static void test_and(void) {
|
|||
}
|
||||
|
||||
static void test_not(void) {
|
||||
const HParser *not_1 = sequence(ch('a'), choice(ch('+'), token((const uint8_t*)"++", 2), NULL), ch('b'), NULL);
|
||||
const HParser *not_2 = sequence(ch('a'),
|
||||
choice(sequence(ch('+'), not(ch('+')), NULL),
|
||||
token((const uint8_t*)"++", 2),
|
||||
NULL), ch('b'), NULL);
|
||||
const HParser *not_1 = h_sequence(h_ch('a'), h_choice(h_ch('+'), h_token((const uint8_t*)"++", 2), NULL), h_ch('b'), NULL);
|
||||
const HParser *not_2 = h_sequence(h_ch('a'),
|
||||
h_choice(h_sequence(h_ch('+'), h_not(h_ch('+')), NULL),
|
||||
h_token((const uint8_t*)"++", 2),
|
||||
NULL), h_ch('b'), NULL);
|
||||
|
||||
g_check_parse_ok(not_1, "a+b", 3, "(u0x61 u0x2b u0x62)");
|
||||
g_check_parse_failed(not_1, "a++b", 4);
|
||||
|
|
|
|||
89
src/hammer.h
89
src/hammer.h
|
|
@ -19,6 +19,7 @@
|
|||
#define HAMMER_HAMMER__H
|
||||
#include <glib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include "allocator.h"
|
||||
|
||||
#define BYTE_BIG_ENDIAN 0x1
|
||||
|
|
@ -106,14 +107,14 @@ typedef struct HParser_ {
|
|||
* Top-level function to call a parser that has been built over some
|
||||
* piece of input (of known size).
|
||||
*/
|
||||
HParseResult* parse(const HParser* parser, const uint8_t* input, size_t length);
|
||||
HParseResult* h_parse(const HParser* parser, const uint8_t* input, size_t length);
|
||||
|
||||
/**
|
||||
* Given a string, returns a parser that parses that string value.
|
||||
*
|
||||
* Result token type: TT_BYTES
|
||||
*/
|
||||
const HParser* token(const uint8_t *str, const size_t len);
|
||||
const HParser* h_token(const uint8_t *str, const size_t len);
|
||||
|
||||
/**
|
||||
* Given a single character, returns a parser that parses that
|
||||
|
|
@ -121,7 +122,7 @@ const HParser* token(const uint8_t *str, const size_t len);
|
|||
*
|
||||
* Result token type: TT_UINT
|
||||
*/
|
||||
const HParser* ch(const uint8_t c);
|
||||
const HParser* h_ch(const uint8_t c);
|
||||
|
||||
/**
|
||||
* Given two single-character bounds, lower and upper, returns a parser
|
||||
|
|
@ -130,14 +131,14 @@ const HParser* ch(const uint8_t c);
|
|||
*
|
||||
* Result token type: TT_UINT
|
||||
*/
|
||||
const HParser* ch_range(const uint8_t lower, const uint8_t upper);
|
||||
const HParser* h_ch_range(const uint8_t lower, const uint8_t upper);
|
||||
|
||||
/**
|
||||
* Given an integer parser, p, and two integer bounds, lower and upper,
|
||||
* returns a parser that parses an integral value within the range
|
||||
* [lower, upper] (inclusive).
|
||||
*/
|
||||
const HParser* int_range(const HParser *p, const int64_t lower, const int64_t upper);
|
||||
const HParser* h_int_range(const HParser *p, const int64_t lower, const int64_t upper);
|
||||
|
||||
/**
|
||||
* Returns a parser that parses the specified number of bits. sign ==
|
||||
|
|
@ -145,63 +146,63 @@ const HParser* int_range(const HParser *p, const int64_t lower, const int64_t up
|
|||
*
|
||||
* Result token type: TT_SINT if sign == true, TT_UINT if sign == false
|
||||
*/
|
||||
const HParser* bits(size_t len, bool sign);
|
||||
const HParser* h_bits(size_t len, bool sign);
|
||||
|
||||
/**
|
||||
* Returns a parser that parses a signed 8-byte integer value.
|
||||
*
|
||||
* Result token type: TT_SINT
|
||||
*/
|
||||
const HParser* int64();
|
||||
const HParser* h_int64();
|
||||
|
||||
/**
|
||||
* Returns a parser that parses a signed 4-byte integer value.
|
||||
*
|
||||
* Result token type: TT_SINT
|
||||
*/
|
||||
const HParser* int32();
|
||||
const HParser* h_int32();
|
||||
|
||||
/**
|
||||
* Returns a parser that parses a signed 2-byte integer value.
|
||||
*
|
||||
* Result token type: TT_SINT
|
||||
*/
|
||||
const HParser* int16();
|
||||
const HParser* h_int16();
|
||||
|
||||
/**
|
||||
* Returns a parser that parses a signed 1-byte integer value.
|
||||
*
|
||||
* Result token type: TT_SINT
|
||||
*/
|
||||
const HParser* int8();
|
||||
const HParser* h_int8();
|
||||
|
||||
/**
|
||||
* Returns a parser that parses an unsigned 8-byte integer value.
|
||||
*
|
||||
* Result token type: TT_UINT
|
||||
*/
|
||||
const HParser* uint64();
|
||||
const HParser* h_uint64();
|
||||
|
||||
/**
|
||||
* Returns a parser that parses an unsigned 4-byte integer value.
|
||||
*
|
||||
* Result token type: TT_UINT
|
||||
*/
|
||||
const HParser* uint32();
|
||||
const HParser* h_uint32();
|
||||
|
||||
/**
|
||||
* Returns a parser that parses an unsigned 2-byte integer value.
|
||||
*
|
||||
* Result token type: TT_UINT
|
||||
*/
|
||||
const HParser* uint16();
|
||||
const HParser* h_uint16();
|
||||
|
||||
/**
|
||||
* Returns a parser that parses an unsigned 1-byte integer value.
|
||||
*
|
||||
* Result token type: TT_UINT
|
||||
*/
|
||||
const HParser* uint8();
|
||||
const HParser* h_uint8();
|
||||
|
||||
/**
|
||||
* Given another parser, p, returns a parser that skips any whitespace
|
||||
|
|
@ -209,7 +210,7 @@ const HParser* uint8();
|
|||
*
|
||||
* Result token type: p's result type
|
||||
*/
|
||||
const HParser* whitespace(const HParser* p);
|
||||
const HParser* h_whitespace(const HParser* p);
|
||||
|
||||
/**
|
||||
* Given another parser, p, and a function f, returns a parser that
|
||||
|
|
@ -217,14 +218,14 @@ const HParser* whitespace(const HParser* p);
|
|||
*
|
||||
* Result token type: any
|
||||
*/
|
||||
const HParser* action(const HParser* p, const HAction a);
|
||||
const HParser* h_action(const HParser* p, const HAction a);
|
||||
|
||||
/**
|
||||
* Parse a single character *NOT* in the given charset.
|
||||
*
|
||||
* Result token type: TT_UINT
|
||||
*/
|
||||
const HParser* not_in(const uint8_t *charset, int length);
|
||||
const HParser* h_not_in(const uint8_t *charset, int length);
|
||||
|
||||
/**
|
||||
* A no-argument parser that succeeds if there is no more input to
|
||||
|
|
@ -232,14 +233,14 @@ const HParser* not_in(const uint8_t *charset, int length);
|
|||
*
|
||||
* Result token type: None. The HParseResult exists but its AST is NULL.
|
||||
*/
|
||||
const HParser* end_p();
|
||||
const HParser* h_end_p();
|
||||
|
||||
/**
|
||||
* This parser always fails.
|
||||
*
|
||||
* Result token type: NULL. Always.
|
||||
*/
|
||||
const HParser* nothing_p();
|
||||
const HParser* h_nothing_p();
|
||||
|
||||
/**
|
||||
* Given a null-terminated list of parsers, apply each parser in order.
|
||||
|
|
@ -247,7 +248,7 @@ const HParser* nothing_p();
|
|||
*
|
||||
* Result token type: TT_SEQUENCE
|
||||
*/
|
||||
const HParser* sequence(const HParser* p, ...) __attribute__((sentinel));
|
||||
const HParser* h_sequence(const HParser* p, ...) __attribute__((sentinel));
|
||||
|
||||
/**
|
||||
* Given an array of parsers, p_array, apply each parser in order. The
|
||||
|
|
@ -256,7 +257,7 @@ const HParser* sequence(const HParser* p, ...) __attribute__((sentinel));
|
|||
*
|
||||
* Result token type: The type of the first successful parser's result.
|
||||
*/
|
||||
const HParser* choice(const HParser* p, ...) __attribute__((sentinel));
|
||||
const HParser* h_choice(const HParser* p, ...) __attribute__((sentinel));
|
||||
|
||||
/**
|
||||
* Given two parsers, p1 and p2, this parser succeeds in the following
|
||||
|
|
@ -266,7 +267,7 @@ const HParser* choice(const HParser* p, ...) __attribute__((sentinel));
|
|||
*
|
||||
* Result token type: p1's result type.
|
||||
*/
|
||||
const HParser* butnot(const HParser* p1, const HParser* p2);
|
||||
const HParser* h_butnot(const HParser* p1, const HParser* p2);
|
||||
|
||||
/**
|
||||
* Given two parsers, p1 and p2, this parser succeeds in the following
|
||||
|
|
@ -276,7 +277,7 @@ const HParser* butnot(const HParser* p1, const HParser* p2);
|
|||
*
|
||||
* Result token type: p1's result type.
|
||||
*/
|
||||
const HParser* difference(const HParser* p1, const HParser* p2);
|
||||
const HParser* h_difference(const HParser* p1, const HParser* p2);
|
||||
|
||||
/**
|
||||
* Given two parsers, p1 and p2, this parser succeeds if *either* p1 or
|
||||
|
|
@ -284,7 +285,7 @@ const HParser* difference(const HParser* p1, const HParser* p2);
|
|||
*
|
||||
* Result token type: The type of the result of whichever parser succeeded.
|
||||
*/
|
||||
const HParser* xor(const HParser* p1, const HParser* p2);
|
||||
const HParser* h_xor(const HParser* p1, const HParser* p2);
|
||||
|
||||
/**
|
||||
* Given a parser, p, this parser succeeds for zero or more repetitions
|
||||
|
|
@ -292,7 +293,7 @@ const HParser* xor(const HParser* p1, const HParser* p2);
|
|||
*
|
||||
* Result token type: TT_SEQUENCE
|
||||
*/
|
||||
const HParser* many(const HParser* p);
|
||||
const HParser* h_many(const HParser* p);
|
||||
|
||||
/**
|
||||
* Given a parser, p, this parser succeeds for one or more repetitions
|
||||
|
|
@ -300,7 +301,7 @@ const HParser* many(const HParser* p);
|
|||
*
|
||||
* Result token type: TT_SEQUENCE
|
||||
*/
|
||||
const HParser* many1(const HParser* p);
|
||||
const HParser* h_many1(const HParser* p);
|
||||
|
||||
/**
|
||||
* Given a parser, p, this parser succeeds for exactly N repetitions
|
||||
|
|
@ -308,7 +309,7 @@ const HParser* many1(const HParser* p);
|
|||
*
|
||||
* Result token type: TT_SEQUENCE
|
||||
*/
|
||||
const HParser* repeat_n(const HParser* p, const size_t n);
|
||||
const HParser* h_repeat_n(const HParser* p, const size_t n);
|
||||
|
||||
/**
|
||||
* Given a parser, p, this parser succeeds with the value p parsed or
|
||||
|
|
@ -316,7 +317,7 @@ const HParser* repeat_n(const HParser* p, const size_t n);
|
|||
*
|
||||
* Result token type: If p succeeded, the type of its result; if not, TT_NONE.
|
||||
*/
|
||||
const HParser* optional(const HParser* p);
|
||||
const HParser* h_optional(const HParser* p);
|
||||
|
||||
/**
|
||||
* Given a parser, p, this parser succeeds if p succeeds, but doesn't
|
||||
|
|
@ -324,7 +325,7 @@ const HParser* optional(const HParser* p);
|
|||
*
|
||||
* Result token type: None. The HParseResult exists but its AST is NULL.
|
||||
*/
|
||||
const HParser* ignore(const HParser* p);
|
||||
const HParser* h_ignore(const HParser* p);
|
||||
|
||||
/**
|
||||
* Given a parser, p, and a parser for a separator, sep, this parser
|
||||
|
|
@ -335,7 +336,7 @@ const HParser* ignore(const HParser* p);
|
|||
*
|
||||
* Result token type: TT_SEQUENCE
|
||||
*/
|
||||
const HParser* sepBy(const HParser* p, const HParser* sep);
|
||||
const HParser* h_sepBy(const HParser* p, const HParser* sep);
|
||||
|
||||
/**
|
||||
* Given a parser, p, and a parser for a separator, sep, this parser matches a list of things that p can parse, separated by sep. Unlike sepBy, this ensures that the result has at least one element.
|
||||
|
|
@ -343,14 +344,14 @@ const HParser* sepBy(const HParser* p, const HParser* sep);
|
|||
*
|
||||
* Result token type: TT_SEQUENCE
|
||||
*/
|
||||
const HParser* sepBy1(const HParser* p, const HParser* sep);
|
||||
const HParser* h_sepBy1(const HParser* p, const HParser* sep);
|
||||
|
||||
/**
|
||||
* This parser always returns a zero length match, i.e., empty string.
|
||||
*
|
||||
* Result token type: None. The HParseResult exists but its AST is NULL.
|
||||
*/
|
||||
const HParser* epsilon_p();
|
||||
const HParser* h_epsilon_p();
|
||||
|
||||
/**
|
||||
* This parser applies its first argument to read an unsigned integer
|
||||
|
|
@ -361,7 +362,7 @@ const HParser* epsilon_p();
|
|||
*
|
||||
* Result token type: TT_SEQUENCE
|
||||
*/
|
||||
const HParser* length_value(const HParser* length, const HParser* value);
|
||||
const HParser* h_length_value(const HParser* length, const HParser* value);
|
||||
|
||||
/**
|
||||
* This parser attaches a predicate function, which returns true or
|
||||
|
|
@ -376,7 +377,7 @@ const HParser* length_value(const HParser* length, const HParser* value);
|
|||
*
|
||||
* Result token type: p's result type if pred succeeded, NULL otherwise.
|
||||
*/
|
||||
const HParser* attr_bool(const HParser* p, HPredicate pred);
|
||||
const HParser* h_attr_bool(const HParser* p, HPredicate pred);
|
||||
|
||||
/**
|
||||
* The 'and' parser asserts that a conditional syntax is satisfied,
|
||||
|
|
@ -393,7 +394,7 @@ const HParser* attr_bool(const HParser* p, HPredicate pred);
|
|||
*
|
||||
* Result token type: None. The HParseResult exists but its AST is NULL.
|
||||
*/
|
||||
const HParser* and(const HParser* p);
|
||||
const HParser* h_and(const HParser* p);
|
||||
|
||||
/**
|
||||
* The 'not' parser asserts that a conditional syntax is *not*
|
||||
|
|
@ -413,7 +414,7 @@ const HParser* and(const HParser* p);
|
|||
*
|
||||
* Result token type: None. The HParseResult exists but its AST is NULL.
|
||||
*/
|
||||
const HParser* not(const HParser* p);
|
||||
const HParser* h_not(const HParser* p);
|
||||
|
||||
/**
|
||||
* Create a parser that just calls out to another, as yet unknown,
|
||||
|
|
@ -424,12 +425,24 @@ const HParser* not(const HParser* p);
|
|||
* Result token type: the type of whatever parser is bound to it with
|
||||
* bind_indirect().
|
||||
*/
|
||||
HParser *indirect();
|
||||
HParser *h_indirect();
|
||||
|
||||
/**
|
||||
* Set the inner parser of an indirect. See comments on indirect for
|
||||
* details.
|
||||
*/
|
||||
void bind_indirect(HParser* indirect, HParser* inner);
|
||||
void h_bind_indirect(HParser* indirect, HParser* inner);
|
||||
|
||||
// Some debugging aids
|
||||
/**
|
||||
* Format token into a compact unambiguous form. Useful for parser test cases.
|
||||
* Caller is responsible for freeing the result.
|
||||
*/
|
||||
char* h_write_result_unamb(const HParsedToken* tok);
|
||||
/**
|
||||
* Format token to the given output stream. Indent starting at
|
||||
* [indent] spaces, with [delta] spaces between levels.
|
||||
*/
|
||||
void h_pprint(FILE* stream, const HParsedToken* tok, int indent, int delta);
|
||||
|
||||
#endif // #ifndef HAMMER_HAMMER__H
|
||||
|
|
|
|||
|
|
@ -139,16 +139,14 @@ static inline void charset_set(HCharset cs, uint8_t pos, int val) {
|
|||
|
||||
// TODO(thequux): Set symbol visibility for these functions so that they aren't exported.
|
||||
|
||||
long long read_bits(HInputStream* state, int count, char signed_p);
|
||||
HParseResult* do_parse(const HParser* parser, HParseState *state);
|
||||
long long h_read_bits(HInputStream* state, int count, char signed_p);
|
||||
// need to decide if we want to make this public.
|
||||
HParseResult* h_do_parse(const HParser* parser, HParseState *state);
|
||||
void put_cached(HParseState *ps, const HParser *p, HParseResult *cached);
|
||||
guint djbhash(const uint8_t *buf, size_t len);
|
||||
char* write_result_unamb(const HParsedToken* tok);
|
||||
void pprint(const HParsedToken* tok, int indent, int delta);
|
||||
|
||||
HCountedArray *carray_new_sized(HArena * arena, size_t size);
|
||||
HCountedArray *carray_new(HArena * arena);
|
||||
void carray_append(HCountedArray *array, void* item);
|
||||
HCountedArray *h_carray_new_sized(HArena * arena, size_t size);
|
||||
HCountedArray *h_carray_new(HArena * arena);
|
||||
void h_carray_append(HCountedArray *array, void* item);
|
||||
|
||||
#if 0
|
||||
#include <malloc.h>
|
||||
|
|
|
|||
31
src/pprint.c
31
src/pprint.c
|
|
@ -28,40 +28,41 @@ typedef struct pp_state {
|
|||
int at_bol;
|
||||
} pp_state_t;
|
||||
|
||||
void pprint(const HParsedToken* tok, int indent, int delta) {
|
||||
void h_pprint(FILE* stream, const HParsedToken* tok, int indent, int delta) {
|
||||
switch (tok->token_type) {
|
||||
case TT_NONE:
|
||||
printf("%*snull\n", indent, "");
|
||||
fprintf(stream, "%*snull\n", indent, "");
|
||||
break;
|
||||
case TT_BYTES:
|
||||
if (tok->bytes.len == 0)
|
||||
printf("%*s<>\n", indent, "");
|
||||
fprintf(stream, "%*s<>\n", indent, "");
|
||||
else {
|
||||
printf("%*s", indent, "");
|
||||
fprintf(stream, "%*s", indent, "");
|
||||
for (size_t i = 0; i < tok->bytes.len; i++) {
|
||||
printf("%c%02hhx",
|
||||
(i == 0) ? '<' : '.',
|
||||
tok->bytes.token[i]);
|
||||
fprintf(stream,
|
||||
"%c%02hhx",
|
||||
(i == 0) ? '<' : '.',
|
||||
tok->bytes.token[i]);
|
||||
}
|
||||
printf(">\n");
|
||||
fprintf(stream, ">\n");
|
||||
}
|
||||
break;
|
||||
case TT_SINT:
|
||||
if (tok->sint < 0)
|
||||
printf("%*ss -%#lx\n", indent, "", -tok->sint);
|
||||
fprintf(stream, "%*ss -%#lx\n", indent, "", -tok->sint);
|
||||
else
|
||||
printf("%*ss %#lx\n", indent, "", tok->sint);
|
||||
fprintf(stream, "%*ss %#lx\n", indent, "", tok->sint);
|
||||
|
||||
break;
|
||||
case TT_UINT:
|
||||
printf("%*su %#lx\n", indent, "", tok->uint);
|
||||
fprintf(stream, "%*su %#lx\n", indent, "", tok->uint);
|
||||
break;
|
||||
case TT_SEQUENCE: {
|
||||
printf("%*s[\n", indent, "");
|
||||
fprintf(stream, "%*s[\n", indent, "");
|
||||
for (size_t i = 0; i < tok->seq->used; i++) {
|
||||
pprint(tok->seq->elements[i], indent + delta, delta);
|
||||
h_pprint(stream, tok->seq->elements[i], indent + delta, delta);
|
||||
}
|
||||
printf("%*s]\n", indent, "");
|
||||
fprintf(stream, "%*s]\n", indent, "");
|
||||
} // TODO: implement this
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
|
|
@ -149,7 +150,7 @@ static void unamb_sub(const HParsedToken* tok, struct result_buf *buf) {
|
|||
}
|
||||
|
||||
|
||||
char* write_result_unamb(const HParsedToken* tok) {
|
||||
char* h_write_result_unamb(const HParsedToken* tok) {
|
||||
struct result_buf buf = {
|
||||
.output = g_malloc0(16),
|
||||
.len = 0,
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@
|
|||
} while(0)
|
||||
|
||||
#define g_check_parse_failed(parser, input, inp_len) do { \
|
||||
const HParseResult *result = parse(parser, (const uint8_t*)input, inp_len); \
|
||||
const HParseResult *result = h_parse(parser, (const uint8_t*)input, inp_len); \
|
||||
if (NULL != result) { \
|
||||
g_test_message("Check failed: shouldn't have succeeded, but did"); \
|
||||
g_test_fail(); \
|
||||
|
|
@ -70,21 +70,21 @@
|
|||
} while(0)
|
||||
|
||||
#define g_check_parse_ok(parser, input, inp_len, result) do { \
|
||||
HParseResult *res = parse(parser, (const uint8_t*)input, inp_len); \
|
||||
HParseResult *res = h_parse(parser, (const uint8_t*)input, inp_len); \
|
||||
if (!res) { \
|
||||
g_test_message("Parse failed on line %d", __LINE__); \
|
||||
g_test_fail(); \
|
||||
} else { \
|
||||
char* cres = write_result_unamb(res->ast); \
|
||||
char* cres = h_write_result_unamb(res->ast); \
|
||||
g_check_string(cres, ==, result); \
|
||||
g_free(cres); \
|
||||
HArenaStats stats; \
|
||||
allocator_stats(res->arena, &stats); \
|
||||
h_allocator_stats(res->arena, &stats); \
|
||||
g_test_message("Parse used %zd bytes, wasted %zd bytes. " \
|
||||
"Inefficiency: %5f%%", \
|
||||
stats.used, stats.wasted, \
|
||||
stats.wasted * 100. / (stats.used+stats.wasted)); \
|
||||
delete_arena(res->arena); \
|
||||
h_delete_arena(res->arena); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue