fixed merge conflict
This commit is contained in:
commit
0e69b8e2dd
6 changed files with 25 additions and 17 deletions
3
Makefile
3
Makefile
|
|
@ -3,8 +3,7 @@
|
||||||
# and kick off a recursive make
|
# and kick off a recursive make
|
||||||
# Also, "make src/all" turns into "make -C src all"
|
# Also, "make src/all" turns into "make -C src all"
|
||||||
|
|
||||||
SUBDIRS = src \
|
SUBDIRS = src
|
||||||
lib
|
|
||||||
|
|
||||||
.DEFAULT_GOAL := all
|
.DEFAULT_GOAL := all
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ all: libhammer.a test_suite
|
||||||
test_suite: test_suite.o libhammer.a
|
test_suite: test_suite.o libhammer.a
|
||||||
$(call hush, "Linking $@") $(CC) -o $@ $^ $(LDFLAGS)
|
$(call hush, "Linking $@") $(CC) -o $@ $^ $(LDFLAGS)
|
||||||
|
|
||||||
libhammer.a: bitreader.o hammer.o pprint.o
|
libhammer.a: bitreader.o hammer.o pprint.o allocator.o
|
||||||
|
|
||||||
bitreader.o: test_suite.h
|
bitreader.o: test_suite.h
|
||||||
hammer.o: hammer.h
|
hammer.o: hammer.h
|
||||||
|
|
|
||||||
34
src/hammer.c
34
src/hammer.c
|
|
@ -17,11 +17,16 @@
|
||||||
|
|
||||||
#include "hammer.h"
|
#include "hammer.h"
|
||||||
#include "internal.h"
|
#include "internal.h"
|
||||||
|
#include "allocator.h"
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
|
#define a_new_(arena, typ, count) ((typ*)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) {
|
guint djbhash(const uint8_t *buf, size_t len) {
|
||||||
guint hash = 5381;
|
guint hash = 5381;
|
||||||
while (len--) {
|
while (len--) {
|
||||||
|
|
@ -64,8 +69,8 @@ parse_result_t* do_parse(const parser_t* parser, parse_state_t *state) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Helper function, since these lines appear in every parser */
|
/* Helper function, since these lines appear in every parser */
|
||||||
parse_result_t* make_result(parsed_token_t *tok) {
|
parse_result_t* make_result(parse_state_t *state, parsed_token_t *tok) {
|
||||||
parse_result_t *ret = g_new(parse_result_t, 1);
|
parse_result_t *ret = a_new(parse_result_t, 1);
|
||||||
ret->ast = tok;
|
ret->ast = tok;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
@ -83,9 +88,9 @@ static parse_result_t* parse_token(void *env, parse_state_t *state) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
parsed_token_t *tok = g_new(parsed_token_t, 1);
|
parsed_token_t *tok = a_new(parsed_token_t, 1);
|
||||||
tok->token_type = TT_BYTES; tok->bytes.token = t->str; tok->bytes.len = t->len;
|
tok->token_type = TT_BYTES; tok->bytes.token = t->str; tok->bytes.len = t->len;
|
||||||
return make_result(tok);
|
return make_result(state, tok);
|
||||||
}
|
}
|
||||||
|
|
||||||
const parser_t* token(const uint8_t *str, const size_t len) {
|
const parser_t* token(const uint8_t *str, const size_t len) {
|
||||||
|
|
@ -100,9 +105,9 @@ static parse_result_t* parse_ch(void* env, parse_state_t *state) {
|
||||||
uint8_t c = (uint8_t)GPOINTER_TO_UINT(env);
|
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)read_bits(&state->input_stream, 8, false);
|
||||||
if (c == r) {
|
if (c == r) {
|
||||||
parsed_token_t *tok = g_new(parsed_token_t, 1);
|
parsed_token_t *tok = a_new(parsed_token_t, 1);
|
||||||
tok->token_type = TT_UINT; tok->uint = r;
|
tok->token_type = TT_UINT; tok->uint = r;
|
||||||
return make_result(tok);
|
return make_result(state, tok);
|
||||||
} else {
|
} else {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
@ -146,9 +151,9 @@ static parse_result_t* parse_charset(void *env, parse_state_t *state) {
|
||||||
charset cs = (charset)env;
|
charset cs = (charset)env;
|
||||||
|
|
||||||
if (charset_isset(cs, in)) {
|
if (charset_isset(cs, in)) {
|
||||||
parsed_token_t *tok = g_new(parsed_token_t, 1);
|
parsed_token_t *tok = a_new(parsed_token_t, 1);
|
||||||
tok->token_type = TT_UINT; tok->uint = in;
|
tok->token_type = TT_UINT; tok->uint = in;
|
||||||
return make_result(tok);
|
return make_result(state, tok);
|
||||||
} else
|
} else
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
@ -176,7 +181,7 @@ const parser_t* not_in(const uint8_t *options, int count) {
|
||||||
|
|
||||||
static parse_result_t* parse_end(void *env, parse_state_t *state) {
|
static parse_result_t* parse_end(void *env, parse_state_t *state) {
|
||||||
if (state->input_stream.index == state->input_stream.length) {
|
if (state->input_stream.index == state->input_stream.length) {
|
||||||
parse_result_t *ret = g_new(parse_result_t, 1);
|
parse_result_t *ret = a_new(parse_result_t, 1);
|
||||||
ret->ast = NULL;
|
ret->ast = NULL;
|
||||||
return ret;
|
return ret;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -219,9 +224,9 @@ static parse_result_t* parse_sequence(void *env, parse_state_t *state) {
|
||||||
g_sequence_append(seq, (void*)tmp->ast);
|
g_sequence_append(seq, (void*)tmp->ast);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
parsed_token_t *tok = g_new(parsed_token_t, 1);
|
parsed_token_t *tok = a_new(parsed_token_t, 1);
|
||||||
tok->token_type = TT_SEQUENCE; tok->seq = seq;
|
tok->token_type = TT_SEQUENCE; tok->seq = seq;
|
||||||
return make_result(tok);
|
return make_result(state, tok);
|
||||||
}
|
}
|
||||||
|
|
||||||
const parser_t* sequence(const parser_t *p, ...) {
|
const parser_t* sequence(const parser_t *p, ...) {
|
||||||
|
|
@ -456,14 +461,17 @@ static gboolean cache_key_equal(gconstpointer key1, gconstpointer key2) {
|
||||||
|
|
||||||
parse_result_t* parse(const parser_t* parser, const uint8_t* input, size_t length) {
|
parse_result_t* parse(const parser_t* parser, const uint8_t* input, size_t length) {
|
||||||
// Set up a parse state...
|
// Set up a parse state...
|
||||||
parse_state_t *parse_state = g_new0(parse_state_t, 1);
|
arena_t arena = new_arena(0);
|
||||||
|
parse_state_t *parse_state = a_new_(arena, parse_state_t, 1);
|
||||||
parse_state->cache = g_hash_table_new(cache_key_hash, // hash_func
|
parse_state->cache = g_hash_table_new(cache_key_hash, // hash_func
|
||||||
cache_key_equal);// key_equal_func
|
cache_key_equal);// key_equal_func
|
||||||
parse_state->input_stream.input = input;
|
parse_state->input_stream.input = input;
|
||||||
|
parse_state->input_stream.index = 0;
|
||||||
parse_state->input_stream.bit_offset = 8; // bit big endian
|
parse_state->input_stream.bit_offset = 8; // bit big endian
|
||||||
|
parse_state->input_stream.overrun = 0;
|
||||||
parse_state->input_stream.endianness = BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN;
|
parse_state->input_stream.endianness = BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN;
|
||||||
parse_state->input_stream.length = length;
|
parse_state->input_stream.length = length;
|
||||||
|
parse_state->arena = arena;
|
||||||
parse_result_t *res = do_parse(parser, parse_state);
|
parse_result_t *res = do_parse(parser, parse_state);
|
||||||
// tear down the parse state. For now, leak like a sieve.
|
// tear down the parse state. For now, leak like a sieve.
|
||||||
// BUG: Leaks like a sieve.
|
// BUG: Leaks like a sieve.
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@
|
||||||
#define HAMMER_HAMMER__H
|
#define HAMMER_HAMMER__H
|
||||||
#include <glib.h>
|
#include <glib.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include "allocator.h"
|
||||||
/* The state of the parser.
|
/* The state of the parser.
|
||||||
*
|
*
|
||||||
* Members:
|
* Members:
|
||||||
|
|
@ -48,6 +48,7 @@ typedef struct input_stream {
|
||||||
typedef struct parse_state {
|
typedef struct parse_state {
|
||||||
GHashTable *cache;
|
GHashTable *cache;
|
||||||
input_stream_t input_stream;
|
input_stream_t input_stream;
|
||||||
|
arena_t arena;
|
||||||
} parse_state_t;
|
} parse_state_t;
|
||||||
|
|
||||||
typedef enum token_type {
|
typedef enum token_type {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue