Merge remote-tracking branch 'peewee-vb/great-symbol-renaming'
This commit is contained in:
commit
55d0ace87b
10 changed files with 443 additions and 435 deletions
24
lib/hush.c
24
lib/hush.c
|
|
@ -1,9 +1,11 @@
|
|||
// -*- c-basic-offset: 8; tab-width: 8 -*-
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/wait.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main (int argc, char** argv) {
|
||||
// Argv[1] is the message
|
||||
|
|
@ -11,7 +13,12 @@ int main (int argc, char** argv) {
|
|||
// the rest are passed to the child
|
||||
if (argc < 3) return 1;
|
||||
printf ("\x1b[1;32m*\x1b[0m %s...", argv[1]);
|
||||
char cbuf[4096];
|
||||
|
||||
char *cbuf = malloc(4096);
|
||||
size_t cbuf_cap = 4096;
|
||||
size_t cbuf_len = 0;
|
||||
char cbuf2[4096];
|
||||
|
||||
argc-=2;
|
||||
argv+=1;
|
||||
for (int i = 0; i < argc; i++)
|
||||
|
|
@ -40,24 +47,27 @@ int main (int argc, char** argv) {
|
|||
if (cols1) { cols = atoi(cols1); } else { cols = 80; }
|
||||
close(fd[0]);
|
||||
int delta = 1;
|
||||
int ct = 0;
|
||||
while (delta != 0) {
|
||||
delta = read (fd[1], cbuf+ct, 4096-ct);
|
||||
ct+= delta;
|
||||
delta = read (fd[1], cbuf2, 4096);
|
||||
while ((cbuf_len + delta) >= cbuf_cap)
|
||||
cbuf = realloc(cbuf, cbuf_cap *= 2);
|
||||
memcpy(cbuf + cbuf_len, cbuf2, delta);
|
||||
cbuf_len += delta;
|
||||
}
|
||||
cbuf[ct] = 0;
|
||||
|
||||
int status;
|
||||
wait(&status);
|
||||
fflush (NULL);
|
||||
if (status) {
|
||||
fprintf (stderr, "\x1b[%dG\x1b[1;34m[\x1b[1;31m!!\x1b[1;34m]\x1b[0m\n", cols-4);
|
||||
} else if (ct) {
|
||||
} else if (cbuf_len) {
|
||||
fprintf (stderr, "\x1b[%dG\x1b[1;34m[\x1b[0;33mWW\x1b[1;34m]\x1b[0m\n", cols-4);
|
||||
} else {
|
||||
fprintf (stderr, "\x1b[%dG\x1b[1;34m[\x1b[1;32mOK\x1b[1;34m]\x1b[0m\n", cols-4);
|
||||
}
|
||||
fflush (NULL);
|
||||
printf ("%s", cbuf);
|
||||
write(2, cbuf, cbuf_len);
|
||||
free(cbuf);
|
||||
fflush (NULL);
|
||||
return WEXITSTATUS(status);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,17 +34,17 @@ struct arena_link {
|
|||
uint8_t rest[];
|
||||
} ;
|
||||
|
||||
struct arena {
|
||||
struct HArena_ {
|
||||
struct arena_link *head;
|
||||
size_t block_size;
|
||||
size_t used;
|
||||
size_t wasted;
|
||||
};
|
||||
|
||||
arena_t new_arena(size_t block_size) {
|
||||
HArena *new_arena(size_t block_size) {
|
||||
if (block_size == 0)
|
||||
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);
|
||||
link->free = block_size;
|
||||
link->used = 0;
|
||||
|
|
@ -52,11 +52,11 @@ arena_t new_arena(size_t block_size) {
|
|||
ret->head = link;
|
||||
ret->block_size = block_size;
|
||||
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;
|
||||
}
|
||||
|
||||
void* arena_malloc(arena_t arena, size_t size) {
|
||||
void* arena_malloc(HArena *arena, size_t size) {
|
||||
if (size <= arena->head->free) {
|
||||
// fast path..
|
||||
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;
|
||||
while (link) {
|
||||
struct arena_link *next = link->next;
|
||||
|
|
@ -100,7 +100,7 @@ void delete_arena(arena_t 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->wasted = arena->wasted;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,18 +19,18 @@
|
|||
#define HAMMER_ALLOCATOR__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...
|
||||
void* arena_malloc(arena_t arena, size_t count) __attribute__(( malloc, alloc_size(2) ));
|
||||
void delete_arena(arena_t arena);
|
||||
HArena *new_arena(size_t block_size); // pass 0 for default...
|
||||
void* arena_malloc(HArena *arena, size_t count) __attribute__(( malloc, alloc_size(2) ));
|
||||
void delete_arena(HArena *arena);
|
||||
|
||||
typedef struct {
|
||||
size_t used;
|
||||
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__
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@
|
|||
#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
|
||||
long long out = 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) {
|
||||
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);
|
||||
}
|
||||
|
||||
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, 8, false), ==, 0x52);
|
||||
g_check_cmpint(read_bits(&is, 5, false), ==, 0x1A);
|
||||
}
|
||||
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, 8, false), ==, 0x4D);
|
||||
g_check_cmpint(read_bits(&is, 5, false), ==, 0x0B);
|
||||
}
|
||||
|
||||
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, 5, false), ==, 0x1A);
|
||||
}
|
||||
|
||||
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, 5, false), ==, 0x0B);
|
||||
}
|
||||
|
||||
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, 11, false), ==, 0x25A);
|
||||
}
|
||||
|
||||
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, 11, false), ==, 0x2D3);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@
|
|||
// {{{ counted arrays
|
||||
|
||||
|
||||
counted_array_t *carray_new_sized(arena_t arena, size_t size) {
|
||||
counted_array_t *ret = arena_malloc(arena, sizeof(counted_array_t));
|
||||
HCountedArray *carray_new_sized(HArena * arena, size_t size) {
|
||||
HCountedArray *ret = arena_malloc(arena, sizeof(HCountedArray));
|
||||
assert(size > 0);
|
||||
ret->used = 0;
|
||||
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);
|
||||
return ret;
|
||||
}
|
||||
counted_array_t *carray_new(arena_t arena) {
|
||||
HCountedArray *carray_new(HArena * arena) {
|
||||
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) {
|
||||
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++)
|
||||
elements[i] = array->elements[i];
|
||||
for (size_t i = array->used; i < array->capacity; i++)
|
||||
|
|
|
|||
560
src/hammer.c
560
src/hammer.c
File diff suppressed because it is too large
Load diff
138
src/hammer.h
138
src/hammer.h
|
|
@ -28,9 +28,9 @@
|
|||
|
||||
typedef int bool;
|
||||
|
||||
typedef struct parse_state parse_state_t;
|
||||
typedef struct HParseState_ HParseState;
|
||||
|
||||
typedef enum token_type {
|
||||
typedef enum HTokenType_ {
|
||||
TT_NONE,
|
||||
TT_BYTES,
|
||||
TT_SINT,
|
||||
|
|
@ -39,19 +39,17 @@ typedef enum token_type {
|
|||
TT_USER = 64,
|
||||
TT_ERR,
|
||||
TT_MAX
|
||||
} token_type_t;
|
||||
} HTokenType;
|
||||
|
||||
typedef struct parsed_token parsed_token_t;
|
||||
|
||||
typedef struct counted_array {
|
||||
typedef struct HCountedArray_ {
|
||||
size_t capacity;
|
||||
size_t used;
|
||||
arena_t arena;
|
||||
parsed_token_t **elements;
|
||||
} counted_array_t;
|
||||
HArena * arena;
|
||||
struct HParsedToken_ **elements;
|
||||
} HCountedArray;
|
||||
|
||||
typedef struct parsed_token {
|
||||
token_type_t token_type;
|
||||
typedef struct HParsedToken_ {
|
||||
HTokenType token_type;
|
||||
union {
|
||||
struct {
|
||||
const uint8_t *token;
|
||||
|
|
@ -61,12 +59,12 @@ typedef struct parsed_token {
|
|||
uint64_t uint;
|
||||
double dbl;
|
||||
float flt;
|
||||
counted_array_t *seq; // a sequence of parsed_token_t's
|
||||
HCountedArray *seq; // a sequence of HParsedToken's
|
||||
void *user;
|
||||
};
|
||||
size_t index;
|
||||
char bit_offset;
|
||||
} parsed_token_t;
|
||||
} HParsedToken;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* succeeds) then there's a parse result but its ast is NULL.
|
||||
*/
|
||||
typedef struct parse_result {
|
||||
const parsed_token_t *ast;
|
||||
typedef struct HParseResult_ {
|
||||
const HParsedToken *ast;
|
||||
long long bit_length;
|
||||
arena_t arena;
|
||||
} parse_result_t;
|
||||
HArena * arena;
|
||||
} HParseResult;
|
||||
|
||||
/**
|
||||
* 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*
|
||||
* and returns a parsed_token_t*. (This is so that the user doesn't
|
||||
* It can be any (user-defined) function that takes a HParseResult*
|
||||
* and returns a HParsedToken*. (This is so that the user doesn't
|
||||
* 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,
|
||||
* 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
|
||||
* 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 {
|
||||
parse_result_t* (*fn)(void *env, parse_state_t *state);
|
||||
typedef struct HParser_ {
|
||||
HParseResult* (*fn)(void *env, HParseState *state);
|
||||
void *env;
|
||||
} parser_t;
|
||||
} HParser;
|
||||
|
||||
/**
|
||||
* Top-level function to call a parser that has been built over some
|
||||
* 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.
|
||||
*
|
||||
* 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
|
||||
|
|
@ -123,7 +121,7 @@ const parser_t* token(const uint8_t *str, const size_t len);
|
|||
*
|
||||
* 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
|
||||
|
|
@ -132,14 +130,14 @@ const parser_t* ch(const uint8_t c);
|
|||
*
|
||||
* 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,
|
||||
* returns a parser that parses an integral value within the range
|
||||
* [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 ==
|
||||
|
|
@ -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
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* Result token type: TT_SINT
|
||||
*/
|
||||
const parser_t* int64();
|
||||
const HParser* int64();
|
||||
|
||||
/**
|
||||
* Returns a parser that parses a signed 4-byte integer value.
|
||||
*
|
||||
* Result token type: TT_SINT
|
||||
*/
|
||||
const parser_t* int32();
|
||||
const HParser* int32();
|
||||
|
||||
/**
|
||||
* Returns a parser that parses a signed 2-byte integer value.
|
||||
*
|
||||
* Result token type: TT_SINT
|
||||
*/
|
||||
const parser_t* int16();
|
||||
const HParser* int16();
|
||||
|
||||
/**
|
||||
* Returns a parser that parses a signed 1-byte integer value.
|
||||
*
|
||||
* Result token type: TT_SINT
|
||||
*/
|
||||
const parser_t* int8();
|
||||
const HParser* int8();
|
||||
|
||||
/**
|
||||
* Returns a parser that parses an unsigned 8-byte integer value.
|
||||
*
|
||||
* Result token type: TT_UINT
|
||||
*/
|
||||
const parser_t* uint64();
|
||||
const HParser* uint64();
|
||||
|
||||
/**
|
||||
* Returns a parser that parses an unsigned 4-byte integer value.
|
||||
*
|
||||
* Result token type: TT_UINT
|
||||
*/
|
||||
const parser_t* uint32();
|
||||
const HParser* uint32();
|
||||
|
||||
/**
|
||||
* Returns a parser that parses an unsigned 2-byte integer value.
|
||||
*
|
||||
* Result token type: TT_UINT
|
||||
*/
|
||||
const parser_t* uint16();
|
||||
const HParser* uint16();
|
||||
|
||||
/**
|
||||
* Returns a parser that parses an unsigned 1-byte integer value.
|
||||
*
|
||||
* Result token type: TT_UINT
|
||||
*/
|
||||
const parser_t* uint8();
|
||||
const HParser* uint8();
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
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
|
||||
|
|
@ -219,29 +217,29 @@ const parser_t* whitespace(const parser_t* p);
|
|||
*
|
||||
* 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.
|
||||
*
|
||||
* 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
|
||||
* 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.
|
||||
*
|
||||
* 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.
|
||||
|
|
@ -249,7 +247,7 @@ const parser_t* nothing_p();
|
|||
*
|
||||
* 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
|
||||
|
|
@ -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.
|
||||
*/
|
||||
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
|
||||
|
|
@ -268,7 +266,7 @@ const parser_t* choice(const parser_t* p, ...) __attribute__((sentinel));
|
|||
*
|
||||
* 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
|
||||
|
|
@ -278,7 +276,7 @@ const parser_t* butnot(const parser_t* p1, const parser_t* p2);
|
|||
*
|
||||
* 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
|
||||
|
|
@ -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.
|
||||
*/
|
||||
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
|
||||
|
|
@ -294,7 +292,7 @@ const parser_t* xor(const parser_t* p1, const parser_t* p2);
|
|||
*
|
||||
* 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
|
||||
|
|
@ -302,7 +300,7 @@ const parser_t* many(const parser_t* p);
|
|||
*
|
||||
* 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
|
||||
|
|
@ -310,7 +308,7 @@ const parser_t* many1(const parser_t* p);
|
|||
*
|
||||
* 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
|
||||
|
|
@ -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.
|
||||
*/
|
||||
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
|
||||
* 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
|
||||
|
|
@ -337,7 +335,7 @@ const parser_t* ignore(const parser_t* p);
|
|||
*
|
||||
* 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.
|
||||
|
|
@ -345,14 +343,14 @@ const parser_t* sepBy(const parser_t* p, const parser_t* sep);
|
|||
*
|
||||
* 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.
|
||||
*
|
||||
* 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
|
||||
|
|
@ -363,7 +361,7 @@ const parser_t* epsilon_p();
|
|||
*
|
||||
* 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
|
||||
|
|
@ -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.
|
||||
*/
|
||||
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,
|
||||
|
|
@ -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.
|
||||
*
|
||||
* 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*
|
||||
|
|
@ -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
|
||||
* 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,
|
||||
|
|
@ -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
|
||||
* bind_indirect().
|
||||
*/
|
||||
parser_t *indirect();
|
||||
HParser *indirect();
|
||||
|
||||
/**
|
||||
* Set the inner parser of an indirect. See comments on indirect for
|
||||
* details.
|
||||
*/
|
||||
void bind_indirect(parser_t* indirect, parser_t* inner);
|
||||
void bind_indirect(HParser* indirect, HParser* inner);
|
||||
|
||||
#endif // #ifndef HAMMER_HAMMER__H
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@
|
|||
#define false 0
|
||||
#define true 1
|
||||
|
||||
typedef struct input_stream {
|
||||
typedef struct HInputStream_ {
|
||||
// This should be considered to be a really big value type.
|
||||
const uint8_t *input;
|
||||
size_t index;
|
||||
|
|
@ -40,23 +40,23 @@ typedef struct input_stream {
|
|||
char bit_offset;
|
||||
char endianness;
|
||||
char overrun;
|
||||
} input_stream_t;
|
||||
} HInputStream;
|
||||
|
||||
/* The state of the parser.
|
||||
*
|
||||
* Members:
|
||||
* 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 parser_cache_value_t.
|
||||
* cache - a hash table describing the state of the parse, including partial HParseResult's. It's a hash table from HParserCacheKey to HParserCacheValue.
|
||||
* input_stream - the input stream at this state.
|
||||
* arena - the arena that has been allocated for the parse this state is in.
|
||||
* lr_stack - a stack of LRs, used in Warth's recursion
|
||||
* recursion_heads - table of recursion heads. Keys are parse_cache_key_t's with only an input_state_t (parser can be NULL), values are head_t.
|
||||
* lr_stack - a stack of HLeftRec's, used in Warth's recursion
|
||||
* recursion_heads - table of recursion heads. Keys are HParserCacheKey's with only an HInputStream (parser can be NULL), values are HRecursionHead's.
|
||||
*
|
||||
*/
|
||||
|
||||
struct parse_state {
|
||||
struct HParseState_ {
|
||||
GHashTable *cache;
|
||||
input_stream_t input_stream;
|
||||
arena_t arena;
|
||||
HInputStream input_stream;
|
||||
HArena * arena;
|
||||
GQueue *lr_stack;
|
||||
GHashTable *recursion_heads;
|
||||
};
|
||||
|
|
@ -64,35 +64,35 @@ struct parse_state {
|
|||
/* The (location, parser) tuple used to key the cache.
|
||||
*/
|
||||
|
||||
typedef struct parser_cache_key {
|
||||
input_stream_t input_pos;
|
||||
const parser_t *parser;
|
||||
} parser_cache_key_t;
|
||||
typedef struct HParserCacheKey_ {
|
||||
HInputStream input_pos;
|
||||
const HParser *parser;
|
||||
} HParserCacheKey;
|
||||
|
||||
/* 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
|
||||
* LR_t, which is for left recursion; Right corresponds to
|
||||
* parse_result_t.
|
||||
* HLeftRec, which is for left recursion; Right corresponds to
|
||||
* HParseResult.
|
||||
*/
|
||||
|
||||
typedef enum parser_cache_value_type {
|
||||
typedef enum HParserCacheValueType_ {
|
||||
PC_LEFT,
|
||||
PC_RIGHT
|
||||
} parser_cache_value_type_t;
|
||||
} HParserCacheValueType;
|
||||
|
||||
|
||||
/* A recursion head.
|
||||
*
|
||||
* Members:
|
||||
* 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 -
|
||||
*/
|
||||
typedef struct head {
|
||||
const parser_t *head_parser;
|
||||
typedef struct HRecursionHead_ {
|
||||
const HParser *head_parser;
|
||||
GSList *involved_set;
|
||||
GSList *eval_set;
|
||||
} head_t;
|
||||
} HRecursionHead;
|
||||
|
||||
|
||||
/* A left recursion.
|
||||
|
|
@ -102,35 +102,35 @@ typedef struct head {
|
|||
* rule -
|
||||
* head -
|
||||
*/
|
||||
typedef struct LR {
|
||||
parse_result_t *seed;
|
||||
const parser_t *rule;
|
||||
head_t *head;
|
||||
} LR_t;
|
||||
typedef struct HLeftRec_ {
|
||||
HParseResult *seed;
|
||||
const HParser *rule;
|
||||
HRecursionHead *head;
|
||||
} HLeftRec;
|
||||
|
||||
/* Tagged union for values in the cache: either LR's (Left) or
|
||||
* parse_result_t's (Right).
|
||||
/* Tagged union for values in the cache: either HLeftRec's (Left) or
|
||||
* HParseResult's (Right).
|
||||
*/
|
||||
typedef struct parser_cache_value {
|
||||
parser_cache_value_type_t value_type;
|
||||
typedef struct HParserCacheValue_t {
|
||||
HParserCacheValueType value_type;
|
||||
union {
|
||||
LR_t *left;
|
||||
parse_result_t *right;
|
||||
HLeftRec *left;
|
||||
HParseResult *right;
|
||||
};
|
||||
} parser_cache_value_t;
|
||||
} HParserCacheValue;
|
||||
|
||||
typedef unsigned int *charset;
|
||||
typedef unsigned int *HCharset;
|
||||
|
||||
static inline charset new_charset() {
|
||||
charset cs = g_new0(unsigned int, 256 / sizeof(unsigned int));
|
||||
static inline HCharset new_charset() {
|
||||
HCharset cs = g_new0(unsigned int, 256 / sizeof(unsigned int));
|
||||
return cs;
|
||||
}
|
||||
|
||||
static inline int charset_isset(charset cs, uint8_t pos) {
|
||||
static inline int charset_isset(HCharset cs, uint8_t pos) {
|
||||
return !!(cs[pos / sizeof(*cs)] & (1 << (pos % sizeof(*cs))));
|
||||
}
|
||||
|
||||
static inline void charset_set(charset cs, uint8_t pos, int val) {
|
||||
static inline void charset_set(HCharset cs, uint8_t pos, int val) {
|
||||
cs[pos / sizeof(*cs)] =
|
||||
val
|
||||
? cs[pos / sizeof(*cs)] | (1 << (pos % sizeof(*cs)))
|
||||
|
|
@ -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.
|
||||
|
||||
long long read_bits(input_stream_t* state, int count, char signed_p);
|
||||
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);
|
||||
long long read_bits(HInputStream* state, int count, char signed_p);
|
||||
HParseResult* do_parse(const HParser* parser, HParseState *state);
|
||||
void put_cached(HParseState *ps, const HParser *p, HParseResult *cached);
|
||||
guint djbhash(const uint8_t *buf, size_t len);
|
||||
char* write_result_unamb(const parsed_token_t* tok);
|
||||
void pprint(const parsed_token_t* tok, int indent, int delta);
|
||||
char* write_result_unamb(const HParsedToken* tok);
|
||||
void pprint(const HParsedToken* tok, int indent, int delta);
|
||||
|
||||
counted_array_t *carray_new_sized(arena_t arena, size_t size);
|
||||
counted_array_t *carray_new(arena_t arena);
|
||||
void carray_append(counted_array_t *array, void* item);
|
||||
HCountedArray *carray_new_sized(HArena * arena, size_t size);
|
||||
HCountedArray *carray_new(HArena * arena);
|
||||
void carray_append(HCountedArray *array, void* item);
|
||||
|
||||
#if 0
|
||||
#include <malloc.h>
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ typedef struct pp_state {
|
|||
int at_bol;
|
||||
} 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) {
|
||||
case TT_NONE:
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
int len;
|
||||
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 = {
|
||||
.output = g_malloc0(16),
|
||||
.len = 0,
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@
|
|||
|
||||
// TODO: replace uses of this with g_check_parse_failed
|
||||
#define g_check_failed(res) do { \
|
||||
const parse_result_t *result = (res); \
|
||||
const HParseResult *result = (res); \
|
||||
if (NULL != result) { \
|
||||
g_test_message("Check failed: shouldn't have succeeded, but did"); \
|
||||
g_test_fail(); \
|
||||
|
|
@ -62,7 +62,7 @@
|
|||
} while(0)
|
||||
|
||||
#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) { \
|
||||
g_test_message("Check failed: shouldn't have succeeded, but did"); \
|
||||
g_test_fail(); \
|
||||
|
|
@ -70,7 +70,7 @@
|
|||
} while(0)
|
||||
|
||||
#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) { \
|
||||
g_test_message("Parse failed on line %d", __LINE__); \
|
||||
g_test_fail(); \
|
||||
|
|
@ -78,7 +78,7 @@
|
|||
char* cres = write_result_unamb(res->ast); \
|
||||
g_check_string(cres, ==, result); \
|
||||
g_free(cres); \
|
||||
arena_stats_t stats; \
|
||||
HArenaStats stats; \
|
||||
allocator_stats(res->arena, &stats); \
|
||||
g_test_message("Parse used %zd bytes, wasted %zd bytes. " \
|
||||
"Inefficiency: %5f%%", \
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue