Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
project(HAMMER_PARSE C)

file(GLOB_RECURSE APP_SRC_FILES
"src/*.c"
)

# Create the app module
add_cfe_app(hammer_parse ${APP_SRC_FILES})

# The API to this library (which may be invoked/referenced from other apps)
# is stored in fsw/public_inc. Using "target_include_directories" is the
# preferred method of indicating this (vs. directory-scope "include_directories").
target_include_directories(hammer_parse PUBLIC
src/backends
src/parsers
src)




4 changes: 2 additions & 2 deletions src/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,12 @@ void *h_arena_realloc(HArena *arena, void *ptr, size_t n) {
// much data from the old block as there could have been.

for (link = arena->head; link; link = link->next) {
if (ptr >= (void *)link->rest && ptr <= (void *)link->rest + link->used)
if (ptr >= (void *)link->rest && ptr <= (void *)((char *)link->rest + link->used))
break; /* found it */
}
assert(link != NULL);

ncopy = (void *)link->rest + link->used - ptr;
ncopy = ((char *)link->rest + link->used) - (char *)ptr;
if (n < ncopy)
ncopy = n;

Expand Down
18 changes: 9 additions & 9 deletions src/backends/packrat.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static uint32_t cache_key_hash(const void *key);
static HParserCacheValue *cached_result(HParseState *state, HParseResult *result) {
HParserCacheValue *ret = a_new(HParserCacheValue, 1);
ret->value_type = PC_RIGHT;
ret->right = result;
ret->cache_value_union.right = result;
ret->input_stream = state->input_stream;
return ret;
}
Expand All @@ -28,7 +28,7 @@ static HParserCacheValue *cached_result(HParseState *state, HParseResult *result
static HParserCacheValue *cached_lr(HParseState *state, HLeftRec *lr) {
HParserCacheValue *ret = a_new(HParserCacheValue, 1);
ret->value_type = PC_LEFT;
ret->left = lr;
ret->cache_value_union.left = lr;
ret->input_stream = state->input_stream;
return ret;
}
Expand Down Expand Up @@ -94,7 +94,7 @@ HParserCacheValue *recall(HParserCacheKey *k, HParseState *state, HHashValue key
h_hashtable_put_precomp(state->cache, k, cached, keyhash);
} else {
cached->value_type = PC_RIGHT;
cached->right = tmp_res;
cached->cache_value_union.right = tmp_res;
cached->input_stream = state->input_stream;
}
}
Expand Down Expand Up @@ -145,7 +145,7 @@ HParseResult *grow(HParserCacheKey *k, HParseState *state, HRecursionHead *head)
HParserCacheValue *old_cached = h_hashtable_get(state->cache, k);
if (!old_cached || PC_LEFT == old_cached->value_type)
h_platform_errx(1, "impossible match");
HParseResult *old_res = old_cached->right;
HParseResult *old_res = old_cached->cache_value_union.right;

// rewind the input
state->input_stream = k->input_pos;
Expand All @@ -164,7 +164,7 @@ HParseResult *grow(HParserCacheKey *k, HParseState *state, HRecursionHead *head)
HParserCacheValue *cached = h_hashtable_get(state->cache, k);
if (cached && PC_RIGHT == cached->value_type) {
state->input_stream = cached->input_stream;
return cached->right;
return cached->cache_value_union.right;
} else {
h_platform_errx(1, "impossible match");
}
Expand Down Expand Up @@ -254,10 +254,10 @@ HParseResult *h_do_parse(const HParser *parser, HParseState *state) {
/* it exists! */
state->input_stream = m->input_stream;
if (PC_LEFT == m->value_type) {
setupLR(parser, state, m->left);
return m->left->seed;
setupLR(parser, state, m->cache_value_union.left);
return m->cache_value_union.left->seed;
} else {
return m->right;
return m->cache_value_union.right;
}
}
}
Expand Down Expand Up @@ -390,7 +390,7 @@ bool h_packrat_parse_chunk(HSuspendedParser *s, HInputStream *input) {
h_platform_errx(1, "input length would overflow");
newlen = cat->length + input->length;
cat->input = h_realloc(mm__, (void *)cat->input, newlen);
memcpy((void *)cat->input + cat->length, input->input, input->length);
memcpy((void *)((char *)cat->input + cat->length), input->input, input->length);
cat->length = newlen;
cat->last_chunk = input->last_chunk;

Expand Down
6 changes: 3 additions & 3 deletions src/benchmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ HBenchmarkResults *h_benchmark__m(HAllocator *mm__, HParser *parser, HParserTest
}
time_diff = h_platform_stopwatch_ns(&stopwatch);
} while (time_diff < 100000000);
ret->results[backend].cases[cur_case].parse_time = (time_diff / count);
ret->results[backend].cases[cur_case].swig_union_2.parse_time = (time_diff / count);
ret->results[backend].cases[cur_case].length = tc->length;
cur_case++;
}
Expand All @@ -123,8 +123,8 @@ void h_benchmark_report(FILE *stream, HBenchmarkResults *result) {
continue;
}
fprintf(stream, "Case %zd: %zd ns/parse, %zd ns/byte\n", j,
result->results[i].cases[j].parse_time,
result->results[i].cases[j].parse_time / result->results[i].cases[j].length);
result->results[i].cases[j].swig_union_2.parse_time,
result->results[i].cases[j].swig_union_2.parse_time / result->results[i].cases[j].length);
}
}
}
36 changes: 18 additions & 18 deletions src/cfgrammar.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ HCFGrammar *h_cfgrammar_(HAllocator *mm__, HCFChoice *desugared) {
// desugared is a terminal. wrap it in a singleton HCF_CHOICE.
HCFChoice *nt = h_new(HCFChoice, 1);
nt->type = HCF_CHOICE;
nt->seq = h_new(HCFSequence *, 2);
nt->seq[0] = h_new(HCFSequence, 1);
nt->seq[0]->items = h_new(HCFChoice *, 2);
nt->seq[0]->items[0] = desugared;
nt->seq[0]->items[1] = NULL;
nt->seq[1] = NULL;
nt->hcfchoice_union.seq = h_new(HCFSequence *, 2);
nt->hcfchoice_union.seq[0] = h_new(HCFSequence, 1);
nt->hcfchoice_union.seq[0]->items = h_new(HCFChoice *, 2);
nt->hcfchoice_union.seq[0]->items[0] = desugared;
nt->hcfchoice_union.seq[0]->items[1] = NULL;
nt->hcfchoice_union.seq[1] = NULL;
nt->pred = NULL;
nt->action = NULL;
nt->reshape = h_act_first;
Expand Down Expand Up @@ -131,7 +131,7 @@ static void collect_nts(HCFGrammar *grammar, HCFChoice *symbol) {

// each element s of symbol->seq (HCFSequence) represents the RHS of
// a production. call self on all symbols (HCFChoice) in s.
for (s = symbol->seq; *s != NULL; s++) {
for (s = symbol->hcfchoice_union.seq; *s != NULL; s++) {
for (x = (*s)->items; *x != NULL; x++) {
collect_nts(grammar, *x);
}
Expand Down Expand Up @@ -197,7 +197,7 @@ static void collect_geneps(HCFGrammar *g) {

// this NT derives epsilon if any one of its productions does.
HCFSequence **p;
for (p = symbol->seq; *p != NULL; p++) {
for (p = symbol->hcfchoice_union.seq; *p != NULL; p++) {
if (h_derives_epsilon_seq(g, (*p)->items)) {
h_hashset_put(g->geneps, symbol);
break;
Expand Down Expand Up @@ -229,7 +229,7 @@ static void remove_productions_with(HCFGrammar *g, const HCFChoice *x) {
assert(symbol->type == HCF_CHOICE);

HCFSequence **p, **q;
for (p = symbol->seq; *p != NULL;) {
for (p = symbol->hcfchoice_union.seq; *p != NULL;) {
if (mentions_symbol((*p)->items, x)) {
// remove production p
for (q = p; *(q + 1) != NULL; q++)
Expand Down Expand Up @@ -260,7 +260,7 @@ static void eliminate_dead_rules(HCFGrammar *g) {
assert(symbol->type == HCF_CHOICE);

// this NT is dead if it has no productions
if (*symbol->seq == NULL)
if (*symbol->hcfchoice_union.seq == NULL)
found = true;
}
}
Expand Down Expand Up @@ -472,12 +472,12 @@ static const HStringMap *h_first_work(size_t k, HCFGrammar *g, HHashTable **pws,

switch (x->type) {
case HCF_CHAR:
h_stringmap_put_char(ret, x->chr, INSET);
h_stringmap_put_char(ret, x->hcfchoice_union.chr, INSET);
break;
case HCF_CHARSET:
c = 0;
do {
if (charset_isset(x->charset, c)) {
if (charset_isset(x->hcfchoice_union.charset, c)) {
h_stringmap_put_char(ret, c, INSET);
}
} while (c++ < 255);
Expand All @@ -494,7 +494,7 @@ static const HStringMap *h_first_work(size_t k, HCFGrammar *g, HHashTable **pws,
h_hashtable_put(ws, pkx, ret);

// return the union of the first sets of all productions
for (p = x->seq; *p; ++p) {
for (p = x->hcfchoice_union.seq; *p; ++p) {
const HStringMap *first_rhs = h_first_seq_work(k, g, pws, (*p)->items);
assert(ws == *pws); // call above did not change the workset pointer
taint |= first_rhs->taint;
Expand Down Expand Up @@ -723,7 +723,7 @@ static const HStringMap *h_follow_work(size_t k, HCFGrammar *g, HHashTable **pws

// iterate over the productions for A
HCFSequence **p;
for (p = a->seq; *p; p++) {
for (p = a->hcfchoice_union.seq; *p; p++) {
HCFChoice **s = (*p)->items; // production's right-hand side

for (; *s; s++) {
Expand Down Expand Up @@ -918,7 +918,7 @@ static HCFChoice **pprint_string(FILE *f, HCFChoice **x) {
if ((*x)->type != HCF_CHAR) {
break;
}
h_pprint_char(f, (*x)->chr);
h_pprint_char(f, (*x)->hcfchoice_union.chr);
}
fputc('"', f);
return x;
Expand All @@ -928,14 +928,14 @@ void h_pprint_symbol(FILE *f, const HCFGrammar *g, const HCFChoice *x) {
switch (x->type) {
case HCF_CHAR:
fputc('"', f);
h_pprint_char(f, x->chr);
h_pprint_char(f, x->hcfchoice_union.chr);
fputc('"', f);
break;
case HCF_END:
fputc('$', f);
break;
case HCF_CHARSET:
pprint_charset(f, x->charset);
pprint_charset(f, x->hcfchoice_union.charset);
break;
default:
fputs(nonterminal_name(g, x), f);
Expand Down Expand Up @@ -985,7 +985,7 @@ static void pprint_ntrules(FILE *f, const HCFGrammar *g, const HCFChoice *nt, in
fputc(' ', f);

assert(nt->type == HCF_CHOICE);
HCFSequence **p = nt->seq;
HCFSequence **p = nt->hcfchoice_union.seq;
if (*p == NULL) {
fputs(" -x\n", f); // empty choice, e.g. h_nothing_p()
return;
Expand Down
Loading