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-05-01 00:33:47 +01:00
|
|
|
#include <assert.h>
|
2012-05-12 01:28:52 +01:00
|
|
|
#include <ctype.h>
|
2012-05-24 13:22:43 +02:00
|
|
|
#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,
|
2013-05-11 19:04:59 +02:00
|
|
|
&h__llk_backend_vtable,
|
2013-06-04 22:14:06 +02:00
|
|
|
&h__lalr_backend_vtable,
|
2013-06-19 17:20:53 +02:00
|
|
|
&h__glr_backend_vtable,
|
2013-04-26 20:36:54 -07:00
|
|
|
};
|
|
|
|
|
|
2012-04-22 05:15:40 +01:00
|
|
|
|
2012-05-01 00:33:47 +01:00
|
|
|
/* Helper function, since these lines appear in every parser */
|
2012-05-01 03:21:14 +01:00
|
|
|
|
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-04-30 03:44:10 +01:00
|
|
|
|
2012-05-03 01:58:09 +01:00
|
|
|
|
2015-09-03 15:03:01 +02:00
|
|
|
#define DEFAULT_ENDIANNESS (BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN)
|
2012-05-03 01:58:09 +01:00
|
|
|
|
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) {
|
2012-05-03 01:58:09 +01:00
|
|
|
// Set up a parse state...
|
2013-04-26 20:36:54 -07:00
|
|
|
HInputStream input_stream = {
|
|
|
|
|
.index = 0,
|
2015-01-04 04:00:09 +01:00
|
|
|
.bit_offset = 0,
|
2013-04-26 20:36:54 -07:00
|
|
|
.overrun = 0,
|
2015-09-03 15:03:01 +02:00
|
|
|
.endianness = DEFAULT_ENDIANNESS,
|
2013-04-26 20:36:54 -07:00
|
|
|
.length = length,
|
|
|
|
|
.input = input
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return backends[parser->backend]->parse(mm__, parser, &input_stream);
|
2012-05-03 01:58:09 +01:00
|
|
|
}
|
2012-05-11 15:14:30 +01:00
|
|
|
|
2013-11-01 18:00:50 -04:00
|
|
|
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) {
|
2013-05-11 21:45:02 +02:00
|
|
|
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;
|
|
|
|
|
}
|
2012-05-11 15:14:30 +01:00
|
|
|
|
2013-03-09 21:42:49 -08:00
|
|
|
bool h_true(void* env) {
|
|
|
|
|
(void)env;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2013-04-22 18:06:17 -07:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2015-09-03 15:03:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
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->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);
|
|
|
|
|
|
|
|
|
|
// input
|
|
|
|
|
HInputStream input_stream = {
|
|
|
|
|
.index = 0,
|
|
|
|
|
.bit_offset = 0,
|
|
|
|
|
.overrun = 0,
|
|
|
|
|
.endianness = s->endianness,
|
|
|
|
|
.length = length,
|
|
|
|
|
.input = input
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// process chunk
|
|
|
|
|
backends[s->parser->backend]->parse_chunk(s, &input_stream);
|
|
|
|
|
s->endianness = input_stream.endianness;
|
|
|
|
|
|
|
|
|
|
return !input_stream.overrun; // parser wants no more input? done.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HParseResult* h_parse_finish(HSuspendedParser* s) {
|
|
|
|
|
assert(backends[s->parser->backend]->parse_finish != NULL);
|
|
|
|
|
|
|
|
|
|
HAllocator *mm__ = s->mm__;
|
|
|
|
|
|
|
|
|
|
HParseResult *r = backends[s->parser->backend]->parse_finish(s);
|
|
|
|
|
// NB: backend should have freed backend_state
|
|
|
|
|
h_free(s);
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|