Renamed more types

This commit is contained in:
Dan Hirsch 2012-05-26 13:01:23 +02:00
parent b10ef575e2
commit 199cde7058
9 changed files with 377 additions and 379 deletions

View file

@ -34,17 +34,17 @@ struct arena_link {
uint8_t rest[]; uint8_t rest[];
} ; } ;
struct arena { struct HArena_ {
struct arena_link *head; struct arena_link *head;
size_t block_size; size_t block_size;
size_t used; size_t used;
size_t wasted; size_t wasted;
}; };
arena_t new_arena(size_t block_size) { HArena *new_arena(size_t block_size) {
if (block_size == 0) if (block_size == 0)
block_size = 4096; block_size = 4096;
struct arena *ret = g_new(struct arena, 1); struct HArena_ *ret = g_new(struct HArena_, 1);
struct arena_link *link = (struct arena_link*)g_malloc0(sizeof(struct arena_link) + block_size); struct arena_link *link = (struct arena_link*)g_malloc0(sizeof(struct arena_link) + block_size);
link->free = block_size; link->free = block_size;
link->used = 0; link->used = 0;
@ -52,11 +52,11 @@ arena_t new_arena(size_t block_size) {
ret->head = link; ret->head = link;
ret->block_size = block_size; ret->block_size = block_size;
ret->used = 0; ret->used = 0;
ret->wasted = sizeof(struct arena_link) + sizeof(struct arena) + block_size; ret->wasted = sizeof(struct arena_link) + sizeof(struct HArena_) + block_size;
return ret; return ret;
} }
void* arena_malloc(arena_t arena, size_t size) { void* arena_malloc(HArena *arena, size_t size) {
if (size <= arena->head->free) { if (size <= arena->head->free) {
// fast path.. // fast path..
void* ret = arena->head->rest + arena->head->used; void* ret = arena->head->rest + arena->head->used;
@ -87,7 +87,7 @@ void* arena_malloc(arena_t arena, size_t size) {
} }
} }
void delete_arena(arena_t arena) { void delete_arena(HArena *arena) {
struct arena_link *link = arena->head; struct arena_link *link = arena->head;
while (link) { while (link) {
struct arena_link *next = link->next; struct arena_link *next = link->next;
@ -100,7 +100,7 @@ void delete_arena(arena_t arena) {
g_free(arena); g_free(arena);
} }
void allocator_stats(arena_t arena, arena_stats_t *stats) { void allocator_stats(HArena *arena, HArenaStats *stats) {
stats->used = arena->used; stats->used = arena->used;
stats->wasted = arena->wasted; stats->wasted = arena->wasted;
} }

View file

@ -19,18 +19,18 @@
#define HAMMER_ALLOCATOR__H__ #define HAMMER_ALLOCATOR__H__
#include <sys/types.h> #include <sys/types.h>
typedef struct arena* arena_t; // hidden implementation typedef struct HArena_ HArena ; // hidden implementation
arena_t new_arena(size_t block_size); // pass 0 for default... HArena *new_arena(size_t block_size); // pass 0 for default...
void* arena_malloc(arena_t arena, size_t count) __attribute__(( malloc, alloc_size(2) )); void* arena_malloc(HArena *arena, size_t count) __attribute__(( malloc, alloc_size(2) ));
void delete_arena(arena_t arena); void delete_arena(HArena *arena);
typedef struct { typedef struct {
size_t used; size_t used;
size_t wasted; size_t wasted;
} arena_stats_t; } HArenaStats;
void allocator_stats(arena_t arena, arena_stats_t *stats); void allocator_stats(HArena *arena, HArenaStats *stats);
#endif // #ifndef LIB_ALLOCATOR__H__ #endif // #ifndef LIB_ALLOCATOR__H__

View file

@ -26,7 +26,7 @@
#define LDB(range,i) (((i)>>LSB(range))&((1<<(MSB(range)-LSB(range)+1))-1)) #define LDB(range,i) (((i)>>LSB(range))&((1<<(MSB(range)-LSB(range)+1))-1))
long long read_bits(input_stream_t* state, int count, char signed_p) { long long read_bits(HInputStream* state, int count, char signed_p) {
// BUG: Does not // BUG: Does not
long long out = 0; long long out = 0;
int offset = 0; int offset = 0;
@ -122,43 +122,43 @@ long long read_bits(input_stream_t* state, int count, char signed_p) {
static void test_bitreader_ints(void) { static void test_bitreader_ints(void) {
input_stream_t is = MK_INPUT_STREAM("\xFF\xFF\xFF\xFE\x00\x00\x00\x00", 8, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN); 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(read_bits(&is, 64, true), ==, -0x200000000);
} }
static void test_bitreader_be(void) { static void test_bitreader_be(void) {
input_stream_t is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN); 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, 3, false), ==, 0x03);
g_check_cmpint(read_bits(&is, 8, false), ==, 0x52); g_check_cmpint(read_bits(&is, 8, false), ==, 0x52);
g_check_cmpint(read_bits(&is, 5, false), ==, 0x1A); g_check_cmpint(read_bits(&is, 5, false), ==, 0x1A);
} }
static void test_bitreader_le(void) { static void test_bitreader_le(void) {
input_stream_t is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_LITTLE_ENDIAN | BYTE_LITTLE_ENDIAN); 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, 3, false), ==, 0x02);
g_check_cmpint(read_bits(&is, 8, false), ==, 0x4D); g_check_cmpint(read_bits(&is, 8, false), ==, 0x4D);
g_check_cmpint(read_bits(&is, 5, false), ==, 0x0B); g_check_cmpint(read_bits(&is, 5, false), ==, 0x0B);
} }
static void test_largebits_be(void) { static void test_largebits_be(void) {
input_stream_t is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN); 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, 11, false), ==, 0x352);
g_check_cmpint(read_bits(&is, 5, false), ==, 0x1A); g_check_cmpint(read_bits(&is, 5, false), ==, 0x1A);
} }
static void test_largebits_le(void) { static void test_largebits_le(void) {
input_stream_t is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_LITTLE_ENDIAN | BYTE_LITTLE_ENDIAN); 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, 11, false), ==, 0x26A);
g_check_cmpint(read_bits(&is, 5, false), ==, 0x0B); g_check_cmpint(read_bits(&is, 5, false), ==, 0x0B);
} }
static void test_offset_largebits_be(void) { static void test_offset_largebits_be(void) {
input_stream_t is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN); 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, 5, false), ==, 0xD);
g_check_cmpint(read_bits(&is, 11, false), ==, 0x25A); g_check_cmpint(read_bits(&is, 11, false), ==, 0x25A);
} }
static void test_offset_largebits_le(void) { static void test_offset_largebits_le(void) {
input_stream_t is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_LITTLE_ENDIAN | BYTE_LITTLE_ENDIAN); 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, 5, false), ==, 0xA);
g_check_cmpint(read_bits(&is, 11, false), ==, 0x2D3); g_check_cmpint(read_bits(&is, 11, false), ==, 0x2D3);
} }

View file

@ -6,8 +6,8 @@
// {{{ counted arrays // {{{ counted arrays
counted_array_t *carray_new_sized(arena_t arena, size_t size) { HCountedArray *carray_new_sized(HArena * arena, size_t size) {
counted_array_t *ret = arena_malloc(arena, sizeof(counted_array_t)); HCountedArray *ret = arena_malloc(arena, sizeof(HCountedArray));
assert(size > 0); assert(size > 0);
ret->used = 0; ret->used = 0;
ret->capacity = size; ret->capacity = size;
@ -15,13 +15,13 @@ counted_array_t *carray_new_sized(arena_t arena, size_t size) {
ret->elements = arena_malloc(arena, sizeof(void*) * size); ret->elements = arena_malloc(arena, sizeof(void*) * size);
return ret; return ret;
} }
counted_array_t *carray_new(arena_t arena) { HCountedArray *carray_new(HArena * arena) {
return carray_new_sized(arena, 4); return carray_new_sized(arena, 4);
} }
void carray_append(counted_array_t *array, void* item) { void carray_append(HCountedArray *array, void* item) {
if (array->used >= array->capacity) { if (array->used >= array->capacity) {
parsed_token_t **elements = arena_malloc(array->arena, (array->capacity *= 2) * sizeof(counted_array_t*)); HParsedToken **elements = arena_malloc(array->arena, (array->capacity *= 2) * sizeof(HCountedArray*));
for (size_t i = 0; i < array->used; i++) for (size_t i = 0; i < array->used; i++)
elements[i] = array->elements[i]; elements[i] = array->elements[i];
for (size_t i = array->used; i < array->capacity; i++) for (size_t i = array->used; i < array->capacity; i++)

File diff suppressed because it is too large Load diff

View file

@ -41,16 +41,14 @@ typedef enum HTokenType_ {
TT_MAX TT_MAX
} HTokenType; } HTokenType;
typedef struct parsed_token parsed_token_t; typedef struct HCountedArray_ {
typedef struct counted_array {
size_t capacity; size_t capacity;
size_t used; size_t used;
arena_t arena; HArena * arena;
parsed_token_t **elements; struct HParsedToken_ **elements;
} counted_array_t; } HCountedArray;
typedef struct parsed_token { typedef struct HParsedToken_ {
HTokenType token_type; HTokenType token_type;
union { union {
struct { struct {
@ -61,12 +59,12 @@ typedef struct parsed_token {
uint64_t uint; uint64_t uint;
double dbl; double dbl;
float flt; float flt;
counted_array_t *seq; // a sequence of parsed_token_t's HCountedArray *seq; // a sequence of HParsedToken's
void *user; void *user;
}; };
size_t index; size_t index;
char bit_offset; char bit_offset;
} parsed_token_t; } HParsedToken;
/** /**
* The result of a successful parse. * The result of a successful parse.
@ -74,48 +72,48 @@ typedef struct parsed_token {
* If a parse is successful but there's nothing there (i.e., if end_p * If a parse is successful but there's nothing there (i.e., if end_p
* succeeds) then there's a parse result but its ast is NULL. * succeeds) then there's a parse result but its ast is NULL.
*/ */
typedef struct parse_result { typedef struct HParseResult_ {
const parsed_token_t *ast; const HParsedToken *ast;
long long bit_length; long long bit_length;
arena_t arena; HArena * arena;
} parse_result_t; } HParseResult;
/** /**
* Type of an action to apply to an AST, used in the action() parser. * Type of an action to apply to an AST, used in the action() parser.
* It can be any (user-defined) function that takes a parse_result_t* * It can be any (user-defined) function that takes a HParseResult*
* and returns a parsed_token_t*. (This is so that the user doesn't * and returns a HParsedToken*. (This is so that the user doesn't
* have to worry about memory allocation; action() does that for you.) * have to worry about memory allocation; action() does that for you.)
* Note that the tagged union in parsed_token_t* supports user-defined * Note that the tagged union in HParsedToken* supports user-defined
* types, so you can create your own token types (corresponding to, * types, so you can create your own token types (corresponding to,
* say, structs) and stuff values for them into the void* in the * say, structs) and stuff values for them into the void* in the
* tagged union in parsed_token_t. * tagged union in HParsedToken.
*/ */
typedef const parsed_token_t* (*action_t)(const parse_result_t *p); typedef const HParsedToken* (*HAction)(const HParseResult *p);
/** /**
* Type of a boolean attribute-checking function, used in the * Type of a boolean attribute-checking function, used in the
* attr_bool() parser. It can be any (user-defined) function that takes * attr_bool() parser. It can be any (user-defined) function that takes
* a parse_result_t* and returns true or false. * a HParseResult* and returns true or false.
*/ */
typedef bool (*predicate_t)(parse_result_t *p); typedef bool (*HPredicate)(HParseResult *p);
typedef struct parser { typedef struct HParser_ {
parse_result_t* (*fn)(void *env, HParseState *state); HParseResult* (*fn)(void *env, HParseState *state);
void *env; void *env;
} parser_t; } HParser;
/** /**
* Top-level function to call a parser that has been built over some * Top-level function to call a parser that has been built over some
* piece of input (of known size). * piece of input (of known size).
*/ */
parse_result_t* parse(const parser_t* parser, const uint8_t* input, size_t length); HParseResult* parse(const HParser* parser, const uint8_t* input, size_t length);
/** /**
* Given a string, returns a parser that parses that string value. * Given a string, returns a parser that parses that string value.
* *
* Result token type: TT_BYTES * Result token type: TT_BYTES
*/ */
const parser_t* token(const uint8_t *str, const size_t len); const HParser* token(const uint8_t *str, const size_t len);
/** /**
* Given a single character, returns a parser that parses that * Given a single character, returns a parser that parses that
@ -123,7 +121,7 @@ const parser_t* token(const uint8_t *str, const size_t len);
* *
* Result token type: TT_UINT * Result token type: TT_UINT
*/ */
const parser_t* ch(const uint8_t c); const HParser* ch(const uint8_t c);
/** /**
* Given two single-character bounds, lower and upper, returns a parser * Given two single-character bounds, lower and upper, returns a parser
@ -132,14 +130,14 @@ const parser_t* ch(const uint8_t c);
* *
* Result token type: TT_UINT * Result token type: TT_UINT
*/ */
const parser_t* ch_range(const uint8_t lower, const uint8_t upper); const HParser* ch_range(const uint8_t lower, const uint8_t upper);
/** /**
* Given an integer parser, p, and two integer bounds, lower and upper, * Given an integer parser, p, and two integer bounds, lower and upper,
* returns a parser that parses an integral value within the range * returns a parser that parses an integral value within the range
* [lower, upper] (inclusive). * [lower, upper] (inclusive).
*/ */
const parser_t* int_range(const parser_t *p, const int64_t lower, const int64_t upper); const HParser* int_range(const HParser *p, const int64_t lower, const int64_t upper);
/** /**
* Returns a parser that parses the specified number of bits. sign == * Returns a parser that parses the specified number of bits. sign ==
@ -147,63 +145,63 @@ const parser_t* int_range(const parser_t *p, const int64_t lower, const int64_t
* *
* Result token type: TT_SINT if sign == true, TT_UINT if sign == false * Result token type: TT_SINT if sign == true, TT_UINT if sign == false
*/ */
const parser_t* bits(size_t len, bool sign); const HParser* bits(size_t len, bool sign);
/** /**
* Returns a parser that parses a signed 8-byte integer value. * Returns a parser that parses a signed 8-byte integer value.
* *
* Result token type: TT_SINT * Result token type: TT_SINT
*/ */
const parser_t* int64(); const HParser* int64();
/** /**
* Returns a parser that parses a signed 4-byte integer value. * Returns a parser that parses a signed 4-byte integer value.
* *
* Result token type: TT_SINT * Result token type: TT_SINT
*/ */
const parser_t* int32(); const HParser* int32();
/** /**
* Returns a parser that parses a signed 2-byte integer value. * Returns a parser that parses a signed 2-byte integer value.
* *
* Result token type: TT_SINT * Result token type: TT_SINT
*/ */
const parser_t* int16(); const HParser* int16();
/** /**
* Returns a parser that parses a signed 1-byte integer value. * Returns a parser that parses a signed 1-byte integer value.
* *
* Result token type: TT_SINT * Result token type: TT_SINT
*/ */
const parser_t* int8(); const HParser* int8();
/** /**
* Returns a parser that parses an unsigned 8-byte integer value. * Returns a parser that parses an unsigned 8-byte integer value.
* *
* Result token type: TT_UINT * Result token type: TT_UINT
*/ */
const parser_t* uint64(); const HParser* uint64();
/** /**
* Returns a parser that parses an unsigned 4-byte integer value. * Returns a parser that parses an unsigned 4-byte integer value.
* *
* Result token type: TT_UINT * Result token type: TT_UINT
*/ */
const parser_t* uint32(); const HParser* uint32();
/** /**
* Returns a parser that parses an unsigned 2-byte integer value. * Returns a parser that parses an unsigned 2-byte integer value.
* *
* Result token type: TT_UINT * Result token type: TT_UINT
*/ */
const parser_t* uint16(); const HParser* uint16();
/** /**
* Returns a parser that parses an unsigned 1-byte integer value. * Returns a parser that parses an unsigned 1-byte integer value.
* *
* Result token type: TT_UINT * Result token type: TT_UINT
*/ */
const parser_t* uint8(); const HParser* uint8();
/** /**
* Given another parser, p, returns a parser that skips any whitespace * Given another parser, p, returns a parser that skips any whitespace
@ -211,7 +209,7 @@ const parser_t* uint8();
* *
* Result token type: p's result type * Result token type: p's result type
*/ */
const parser_t* whitespace(const parser_t* p); const HParser* whitespace(const HParser* p);
/** /**
* Given another parser, p, and a function f, returns a parser that * Given another parser, p, and a function f, returns a parser that
@ -219,29 +217,29 @@ const parser_t* whitespace(const parser_t* p);
* *
* Result token type: any * Result token type: any
*/ */
const parser_t* action(const parser_t* p, const action_t a); const HParser* action(const HParser* p, const HAction a);
/** /**
* Parse a single character *NOT* in the given charset. * Parse a single character *NOT* in the given charset.
* *
* Result token type: TT_UINT * Result token type: TT_UINT
*/ */
const parser_t* not_in(const uint8_t *charset, int length); const HParser* not_in(const uint8_t *charset, int length);
/** /**
* A no-argument parser that succeeds if there is no more input to * A no-argument parser that succeeds if there is no more input to
* parse. * parse.
* *
* Result token type: None. The parse_result_t exists but its AST is NULL. * Result token type: None. The HParseResult exists but its AST is NULL.
*/ */
const parser_t* end_p(); const HParser* end_p();
/** /**
* This parser always fails. * This parser always fails.
* *
* Result token type: NULL. Always. * Result token type: NULL. Always.
*/ */
const parser_t* nothing_p(); const HParser* nothing_p();
/** /**
* Given a null-terminated list of parsers, apply each parser in order. * Given a null-terminated list of parsers, apply each parser in order.
@ -249,7 +247,7 @@ const parser_t* nothing_p();
* *
* Result token type: TT_SEQUENCE * Result token type: TT_SEQUENCE
*/ */
const parser_t* sequence(const parser_t* p, ...) __attribute__((sentinel)); const HParser* sequence(const HParser* p, ...) __attribute__((sentinel));
/** /**
* Given an array of parsers, p_array, apply each parser in order. The * Given an array of parsers, p_array, apply each parser in order. The
@ -258,7 +256,7 @@ const parser_t* sequence(const parser_t* p, ...) __attribute__((sentinel));
* *
* Result token type: The type of the first successful parser's result. * Result token type: The type of the first successful parser's result.
*/ */
const parser_t* choice(const parser_t* p, ...) __attribute__((sentinel)); const HParser* choice(const HParser* p, ...) __attribute__((sentinel));
/** /**
* Given two parsers, p1 and p2, this parser succeeds in the following * Given two parsers, p1 and p2, this parser succeeds in the following
@ -268,7 +266,7 @@ const parser_t* choice(const parser_t* p, ...) __attribute__((sentinel));
* *
* Result token type: p1's result type. * Result token type: p1's result type.
*/ */
const parser_t* butnot(const parser_t* p1, const parser_t* p2); const HParser* butnot(const HParser* p1, const HParser* p2);
/** /**
* Given two parsers, p1 and p2, this parser succeeds in the following * Given two parsers, p1 and p2, this parser succeeds in the following
@ -278,7 +276,7 @@ const parser_t* butnot(const parser_t* p1, const parser_t* p2);
* *
* Result token type: p1's result type. * Result token type: p1's result type.
*/ */
const parser_t* difference(const parser_t* p1, const parser_t* p2); const HParser* difference(const HParser* p1, const HParser* p2);
/** /**
* Given two parsers, p1 and p2, this parser succeeds if *either* p1 or * Given two parsers, p1 and p2, this parser succeeds if *either* p1 or
@ -286,7 +284,7 @@ const parser_t* difference(const parser_t* p1, const parser_t* p2);
* *
* Result token type: The type of the result of whichever parser succeeded. * Result token type: The type of the result of whichever parser succeeded.
*/ */
const parser_t* xor(const parser_t* p1, const parser_t* p2); const HParser* xor(const HParser* p1, const HParser* p2);
/** /**
* Given a parser, p, this parser succeeds for zero or more repetitions * Given a parser, p, this parser succeeds for zero or more repetitions
@ -294,7 +292,7 @@ const parser_t* xor(const parser_t* p1, const parser_t* p2);
* *
* Result token type: TT_SEQUENCE * Result token type: TT_SEQUENCE
*/ */
const parser_t* many(const parser_t* p); const HParser* many(const HParser* p);
/** /**
* Given a parser, p, this parser succeeds for one or more repetitions * Given a parser, p, this parser succeeds for one or more repetitions
@ -302,7 +300,7 @@ const parser_t* many(const parser_t* p);
* *
* Result token type: TT_SEQUENCE * Result token type: TT_SEQUENCE
*/ */
const parser_t* many1(const parser_t* p); const HParser* many1(const HParser* p);
/** /**
* Given a parser, p, this parser succeeds for exactly N repetitions * Given a parser, p, this parser succeeds for exactly N repetitions
@ -310,7 +308,7 @@ const parser_t* many1(const parser_t* p);
* *
* Result token type: TT_SEQUENCE * Result token type: TT_SEQUENCE
*/ */
const parser_t* repeat_n(const parser_t* p, const size_t n); const HParser* repeat_n(const HParser* p, const size_t n);
/** /**
* Given a parser, p, this parser succeeds with the value p parsed or * Given a parser, p, this parser succeeds with the value p parsed or
@ -318,15 +316,15 @@ const parser_t* repeat_n(const parser_t* p, const size_t n);
* *
* Result token type: If p succeeded, the type of its result; if not, TT_NONE. * Result token type: If p succeeded, the type of its result; if not, TT_NONE.
*/ */
const parser_t* optional(const parser_t* p); const HParser* optional(const HParser* p);
/** /**
* Given a parser, p, this parser succeeds if p succeeds, but doesn't * Given a parser, p, this parser succeeds if p succeeds, but doesn't
* include p's result in the result. * include p's result in the result.
* *
* Result token type: None. The parse_result_t exists but its AST is NULL. * Result token type: None. The HParseResult exists but its AST is NULL.
*/ */
const parser_t* ignore(const parser_t* p); const HParser* ignore(const HParser* p);
/** /**
* Given a parser, p, and a parser for a separator, sep, this parser * Given a parser, p, and a parser for a separator, sep, this parser
@ -337,7 +335,7 @@ const parser_t* ignore(const parser_t* p);
* *
* Result token type: TT_SEQUENCE * Result token type: TT_SEQUENCE
*/ */
const parser_t* sepBy(const parser_t* p, const parser_t* sep); const HParser* 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. * 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.
@ -345,14 +343,14 @@ const parser_t* sepBy(const parser_t* p, const parser_t* sep);
* *
* Result token type: TT_SEQUENCE * Result token type: TT_SEQUENCE
*/ */
const parser_t* sepBy1(const parser_t* p, const parser_t* sep); const HParser* sepBy1(const HParser* p, const HParser* sep);
/** /**
* This parser always returns a zero length match, i.e., empty string. * This parser always returns a zero length match, i.e., empty string.
* *
* Result token type: None. The parse_result_t exists but its AST is NULL. * Result token type: None. The HParseResult exists but its AST is NULL.
*/ */
const parser_t* epsilon_p(); const HParser* epsilon_p();
/** /**
* This parser applies its first argument to read an unsigned integer * This parser applies its first argument to read an unsigned integer
@ -363,7 +361,7 @@ const parser_t* epsilon_p();
* *
* Result token type: TT_SEQUENCE * Result token type: TT_SEQUENCE
*/ */
const parser_t* length_value(const parser_t* length, const parser_t* value); const HParser* length_value(const HParser* length, const HParser* value);
/** /**
* This parser attaches a predicate function, which returns true or * This parser attaches a predicate function, which returns true or
@ -378,7 +376,7 @@ const parser_t* length_value(const parser_t* length, const parser_t* value);
* *
* Result token type: p's result type if pred succeeded, NULL otherwise. * Result token type: p's result type if pred succeeded, NULL otherwise.
*/ */
const parser_t* attr_bool(const parser_t* p, predicate_t pred); const HParser* attr_bool(const HParser* p, HPredicate pred);
/** /**
* The 'and' parser asserts that a conditional syntax is satisfied, * The 'and' parser asserts that a conditional syntax is satisfied,
@ -393,9 +391,9 @@ const parser_t* attr_bool(const parser_t* p, predicate_t pred);
* *
* 'and' succeeds if p succeeds, and fails if p fails. * 'and' succeeds if p succeeds, and fails if p fails.
* *
* Result token type: None. The parse_result_t exists but its AST is NULL. * Result token type: None. The HParseResult exists but its AST is NULL.
*/ */
const parser_t* and(const parser_t* p); const HParser* and(const HParser* p);
/** /**
* The 'not' parser asserts that a conditional syntax is *not* * The 'not' parser asserts that a conditional syntax is *not*
@ -413,9 +411,9 @@ const parser_t* and(const parser_t* p);
* If the input string is "a+b", the first alternative is applied; if * If the input string is "a+b", the first alternative is applied; if
* the input string is "a++b", the second alternative is applied. * the input string is "a++b", the second alternative is applied.
* *
* Result token type: None. The parse_result_t exists but its AST is NULL. * Result token type: None. The HParseResult exists but its AST is NULL.
*/ */
const parser_t* not(const parser_t* p); const HParser* not(const HParser* p);
/** /**
* Create a parser that just calls out to another, as yet unknown, * Create a parser that just calls out to another, as yet unknown,
@ -426,12 +424,12 @@ const parser_t* not(const parser_t* p);
* Result token type: the type of whatever parser is bound to it with * Result token type: the type of whatever parser is bound to it with
* bind_indirect(). * bind_indirect().
*/ */
parser_t *indirect(); HParser *indirect();
/** /**
* Set the inner parser of an indirect. See comments on indirect for * Set the inner parser of an indirect. See comments on indirect for
* details. * details.
*/ */
void bind_indirect(parser_t* indirect, parser_t* inner); void bind_indirect(HParser* indirect, HParser* inner);
#endif // #ifndef HAMMER_HAMMER__H #endif // #ifndef HAMMER_HAMMER__H

View file

@ -32,7 +32,7 @@
#define false 0 #define false 0
#define true 1 #define true 1
typedef struct input_stream { typedef struct HInputStream_ {
// This should be considered to be a really big value type. // This should be considered to be a really big value type.
const uint8_t *input; const uint8_t *input;
size_t index; size_t index;
@ -40,7 +40,7 @@ typedef struct input_stream {
char bit_offset; char bit_offset;
char endianness; char endianness;
char overrun; char overrun;
} input_stream_t; } HInputStream;
/* The state of the parser. /* The state of the parser.
* *
@ -55,8 +55,8 @@ typedef struct input_stream {
struct HParseState_ { struct HParseState_ {
GHashTable *cache; GHashTable *cache;
input_stream_t input_stream; HInputStream input_stream;
arena_t arena; HArena * arena;
GQueue *lr_stack; GQueue *lr_stack;
GHashTable *recursion_heads; GHashTable *recursion_heads;
}; };
@ -65,14 +65,14 @@ struct HParseState_ {
*/ */
typedef struct parser_cache_key { typedef struct parser_cache_key {
input_stream_t input_pos; HInputStream input_pos;
const parser_t *parser; const HParser *parser;
} parser_cache_key_t; } parser_cache_key_t;
/* A value in the cache is either of value Left or Right (this is a /* A value in the cache is either of value Left or Right (this is a
* holdover from Scala, which used Either here). Left corresponds to * holdover from Scala, which used Either here). Left corresponds to
* LR_t, which is for left recursion; Right corresponds to * LR_t, which is for left recursion; Right corresponds to
* parse_result_t. * HParseResult.
*/ */
typedef enum parser_cache_value_type { typedef enum parser_cache_value_type {
@ -85,11 +85,11 @@ typedef enum parser_cache_value_type {
* *
* Members: * Members:
* head_parser - the parse rule that started this recursion * head_parser - the parse rule that started this recursion
* involved_set - A list of rules (parser_t's) involved in the recursion * involved_set - A list of rules (HParser's) involved in the recursion
* eval_set - * eval_set -
*/ */
typedef struct head { typedef struct head {
const parser_t *head_parser; const HParser *head_parser;
GSList *involved_set; GSList *involved_set;
GSList *eval_set; GSList *eval_set;
} head_t; } head_t;
@ -103,19 +103,19 @@ typedef struct head {
* head - * head -
*/ */
typedef struct LR { typedef struct LR {
parse_result_t *seed; HParseResult *seed;
const parser_t *rule; const HParser *rule;
head_t *head; head_t *head;
} LR_t; } LR_t;
/* Tagged union for values in the cache: either LR's (Left) or /* Tagged union for values in the cache: either LR's (Left) or
* parse_result_t's (Right). * HParseResult's (Right).
*/ */
typedef struct parser_cache_value { typedef struct parser_cache_value {
parser_cache_value_type_t value_type; parser_cache_value_type_t value_type;
union { union {
LR_t *left; LR_t *left;
parse_result_t *right; HParseResult *right;
}; };
} parser_cache_value_t; } parser_cache_value_t;
@ -139,16 +139,16 @@ static inline void charset_set(charset cs, uint8_t pos, int val) {
// TODO(thequux): Set symbol visibility for these functions so that they aren't exported. // TODO(thequux): Set symbol visibility for these functions so that they aren't exported.
long long read_bits(input_stream_t* state, int count, char signed_p); long long read_bits(HInputStream* state, int count, char signed_p);
parse_result_t* do_parse(const parser_t* parser, HParseState *state); HParseResult* do_parse(const HParser* parser, HParseState *state);
void put_cached(HParseState *ps, const parser_t *p, parse_result_t *cached); void put_cached(HParseState *ps, const HParser *p, HParseResult *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 HParsedToken* tok);
void pprint(const parsed_token_t* tok, int indent, int delta); void pprint(const HParsedToken* tok, int indent, int delta);
counted_array_t *carray_new_sized(arena_t arena, size_t size); HCountedArray *carray_new_sized(HArena * arena, size_t size);
counted_array_t *carray_new(arena_t arena); HCountedArray *carray_new(HArena * arena);
void carray_append(counted_array_t *array, void* item); void carray_append(HCountedArray *array, void* item);
#if 0 #if 0
#include <malloc.h> #include <malloc.h>

View file

@ -28,7 +28,7 @@ typedef struct pp_state {
int at_bol; int at_bol;
} pp_state_t; } pp_state_t;
void pprint(const parsed_token_t* tok, int indent, int delta) { void pprint(const HParsedToken* tok, int indent, int delta) {
switch (tok->token_type) { switch (tok->token_type) {
case TT_NONE: case TT_NONE:
printf("%*snull\n", indent, ""); printf("%*snull\n", indent, "");
@ -91,7 +91,7 @@ static inline void append_buf_c(struct result_buf *buf, char v) {
buf->output[buf->len++] = v; buf->output[buf->len++] = v;
} }
static void unamb_sub(const parsed_token_t* tok, struct result_buf *buf) { static void unamb_sub(const HParsedToken* tok, struct result_buf *buf) {
char* tmpbuf; char* tmpbuf;
int len; int len;
if (!tok) { if (!tok) {
@ -149,7 +149,7 @@ static void unamb_sub(const parsed_token_t* tok, struct result_buf *buf) {
} }
char* write_result_unamb(const parsed_token_t* tok) { char* write_result_unamb(const HParsedToken* tok) {
struct result_buf buf = { struct result_buf buf = {
.output = g_malloc0(16), .output = g_malloc0(16),
.len = 0, .len = 0,

View file

@ -54,7 +54,7 @@
// TODO: replace uses of this with g_check_parse_failed // TODO: replace uses of this with g_check_parse_failed
#define g_check_failed(res) do { \ #define g_check_failed(res) do { \
const parse_result_t *result = (res); \ const HParseResult *result = (res); \
if (NULL != result) { \ if (NULL != result) { \
g_test_message("Check failed: shouldn't have succeeded, but did"); \ g_test_message("Check failed: shouldn't have succeeded, but did"); \
g_test_fail(); \ g_test_fail(); \
@ -62,7 +62,7 @@
} while(0) } while(0)
#define g_check_parse_failed(parser, input, inp_len) do { \ #define g_check_parse_failed(parser, input, inp_len) do { \
const parse_result_t *result = parse(parser, (const uint8_t*)input, inp_len); \ const HParseResult *result = parse(parser, (const uint8_t*)input, inp_len); \
if (NULL != result) { \ if (NULL != result) { \
g_test_message("Check failed: shouldn't have succeeded, but did"); \ g_test_message("Check failed: shouldn't have succeeded, but did"); \
g_test_fail(); \ g_test_fail(); \
@ -70,7 +70,7 @@
} while(0) } while(0)
#define g_check_parse_ok(parser, input, inp_len, result) do { \ #define g_check_parse_ok(parser, input, inp_len, result) do { \
parse_result_t *res = parse(parser, (const uint8_t*)input, inp_len); \ HParseResult *res = parse(parser, (const uint8_t*)input, inp_len); \
if (!res) { \ if (!res) { \
g_test_message("Parse failed on line %d", __LINE__); \ g_test_message("Parse failed on line %d", __LINE__); \
g_test_fail(); \ g_test_fail(); \
@ -78,7 +78,7 @@
char* cres = write_result_unamb(res->ast); \ char* cres = write_result_unamb(res->ast); \
g_check_string(cres, ==, result); \ g_check_string(cres, ==, result); \
g_free(cres); \ g_free(cres); \
arena_stats_t stats; \ HArenaStats stats; \
allocator_stats(res->arena, &stats); \ allocator_stats(res->arena, &stats); \
g_test_message("Parse used %zd bytes, wasted %zd bytes. " \ g_test_message("Parse used %zd bytes, wasted %zd bytes. " \
"Inefficiency: %5f%%", \ "Inefficiency: %5f%%", \