2012-04-23 00:02:42 +01:00
|
|
|
/* Parser combinators for binary formats.
|
|
|
|
|
* Copyright (C) 2012 Meredith L. Patterson, Dan "TQ" Hirsch
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation, version 2.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-04-22 04:47:08 +01:00
|
|
|
#include "hammer.h"
|
2012-05-01 00:33:47 +01:00
|
|
|
#include "internal.h"
|
2012-05-12 21:20:38 +01:00
|
|
|
#include "allocator.h"
|
2012-05-01 00:33:47 +01:00
|
|
|
#include <assert.h>
|
2012-05-12 01:28:52 +01:00
|
|
|
#include <ctype.h>
|
2012-05-17 14:45:09 +02:00
|
|
|
#include <error.h>
|
2012-05-24 13:22:43 +02:00
|
|
|
#include <limits.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <string.h>
|
2012-05-04 21:23:56 +01:00
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
#define a_new_(arena, typ, count) ((typ*)h_arena_malloc((arena), sizeof(typ)*(count)))
|
2012-05-12 21:20:38 +01:00
|
|
|
#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.
|
2012-04-22 04:47:08 +01:00
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
static guint djbhash(const uint8_t *buf, size_t len) {
|
2012-05-03 01:58:09 +01:00
|
|
|
guint hash = 5381;
|
|
|
|
|
while (len--) {
|
|
|
|
|
hash = hash * 33 + *buf++;
|
2012-04-30 03:44:10 +01:00
|
|
|
}
|
|
|
|
|
return hash;
|
2012-04-22 04:47:08 +01:00
|
|
|
}
|
2012-04-22 05:15:40 +01:00
|
|
|
|
2012-05-26 13:13:41 +02:00
|
|
|
HParserCacheValue* recall(HParserCacheKey *k, HParseState *state) {
|
|
|
|
|
HParserCacheValue *cached = g_hash_table_lookup(state->cache, k);
|
|
|
|
|
HRecursionHead *head = g_hash_table_lookup(state->recursion_heads, k);
|
2012-05-17 13:22:56 +02:00
|
|
|
if (!head) { // No heads found
|
|
|
|
|
return cached;
|
|
|
|
|
} else { // Some heads found
|
|
|
|
|
if (!cached && head->head_parser != k->parser && !g_slist_find(head->involved_set, k->parser)) {
|
|
|
|
|
// Nothing in the cache, and the key parser is not involved
|
2012-05-26 13:01:23 +02:00
|
|
|
HParseResult *tmp = g_new(HParseResult, 1);
|
2012-05-17 15:47:14 +02:00
|
|
|
tmp->ast = NULL; tmp->arena = state->arena;
|
2012-05-26 13:13:41 +02:00
|
|
|
HParserCacheValue *ret = g_new(HParserCacheValue, 1);
|
2012-05-17 15:47:14 +02:00
|
|
|
ret->value_type = PC_RIGHT; ret->right = tmp;
|
|
|
|
|
return ret;
|
2012-05-17 13:22:56 +02:00
|
|
|
}
|
|
|
|
|
if (g_slist_find(head->eval_set, k->parser)) {
|
|
|
|
|
// Something is in the cache, and the key parser is in the eval set. Remove the key parser from the eval set of the head.
|
|
|
|
|
head->eval_set = g_slist_remove_all(head->eval_set, k->parser);
|
2012-05-26 15:15:38 +02:00
|
|
|
HParseResult *tmp_res = k->parser->vtable->parse(k->parser->env, state);
|
2012-05-17 13:22:56 +02:00
|
|
|
if (tmp_res)
|
|
|
|
|
tmp_res->arena = state->arena;
|
|
|
|
|
// we know that cached has an entry here, modify it
|
|
|
|
|
cached->value_type = PC_RIGHT;
|
|
|
|
|
cached->right = tmp_res;
|
|
|
|
|
}
|
|
|
|
|
return cached;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-17 14:05:10 +02:00
|
|
|
/* Setting up the left recursion. We have the LR for the rule head;
|
|
|
|
|
* we modify the involved_sets of all LRs in the stack, until we
|
|
|
|
|
* see the current parser again.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-05-26 13:13:41 +02:00
|
|
|
void setupLR(const HParser *p, GQueue *stack, HLeftRec *rec_detect) {
|
2012-05-17 13:22:56 +02:00
|
|
|
if (!rec_detect->head) {
|
2012-05-26 13:13:41 +02:00
|
|
|
HRecursionHead *some = g_new(HRecursionHead, 1);
|
2012-05-17 13:22:56 +02:00
|
|
|
some->head_parser = p; some->involved_set = NULL; some->eval_set = NULL;
|
|
|
|
|
rec_detect->head = some;
|
|
|
|
|
}
|
2012-05-17 14:05:10 +02:00
|
|
|
size_t i = 0;
|
2012-05-26 13:13:41 +02:00
|
|
|
HLeftRec *lr = g_queue_peek_nth(stack, i);
|
2012-05-17 14:05:10 +02:00
|
|
|
while (lr && lr->rule != p) {
|
|
|
|
|
lr->head = rec_detect->head;
|
|
|
|
|
lr->head->involved_set = g_slist_prepend(lr->head->involved_set, (gpointer)lr->rule);
|
|
|
|
|
}
|
2012-05-13 01:18:18 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-17 14:45:09 +02:00
|
|
|
/* If recall() returns NULL, we need to store a dummy failure in the cache and compute the
|
|
|
|
|
* future parse.
|
|
|
|
|
*/
|
2012-05-13 01:18:18 +01:00
|
|
|
|
2012-05-26 13:13:41 +02:00
|
|
|
HParseResult* grow(HParserCacheKey *k, HParseState *state, HRecursionHead *head) {
|
2012-05-17 15:47:14 +02:00
|
|
|
// Store the head into the recursion_heads
|
|
|
|
|
g_hash_table_replace(state->recursion_heads, k, head);
|
2012-05-26 13:13:41 +02:00
|
|
|
HParserCacheValue *old_cached = g_hash_table_lookup(state->cache, k);
|
2012-05-17 15:47:14 +02:00
|
|
|
if (!old_cached || PC_LEFT == old_cached->value_type)
|
|
|
|
|
errx(1, "impossible match");
|
2012-05-26 13:01:23 +02:00
|
|
|
HParseResult *old_res = old_cached->right;
|
2012-05-03 01:58:09 +01:00
|
|
|
|
2012-05-17 15:47:14 +02:00
|
|
|
// reset the eval_set of the head of the recursion at each beginning of growth
|
|
|
|
|
head->eval_set = head->involved_set;
|
2012-05-26 13:01:23 +02:00
|
|
|
HParseResult *tmp_res;
|
2012-05-17 15:47:14 +02:00
|
|
|
if (k->parser) {
|
2012-05-26 15:15:38 +02:00
|
|
|
tmp_res = k->parser->vtable->parse(k->parser->env, state);
|
2012-05-17 15:47:14 +02:00
|
|
|
if (tmp_res)
|
|
|
|
|
tmp_res->arena = state->arena;
|
|
|
|
|
} else
|
|
|
|
|
tmp_res = NULL;
|
|
|
|
|
if (tmp_res) {
|
|
|
|
|
if ((old_res->ast->index < tmp_res->ast->index) ||
|
|
|
|
|
(old_res->ast->index == tmp_res->ast->index && old_res->ast->bit_offset < tmp_res->ast->bit_offset)) {
|
2012-05-26 13:13:41 +02:00
|
|
|
HParserCacheValue *v = g_new(HParserCacheValue, 1);
|
2012-05-17 15:47:14 +02:00
|
|
|
v->value_type = PC_RIGHT; v->right = tmp_res;
|
|
|
|
|
g_hash_table_replace(state->cache, k, v);
|
|
|
|
|
return grow(k, state, head);
|
|
|
|
|
} else {
|
|
|
|
|
// we're done with growing, we can remove data from the recursion head
|
|
|
|
|
g_hash_table_remove(state->recursion_heads, k);
|
2012-05-26 13:13:41 +02:00
|
|
|
HParserCacheValue *cached = g_hash_table_lookup(state->cache, k);
|
2012-05-17 15:47:14 +02:00
|
|
|
if (cached && PC_RIGHT == cached->value_type) {
|
|
|
|
|
return cached->right;
|
|
|
|
|
} else {
|
|
|
|
|
errx(1, "impossible match");
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-04-30 03:44:10 +01:00
|
|
|
} else {
|
2012-05-17 15:47:14 +02:00
|
|
|
g_hash_table_remove(state->recursion_heads, k);
|
|
|
|
|
return old_res;
|
|
|
|
|
}
|
2012-05-13 01:18:18 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-26 13:13:41 +02:00
|
|
|
HParseResult* lr_answer(HParserCacheKey *k, HParseState *state, HLeftRec *growable) {
|
2012-05-17 14:45:09 +02:00
|
|
|
if (growable->head) {
|
|
|
|
|
if (growable->head->head_parser != k->parser) {
|
|
|
|
|
// not the head rule, so not growing
|
|
|
|
|
return growable->seed;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// update cache
|
2012-05-26 13:13:41 +02:00
|
|
|
HParserCacheValue *v = g_new(HParserCacheValue, 1);
|
2012-05-17 14:45:09 +02:00
|
|
|
v->value_type = PC_RIGHT; v->right = growable->seed;
|
|
|
|
|
g_hash_table_replace(state->cache, k, v);
|
|
|
|
|
if (!growable->seed)
|
|
|
|
|
return NULL;
|
|
|
|
|
else
|
2012-05-17 15:47:14 +02:00
|
|
|
return grow(k, state, growable->head);
|
2012-05-17 14:45:09 +02:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
errx(1, "lrAnswer with no head");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-17 13:22:56 +02:00
|
|
|
/* Warth's recursion. Hi Alessandro! */
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult* h_do_parse(const HParser* parser, HParseState *state) {
|
2012-05-26 13:13:41 +02:00
|
|
|
HParserCacheKey *key = a_new(HParserCacheKey, 1);
|
2012-05-17 15:47:14 +02:00
|
|
|
key->input_pos = state->input_stream; key->parser = parser;
|
2012-05-26 13:13:41 +02:00
|
|
|
HParserCacheValue *m = recall(key, state);
|
2012-05-03 01:58:09 +01:00
|
|
|
// check to see if there is already a result for this object...
|
2012-05-17 15:47:14 +02:00
|
|
|
if (!m) {
|
2012-05-13 01:18:18 +01:00
|
|
|
// It doesn't exist, so create a dummy result to cache
|
2012-05-26 13:13:41 +02:00
|
|
|
HLeftRec *base = a_new(HLeftRec, 1);
|
2012-05-13 01:18:18 +01:00
|
|
|
base->seed = NULL; base->rule = parser; base->head = NULL;
|
2012-05-13 01:25:45 +01:00
|
|
|
g_queue_push_head(state->lr_stack, base);
|
2012-05-13 01:18:18 +01:00
|
|
|
// cache it
|
2012-05-26 13:13:41 +02:00
|
|
|
HParserCacheValue *dummy = a_new(HParserCacheValue, 1);
|
2012-05-13 01:18:18 +01:00
|
|
|
dummy->value_type = PC_LEFT; dummy->left = base;
|
|
|
|
|
g_hash_table_replace(state->cache, key, dummy);
|
|
|
|
|
// parse the input
|
2012-05-26 13:01:23 +02:00
|
|
|
HParseResult *tmp_res;
|
2012-05-12 23:04:54 +01:00
|
|
|
if (parser) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HInputStream bak = state->input_stream;
|
2012-05-26 15:15:38 +02:00
|
|
|
tmp_res = parser->vtable->parse(parser->env, state);
|
2012-05-24 15:35:04 +02:00
|
|
|
if (tmp_res) {
|
2012-05-17 13:22:56 +02:00
|
|
|
tmp_res->arena = state->arena;
|
2012-05-24 15:35:04 +02:00
|
|
|
if (!state->input_stream.overrun) {
|
|
|
|
|
tmp_res->bit_length = ((state->input_stream.index - bak.index) << 3);
|
|
|
|
|
if (state->input_stream.endianness & BIT_BIG_ENDIAN)
|
|
|
|
|
tmp_res->bit_length += state->input_stream.bit_offset - bak.bit_offset;
|
|
|
|
|
else
|
|
|
|
|
tmp_res->bit_length += bak.bit_offset - state->input_stream.bit_offset;
|
|
|
|
|
} else
|
|
|
|
|
tmp_res->bit_length = 0;
|
|
|
|
|
}
|
2012-05-12 23:04:54 +01:00
|
|
|
} else
|
2012-05-13 01:18:18 +01:00
|
|
|
tmp_res = NULL;
|
2012-05-11 23:35:21 +01:00
|
|
|
if (state->input_stream.overrun)
|
2012-05-13 01:18:18 +01:00
|
|
|
return NULL; // overrun is always failure.
|
2012-05-04 21:23:56 +01:00
|
|
|
#ifdef CONSISTENCY_CHECK
|
2012-05-13 01:18:18 +01:00
|
|
|
if (!tmp_res) {
|
2012-05-04 21:23:56 +01:00
|
|
|
state->input_stream = INVALID;
|
2012-05-13 01:18:18 +01:00
|
|
|
state->input_stream.input = key->input_pos.input;
|
2012-05-04 21:23:56 +01:00
|
|
|
}
|
|
|
|
|
#endif
|
2012-05-13 01:18:18 +01:00
|
|
|
// the base variable has passed equality tests with the cache
|
2012-05-13 01:25:45 +01:00
|
|
|
g_queue_pop_head(state->lr_stack);
|
2012-05-13 01:18:18 +01:00
|
|
|
// setupLR, used below, mutates the LR to have a head if appropriate, so we check to see if we have one
|
|
|
|
|
if (NULL == base->head) {
|
2012-05-26 13:13:41 +02:00
|
|
|
HParserCacheValue *right = a_new(HParserCacheValue, 1);
|
2012-05-13 01:18:18 +01:00
|
|
|
right->value_type = PC_RIGHT; right->right = tmp_res;
|
|
|
|
|
g_hash_table_replace(state->cache, key, right);
|
|
|
|
|
return tmp_res;
|
|
|
|
|
} else {
|
|
|
|
|
base->seed = tmp_res;
|
2012-05-26 13:01:23 +02:00
|
|
|
HParseResult *res = lr_answer(key, state, base);
|
2012-05-13 01:18:18 +01:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// it exists!
|
2012-05-17 15:47:14 +02:00
|
|
|
if (PC_LEFT == m->value_type) {
|
|
|
|
|
setupLR(parser, state->lr_stack, m->left);
|
|
|
|
|
return m->left->seed; // BUG: this might not be correct
|
2012-05-13 01:18:18 +01:00
|
|
|
} else {
|
2012-05-17 15:47:14 +02:00
|
|
|
return m->right;
|
2012-05-13 01:18:18 +01:00
|
|
|
}
|
2012-04-30 03:44:10 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-01 00:33:47 +01:00
|
|
|
/* Helper function, since these lines appear in every parser */
|
2012-05-26 14:06:52 +02:00
|
|
|
static HParseResult* make_result(HParseState *state, HParsedToken *tok) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParseResult *ret = a_new(HParseResult, 1);
|
2012-05-01 03:21:14 +01:00
|
|
|
ret->ast = tok;
|
2012-05-12 21:53:03 +01:00
|
|
|
ret->arena = state->arena;
|
2012-05-01 00:33:47 +01:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
uint8_t *str;
|
|
|
|
|
uint8_t len;
|
2012-05-26 13:01:23 +02:00
|
|
|
} HToken;
|
2012-05-01 00:33:47 +01:00
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_unimplemented(void* env, HParseState *state) {
|
2012-05-12 21:53:54 +01:00
|
|
|
(void) env;
|
|
|
|
|
(void) state;
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParsedToken token = {
|
2012-05-12 21:53:54 +01:00
|
|
|
.token_type = TT_ERR
|
|
|
|
|
};
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult result = {
|
2012-05-12 21:53:54 +01:00
|
|
|
.ast = &token
|
|
|
|
|
};
|
|
|
|
|
return &result;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable unimplemented_vt = {
|
|
|
|
|
.parse = parse_unimplemented,
|
|
|
|
|
};
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParser unimplemented __attribute__((unused)) = {
|
2012-05-26 15:15:38 +02:00
|
|
|
.vtable = &unimplemented_vt,
|
2012-05-12 21:53:54 +01:00
|
|
|
.env = NULL
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
struct bits_env {
|
|
|
|
|
uint8_t length;
|
|
|
|
|
uint8_t signedp;
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_bits(void* env, HParseState *state) {
|
2012-05-18 18:43:02 +02:00
|
|
|
struct bits_env *env_ = env;
|
2012-05-26 13:01:23 +02:00
|
|
|
HParsedToken *result = a_new(HParsedToken, 1);
|
2012-05-18 18:43:02 +02:00
|
|
|
result->token_type = (env_->signedp ? TT_SINT : TT_UINT);
|
|
|
|
|
if (env_->signedp)
|
2012-05-26 14:06:52 +02:00
|
|
|
result->sint = h_read_bits(&state->input_stream, env_->length, true);
|
2012-05-18 18:43:02 +02:00
|
|
|
else
|
2012-05-26 14:06:52 +02:00
|
|
|
result->uint = h_read_bits(&state->input_stream, env_->length, false);
|
2012-05-18 18:43:02 +02:00
|
|
|
return make_result(state, result);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable bits_vt = {
|
|
|
|
|
.parse = parse_bits,
|
|
|
|
|
};
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_bits(size_t len, bool sign) {
|
2012-05-18 18:43:02 +02:00
|
|
|
struct bits_env *env = g_new(struct bits_env, 1);
|
|
|
|
|
env->length = len;
|
|
|
|
|
env->signedp = sign;
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *res = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
res->vtable = &bits_vt;
|
2012-05-18 18:43:02 +02:00
|
|
|
res->env = env;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define SIZED_BITS(name_pre, len, signedp) \
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_##name_pre##len () { \
|
|
|
|
|
return h_bits(len, signedp); \
|
2012-05-18 18:43:02 +02:00
|
|
|
}
|
|
|
|
|
SIZED_BITS(int, 8, true)
|
|
|
|
|
SIZED_BITS(int, 16, true)
|
|
|
|
|
SIZED_BITS(int, 32, true)
|
|
|
|
|
SIZED_BITS(int, 64, true)
|
|
|
|
|
SIZED_BITS(uint, 8, false)
|
|
|
|
|
SIZED_BITS(uint, 16, false)
|
|
|
|
|
SIZED_BITS(uint, 32, false)
|
|
|
|
|
SIZED_BITS(uint, 64, false)
|
|
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_token(void *env, HParseState *state) {
|
|
|
|
|
HToken *t = (HToken*)env;
|
2012-05-01 00:33:47 +01:00
|
|
|
for (int i=0; i<t->len; ++i) {
|
2012-05-26 14:06:52 +02:00
|
|
|
uint8_t chr = (uint8_t)h_read_bits(&state->input_stream, 8, false);
|
2012-05-01 00:33:47 +01:00
|
|
|
if (t->str[i] != chr) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-05-26 13:01:23 +02:00
|
|
|
HParsedToken *tok = a_new(HParsedToken, 1);
|
2012-05-01 03:21:14 +01:00
|
|
|
tok->token_type = TT_BYTES; tok->bytes.token = t->str; tok->bytes.len = t->len;
|
2012-05-12 21:20:38 +01:00
|
|
|
return make_result(state, tok);
|
2012-05-01 00:33:47 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
const const HParserVtable token_vt = {
|
|
|
|
|
.parse = parse_token,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_token(const uint8_t *str, const size_t len) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HToken *t = g_new(HToken, 1);
|
2012-05-01 00:33:47 +01:00
|
|
|
t->str = (uint8_t*)str, t->len = len;
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *ret = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
ret->vtable = &token_vt;
|
|
|
|
|
ret->env = t;
|
2012-05-26 13:01:23 +02:00
|
|
|
return (const HParser*)ret;
|
2012-05-01 00:33:47 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_ch(void* env, HParseState *state) {
|
2012-05-01 02:05:58 +01:00
|
|
|
uint8_t c = (uint8_t)GPOINTER_TO_UINT(env);
|
2012-05-26 14:06:52 +02:00
|
|
|
uint8_t r = (uint8_t)h_read_bits(&state->input_stream, 8, false);
|
2012-05-01 00:33:47 +01:00
|
|
|
if (c == r) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParsedToken *tok = a_new(HParsedToken, 1);
|
2012-05-01 03:21:14 +01:00
|
|
|
tok->token_type = TT_UINT; tok->uint = r;
|
2012-05-12 21:20:38 +01:00
|
|
|
return make_result(state, tok);
|
2012-05-01 00:33:47 +01:00
|
|
|
} else {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable ch_vt = {
|
|
|
|
|
.parse = parse_ch,
|
|
|
|
|
};
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_ch(const uint8_t c) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *ret = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
ret->vtable = &ch_vt;
|
|
|
|
|
ret->env = GUINT_TO_POINTER(c);
|
2012-05-26 13:01:23 +02:00
|
|
|
return (const HParser*)ret;
|
2012-05-01 00:33:47 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_whitespace(void* env, HParseState *state) {
|
2012-05-12 01:28:52 +01:00
|
|
|
char c;
|
2012-05-26 13:01:23 +02:00
|
|
|
HInputStream bak;
|
2012-05-12 01:28:52 +01:00
|
|
|
do {
|
|
|
|
|
bak = state->input_stream;
|
2012-05-26 14:06:52 +02:00
|
|
|
c = h_read_bits(&state->input_stream, 8, false);
|
2012-05-12 01:28:52 +01:00
|
|
|
if (state->input_stream.overrun)
|
|
|
|
|
return NULL;
|
|
|
|
|
} while (isspace(c));
|
|
|
|
|
state->input_stream = bak;
|
2012-05-26 14:06:52 +02:00
|
|
|
return h_do_parse((HParser*)env, state);
|
2012-05-12 01:28:52 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable whitespace_vt = {
|
|
|
|
|
.parse = parse_whitespace,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_whitespace(const HParser* p) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *ret = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
ret->vtable = &whitespace_vt;
|
2012-05-12 01:28:52 +01:00
|
|
|
ret->env = (void*)p;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2012-05-01 00:33:47 +01:00
|
|
|
|
2012-05-18 12:35:40 +02:00
|
|
|
typedef struct {
|
2012-05-26 13:01:23 +02:00
|
|
|
const HParser *p;
|
|
|
|
|
HAction action;
|
|
|
|
|
} HParseAction;
|
2012-05-18 12:35:40 +02:00
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_action(void *env, HParseState *state) {
|
|
|
|
|
HParseAction *a = (HParseAction*)env;
|
2012-05-18 12:35:40 +02:00
|
|
|
if (a->p && a->action) {
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *tmp = h_do_parse(a->p, state);
|
|
|
|
|
//HParsedToken *tok = a->action(h_do_parse(a->p, state));
|
2012-05-26 13:01:23 +02:00
|
|
|
const HParsedToken *tok = a->action(tmp);
|
|
|
|
|
return make_result(state, (HParsedToken*)tok);
|
2012-05-18 12:35:40 +02:00
|
|
|
} else // either the parser's missing or the action's missing
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable action_vt = {
|
|
|
|
|
.parse = parse_action,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_action(const HParser* p, const HAction a) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *res = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
res->vtable = &action_vt;
|
2012-05-26 13:01:23 +02:00
|
|
|
HParseAction *env = g_new(HParseAction, 1);
|
2012-05-18 12:35:40 +02:00
|
|
|
env->p = p;
|
|
|
|
|
env->action = a;
|
|
|
|
|
res->env = (void*)env;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2012-05-01 00:33:47 +01:00
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_charset(void *env, HParseState *state) {
|
2012-05-26 14:06:52 +02:00
|
|
|
uint8_t in = h_read_bits(&state->input_stream, 8, false);
|
2012-05-26 13:13:41 +02:00
|
|
|
HCharset cs = (HCharset)env;
|
2012-05-04 21:23:56 +01:00
|
|
|
|
|
|
|
|
if (charset_isset(cs, in)) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParsedToken *tok = a_new(HParsedToken, 1);
|
2012-05-04 21:23:56 +01:00
|
|
|
tok->token_type = TT_UINT; tok->uint = in;
|
2012-05-12 21:20:38 +01:00
|
|
|
return make_result(state, tok);
|
2012-05-04 21:23:56 +01:00
|
|
|
} else
|
2012-05-01 00:33:47 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable charset_vt = {
|
|
|
|
|
.parse = parse_charset,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_ch_range(const uint8_t lower, const uint8_t upper) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *ret = g_new(HParser, 1);
|
2012-05-26 13:13:41 +02:00
|
|
|
HCharset cs = new_charset();
|
2012-05-04 21:23:56 +01:00
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
|
charset_set(cs, i, (lower <= i) && (i <= upper));
|
2012-05-26 15:15:38 +02:00
|
|
|
ret->vtable = &charset_vt;
|
|
|
|
|
ret->env = (void*)cs;
|
2012-05-26 13:01:23 +02:00
|
|
|
return (const HParser*)ret;
|
2012-05-04 21:23:56 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-24 11:01:18 +02:00
|
|
|
typedef struct {
|
2012-05-26 13:01:23 +02:00
|
|
|
const HParser *p;
|
2012-05-24 11:01:18 +02:00
|
|
|
int64_t lower;
|
|
|
|
|
int64_t upper;
|
2012-05-26 13:01:23 +02:00
|
|
|
} HRange;
|
2012-05-24 11:01:18 +02:00
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_int_range(void *env, HParseState *state) {
|
|
|
|
|
HRange *r_env = (HRange*)env;
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *ret = h_do_parse(r_env->p, state);
|
2012-05-24 12:09:38 +02:00
|
|
|
if (!ret || !ret->ast)
|
|
|
|
|
return NULL;
|
|
|
|
|
switch(ret->ast->token_type) {
|
|
|
|
|
case TT_SINT:
|
|
|
|
|
if (r_env->lower <= ret->ast->sint && r_env->upper >= ret->ast->sint)
|
|
|
|
|
return ret;
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
case TT_UINT:
|
|
|
|
|
if ((uint64_t)r_env->lower <= ret->ast->uint && (uint64_t)r_env->upper >= ret->ast->uint)
|
|
|
|
|
return ret;
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
default:
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2012-05-24 12:00:23 +02:00
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable int_range_vt = {
|
|
|
|
|
.parse = parse_int_range,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_int_range(const HParser *p, const int64_t lower, const int64_t upper) {
|
2012-05-24 12:00:23 +02:00
|
|
|
struct bits_env *b_env = p->env;
|
2012-05-24 13:22:43 +02:00
|
|
|
// p must be an integer parser, which means it's using parse_bits
|
2012-05-26 15:15:38 +02:00
|
|
|
assert_message(p->vtable == &bits_vt, "int_range requires an integer parser");
|
2012-05-24 13:22:43 +02:00
|
|
|
// if it's a uint parser, it can't be uint64
|
2012-05-24 12:00:23 +02:00
|
|
|
assert_message(!(b_env->signedp) ? (b_env->length < 64) : true, "int_range can't use a uint64 parser");
|
2012-05-24 13:22:43 +02:00
|
|
|
// and regardless, the bounds need to fit in the parser in question
|
|
|
|
|
switch(b_env->length) {
|
|
|
|
|
case 32:
|
|
|
|
|
if (b_env->signedp)
|
|
|
|
|
assert_message(lower >= INT_MIN && upper <= INT_MAX, "bounds for 32-bit signed integer exceeded");
|
|
|
|
|
else
|
|
|
|
|
assert_message(lower >= 0 && upper <= UINT_MAX, "bounds for 32-bit unsigned integer exceeded");
|
|
|
|
|
break;
|
|
|
|
|
case 16:
|
|
|
|
|
if (b_env->signedp)
|
|
|
|
|
assert_message(lower >= SHRT_MIN && upper <= SHRT_MAX, "bounds for 16-bit signed integer exceeded");
|
|
|
|
|
else
|
|
|
|
|
assert_message(lower >= 0 && upper <= USHRT_MAX, "bounds for 16-bit unsigned integer exceeded");
|
|
|
|
|
break;
|
|
|
|
|
case 8:
|
|
|
|
|
if (b_env->signedp)
|
|
|
|
|
assert_message(lower >= SCHAR_MIN && upper <= SCHAR_MAX, "bounds for 8-bit signed integer exceeded");
|
|
|
|
|
else
|
|
|
|
|
assert_message(lower >= 0 && upper <= UCHAR_MAX, "bounds for 8-bit unsigned integer exceeded");
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// how'd that happen? if we got here, this parser is broken.
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2012-05-24 11:01:18 +02:00
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
HRange *r_env = g_new(HRange, 1);
|
2012-05-24 12:09:38 +02:00
|
|
|
r_env->p = p;
|
2012-05-24 12:00:23 +02:00
|
|
|
r_env->lower = lower;
|
|
|
|
|
r_env->upper = upper;
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *ret = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
ret->vtable = &int_range_vt;
|
2012-05-24 12:00:23 +02:00
|
|
|
ret->env = (void*)r_env;
|
2012-05-24 11:01:18 +02:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_not_in(const uint8_t *options, int count) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *ret = g_new(HParser, 1);
|
2012-05-26 13:13:41 +02:00
|
|
|
HCharset cs = new_charset();
|
2012-05-04 21:23:56 +01:00
|
|
|
for (int i = 0; i < 256; i++)
|
|
|
|
|
charset_set(cs, i, 1);
|
|
|
|
|
for (int i = 0; i < count; i++)
|
2012-05-12 01:28:52 +01:00
|
|
|
charset_set(cs, options[i], 0);
|
2012-05-04 21:23:56 +01:00
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
ret->vtable = &charset_vt;
|
|
|
|
|
ret->env = (void*)cs;
|
2012-05-26 13:01:23 +02:00
|
|
|
return (const HParser*)ret;
|
2012-05-01 00:33:47 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_end(void *env, HParseState *state) {
|
2012-05-01 00:33:47 +01:00
|
|
|
if (state->input_stream.index == state->input_stream.length) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParseResult *ret = a_new(HParseResult, 1);
|
2012-05-01 00:33:47 +01:00
|
|
|
ret->ast = NULL;
|
|
|
|
|
return ret;
|
|
|
|
|
} else {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable end_vt = {
|
|
|
|
|
.parse = parse_end,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_end_p() {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *ret = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
ret->vtable = &end_vt; ret->env = NULL;
|
2012-05-26 13:01:23 +02:00
|
|
|
return (const HParser*)ret;
|
2012-05-01 00:33:47 +01:00
|
|
|
}
|
2012-05-11 23:38:45 +01:00
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_nothing() {
|
2012-05-01 00:33:47 +01:00
|
|
|
// not a mistake, this parser always fails
|
2012-05-11 23:38:45 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable nothing_vt = {
|
|
|
|
|
.parse = parse_nothing,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_nothing_p() {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *ret = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
ret->vtable = ¬hing_vt; ret->env = NULL;
|
2012-05-26 13:01:23 +02:00
|
|
|
return (const HParser*)ret;
|
2012-05-01 00:33:47 +01:00
|
|
|
}
|
2012-05-01 03:21:14 +01:00
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
size_t len;
|
2012-05-26 13:01:23 +02:00
|
|
|
const HParser **p_array;
|
|
|
|
|
} HSequence;
|
2012-05-01 03:21:14 +01:00
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_sequence(void *env, HParseState *state) {
|
|
|
|
|
HSequence *s = (HSequence*)env;
|
2012-05-26 14:06:52 +02:00
|
|
|
HCountedArray *seq = h_carray_new_sized(state->arena, (s->len > 0) ? s->len : 4);
|
2012-05-01 03:57:37 +01:00
|
|
|
for (size_t i=0; i<s->len; ++i) {
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *tmp = h_do_parse(s->p_array[i], state);
|
2012-05-03 01:40:23 +01:00
|
|
|
// if the interim parse fails, the whole thing fails
|
|
|
|
|
if (NULL == tmp) {
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
2012-05-12 01:28:52 +01:00
|
|
|
if (tmp->ast)
|
2012-05-26 14:06:52 +02:00
|
|
|
h_carray_append(seq, (void*)tmp->ast);
|
2012-05-03 01:40:23 +01:00
|
|
|
}
|
2012-05-01 03:21:14 +01:00
|
|
|
}
|
2012-05-26 13:01:23 +02:00
|
|
|
HParsedToken *tok = a_new(HParsedToken, 1);
|
2012-05-01 03:21:14 +01:00
|
|
|
tok->token_type = TT_SEQUENCE; tok->seq = seq;
|
2012-05-12 21:20:38 +01:00
|
|
|
return make_result(state, tok);
|
2012-05-01 03:21:14 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable sequence_vt = {
|
|
|
|
|
.parse = parse_sequence,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_sequence(const HParser *p, ...) {
|
2012-05-12 00:40:54 +01:00
|
|
|
va_list ap;
|
|
|
|
|
size_t len = 0;
|
2012-05-26 13:01:23 +02:00
|
|
|
const HParser *arg;
|
2012-05-12 00:40:54 +01:00
|
|
|
va_start(ap, p);
|
|
|
|
|
do {
|
|
|
|
|
len++;
|
2012-05-26 13:01:23 +02:00
|
|
|
arg = va_arg(ap, const HParser *);
|
2012-05-12 00:40:54 +01:00
|
|
|
} while (arg);
|
|
|
|
|
va_end(ap);
|
2012-05-26 13:01:23 +02:00
|
|
|
HSequence *s = g_new(HSequence, 1);
|
|
|
|
|
s->p_array = g_new(const HParser *, len);
|
2012-05-12 00:40:54 +01:00
|
|
|
|
|
|
|
|
va_start(ap, p);
|
|
|
|
|
s->p_array[0] = p;
|
|
|
|
|
for (size_t i = 1; i < len; i++) {
|
2012-05-26 13:01:23 +02:00
|
|
|
s->p_array[i] = va_arg(ap, const HParser *);
|
2012-05-12 00:40:54 +01:00
|
|
|
} while (arg);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
|
|
s->len = len;
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *ret = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
ret->vtable = &sequence_vt; ret->env = (void*)s;
|
2012-05-01 03:21:14 +01:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_choice(void *env, HParseState *state) {
|
|
|
|
|
HSequence *s = (HSequence*)env;
|
|
|
|
|
HInputStream backup = state->input_stream;
|
2012-05-01 03:57:37 +01:00
|
|
|
for (size_t i=0; i<s->len; ++i) {
|
2012-05-04 21:23:56 +01:00
|
|
|
if (i != 0)
|
|
|
|
|
state->input_stream = backup;
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *tmp = h_do_parse(s->p_array[i], state);
|
2012-05-01 03:21:14 +01:00
|
|
|
if (NULL != tmp)
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
// nothing succeeded, so fail
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable choice_vt = {
|
|
|
|
|
.parse = parse_choice,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_choice(const HParser* p, ...) {
|
2012-05-12 00:40:54 +01:00
|
|
|
va_list ap;
|
|
|
|
|
size_t len = 0;
|
2012-05-26 13:01:23 +02:00
|
|
|
HSequence *s = g_new(HSequence, 1);
|
2012-05-12 00:40:54 +01:00
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
const HParser *arg;
|
2012-05-12 00:40:54 +01:00
|
|
|
va_start(ap, p);
|
|
|
|
|
do {
|
|
|
|
|
len++;
|
2012-05-26 13:01:23 +02:00
|
|
|
arg = va_arg(ap, const HParser *);
|
2012-05-12 00:40:54 +01:00
|
|
|
} while (arg);
|
|
|
|
|
va_end(ap);
|
2012-05-26 13:01:23 +02:00
|
|
|
s->p_array = g_new(const HParser *, len);
|
2012-05-12 00:40:54 +01:00
|
|
|
|
|
|
|
|
va_start(ap, p);
|
|
|
|
|
s->p_array[0] = p;
|
|
|
|
|
for (size_t i = 1; i < len; i++) {
|
2012-05-26 13:01:23 +02:00
|
|
|
s->p_array[i] = va_arg(ap, const HParser *);
|
2012-05-12 00:40:54 +01:00
|
|
|
} while (arg);
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
|
|
s->len = len;
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *ret = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
ret->vtable = &choice_vt; ret->env = (void*)s;
|
2012-05-01 03:21:14 +01:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-01 03:59:49 +01:00
|
|
|
typedef struct {
|
2012-05-26 13:01:23 +02:00
|
|
|
const HParser *p1;
|
|
|
|
|
const HParser *p2;
|
|
|
|
|
} HTwoParsers;
|
2012-05-01 03:59:49 +01:00
|
|
|
|
2012-05-24 15:35:04 +02:00
|
|
|
// return token size in bits...
|
2012-05-26 13:01:23 +02:00
|
|
|
size_t token_length(HParseResult *pr) {
|
2012-05-24 15:35:04 +02:00
|
|
|
if (pr) {
|
|
|
|
|
return pr->bit_length;
|
2012-05-03 01:40:23 +01:00
|
|
|
} else {
|
2012-05-24 15:35:04 +02:00
|
|
|
return 0;
|
2012-05-03 01:40:23 +01:00
|
|
|
}
|
|
|
|
|
}
|
2012-05-01 03:59:49 +01:00
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_butnot(void *env, HParseState *state) {
|
|
|
|
|
HTwoParsers *parsers = (HTwoParsers*)env;
|
2012-05-03 01:40:23 +01:00
|
|
|
// cache the initial state of the input stream
|
2012-05-26 13:01:23 +02:00
|
|
|
HInputStream start_state = state->input_stream;
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *r1 = h_do_parse(parsers->p1, state);
|
2012-05-03 02:31:22 +01:00
|
|
|
// if p1 failed, bail out early
|
2012-05-03 01:40:23 +01:00
|
|
|
if (NULL == r1) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
// cache the state after parse #1, since we might have to back up to it
|
2012-05-26 13:01:23 +02:00
|
|
|
HInputStream after_p1_state = state->input_stream;
|
2012-05-03 01:40:23 +01:00
|
|
|
state->input_stream = start_state;
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *r2 = h_do_parse(parsers->p2, state);
|
2012-05-03 01:40:23 +01:00
|
|
|
// TODO(mlp): I'm pretty sure the input stream state should be the post-p1 state in all cases
|
|
|
|
|
state->input_stream = after_p1_state;
|
2012-05-03 02:31:22 +01:00
|
|
|
// if p2 failed, restore post-p1 state and bail out early
|
2012-05-03 01:40:23 +01:00
|
|
|
if (NULL == r2) {
|
|
|
|
|
return r1;
|
|
|
|
|
}
|
|
|
|
|
size_t r1len = token_length(r1);
|
|
|
|
|
size_t r2len = token_length(r2);
|
2012-05-24 15:35:04 +02:00
|
|
|
// if both match but p1's text is shorter than than p2's (or the same length), fail
|
|
|
|
|
if (r1len <= r2len) {
|
2012-05-03 01:40:23 +01:00
|
|
|
return NULL;
|
|
|
|
|
} else {
|
|
|
|
|
return r1;
|
|
|
|
|
}
|
2012-05-01 03:59:49 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable butnot_vt = {
|
|
|
|
|
.parse = parse_butnot,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_butnot(const HParser* p1, const HParser* p2) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HTwoParsers *env = g_new(HTwoParsers, 1);
|
2012-05-01 03:59:49 +01:00
|
|
|
env->p1 = p1; env->p2 = p2;
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *ret = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
ret->vtable = &butnot_vt; ret->env = (void*)env;
|
2012-05-03 01:40:23 +01:00
|
|
|
return ret;
|
2012-05-01 03:59:49 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_difference(void *env, HParseState *state) {
|
|
|
|
|
HTwoParsers *parsers = (HTwoParsers*)env;
|
2012-05-03 02:31:22 +01:00
|
|
|
// cache the initial state of the input stream
|
2012-05-26 13:01:23 +02:00
|
|
|
HInputStream start_state = state->input_stream;
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *r1 = h_do_parse(parsers->p1, state);
|
2012-05-03 02:31:22 +01:00
|
|
|
// if p1 failed, bail out early
|
|
|
|
|
if (NULL == r1) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
// cache the state after parse #1, since we might have to back up to it
|
2012-05-26 13:01:23 +02:00
|
|
|
HInputStream after_p1_state = state->input_stream;
|
2012-05-03 02:31:22 +01:00
|
|
|
state->input_stream = start_state;
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *r2 = h_do_parse(parsers->p2, state);
|
2012-05-03 02:31:22 +01:00
|
|
|
// TODO(mlp): I'm pretty sure the input stream state should be the post-p1 state in all cases
|
|
|
|
|
state->input_stream = after_p1_state;
|
|
|
|
|
// if p2 failed, restore post-p1 state and bail out early
|
|
|
|
|
if (NULL == r2) {
|
|
|
|
|
return r1;
|
|
|
|
|
}
|
|
|
|
|
size_t r1len = token_length(r1);
|
|
|
|
|
size_t r2len = token_length(r2);
|
|
|
|
|
// if both match but p1's text is shorter than p2's, fail
|
|
|
|
|
if (r1len < r2len) {
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
|
|
|
|
return r1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static HParserVtable difference_vt = {
|
|
|
|
|
.parse = parse_difference,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_difference(const HParser* p1, const HParser* p2) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HTwoParsers *env = g_new(HTwoParsers, 1);
|
2012-05-03 02:31:22 +01:00
|
|
|
env->p1 = p1; env->p2 = p2;
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *ret = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
ret->vtable = &difference_vt; ret->env = (void*)env;
|
2012-05-03 02:31:22 +01:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_xor(void *env, HParseState *state) {
|
|
|
|
|
HTwoParsers *parsers = (HTwoParsers*)env;
|
2012-05-03 02:31:22 +01:00
|
|
|
// cache the initial state of the input stream
|
2012-05-26 13:01:23 +02:00
|
|
|
HInputStream start_state = state->input_stream;
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *r1 = h_do_parse(parsers->p1, state);
|
2012-05-26 13:01:23 +02:00
|
|
|
HInputStream after_p1_state = state->input_stream;
|
2012-05-03 02:31:22 +01:00
|
|
|
// reset input stream, parse again
|
|
|
|
|
state->input_stream = start_state;
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *r2 = h_do_parse(parsers->p2, state);
|
2012-05-03 02:31:22 +01:00
|
|
|
if (NULL == r1) {
|
|
|
|
|
if (NULL != r2) {
|
|
|
|
|
return r2;
|
|
|
|
|
} else {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (NULL == r2) {
|
|
|
|
|
state->input_stream = after_p1_state;
|
|
|
|
|
return r1;
|
|
|
|
|
} else {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable xor_vt = {
|
|
|
|
|
.parse = parse_xor,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_xor(const HParser* p1, const HParser* p2) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HTwoParsers *env = g_new(HTwoParsers, 1);
|
2012-05-03 02:31:22 +01:00
|
|
|
env->p1 = p1; env->p2 = p2;
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *ret = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
ret->vtable = &xor_vt; ret->env = (void*)env;
|
2012-05-03 02:31:22 +01:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-13 01:01:26 +01:00
|
|
|
typedef struct {
|
2012-05-26 13:01:23 +02:00
|
|
|
const HParser *p, *sep;
|
2012-05-13 01:01:26 +01:00
|
|
|
size_t count;
|
|
|
|
|
bool min_p;
|
2012-05-26 13:01:23 +02:00
|
|
|
} HRepeat;
|
2012-05-17 15:51:19 +02:00
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult *parse_many(void* env, HParseState *state) {
|
|
|
|
|
HRepeat *env_ = (HRepeat*) env;
|
2012-05-26 14:06:52 +02:00
|
|
|
HCountedArray *seq = h_carray_new_sized(state->arena, (env_->count > 0 ? env_->count : 4));
|
2012-05-13 01:01:26 +01:00
|
|
|
size_t count = 0;
|
2012-05-26 13:01:23 +02:00
|
|
|
HInputStream bak;
|
2012-05-13 01:01:26 +01:00
|
|
|
while (env_->min_p || env_->count > count) {
|
|
|
|
|
bak = state->input_stream;
|
|
|
|
|
if (count > 0) {
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *sep = h_do_parse(env_->sep, state);
|
2012-05-13 01:01:26 +01:00
|
|
|
if (!sep)
|
|
|
|
|
goto err0;
|
|
|
|
|
}
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *elem = h_do_parse(env_->p, state);
|
2012-05-13 01:01:26 +01:00
|
|
|
if (!elem)
|
|
|
|
|
goto err0;
|
|
|
|
|
if (elem->ast)
|
2012-05-26 14:06:52 +02:00
|
|
|
h_carray_append(seq, (void*)elem->ast);
|
2012-05-13 01:01:26 +01:00
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
if (count < env_->count)
|
|
|
|
|
goto err;
|
|
|
|
|
succ:
|
|
|
|
|
; // necessary for the label to be here...
|
2012-05-26 13:01:23 +02:00
|
|
|
HParsedToken *res = a_new(HParsedToken, 1);
|
2012-05-13 01:01:26 +01:00
|
|
|
res->token_type = TT_SEQUENCE;
|
|
|
|
|
res->seq = seq;
|
|
|
|
|
return make_result(state, res);
|
|
|
|
|
err0:
|
|
|
|
|
if (count >= env_->count) {
|
|
|
|
|
state->input_stream = bak;
|
|
|
|
|
goto succ;
|
|
|
|
|
}
|
|
|
|
|
err:
|
|
|
|
|
state->input_stream = bak;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2012-05-12 23:24:39 +01:00
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable many_vt = {
|
|
|
|
|
.parse = parse_many,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_many(const HParser* p) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *res = g_new(HParser, 1);
|
|
|
|
|
HRepeat *env = g_new(HRepeat, 1);
|
2012-05-13 01:01:26 +01:00
|
|
|
env->p = p;
|
2012-05-26 14:06:52 +02:00
|
|
|
env->sep = h_epsilon_p();
|
2012-05-13 01:01:26 +01:00
|
|
|
env->count = 0;
|
|
|
|
|
env->min_p = true;
|
2012-05-26 15:15:38 +02:00
|
|
|
res->vtable = &many_vt;
|
2012-05-13 01:01:26 +01:00
|
|
|
res->env = env;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_many1(const HParser* p) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *res = g_new(HParser, 1);
|
|
|
|
|
HRepeat *env = g_new(HRepeat, 1);
|
2012-05-13 01:01:26 +01:00
|
|
|
env->p = p;
|
2012-05-26 14:06:52 +02:00
|
|
|
env->sep = h_epsilon_p();
|
2012-05-13 01:01:26 +01:00
|
|
|
env->count = 1;
|
|
|
|
|
env->min_p = true;
|
2012-05-26 15:15:38 +02:00
|
|
|
res->vtable = &many_vt;
|
2012-05-13 01:01:26 +01:00
|
|
|
res->env = env;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2012-05-12 23:24:39 +01:00
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_repeat_n(const HParser* p, const size_t n) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *res = g_new(HParser, 1);
|
|
|
|
|
HRepeat *env = g_new(HRepeat, 1);
|
2012-05-13 01:01:26 +01:00
|
|
|
env->p = p;
|
2012-05-26 14:06:52 +02:00
|
|
|
env->sep = h_epsilon_p();
|
2012-05-13 01:01:26 +01:00
|
|
|
env->count = n;
|
|
|
|
|
env->min_p = false;
|
2012-05-26 15:15:38 +02:00
|
|
|
res->vtable = &many_vt;
|
2012-05-13 01:01:26 +01:00
|
|
|
res->env = env;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_ignore(void* env, HParseState* state) {
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *res0 = h_do_parse((HParser*)env, state);
|
2012-05-13 01:01:26 +01:00
|
|
|
if (!res0)
|
|
|
|
|
return NULL;
|
2012-05-26 13:01:23 +02:00
|
|
|
HParseResult *res = a_new(HParseResult, 1);
|
2012-05-13 01:01:26 +01:00
|
|
|
res->ast = NULL;
|
|
|
|
|
res->arena = state->arena;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2012-05-26 15:15:38 +02:00
|
|
|
|
|
|
|
|
static const HParserVtable ignore_vt = {
|
|
|
|
|
.parse = parse_ignore,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_ignore(const HParser* p) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser* ret = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
ret->vtable = &ignore_vt;
|
2012-05-13 01:01:26 +01:00
|
|
|
ret->env = (void*)p;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2012-05-12 23:24:39 +01:00
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_optional(void* env, HParseState* state) {
|
|
|
|
|
HInputStream bak = state->input_stream;
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *res0 = h_do_parse((HParser*)env, state);
|
2012-05-12 23:24:39 +01:00
|
|
|
if (res0)
|
|
|
|
|
return res0;
|
|
|
|
|
state->input_stream = bak;
|
2012-05-26 13:01:23 +02:00
|
|
|
HParsedToken *ast = a_new(HParsedToken, 1);
|
2012-05-12 23:24:39 +01:00
|
|
|
ast->token_type = TT_NONE;
|
|
|
|
|
return make_result(state, ast);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable optional_vt = {
|
|
|
|
|
.parse = parse_optional,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_optional(const HParser* p) {
|
2012-05-26 15:15:38 +02:00
|
|
|
assert_message(p->vtable != &ignore_vt, "Thou shalt ignore an option, rather than the other way 'round.");
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *ret = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
ret->vtable = &optional_vt;
|
2012-05-12 23:24:39 +01:00
|
|
|
ret->env = (void*)p;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2012-05-12 23:13:52 +01:00
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_sepBy(const HParser* p, const HParser* sep) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *res = g_new(HParser, 1);
|
|
|
|
|
HRepeat *env = g_new(HRepeat, 1);
|
2012-05-13 01:01:26 +01:00
|
|
|
env->p = p;
|
|
|
|
|
env->sep = sep;
|
|
|
|
|
env->count = 0;
|
|
|
|
|
env->min_p = true;
|
2012-05-26 15:15:38 +02:00
|
|
|
res->vtable = &many_vt;
|
2012-05-13 01:01:26 +01:00
|
|
|
res->env = env;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_sepBy1(const HParser* p, const HParser* sep) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *res = g_new(HParser, 1);
|
|
|
|
|
HRepeat *env = g_new(HRepeat, 1);
|
2012-05-13 01:01:26 +01:00
|
|
|
env->p = p;
|
|
|
|
|
env->sep = sep;
|
|
|
|
|
env->count = 1;
|
|
|
|
|
env->min_p = true;
|
2012-05-26 15:15:38 +02:00
|
|
|
res->vtable = &many_vt;
|
2012-05-13 01:01:26 +01:00
|
|
|
res->env = env;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_epsilon(void* env, HParseState* state) {
|
2012-05-13 01:01:26 +01:00
|
|
|
(void)env;
|
2012-05-26 13:01:23 +02:00
|
|
|
HParseResult* res = a_new(HParseResult, 1);
|
2012-05-12 23:13:52 +01:00
|
|
|
res->ast = NULL;
|
|
|
|
|
res->arena = state->arena;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2012-05-13 01:01:26 +01:00
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable epsilon_vt = {
|
|
|
|
|
.parse = parse_epsilon,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_epsilon_p() {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *res = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
res->vtable = &epsilon_vt;
|
2012-05-13 01:01:26 +01:00
|
|
|
res->env = NULL;
|
|
|
|
|
return res;
|
2012-05-12 23:13:52 +01:00
|
|
|
}
|
2012-05-17 18:27:59 +02:00
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_indirect(void* env, HParseState* state) {
|
2012-05-26 14:06:52 +02:00
|
|
|
return h_do_parse(env, state);
|
2012-05-17 18:27:59 +02:00
|
|
|
}
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable indirect_vt = {
|
|
|
|
|
.parse = parse_indirect,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
void h_bind_indirect(HParser* indirect, HParser* inner) {
|
2012-05-26 15:15:38 +02:00
|
|
|
assert_message(indirect->vtable == &indirect_vt, "You can only bind an indirect parser");
|
2012-05-17 18:27:59 +02:00
|
|
|
indirect->env = inner;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
HParser* h_indirect() {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *res = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
res->vtable = &indirect_vt;
|
2012-05-17 18:27:59 +02:00
|
|
|
res->env = NULL;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-18 11:44:38 +02:00
|
|
|
typedef struct {
|
2012-05-26 13:01:23 +02:00
|
|
|
const HParser *p;
|
|
|
|
|
HPredicate pred;
|
|
|
|
|
} HAttrBool;
|
2012-05-18 11:44:38 +02:00
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_attr_bool(void *env, HParseState *state) {
|
|
|
|
|
HAttrBool *a = (HAttrBool*)env;
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *res = h_do_parse(a->p, state);
|
2012-05-24 11:01:18 +02:00
|
|
|
if (res && res->ast) {
|
2012-05-18 12:18:19 +02:00
|
|
|
if (a->pred(res))
|
|
|
|
|
return res;
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
} else
|
|
|
|
|
return NULL;
|
2012-05-18 11:44:38 +02:00
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable attr_bool_vt = {
|
|
|
|
|
.parse = parse_attr_bool,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_attr_bool(const HParser* p, HPredicate pred) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *res = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
res->vtable = &attr_bool_vt;
|
2012-05-26 13:01:23 +02:00
|
|
|
HAttrBool *env = g_new(HAttrBool, 1);
|
2012-05-18 12:18:19 +02:00
|
|
|
env->p = p;
|
|
|
|
|
env->pred = pred;
|
|
|
|
|
res->env = (void*)env;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2012-05-18 11:44:38 +02:00
|
|
|
|
2012-05-22 02:40:48 +02:00
|
|
|
typedef struct {
|
2012-05-26 13:01:23 +02:00
|
|
|
const HParser *length;
|
|
|
|
|
const HParser *value;
|
|
|
|
|
} HLenVal;
|
2012-05-22 02:40:48 +02:00
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_length_value(void *env, HParseState *state) {
|
|
|
|
|
HLenVal *lv = (HLenVal*)env;
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *len = h_do_parse(lv->length, state);
|
2012-05-22 02:40:48 +02:00
|
|
|
if (!len)
|
|
|
|
|
return NULL;
|
|
|
|
|
if (len->ast->token_type != TT_UINT)
|
|
|
|
|
errx(1, "Length parser must return an unsigned integer");
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser epsilon_local = {
|
2012-05-26 15:15:38 +02:00
|
|
|
.vtable = &epsilon_vt,
|
2012-05-22 02:40:48 +02:00
|
|
|
.env = NULL
|
|
|
|
|
};
|
2012-05-26 13:01:23 +02:00
|
|
|
HRepeat repeat = {
|
2012-05-22 02:40:48 +02:00
|
|
|
.p = lv->value,
|
|
|
|
|
.sep = &epsilon_local,
|
|
|
|
|
.count = len->ast->uint,
|
|
|
|
|
.min_p = false
|
2012-05-22 02:55:00 +02:00
|
|
|
};
|
|
|
|
|
return parse_many(&repeat, state);
|
2012-05-22 02:40:48 +02:00
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable length_value_vt = {
|
|
|
|
|
.parse = parse_length_value,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_length_value(const HParser* length, const HParser* value) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *res = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
res->vtable = &length_value_vt;
|
2012-05-26 13:01:23 +02:00
|
|
|
HLenVal *env = g_new(HLenVal, 1);
|
2012-05-22 02:40:48 +02:00
|
|
|
env->length = length;
|
|
|
|
|
env->value = value;
|
|
|
|
|
res->env = (void*)env;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult *parse_and(void* env, HParseState* state) {
|
|
|
|
|
HInputStream bak = state->input_stream;
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *res = h_do_parse((HParser*)env, state);
|
2012-05-23 15:55:40 +02:00
|
|
|
state->input_stream = bak;
|
|
|
|
|
if (res)
|
|
|
|
|
return make_result(state, NULL);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable and_vt = {
|
|
|
|
|
.parse = parse_and,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_and(const HParser* p) {
|
2012-05-23 15:55:40 +02:00
|
|
|
// zero-width postive lookahead
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *res = g_new(HParser, 1);
|
2012-05-23 15:55:40 +02:00
|
|
|
res->env = (void*)p;
|
2012-05-26 15:15:38 +02:00
|
|
|
res->vtable = &and_vt;
|
2012-05-23 15:55:40 +02:00
|
|
|
return res;
|
|
|
|
|
}
|
2012-05-13 01:01:26 +01:00
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
static HParseResult* parse_not(void* env, HParseState* state) {
|
|
|
|
|
HInputStream bak = state->input_stream;
|
2012-05-26 14:06:52 +02:00
|
|
|
if (h_do_parse((HParser*)env, state))
|
2012-05-13 01:01:26 +01:00
|
|
|
return NULL;
|
|
|
|
|
else {
|
|
|
|
|
state->input_stream = bak;
|
|
|
|
|
return make_result(state, NULL);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-26 15:15:38 +02:00
|
|
|
static const HParserVtable not_vt = {
|
|
|
|
|
.parse = parse_not,
|
|
|
|
|
};
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser* h_not(const HParser* p) {
|
2012-05-26 13:01:23 +02:00
|
|
|
HParser *res = g_new(HParser, 1);
|
2012-05-26 15:15:38 +02:00
|
|
|
res->vtable = ¬_vt;
|
2012-05-13 01:01:26 +01:00
|
|
|
res->env = (void*)p;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2012-04-30 03:44:10 +01:00
|
|
|
|
2012-05-03 01:58:09 +01:00
|
|
|
static guint cache_key_hash(gconstpointer key) {
|
2012-05-26 13:13:41 +02:00
|
|
|
return djbhash(key, sizeof(HParserCacheKey));
|
2012-05-03 01:58:09 +01:00
|
|
|
}
|
|
|
|
|
static gboolean cache_key_equal(gconstpointer key1, gconstpointer key2) {
|
2012-05-26 13:13:41 +02:00
|
|
|
return memcmp(key1, key2, sizeof(HParserCacheKey)) == 0;
|
2012-05-03 01:58:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult* h_parse(const HParser* parser, const uint8_t* input, size_t length) {
|
2012-05-03 01:58:09 +01:00
|
|
|
// Set up a parse state...
|
2012-05-26 14:06:52 +02:00
|
|
|
HArena * arena = h_new_arena(0);
|
2012-05-26 12:03:58 +02:00
|
|
|
HParseState *parse_state = a_new_(arena, HParseState, 1);
|
2012-05-03 01:58:09 +01:00
|
|
|
parse_state->cache = g_hash_table_new(cache_key_hash, // hash_func
|
|
|
|
|
cache_key_equal);// key_equal_func
|
|
|
|
|
parse_state->input_stream.input = input;
|
2012-05-12 21:20:38 +01:00
|
|
|
parse_state->input_stream.index = 0;
|
2012-05-03 01:58:09 +01:00
|
|
|
parse_state->input_stream.bit_offset = 8; // bit big endian
|
2012-05-12 21:20:38 +01:00
|
|
|
parse_state->input_stream.overrun = 0;
|
2012-05-03 01:58:09 +01:00
|
|
|
parse_state->input_stream.endianness = BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN;
|
|
|
|
|
parse_state->input_stream.length = length;
|
2012-05-13 01:25:45 +01:00
|
|
|
parse_state->lr_stack = g_queue_new();
|
2012-05-17 15:47:14 +02:00
|
|
|
parse_state->recursion_heads = g_hash_table_new(cache_key_hash,
|
|
|
|
|
cache_key_equal);
|
2012-05-12 21:20:38 +01:00
|
|
|
parse_state->arena = arena;
|
2012-05-26 14:06:52 +02:00
|
|
|
HParseResult *res = h_do_parse(parser, parse_state);
|
2012-05-17 18:27:59 +02:00
|
|
|
g_queue_free(parse_state->lr_stack);
|
|
|
|
|
g_hash_table_destroy(parse_state->recursion_heads);
|
2012-05-17 15:51:19 +02:00
|
|
|
// tear down the parse state
|
2012-05-03 01:58:09 +01:00
|
|
|
g_hash_table_destroy(parse_state->cache);
|
2012-05-17 15:51:19 +02:00
|
|
|
if (!res)
|
2012-05-26 14:06:52 +02:00
|
|
|
h_delete_arena(parse_state->arena);
|
2012-05-03 01:58:09 +01:00
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2012-05-11 15:14:30 +01:00
|
|
|
|
|
|
|
|
#ifdef INCLUDE_TESTS
|
|
|
|
|
|
2012-05-11 18:37:53 +01:00
|
|
|
#include "test_suite.h"
|
2012-05-11 15:14:30 +01:00
|
|
|
static void test_token(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *token_ = h_token((const uint8_t*)"95\xa2", 3);
|
2012-05-12 11:22:51 +01:00
|
|
|
|
2012-05-13 01:01:26 +01:00
|
|
|
g_check_parse_ok(token_, "95\xa2", 3, "<39.35.a2>");
|
2012-05-12 11:22:51 +01:00
|
|
|
g_check_parse_failed(token_, "95", 2);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_ch(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *ch_ = h_ch(0xa2);
|
2012-05-12 11:22:51 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(ch_, "\xa2", 1, "u0xa2");
|
2012-05-12 11:22:51 +01:00
|
|
|
g_check_parse_failed(ch_, "\xa3", 1);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-24 12:00:23 +02:00
|
|
|
static void test_ch_range(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *range_ = h_ch_range('a', 'c');
|
2012-05-12 11:22:51 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(range_, "b", 1, "u0x62");
|
2012-05-12 11:22:51 +01:00
|
|
|
g_check_parse_failed(range_, "d", 1);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
//@MARK_START
|
2012-05-11 15:14:30 +01:00
|
|
|
static void test_int64(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *int64_ = h_int64();
|
2012-05-12 11:22:51 +01:00
|
|
|
|
2012-05-23 01:06:40 +02:00
|
|
|
g_check_parse_ok(int64_, "\xff\xff\xff\xfe\x00\x00\x00\x00", 8, "s-0x200000000");
|
2012-05-12 11:22:51 +01:00
|
|
|
g_check_parse_failed(int64_, "\xff\xff\xff\xfe\x00\x00\x00", 7);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_int32(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *int32_ = h_int32();
|
2012-05-12 11:22:51 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(int32_, "\xff\xfe\x00\x00", 4, "s-0x20000");
|
2012-05-12 11:22:51 +01:00
|
|
|
g_check_parse_failed(int32_, "\xff\xfe\x00", 3);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_int16(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *int16_ = h_int16();
|
2012-05-12 11:22:51 +01:00
|
|
|
|
2012-05-23 01:02:31 +02:00
|
|
|
g_check_parse_ok(int16_, "\xfe\x00", 2, "s-0x200");
|
2012-05-12 11:22:51 +01:00
|
|
|
g_check_parse_failed(int16_, "\xfe", 1);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_int8(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *int8_ = h_int8();
|
2012-05-12 11:22:51 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(int8_, "\x88", 1, "s-0x78");
|
|
|
|
|
g_check_parse_failed(int8_, "", 0);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_uint64(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *uint64_ = h_uint64();
|
2012-05-12 11:22:51 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(uint64_, "\x00\x00\x00\x02\x00\x00\x00\x00", 8, "u0x200000000");
|
2012-05-12 11:22:51 +01:00
|
|
|
g_check_parse_failed(uint64_, "\x00\x00\x00\x02\x00\x00\x00", 7);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_uint32(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *uint32_ = h_uint32();
|
2012-05-12 11:22:51 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(uint32_, "\x00\x02\x00\x00", 4, "u0x20000");
|
|
|
|
|
g_check_parse_failed(uint32_, "\x00\x02\x00", 3);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_uint16(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *uint16_ = h_uint16();
|
2012-05-12 11:22:51 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(uint16_, "\x02\x00", 2, "u0x200");
|
2012-05-12 11:22:51 +01:00
|
|
|
g_check_parse_failed(uint16_, "\x02", 1);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_uint8(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *uint8_ = h_uint8();
|
2012-05-12 11:22:51 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(uint8_, "\x78", 1, "u0x78");
|
2012-05-12 11:22:51 +01:00
|
|
|
g_check_parse_failed(uint8_, "", 0);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
2012-05-18 18:43:02 +02:00
|
|
|
//@MARK_END
|
2012-05-11 15:14:30 +01:00
|
|
|
|
2012-05-24 12:23:55 +02:00
|
|
|
static void test_int_range(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *int_range_ = h_int_range(h_uint8(), 3, 10);
|
2012-05-24 12:23:55 +02:00
|
|
|
|
|
|
|
|
g_check_parse_ok(int_range_, "\x05", 1, "u0x5");
|
|
|
|
|
g_check_parse_failed(int_range_, "\xb", 1);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
#if 0
|
2012-05-11 15:14:30 +01:00
|
|
|
static void test_float64(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *float64_ = h_float64();
|
2012-05-12 11:22:51 +01:00
|
|
|
|
|
|
|
|
g_check_parse_ok(float64_, "\x3f\xf0\x00\x00\x00\x00\x00\x00", 8, 1.0);
|
|
|
|
|
g_check_parse_failed(float64_, "\x3f\xf0\x00\x00\x00\x00\x00", 7);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_float32(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *float32_ = h_float32();
|
2012-05-12 11:22:51 +01:00
|
|
|
|
|
|
|
|
g_check_parse_ok(float32_, "\x3f\x80\x00\x00", 4, 1.0);
|
|
|
|
|
g_check_parse_failed(float32_, "\x3f\x80\x00");
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
2012-05-12 00:40:54 +01:00
|
|
|
#endif
|
|
|
|
|
|
2012-05-11 15:14:30 +01:00
|
|
|
|
|
|
|
|
static void test_whitespace(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *whitespace_ = h_whitespace(h_ch('a'));
|
2012-05-12 15:49:46 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(whitespace_, "a", 1, "u0x61");
|
|
|
|
|
g_check_parse_ok(whitespace_, " a", 2, "u0x61");
|
|
|
|
|
g_check_parse_ok(whitespace_, " a", 3, "u0x61");
|
|
|
|
|
g_check_parse_ok(whitespace_, "\ta", 2, "u0x61");
|
2012-05-12 01:28:52 +01:00
|
|
|
g_check_parse_failed(whitespace_, "_a", 2);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-22 02:55:00 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
2012-05-26 13:01:23 +02:00
|
|
|
const HParsedToken* upcase(const HParseResult *p) {
|
2012-05-22 02:40:48 +02:00
|
|
|
switch(p->ast->token_type) {
|
|
|
|
|
case TT_SEQUENCE:
|
2012-05-22 03:57:27 +02:00
|
|
|
{
|
2012-05-26 13:01:23 +02:00
|
|
|
HParsedToken *ret = a_new_(p->arena, HParsedToken, 1);
|
2012-05-26 14:06:52 +02:00
|
|
|
HCountedArray *seq = h_carray_new_sized(p->arena, p->ast->seq->used);
|
2012-05-22 03:57:27 +02:00
|
|
|
ret->token_type = TT_SEQUENCE;
|
|
|
|
|
for (size_t i=0; i<p->ast->seq->used; ++i) {
|
2012-05-26 13:01:23 +02:00
|
|
|
if (TT_UINT == ((HParsedToken*)p->ast->seq->elements[i])->token_type) {
|
|
|
|
|
HParsedToken *tmp = a_new_(p->arena, HParsedToken, 1);
|
2012-05-22 03:57:27 +02:00
|
|
|
tmp->token_type = TT_UINT;
|
2012-05-26 13:01:23 +02:00
|
|
|
tmp->uint = toupper(((HParsedToken*)p->ast->seq->elements[i])->uint);
|
2012-05-26 14:06:52 +02:00
|
|
|
h_carray_append(seq, tmp);
|
2012-05-22 03:57:27 +02:00
|
|
|
} else {
|
2012-05-26 14:06:52 +02:00
|
|
|
h_carray_append(seq, p->ast->seq->elements[i]);
|
2012-05-22 03:57:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ret->seq = seq;
|
2012-05-26 13:01:23 +02:00
|
|
|
return (const HParsedToken*)ret;
|
2012-05-22 02:40:48 +02:00
|
|
|
}
|
|
|
|
|
case TT_UINT:
|
2012-05-22 02:55:00 +02:00
|
|
|
{
|
2012-05-26 13:01:23 +02:00
|
|
|
HParsedToken *ret = a_new_(p->arena, HParsedToken, 1);
|
2012-05-22 03:57:27 +02:00
|
|
|
ret->token_type = TT_UINT;
|
|
|
|
|
ret->uint = toupper(p->ast->uint);
|
2012-05-26 13:01:23 +02:00
|
|
|
return (const HParsedToken*)ret;
|
2012-05-22 02:55:00 +02:00
|
|
|
}
|
2012-05-22 02:40:48 +02:00
|
|
|
default:
|
2012-05-22 03:57:27 +02:00
|
|
|
return p->ast;
|
2012-05-22 02:40:48 +02:00
|
|
|
}
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-12 15:49:46 +01:00
|
|
|
static void test_action(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *action_ = h_action(h_sequence(h_choice(h_ch('a'),
|
|
|
|
|
h_ch('A'),
|
|
|
|
|
NULL),
|
|
|
|
|
h_choice(h_ch('b'),
|
|
|
|
|
h_ch('B'),
|
|
|
|
|
NULL),
|
|
|
|
|
NULL),
|
|
|
|
|
upcase);
|
|
|
|
|
|
2012-05-22 03:57:27 +02:00
|
|
|
g_check_parse_ok(action_, "ab", 2, "(u0x41 u0x42)");
|
|
|
|
|
g_check_parse_ok(action_, "AB", 2, "(u0x41 u0x42)");
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-11 23:42:21 +01:00
|
|
|
static void test_not_in(void) {
|
2012-05-11 23:38:45 +01:00
|
|
|
uint8_t options[3] = { 'a', 'b', 'c' };
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *not_in_ = h_not_in(options, 3);
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(not_in_, "d", 1, "u0x64");
|
2012-05-12 01:28:52 +01:00
|
|
|
g_check_parse_failed(not_in_, "a", 1);
|
|
|
|
|
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_end_p(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *end_p_ = h_sequence(h_ch('a'), h_end_p(), NULL);
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(end_p_, "a", 1, "(u0x61)");
|
2012-05-12 01:28:52 +01:00
|
|
|
g_check_parse_failed(end_p_, "aa", 2);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_nothing_p(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *nothing_p_ = h_nothing_p();
|
|
|
|
|
g_check_parse_failed(nothing_p_, "a", 1);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_sequence(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *sequence_1 = h_sequence(h_ch('a'), h_ch('b'), NULL);
|
|
|
|
|
const HParser *sequence_2 = h_sequence(h_ch('a'), h_whitespace(h_ch('b')), NULL);
|
2012-05-12 00:40:54 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(sequence_1, "ab", 2, "(u0x61 u0x62)");
|
2012-05-12 00:40:54 +01:00
|
|
|
g_check_parse_failed(sequence_1, "a", 1);
|
|
|
|
|
g_check_parse_failed(sequence_1, "b", 1);
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(sequence_2, "ab", 2, "(u0x61 u0x62)");
|
|
|
|
|
g_check_parse_ok(sequence_2, "a b", 3, "(u0x61 u0x62)");
|
|
|
|
|
g_check_parse_ok(sequence_2, "a b", 4, "(u0x61 u0x62)");
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_choice(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *choice_ = h_choice(h_ch('a'), h_ch('b'), NULL);
|
2012-05-12 11:22:51 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(choice_, "a", 1, "u0x61");
|
|
|
|
|
g_check_parse_ok(choice_, "b", 1, "u0x62");
|
2012-05-12 11:22:51 +01:00
|
|
|
g_check_parse_failed(choice_, "c", 1);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_butnot(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *butnot_1 = h_butnot(h_ch('a'), h_token((const uint8_t*)"ab", 2));
|
|
|
|
|
const HParser *butnot_2 = h_butnot(h_ch_range('0', '9'), h_ch('6'));
|
2012-05-12 11:22:51 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(butnot_1, "a", 1, "u0x61");
|
2012-05-12 11:22:51 +01:00
|
|
|
g_check_parse_failed(butnot_1, "ab", 2);
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(butnot_1, "aa", 2, "u0x61");
|
2012-05-12 11:22:51 +01:00
|
|
|
g_check_parse_failed(butnot_2, "6", 1);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_difference(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *difference_ = h_difference(h_token((const uint8_t*)"ab", 2), h_ch('a'));
|
2012-05-12 11:22:51 +01:00
|
|
|
|
|
|
|
|
g_check_parse_ok(difference_, "ab", 2, "<61.62>");
|
|
|
|
|
g_check_parse_failed(difference_, "a", 1);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_xor(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *xor_ = h_xor(h_ch_range('0', '6'), h_ch_range('5', '9'));
|
2012-05-12 11:22:51 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(xor_, "0", 1, "u0x30");
|
|
|
|
|
g_check_parse_ok(xor_, "9", 1, "u0x39");
|
2012-05-12 11:22:51 +01:00
|
|
|
g_check_parse_failed(xor_, "5", 1);
|
|
|
|
|
g_check_parse_failed(xor_, "a", 1);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-13 01:01:26 +01:00
|
|
|
static void test_many(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *many_ = h_many(h_choice(h_ch('a'), h_ch('b'), NULL));
|
2012-05-23 01:07:14 +02:00
|
|
|
g_check_parse_ok(many_, "adef", 4, "(u0x61)");
|
|
|
|
|
g_check_parse_ok(many_, "bdef", 4, "(u0x62)");
|
|
|
|
|
g_check_parse_ok(many_, "aabbabadef", 10, "(u0x61 u0x61 u0x62 u0x62 u0x61 u0x62 u0x61)");
|
|
|
|
|
g_check_parse_ok(many_, "daabbabadef", 11, "()");
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-13 01:01:26 +01:00
|
|
|
static void test_many1(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *many1_ = h_many1(h_choice(h_ch('a'), h_ch('b'), NULL));
|
2012-05-11 15:14:30 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(many1_, "adef", 4, "(u0x61)");
|
|
|
|
|
g_check_parse_ok(many1_, "bdef", 4, "(u0x62)");
|
|
|
|
|
g_check_parse_ok(many1_, "aabbabadef", 10, "(u0x61 u0x61 u0x62 u0x62 u0x61 u0x62 u0x61)");
|
2012-05-13 01:01:26 +01:00
|
|
|
g_check_parse_failed(many1_, "daabbabadef", 11);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_repeat_n(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *repeat_n_ = h_repeat_n(h_choice(h_ch('a'), h_ch('b'), NULL), 2);
|
2012-05-11 15:14:30 +01:00
|
|
|
|
2012-05-12 15:49:46 +01:00
|
|
|
g_check_parse_failed(repeat_n_, "adef", 4);
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(repeat_n_, "abdef", 5, "(u0x61 u0x62)");
|
2012-05-12 15:49:46 +01:00
|
|
|
g_check_parse_failed(repeat_n_, "dabdef", 6);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_optional(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *optional_ = h_sequence(h_ch('a'), h_optional(h_choice(h_ch('b'), h_ch('c'), NULL)), h_ch('d'), NULL);
|
2012-05-12 15:49:46 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(optional_, "abd", 3, "(u0x61 u0x62 u0x64)");
|
|
|
|
|
g_check_parse_ok(optional_, "acd", 3, "(u0x61 u0x63 u0x64)");
|
|
|
|
|
g_check_parse_ok(optional_, "ad", 2, "(u0x61 null u0x64)");
|
2012-05-12 15:49:46 +01:00
|
|
|
g_check_parse_failed(optional_, "aed", 3);
|
|
|
|
|
g_check_parse_failed(optional_, "ab", 2);
|
|
|
|
|
g_check_parse_failed(optional_, "ac", 2);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-12 00:24:56 +01:00
|
|
|
static void test_ignore(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *ignore_ = h_sequence(h_ch('a'), h_ignore(h_ch('b')), h_ch('c'), NULL);
|
2012-05-11 15:14:30 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(ignore_, "abc", 3, "(u0x61 u0x63)");
|
2012-05-12 15:49:46 +01:00
|
|
|
g_check_parse_failed(ignore_, "ac", 2);
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-13 01:01:26 +01:00
|
|
|
static void test_sepBy1(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *sepBy1_ = h_sepBy1(h_choice(h_ch('1'), h_ch('2'), h_ch('3'), NULL), h_ch(','));
|
2012-05-11 15:14:30 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(sepBy1_, "1,2,3", 5, "(u0x31 u0x32 u0x33)");
|
|
|
|
|
g_check_parse_ok(sepBy1_, "1,3,2", 5, "(u0x31 u0x33 u0x32)");
|
|
|
|
|
g_check_parse_ok(sepBy1_, "1,3", 3, "(u0x31 u0x33)");
|
|
|
|
|
g_check_parse_ok(sepBy1_, "3", 1, "(u0x33)");
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_epsilon_p(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *epsilon_p_1 = h_sequence(h_ch('a'), h_epsilon_p(), h_ch('b'), NULL);
|
|
|
|
|
const HParser *epsilon_p_2 = h_sequence(h_epsilon_p(), h_ch('a'), NULL);
|
|
|
|
|
const HParser *epsilon_p_3 = h_sequence(h_ch('a'), h_epsilon_p(), NULL);
|
2012-05-12 15:49:46 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(epsilon_p_1, "ab", 2, "(u0x61 u0x62)");
|
|
|
|
|
g_check_parse_ok(epsilon_p_2, "a", 1, "(u0x61)");
|
|
|
|
|
g_check_parse_ok(epsilon_p_3, "a", 1, "(u0x61)");
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-12 15:49:46 +01:00
|
|
|
static void test_attr_bool(void) {
|
2012-05-11 15:14:30 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_and(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *and_1 = h_sequence(h_and(h_ch('0')), h_ch('0'), NULL);
|
|
|
|
|
const HParser *and_2 = h_sequence(h_and(h_ch('0')), h_ch('1'), NULL);
|
|
|
|
|
const HParser *and_3 = h_sequence(h_ch('1'), h_and(h_ch('2')), NULL);
|
2012-05-11 15:14:30 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(and_1, "0", 1, "(u0x30)");
|
2012-05-12 15:49:46 +01:00
|
|
|
g_check_parse_failed(and_2, "0", 1);
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(and_3, "12", 2, "(u0x31)");
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void test_not(void) {
|
2012-05-26 14:06:52 +02:00
|
|
|
const HParser *not_1 = h_sequence(h_ch('a'), h_choice(h_ch('+'), h_token((const uint8_t*)"++", 2), NULL), h_ch('b'), NULL);
|
|
|
|
|
const HParser *not_2 = h_sequence(h_ch('a'),
|
|
|
|
|
h_choice(h_sequence(h_ch('+'), h_not(h_ch('+')), NULL),
|
|
|
|
|
h_token((const uint8_t*)"++", 2),
|
|
|
|
|
NULL), h_ch('b'), NULL);
|
2012-05-11 15:14:30 +01:00
|
|
|
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(not_1, "a+b", 3, "(u0x61 u0x2b u0x62)");
|
2012-05-12 15:49:46 +01:00
|
|
|
g_check_parse_failed(not_1, "a++b", 4);
|
2012-05-18 18:43:02 +02:00
|
|
|
g_check_parse_ok(not_2, "a+b", 3, "(u0x61 (u0x2b) u0x62)");
|
|
|
|
|
g_check_parse_ok(not_2, "a++b", 4, "(u0x61 <2b.2b> u0x62)");
|
2012-05-11 15:14:30 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-11 15:24:22 +01:00
|
|
|
void register_parser_tests(void) {
|
|
|
|
|
g_test_add_func("/core/parser/token", test_token);
|
|
|
|
|
g_test_add_func("/core/parser/ch", test_ch);
|
2012-05-24 12:00:23 +02:00
|
|
|
g_test_add_func("/core/parser/ch_range", test_ch_range);
|
2012-05-11 15:24:22 +01:00
|
|
|
g_test_add_func("/core/parser/int64", test_int64);
|
|
|
|
|
g_test_add_func("/core/parser/int32", test_int32);
|
|
|
|
|
g_test_add_func("/core/parser/int16", test_int16);
|
|
|
|
|
g_test_add_func("/core/parser/int8", test_int8);
|
|
|
|
|
g_test_add_func("/core/parser/uint64", test_uint64);
|
|
|
|
|
g_test_add_func("/core/parser/uint32", test_uint32);
|
|
|
|
|
g_test_add_func("/core/parser/uint16", test_uint16);
|
|
|
|
|
g_test_add_func("/core/parser/uint8", test_uint8);
|
2012-05-24 12:23:55 +02:00
|
|
|
g_test_add_func("/core/parser/int_range", test_int_range);
|
2012-05-18 18:43:02 +02:00
|
|
|
#if 0
|
2012-05-11 15:24:22 +01:00
|
|
|
g_test_add_func("/core/parser/float64", test_float64);
|
|
|
|
|
g_test_add_func("/core/parser/float32", test_float32);
|
2012-05-12 00:40:54 +01:00
|
|
|
#endif
|
2012-05-11 15:24:22 +01:00
|
|
|
g_test_add_func("/core/parser/whitespace", test_whitespace);
|
|
|
|
|
g_test_add_func("/core/parser/action", test_action);
|
2012-05-11 23:42:21 +01:00
|
|
|
g_test_add_func("/core/parser/not_in", test_not_in);
|
2012-05-11 15:24:22 +01:00
|
|
|
g_test_add_func("/core/parser/end_p", test_end_p);
|
|
|
|
|
g_test_add_func("/core/parser/nothing_p", test_nothing_p);
|
|
|
|
|
g_test_add_func("/core/parser/sequence", test_sequence);
|
|
|
|
|
g_test_add_func("/core/parser/choice", test_choice);
|
|
|
|
|
g_test_add_func("/core/parser/butnot", test_butnot);
|
|
|
|
|
g_test_add_func("/core/parser/difference", test_difference);
|
|
|
|
|
g_test_add_func("/core/parser/xor", test_xor);
|
2012-05-13 01:01:26 +01:00
|
|
|
g_test_add_func("/core/parser/many", test_many);
|
|
|
|
|
g_test_add_func("/core/parser/many1", test_many1);
|
2012-05-11 15:24:22 +01:00
|
|
|
g_test_add_func("/core/parser/repeat_n", test_repeat_n);
|
|
|
|
|
g_test_add_func("/core/parser/optional", test_optional);
|
2012-05-13 01:01:26 +01:00
|
|
|
g_test_add_func("/core/parser/sepBy1", test_sepBy1);
|
2012-05-11 15:24:22 +01:00
|
|
|
g_test_add_func("/core/parser/epsilon_p", test_epsilon_p);
|
2012-05-12 15:49:46 +01:00
|
|
|
g_test_add_func("/core/parser/attr_bool", test_attr_bool);
|
2012-05-11 15:24:22 +01:00
|
|
|
g_test_add_func("/core/parser/and", test_and);
|
|
|
|
|
g_test_add_func("/core/parser/not", test_not);
|
|
|
|
|
g_test_add_func("/core/parser/ignore", test_ignore);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-11 15:14:30 +01:00
|
|
|
#endif // #ifdef INCLUDE_TESTS
|