hammer/src/hammer.c

191 lines
4.8 KiB
C
Raw Normal View History

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.
*/
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <stdarg.h>
#include <string.h>
2012-05-26 16:00:43 +02:00
#include "hammer.h"
#include "internal.h"
#include "allocator.h"
#include "parsers/parser_internal.h"
2012-04-22 04:47:08 +01:00
2013-04-26 20:36:54 -07:00
static HParserBackendVTable *backends[PB_MAX + 1] = {
&h__packrat_backend_vtable,
&h__regex_backend_vtable,
&h__llk_backend_vtable,
2013-06-04 22:14:06 +02:00
&h__lalr_backend_vtable,
&h__glr_backend_vtable,
2013-04-26 20:36:54 -07:00
};
/* Helper function, since these lines appear in every parser */
typedef struct {
2012-05-26 13:01:23 +02:00
const HParser *p1;
const HParser *p2;
} HTwoParsers;
#define DEFAULT_ENDIANNESS (BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN)
2012-10-10 15:58:03 +02:00
HParseResult* h_parse(const HParser* parser, const uint8_t* input, size_t length) {
return h_parse__m(&system_allocator, parser, input, length);
}
2013-04-26 20:36:54 -07:00
HParseResult* h_parse__m(HAllocator* mm__, const HParser* parser, const uint8_t* input, size_t length) {
// Set up a parse state...
2013-04-26 20:36:54 -07:00
HInputStream input_stream = {
.pos = 0,
2013-04-26 20:36:54 -07:00
.index = 0,
.bit_offset = 0,
2013-04-26 20:36:54 -07:00
.overrun = 0,
.endianness = DEFAULT_ENDIANNESS,
2013-04-26 20:36:54 -07:00
.length = length,
2015-09-04 12:55:37 +02:00
.input = input,
.last_chunk = true
2013-04-26 20:36:54 -07:00
};
return backends[parser->backend]->parse(mm__, parser, &input_stream);
}
void h_parse_result_free__m(HAllocator *alloc, HParseResult *result) {
h_parse_result_free(result);
}
2012-07-27 15:06:40 -07:00
void h_parse_result_free(HParseResult *result) {
if(result == NULL) return;
2012-07-27 15:06:40 -07:00
h_delete_arena(result->arena);
}
2013-03-09 21:42:49 -08:00
bool h_false(void* env) {
(void)env;
return false;
}
2013-03-09 21:42:49 -08:00
bool h_true(void* env) {
(void)env;
return true;
}
bool h_not_regular(HRVMProg *prog, void *env) {
(void)env;
return false;
}
2013-04-26 20:36:54 -07:00
int h_compile(HParser* parser, HParserBackend backend, const void* params) {
return h_compile__m(&system_allocator, parser, backend, params);
}
int h_compile__m(HAllocator* mm__, HParser* parser, HParserBackend backend, const void* params) {
2013-05-20 17:10:38 +02:00
backends[parser->backend]->free(parser);
2013-04-26 20:36:54 -07:00
int ret = backends[backend]->compile(mm__, parser, params);
if (!ret)
parser->backend = backend;
return ret;
}
HSuspendedParser* h_parse_start(const HParser* parser) {
return h_parse_start__m(&system_allocator, parser);
}
HSuspendedParser* h_parse_start__m(HAllocator* mm__, const HParser* parser) {
if(!backends[parser->backend]->parse_start)
return NULL;
// allocate and init suspended state
HSuspendedParser *s = h_new(HSuspendedParser, 1);
if(!s)
return NULL;
s->mm__ = mm__;
s->parser = parser;
s->backend_state = NULL;
s->done = false;
s->pos = 0;
s->bit_offset = 0;
s->endianness = DEFAULT_ENDIANNESS;
// backend-specific initialization
// should allocate s->backend_state
backends[parser->backend]->parse_start(s);
return s;
}
bool h_parse_chunk(HSuspendedParser* s, const uint8_t* input, size_t length) {
assert(backends[s->parser->backend]->parse_chunk != NULL);
// no-op if parser is already done
if(s->done)
return true;
// input
HInputStream input_stream = {
.pos = s->pos,
.index = 0,
.bit_offset = 0,
.overrun = 0,
.endianness = s->endianness,
.length = length,
2015-09-04 12:55:37 +02:00
.input = input,
.last_chunk = false
};
// process chunk
s->done = backends[s->parser->backend]->parse_chunk(s, &input_stream);
s->endianness = input_stream.endianness;
s->pos += input_stream.index;
s->bit_offset = input_stream.bit_offset;
return s->done;
}
HParseResult* h_parse_finish(HSuspendedParser* s) {
assert(backends[s->parser->backend]->parse_chunk != NULL);
assert(backends[s->parser->backend]->parse_finish != NULL);
HAllocator *mm__ = s->mm__;
// signal end of input if parser is not already done
if(!s->done) {
HInputStream empty = {
.pos = s->pos,
.index = 0,
.bit_offset = 0,
.overrun = 0,
.endianness = s->endianness,
.length = 0,
.input = NULL,
.last_chunk = true
};
s->done = backends[s->parser->backend]->parse_chunk(s, &empty);
assert(s->done);
}
// extract result
HParseResult *r = backends[s->parser->backend]->parse_finish(s);
if(r)
r->bit_length = s->pos * 8 + s->bit_offset;
// NB: backend should have freed backend_state
h_free(s);
return r;
}