Initial port from jsparse.
This commit is contained in:
commit
8ec43ffed4
2 changed files with 97 additions and 0 deletions
37
hammer.c
Normal file
37
hammer.c
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
#include "hammer.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
parse_state* from(parse_state *ps, const size_t index) {
|
||||||
|
parse_state p = { ps->input, ps->index + index, ps->length - index, ps->cache };
|
||||||
|
parse_state *ret = g_new(parse_state, 1);
|
||||||
|
*ret = p;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint8_t* substring(const parse_state *ps, const size_t start, const size_t end) {
|
||||||
|
if (end > start && (ps->index + end) < ps->length) {
|
||||||
|
gpointer ret = g_malloc(end - start);
|
||||||
|
memcpy(ret, ps->input, end - start);
|
||||||
|
return (const uint8_t*)ret;
|
||||||
|
} else {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const GVariant* at(parse_state *ps, const size_t index) {
|
||||||
|
GVariant *ret = NULL;
|
||||||
|
if (index + ps->index < ps->length)
|
||||||
|
ret = g_variant_new_byte((ps->input)[index + ps->index]);
|
||||||
|
return g_variant_new_maybe(G_VARIANT_TYPE_BYTE, ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
const gchar* to_string(parse_state *ps) {
|
||||||
|
return g_strescape(ps->input, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
const result* get_cached(parse_state *ps, const size_t pid); /* {
|
||||||
|
gpointer p = g_hash_table_lookup(ps->cache, &pid);
|
||||||
|
if (NULL != p)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
int put_cached(parse_state *ps, const size_t pid, result cached);
|
||||||
60
hammer.h
Normal file
60
hammer.h
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
#include <glib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
/* The state of the parser.
|
||||||
|
*
|
||||||
|
* Members:
|
||||||
|
* input - the entire string being parsed
|
||||||
|
* index - current position in input
|
||||||
|
* length - size of input
|
||||||
|
* THE FOLLOWING DESCRIBES HOW JSPARSE DOES IT. OUR MILEAGE MAY VARY.
|
||||||
|
* cache - a hash table describing the state of the parse, including partial parse
|
||||||
|
* results. It's actually a hash table of [parser_id, hash_table[index, result]],
|
||||||
|
* where the parser id is incremented as the parse goes along (parsers that have
|
||||||
|
* already been applied once don't get a new parser_id ... but the global variable
|
||||||
|
* still increments? not sure why that is, need to debug some), and the locations
|
||||||
|
* at which it's been applied are memoized.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
typedef struct {
|
||||||
|
const uint8_t *input;
|
||||||
|
size_t index;
|
||||||
|
size_t length;
|
||||||
|
GHashTable *cache;
|
||||||
|
} parse_state;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const uint8_t *remaining;
|
||||||
|
const uint8_t *matched;
|
||||||
|
const GSequence *ast;
|
||||||
|
} result;
|
||||||
|
|
||||||
|
result (*token(const uint8_t *s))(parse_state);
|
||||||
|
result (*ch(const uint8_t c))(parse_state);
|
||||||
|
result (*range(const uint8_t lower, const uint8_t upper))(parse_state);
|
||||||
|
result (*whitespace(/* fptr to parser */))(parse_state);
|
||||||
|
result (*action(/* fptr to parser, fptr to action */))(parse_state);
|
||||||
|
result (*join_action(/* fptr to parser */, const uint8_t *sep))(parse_state);
|
||||||
|
result (*left_factor_action(/* fptr to parser */))(parse_state);
|
||||||
|
result (*negate(/* fptr to parser */))(parse_state);
|
||||||
|
result end_p(parse_state);
|
||||||
|
result nothing_p(parse_state);
|
||||||
|
result (*sequence(/* array of fptrs! */))(parse_state);
|
||||||
|
result (*choice(/* array of fptrs */))(parse_state);
|
||||||
|
result (*butnot(/* fptr to parser1, fptr to parser2 */))(parse_state);
|
||||||
|
result (*difference(/* fptr to parser1, fptr to parser2 */))(parse_state);
|
||||||
|
result (*xor(/* fptr to parser1, fptr to parser2 */))(parse_state);
|
||||||
|
result (*repeat0(/* fptr to parser */))(parse_state);
|
||||||
|
result (*repeat1(/* fptr to parser */))(parse_state);
|
||||||
|
result (*repeatN(/* fptr to parser */, const size_t n))(parse_state);
|
||||||
|
result (*optional(/* fptr to parser */))(parse_state);
|
||||||
|
void (*expect(/* fptr to parser */))(parse_state);
|
||||||
|
result (*chain(/* fptr to item parser */, /* fptr to separator parser */, /* fptr to function */))(parse_state);
|
||||||
|
result (*chainl(/* fptr to parser */, /* fptr to separator parser */))(parse_state);
|
||||||
|
result (*list(/* fptr to parser */, /* fptr to separator parser */))(parse_state);
|
||||||
|
result epsilon_p(parse_state);
|
||||||
|
result (*semantic(/* fptr to nullary function? */))(parse_state);
|
||||||
|
result (*and(/* fptr to conditional-syntax parser */))(parse_state);
|
||||||
|
result (*not(/* fptr to conditional-syntax parser */))(parse_state);
|
||||||
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue