This commit is contained in:
Meredith L. Patterson 2012-05-12 10:49:41 +01:00
commit c03de603f6
8 changed files with 143 additions and 65 deletions

View file

@ -19,6 +19,8 @@
#include "internal.h"
#include <assert.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
parse_state_t* from(parse_state_t *ps, const size_t index) {
parse_state_t *ret = g_new(parse_state_t, 1);
@ -71,7 +73,10 @@ parse_result_t* do_parse(const parser_t* parser, parse_state_t *state) {
} else {
// It doesn't exist... run the
parse_result_t *res;
res = parser->fn(parser->env, state);
if (parser)
res = parser->fn(parser->env, state);
else
res = NULL;
if (state->input_stream.overrun)
res = NULL; // overrun is always failure.
// update the cache
@ -142,7 +147,25 @@ typedef struct {
uint8_t upper;
} range_t;
const parser_t* whitespace(const parser_t* p) { return NULL; }
static parse_result_t* parse_whitespace(void* env, parse_state_t *state) {
char c;
input_stream_t bak;
do {
bak = state->input_stream;
c = read_bits(&state->input_stream, 8, false);
if (state->input_stream.overrun)
return NULL;
} while (isspace(c));
state->input_stream = bak;
return do_parse((parser_t*)env, state);
}
const parser_t* whitespace(const parser_t* p) {
parser_t *ret = g_new(parser_t, 1);
ret->fn = parse_whitespace;
ret->env = (void*)p;
return ret;
}
//const parser_t* action(const parser_t* p, /* fptr to action on AST */) { return NULL; }
const parser_t* left_factor_action(const parser_t* p) { return NULL; }
@ -174,7 +197,7 @@ const parser_t* not_in(const uint8_t *options, int count) {
for (int i = 0; i < 256; i++)
charset_set(cs, i, 1);
for (int i = 0; i < count; i++)
charset_set(cs, i, 0);
charset_set(cs, options[i], 0);
ret->fn = parse_charset; ret->env = (void*)cs;
return (const parser_t*)ret;
@ -221,7 +244,8 @@ static parse_result_t* parse_sequence(void *env, parse_state_t *state) {
if (NULL == tmp) {
return NULL;
} else {
g_sequence_append(seq, tmp);
if (tmp->ast)
g_sequence_append(seq, (void*)tmp->ast);
}
}
parsed_token_t *tok = g_new(parsed_token_t, 1);
@ -229,10 +253,27 @@ static parse_result_t* parse_sequence(void *env, parse_state_t *state) {
return make_result(tok);
}
const parser_t* sequence(const parser_t* p_array[]) {
size_t len = sizeof(p_array) / sizeof(parser_t*);
const parser_t* sequence(const parser_t *p, ...) {
va_list ap;
size_t len = 0;
const parser_t *arg;
va_start(ap, p);
do {
len++;
arg = va_arg(ap, const parser_t *);
} while (arg);
va_end(ap);
sequence_t *s = g_new(sequence_t, 1);
s->p_array = (const parser_t**)p_array; s->len = len;
s->p_array = g_new(const parser_t *, len);
va_start(ap, p);
s->p_array[0] = p;
for (size_t i = 1; i < len; i++) {
s->p_array[i] = va_arg(ap, const parser_t *);
} while (arg);
va_end(ap);
s->len = len;
parser_t *ret = g_new(parser_t, 1);
ret->fn = parse_sequence; ret->env = (void*)s;
return ret;
@ -252,10 +293,28 @@ static parse_result_t* parse_choice(void *env, parse_state_t *state) {
return NULL;
}
const parser_t* choice(const parser_t* p_array[]) {
size_t len = sizeof(p_array) / sizeof(parser_t*);
const parser_t* choice(const parser_t* p, ...) {
va_list ap;
size_t len = 0;
sequence_t *s = g_new(sequence_t, 1);
s->p_array = (const parser_t**)p_array; s->len = len;
const parser_t *arg;
va_start(ap, p);
do {
len++;
arg = va_arg(ap, const parser_t *);
} while (arg);
va_end(ap);
s->p_array = g_new(const parser_t *, len);
va_start(ap, p);
s->p_array[0] = p;
for (size_t i = 1; i < len; i++) {
s->p_array[i] = va_arg(ap, const parser_t *);
} while (arg);
va_end(ap);
s->len = len;
parser_t *ret = g_new(parser_t, 1);
ret->fn = parse_choice; ret->env = (void*)s;
return ret;
@ -479,6 +538,7 @@ static void test_range(void) {
g_check_failed(ret2);
}
#if 0
static void test_int64(void) {
uint8_t test1[8] = { 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00 };
uint8_t test2[7] = { 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00 };
@ -578,24 +638,16 @@ static void test_float32(void) {
g_check_cmpfloat(ret1->ast->flt, ==, 1);
g_check_failed(ret2);
}
#endif
static void test_whitespace(void) {
uint8_t test1[1] = { 'a' };
uint8_t test2[2] = { ' ', 'a' };
uint8_t test3[3] = { ' ', ' ', 'a' };
uint8_t test4[2] = { '\t', 'a' };
uint8_t test5[2] = { '_', 'a' };
const parser_t *whitespace_ = whitespace(ch('a'));
parse_result_t *ret1 = parse(whitespace_, test1, 1);
parse_result_t *ret2 = parse(whitespace_, test2, 2);
parse_result_t *ret3 = parse(whitespace_, test3, 3);
parse_result_t *ret4 = parse(whitespace_, test4, 2);
parse_result_t *ret5 = parse(whitespace_, test5, 2);
g_check_cmpint(ret1->ast->uint, ==, 'a');
g_check_cmpint(ret2->ast->uint, ==, 'a');
g_check_cmpint(ret3->ast->uint, ==, 'a');
g_check_cmpint(ret4->ast->uint, ==, 'a');
g_check_failed(ret5);
g_check_parse_ok(whitespace_, "a", 1, "s0x61");
g_check_parse_ok(whitespace_, " a", 2, "s0x61");
g_check_parse_ok(whitespace_, " a", 3, "s0x61");
g_check_parse_ok(whitespace_, "\ta", 2, "s0x61");
g_check_parse_failed(whitespace_, "_a", 2);
}
static void test_action(void) {
@ -608,24 +660,16 @@ static void test_left_factor_action(void) {
static void test_not_in(void) {
uint8_t options[3] = { 'a', 'b', 'c' };
uint8_t test1[1] = { 'd' };
uint8_t test2[1] = { 'a' };
const parser_t *not_in_ = not_in(options, 3);
parse_result_t *ret1 = parse(not_in_, test1, 1);
parse_result_t *ret2 = parse(not_in_, test2, 1);
g_check_cmpint(ret1->ast->uint, ==, 'd');
g_check_failed(ret2);
g_check_parse_ok(not_in_, "d", 1, "s0x64");
g_check_parse_failed(not_in_, "a", 1);
}
static void test_end_p(void) {
uint8_t test1[1] = { 'a' };
uint8_t test2[2] = { 'a', 'a' };
const parser_t *p_array[2] = { ch('a'), end_p() };
const parser_t *end_p_ = sequence(p_array);
parse_result_t *ret1 = parse(end_p_, test1, 1);
parse_result_t *ret2 = parse(end_p_, test2, 2);
g_check_cmpint(ret1->ast->uint, ==, 'a');
g_check_failed(ret2);
const parser_t *end_p_ = sequence(ch('a'), end_p(), NULL);
g_check_parse_ok(end_p_, "a", 1, "(s0x61)");
g_check_parse_failed(end_p_, "aa", 2);
}
static void test_nothing_p(void) {
@ -636,22 +680,16 @@ static void test_nothing_p(void) {
}
static void test_sequence(void) {
uint8_t test1[2] = { 'a', 'b' };
uint8_t test2[1] = { 'a' };
uint8_t test3[1] = { 'b' };
uint8_t test4[3] = { 'a', ' ', 'b' };
uint8_t test5[4] = { 'a', ' ', ' ', 'b' };
uint8_t test6[2] = { 'a', 'b' };
const parser_t *s1[2] = { ch('a'), ch('b') };
const parser_t *s2[2] = { ch('a'), whitespace(ch('b')) };
const parser_t *sequence_1 = sequence(s1);
const parser_t *sequence_2 = sequence(s2);
parse_result_t *ret1 = parse(sequence_1, test1, 2);
parse_result_t *ret2 = parse(sequence_1, test2, 1);
parse_result_t *ret3 = parse(sequence_1, test3, 1);
parse_result_t *ret4 = parse(sequence_2, test4, 3);
parse_result_t *ret5 = parse(sequence_2, test5, 4);
parse_result_t *ret6 = parse(sequence_2, test6, 2);
const parser_t *sequence_1 = sequence(ch('a'), ch('b'), NULL);
const parser_t *sequence_2 = sequence(ch('a'), whitespace(ch('b')), NULL);
g_check_parse_ok(sequence_1, "ab", 2, "(s0x61 s0x62)");
g_check_parse_failed(sequence_1, "a", 1);
g_check_parse_failed(sequence_1, "b", 1);
g_check_parse_ok(sequence_2, "ab", 2, "(s0x61 s0x62)");
g_check_parse_ok(sequence_2, "a b", 3, "(s0x61 s0x62)");
g_check_parse_ok(sequence_2, "a b", 4, "(s0x61 s0x62)");
//g_check_cmpseq(ret1->ast->
}
@ -756,6 +794,7 @@ void register_parser_tests(void) {
g_test_add_func("/core/parser/token", test_token);
g_test_add_func("/core/parser/ch", test_ch);
g_test_add_func("/core/parser/range", test_range);
#if 0
g_test_add_func("/core/parser/int64", test_int64);
g_test_add_func("/core/parser/int32", test_int32);
g_test_add_func("/core/parser/int16", test_int16);
@ -766,6 +805,7 @@ void register_parser_tests(void) {
g_test_add_func("/core/parser/uint8", test_uint8);
g_test_add_func("/core/parser/float64", test_float64);
g_test_add_func("/core/parser/float32", test_float32);
#endif
g_test_add_func("/core/parser/whitespace", test_whitespace);
g_test_add_func("/core/parser/action", test_action);
g_test_add_func("/core/parser/left_factor_action", test_left_factor_action);