Fixed all known memory leaks, added indirect parser, which will allow recursion

This commit is contained in:
Dan Hirsch 2012-05-17 18:27:59 +02:00
commit 5ffea6b7bb
4 changed files with 270 additions and 57 deletions

View file

@ -20,36 +20,15 @@
#include <glib.h>
#include <stdint.h>
#include "allocator.h"
/* The state of the parser.
*
* Members:
* input - the entire string being parsed
* index - current position in input
* length - size of input
* cache - a hash table describing the state of the parse, including partial parse_results. It's a hash table from parser_cache_key_t to parse_state_t.
*
*/
#define BYTE_BIG_ENDIAN 0x1
#define BIT_BIG_ENDIAN 0x2
#define BIT_LITTLE_ENDIAN 0x0
#define BYTE_LITTLE_ENDIAN 0x0
typedef int bool;
typedef struct input_stream {
// This should be considered to be a really big value type.
const uint8_t *input;
size_t index;
size_t length;
char bit_offset;
char endianness;
char overrun;
} input_stream_t;
typedef struct parse_state {
GHashTable *cache;
input_stream_t input_stream;
arena_t arena;
} parse_state_t;
typedef struct parse_state parse_state_t;
typedef enum token_type {
TT_NONE,
@ -81,6 +60,8 @@ typedef struct parsed_token {
float flt;
counted_array_t *seq; // a sequence of parsed_token_t's
};
size_t index;
char bit_offset;
} parsed_token_t;
@ -242,4 +223,16 @@ const parser_t* and(const parser_t* p);
*/
const parser_t* not(const parser_t* p);
/**
* Create a parser that just calls out to another, as yet unknown, parser.
* Note that the inner parser gets bound later, with bind_indirect.
* This can be used to create recursive parsers.
*/
parser_t *indirect();
/**
* Set the inner parser of an indirect. See comments on indirect for details.
*/
void bind_indirect(parser_t* indirect, parser_t* inner);
#endif // #ifndef HAMMER_HAMMER__H