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-11-13 22:51:31 -05:00
|
|
|
#include <err.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
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
.bit_offset = 8,
|
|
|
|
|
.overrun = 0,
|
|
|
|
|
.endianness = BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN,
|
|
|
|
|
.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
|
|
|
|
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;
|
|
|
|
|
}
|