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

1
.gitignore vendored
View file

@ -3,3 +3,4 @@
*.a *.a
src/test_suite src/test_suite
lib/hush lib/hush
TAGS

View file

@ -23,3 +23,6 @@ $(foreach dir,$(SUBDIRS),$(eval $(call SUBDIR_TEMPLATE,$(dir))))
#.DEFAULT: #.DEFAULT:
# $(if $(findstring ./,$(dir $@)),$(error No rule to make target `$@'),$(MAKE) -C $(dir $@) $(notdir $@)) # $(if $(findstring ./,$(dir $@)),$(error No rule to make target `$@'),$(MAKE) -C $(dir $@) $(notdir $@))
TAGS: $(shell find * -name "*.c")
etags $^

View file

@ -18,3 +18,6 @@ libhammer.a: bitreader.o hammer.o pprint.o
bitreader.o: test_suite.h bitreader.o: test_suite.h
hammer.o: hammer.h hammer.o: hammer.h
test: test_suite
./test_suite -v

View file

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

View file

@ -147,11 +147,11 @@ const parser_t* end_p();
/* This parser always fails. */ /* This parser always fails. */
const parser_t* nothing_p(); const parser_t* nothing_p();
/* Given an array of parsers, p_array, apply each parser in order. The parse succeeds only if all parsers succeed. */ /* Given an null-terminated list of parsers, apply each parser in order. The parse succeeds only if all parsers succeed. */
const parser_t* sequence(const parser_t* p_array[]); const parser_t* sequence(const parser_t* p, ...) __attribute__((sentinel));
/* Given an array of parsers, p_array, apply each parser in order. The first parser to succeed is the result; if no parsers succeed, the parse fails. */ /* Given an array of parsers, p_array, apply each parser in order. The first parser to succeed is the result; if no parsers succeed, the parse fails. */
const parser_t* choice(const parser_t* p_array[]); const parser_t* choice(const parser_t* p, ...) __attribute__((sentinel));
/* Given two parsers, p1 and p2, this parser succeeds in the following cases: /* Given two parsers, p1 and p2, this parser succeeds in the following cases:
* - if p1 succeeds and p2 fails * - if p1 succeeds and p2 fails

View file

@ -36,4 +36,5 @@ parse_result_t* do_parse(const parser_t* parser, parse_state_t *state);
void put_cached(parse_state_t *ps, const parser_t *p, parse_result_t *cached); void put_cached(parse_state_t *ps, const parser_t *p, parse_result_t *cached);
guint djbhash(const uint8_t *buf, size_t len); guint djbhash(const uint8_t *buf, size_t len);
char* write_result_unamb(const parsed_token_t* tok); char* write_result_unamb(const parsed_token_t* tok);
void pprint(const parsed_token_t* tok, int indent, int delta);
#endif // #ifndef HAMMER_INTERNAL__H #endif // #ifndef HAMMER_INTERNAL__H

View file

@ -99,7 +99,7 @@ static void unamb_sub(const parsed_token_t* tok, struct result_buf *buf) {
append_buf(buf, tmpbuf, len); append_buf(buf, tmpbuf, len);
break; break;
case TT_UINT: case TT_UINT:
len = asprintf(&tmpbuf, "s%#lx\n", tok->uint); len = asprintf(&tmpbuf, "s%#lx", tok->uint);
append_buf(buf, tmpbuf, len); append_buf(buf, tmpbuf, len);
break; break;
case TT_SEQUENCE: { case TT_SEQUENCE: {
@ -117,8 +117,10 @@ static void unamb_sub(const parsed_token_t* tok, struct result_buf *buf) {
unamb_sub(subtok, buf); unamb_sub(subtok, buf);
} }
append_buf_c(buf, ')'); append_buf_c(buf, ')');
} // TODO: implement this }
break;
default: default:
fprintf(stderr, "Unexpected token type %d\n", tok->token_type);
g_assert_not_reached(); g_assert_not_reached();
} }
} }

View file

@ -15,7 +15,7 @@
#define g_check_bytes(len, n1, op, n2) { \ #define g_check_bytes(len, n1, op, n2) { \
const uint8_t *_n1 = (n1); \ const uint8_t *_n1 = (n1); \
uint8_t *_n2 = (n2); \ const uint8_t *_n2 = (n2); \
if (!(memcmp(_n1, _n2, len) op 0)) { \ if (!(memcmp(_n1, _n2, len) op 0)) { \
g_test_message("Check failed: (%s)", \ g_test_message("Check failed: (%s)", \
#n1 " " #op " " #n2); \ #n1 " " #op " " #n2); \
@ -23,10 +23,18 @@
} \ } \
} }
#define g_check_cmpseq(n1, op, n2) { \ #define g_check_string(n1, op, n2) { \
GSequence *seq = (n1); \ const char *_n1 = (n1); \
const char *_n2 = (n2); \
if (!(strcmp(_n1, _n2) op 0)) { \
g_test_message("Check failed: (%s) (%s %s %s)", \
#n1 " " #op " " #n2, \
_n1, #op, _n2); \
g_test_fail(); \
} \
}
// TODO: replace uses of this with g_check_parse_failed
#define g_check_failed(res) { \ #define g_check_failed(res) { \
const parse_result_t *result = (res); \ const parse_result_t *result = (res); \
if (NULL != result) { \ if (NULL != result) { \
@ -35,6 +43,26 @@
} \ } \
} }
#define g_check_parse_failed(parser, input, inp_len) { \
const parse_result_t *result = 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(); \
} \
}
#define g_check_parse_ok(parser, input, inp_len, result) { \
parse_result_t *res = 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); \
g_check_string(cres, ==, result); \
} \
}
#define g_check_cmpint(n1, op, n2) g_check_inttype("%d", int, n1, op, n2) #define g_check_cmpint(n1, op, n2) g_check_inttype("%d", int, n1, op, n2)
#define g_check_cmplong(n1, op, n2) g_check_inttype("%ld", long, n1, op, n2) #define g_check_cmplong(n1, op, n2) g_check_inttype("%ld", long, n1, op, n2)
#define g_check_cmplonglong(n1, op, n2) g_check_inttype("%lld", long long, n1, op, n2) #define g_check_cmplonglong(n1, op, n2) g_check_inttype("%lld", long long, n1, op, n2)