Rewrote test_leftrec to mirror test_rightrec. It fails differently for packrat now!

This commit is contained in:
Meredith L. Patterson 2013-11-19 03:31:59 -06:00
commit ccacccb750
22 changed files with 314 additions and 178 deletions

4
.gitignore vendored
View file

@ -17,3 +17,7 @@ TAGS
docs/milestone2.dot.pdf docs/milestone2.dot.pdf
*.dot.pdf *.dot.pdf
Session.vim Session.vim
*.gcov
cscope.out
build/
.sconsign.dblite

View file

@ -2,6 +2,7 @@ language: c
compiler: compiler:
- gcc - gcc
- clang - clang
script: scons script:
- scons
notifications: notifications:
irc: "irc.upstandinghackers.com#hammer" irc: "irc.upstandinghackers.com#hammer"

View file

@ -2,8 +2,9 @@ Hammer is a parsing library. Like many modern parsing libraries, it provides a p
Hammer is written in C, but will provide bindings for other languages. If you don't see a language you're interested in on the list, just ask. Hammer is written in C, but will provide bindings for other languages. If you don't see a language you're interested in on the list, just ask.
Hammer currently builds under Linux. (Windows and OSX are coming.) Hammer currently builds under Linux and OS X. (Windows is coming.)
[![Build Status](https://travis-ci.org/UpstandingHackers/hammer.png)](https://travis-ci.org/UpstandingHackers/hammer)
Features Features
======== ========
* Bit-oriented -- grammars can include single-bit flags or multi-bit constructs that span character boundaries, with no hassle * Bit-oriented -- grammars can include single-bit flags or multi-bit constructs that span character boundaries, with no hassle
@ -41,14 +42,13 @@ To build, type `scons`. To run the built-in test suite, type `scons test`. For a
If jni.h and jni_md.h aren't already somewhere on your include path, prepend If jni.h and jni_md.h aren't already somewhere on your include path, prepend
`C_INCLUDE_PATH=/path/to/jdk/include` to that. `C_INCLUDE_PATH=/path/to/jdk/include` to that.
There is currently no `install` target; to make Hammer available system-wide, To make Hammer available system-wide, use `scons install`. This places include files in `/usr/local/include/hammer`
copy `libhammer.a` and `libhammer.so` from `build/opt/src` to `/usr/lib/` (or and library files in `/usr/local/lib` by default; to install elsewhere, add a `prefix=<destination>` argument, e.g.
`/usr/local/lib/`, or wherever ld will find it) and `hammer.h` to `scons install prefix=$HOME`.
`/usr/include/`.
Usage Usage
===== =====
Just `#include <hammer.h>` and link with `-lhammer`. Just `#include <hammer/hammer.h>` and link with `-lhammer`.
Examples Examples
======== ========

View file

@ -1,8 +1,39 @@
# -*- python -*- # -*- python -*-
import os import os
env = Environment() import os.path
import sys
env.MergeFlags("-std=gnu99 -Wall -Wextra -Werror -Wno-unused-parameter -Wno-attributes -lrt")
vars = Variables(None, ARGUMENTS)
vars.Add(PathVariable('DESTDIR', "Root directory to install in (useful for packaging scripts)", None, PathVariable.PathIsDirCreate))
vars.Add(PathVariable('prefix', "Where to install in the FHS", "/usr/local", PathVariable.PathAccept))
env = Environment(ENV = {'PATH' : os.environ['PATH']}, variables = vars)
def calcInstallPath(*elements):
path = os.path.abspath(os.path.join(*map(env.subst, elements)))
if 'DESTDIR' in env:
path = os.path.join(env['DESTDIR'], os.path.relpath(path, start="/"))
return path
rel_prefix = not os.path.isabs(env['prefix'])
env['prefix'] = os.path.abspath(env['prefix'])
if 'DESTDIR' in env:
env['DESTDIR'] = os.path.abspath(env['DESTDIR'])
if rel_prefix:
print >>sys.stderr, "--!!-- You used a relative prefix with a DESTDIR. This is probably not what you"
print >>sys.stderr, "--!!-- you want; files will be installed in"
print >>sys.stderr, "--!!-- %s" % (calcInstallPath("$prefix"),)
env['libpath'] = calcInstallPath("$prefix", "lib")
env['incpath'] = calcInstallPath("$prefix", "include", "hammer")
# TODO: Add pkgconfig
env.MergeFlags("-std=gnu99 -Wall -Wextra -Werror -Wno-unused-parameter -Wno-attributes")
if not env['PLATFORM'] == 'darwin':
env.MergeFlags("-lrt")
AddOption("--variant", AddOption("--variant",
dest="variant", dest="variant",
@ -12,6 +43,12 @@ AddOption("--variant",
action="store", action="store",
help="Build variant (debug or opt)") help="Build variant (debug or opt)")
AddOption("--coverage",
dest="coverage",
default=False,
action="store_true",
help="Build with coverage instrumentation")
env['BUILDDIR'] = 'build/$VARIANT' env['BUILDDIR'] = 'build/$VARIANT'
dbg = env.Clone(VARIANT='debug') dbg = env.Clone(VARIANT='debug')
@ -25,12 +62,25 @@ if GetOption("variant") == 'debug':
else: else:
env = opt env = opt
if os.getenv("CC") == "clang": if GetOption("coverage"):
env.Append(CFLAGS=["-fprofile-arcs", "-ftest-coverage"],
CXXFLAGS=["-fprofile-arcs", "-ftest-coverage"],
LDFLAGS=["-fprofile-arcs", "-ftest-coverage"],
LIBS=['gcov'])
if os.getenv("CC") == "clang" or env['PLATFORM'] == 'darwin':
env.Replace(CC="clang", env.Replace(CC="clang",
CXX="clang++") CXX="clang++")
#rootpath = env['ROOTPATH'] = os.path.abspath('.')
#env.Append(CPPPATH=os.path.join('#', "hammer"))
Export('env') Export('env')
env.SConscript(["src/SConscript"], variant_dir='build/$VARIANT/src') env.SConscript(["src/SConscript"], variant_dir='build/$VARIANT/src')
env.SConscript(["examples/SConscript"], variant_dir='build/$VARIANT/examples') env.SConscript(["examples/SConscript"], variant_dir='build/$VARIANT/examples')
env.Command('test', 'build/$VARIANT/src/test_suite', 'env LD_LIBRARY_PATH=build/$VARIANT/src $SOURCE') env.Command('test', 'build/$VARIANT/src/test_suite', 'env LD_LIBRARY_PATH=build/$VARIANT/src $SOURCE')
env.Alias("install", "$libpath")
env.Alias("install", "$incpath")

View file

@ -8,6 +8,7 @@
// base64_sem1.c and base64_sem2.c for examples how to attach appropriate // base64_sem1.c and base64_sem2.c for examples how to attach appropriate
// semantic actions to the grammar. // semantic actions to the grammar.
#include <inttypes.h>
#include "../src/hammer.h" #include "../src/hammer.h"
const HParser* document = NULL; const HParser* document = NULL;
@ -49,12 +50,12 @@ int main(int argc, char **argv)
init_parser(); init_parser();
inputsize = fread(input, 1, sizeof(input), stdin); inputsize = fread(input, 1, sizeof(input), stdin);
fprintf(stderr, "inputsize=%lu\ninput=", inputsize); fprintf(stderr, "inputsize=%zu\ninput=", inputsize);
fwrite(input, 1, inputsize, stderr); fwrite(input, 1, inputsize, stderr);
result = h_parse(document, input, inputsize); result = h_parse(document, input, inputsize);
if(result) { if(result) {
fprintf(stderr, "parsed=%lld bytes\n", result->bit_length/8); fprintf(stderr, "parsed=%" PRId64 " bytes\n", result->bit_length/8);
h_pprint(stdout, result->ast, 0, 0); h_pprint(stdout, result->ast, 0, 0);
return 0; return 0;
} else { } else {

View file

@ -15,6 +15,7 @@
#include "../src/hammer.h" #include "../src/hammer.h"
#include "../src/glue.h" #include "../src/glue.h"
#include <assert.h> #include <assert.h>
#include <inttypes.h>
/// ///
@ -158,12 +159,12 @@ int main(int argc, char **argv)
parser = init_parser(); parser = init_parser();
inputsize = fread(input, 1, sizeof(input), stdin); inputsize = fread(input, 1, sizeof(input), stdin);
fprintf(stderr, "inputsize=%lu\ninput=", inputsize); fprintf(stderr, "inputsize=%zu\ninput=", inputsize);
fwrite(input, 1, inputsize, stderr); fwrite(input, 1, inputsize, stderr);
result = h_parse(parser, input, inputsize); result = h_parse(parser, input, inputsize);
if(result) { if(result) {
fprintf(stderr, "parsed=%lld bytes\n", result->bit_length/8); fprintf(stderr, "parsed=%" PRId64 " bytes\n", result->bit_length/8);
h_pprint(stdout, result->ast, 0, 0); h_pprint(stdout, result->ast, 0, 0);
return 0; return 0;
} else { } else {

View file

@ -16,6 +16,7 @@
#include "../src/hammer.h" #include "../src/hammer.h"
#include "../src/glue.h" #include "../src/glue.h"
#include <assert.h> #include <assert.h>
#include <inttypes.h>
/// ///
@ -162,12 +163,12 @@ int main(int argc, char **argv)
parser = init_parser(); parser = init_parser();
inputsize = fread(input, 1, sizeof(input), stdin); inputsize = fread(input, 1, sizeof(input), stdin);
fprintf(stderr, "inputsize=%lu\ninput=", inputsize); fprintf(stderr, "inputsize=%zu\ninput=", inputsize);
fwrite(input, 1, inputsize, stderr); fwrite(input, 1, inputsize, stderr);
result = h_parse(parser, input, inputsize); result = h_parse(parser, input, inputsize);
if(result) { if(result) {
fprintf(stderr, "parsed=%lld bytes\n", result->bit_length/8); fprintf(stderr, "parsed=%" PRId64 " bytes\n", result->bit_length/8);
h_pprint(stdout, result->ast, 0, 0); h_pprint(stdout, result->ast, 0, 0);
return 0; return 0;
} else { } else {

View file

@ -1,6 +1,14 @@
# -*- python -*- # -*- python -*-
Import('env') Import('env')
bindings = []
dist_headers = [
"hammer.h",
"allocator.h",
"glue.h"
]
parsers = ['parsers/%s.c'%s for s in parsers = ['parsers/%s.c'%s for s in
['action', ['action',
'and', 'and',
@ -50,11 +58,18 @@ tests = ['t_benchmark.c',
't_grammar.c', 't_grammar.c',
't_misc.c'] 't_misc.c']
libhammer = env.SharedLibrary('hammer', parsers + backends + misc_hammer_parts) libhammer_shared = env.SharedLibrary('hammer', parsers + backends + misc_hammer_parts)
libhammer = env.StaticLibrary('hammer', parsers + backends + misc_hammer_parts) libhammer_static = env.StaticLibrary('hammer', parsers + backends + misc_hammer_parts)
env.Install("$libpath", [libhammer_static, libhammer_shared])
env.Install("$incpath", dist_headers)
testenv = env.Clone() testenv = env.Clone()
testenv.ParseConfig('pkg-config --cflags --libs glib-2.0') testenv.ParseConfig('pkg-config --cflags --libs glib-2.0')
testenv.Append(LIBS=['hammer'], LIBPATH=['.']) testenv.Append(LIBS=['hammer'], LIBPATH=['.'])
testenv.Program('test_suite', tests + ['test_suite.c']) testenv.Program('test_suite', tests + ['test_suite.c'])
Export("libhammer_static libhammer_shared")
for b in bindings:
env.SConscript(["bindings/%s/SConscript" % b])

View file

@ -436,14 +436,14 @@ static void pprint_transition(FILE *f, const HCFGrammar *g, const HLRTransition
{ {
fputs("-", f); fputs("-", f);
h_pprint_symbol(f, g, t->symbol); h_pprint_symbol(f, g, t->symbol);
fprintf(f, "->%lu", t->to); fprintf(f, "->%zu", t->to);
} }
void h_pprint_lrdfa(FILE *f, const HCFGrammar *g, void h_pprint_lrdfa(FILE *f, const HCFGrammar *g,
const HLRDFA *dfa, unsigned int indent) const HLRDFA *dfa, unsigned int indent)
{ {
for(size_t i=0; i<dfa->nstates; i++) { for(size_t i=0; i<dfa->nstates; i++) {
unsigned int indent2 = indent + fprintf(f, "%4lu: ", i); unsigned int indent2 = indent + fprintf(f, "%4zu: ", i);
h_pprint_lrstate(f, g, dfa->states[i], indent2); h_pprint_lrstate(f, g, dfa->states[i], indent2);
for(HSlistNode *x = dfa->transitions->head; x; x = x->next) { for(HSlistNode *x = dfa->transitions->head; x; x = x->next) {
const HLRTransition *t = x->elem; const HLRTransition *t = x->elem;
@ -463,7 +463,7 @@ void pprint_lraction(FILE *f, const HCFGrammar *g, const HLRAction *action)
if(action->nextstate == HLR_SUCCESS) if(action->nextstate == HLR_SUCCESS)
fputs("s~", f); fputs("s~", f);
else else
fprintf(f, "s%lu", action->nextstate); fprintf(f, "s%zu", action->nextstate);
break; break;
case HLR_REDUCE: case HLR_REDUCE:
fputs("r(", f); fputs("r(", f);
@ -471,7 +471,7 @@ void pprint_lraction(FILE *f, const HCFGrammar *g, const HLRAction *action)
fputs(" -> ", f); fputs(" -> ", f);
#ifdef NDEBUG #ifdef NDEBUG
// if we can't print the production, at least print its length // if we can't print the production, at least print its length
fprintf(f, "[%lu]", action->production.length); fprintf(f, "[%zu]", action->production.length);
#else #else
HCFSequence seq = {action->production.rhs}; HCFSequence seq = {action->production.rhs};
h_pprint_sequence(f, g, &seq); h_pprint_sequence(f, g, &seq);
@ -510,7 +510,7 @@ void h_pprint_lrtable(FILE *f, const HCFGrammar *g, const HLRTable *table,
{ {
for(size_t i=0; i<table->nrows; i++) { for(size_t i=0; i<table->nrows; i++) {
for(unsigned int j=0; j<indent; j++) fputc(' ', f); for(unsigned int j=0; j<indent; j++) fputc(' ', f);
fprintf(f, "%4lu:", i); fprintf(f, "%4zu:", i);
if(table->forall[i]) { if(table->forall[i]) {
fputc(' ', f); fputc(' ', f);
pprint_lraction(f, g, table->forall[i]); pprint_lraction(f, g, table->forall[i]);
@ -531,7 +531,7 @@ void h_pprint_lrtable(FILE *f, const HCFGrammar *g, const HLRTable *table,
#if 0 #if 0
fputs("inadeq=", f); fputs("inadeq=", f);
for(HSlistNode *x=table->inadeq->head; x; x=x->next) { for(HSlistNode *x=table->inadeq->head; x; x=x->next) {
fprintf(f, "%lu ", (uintptr_t)x->elem); fprintf(f, "%zu ", (uintptr_t)x->elem);
} }
fputc('\n', f); fputc('\n', f);
#endif #endif

View file

@ -1,7 +1,7 @@
// Intended to be included from regex_debug.c // Intended to be included from regex_debug.c
#define _GNU_SOURCE #define _GNU_SOURCE
#include <stdio.h> #include <stdio.h>
#include <malloc.h> #include <stdlib.h>

View file

@ -1,9 +1,33 @@
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include <string.h> #include <string.h>
#include "hammer.h" #include "hammer.h"
#include "internal.h" #include "internal.h"
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
void h_benchmark_clock_gettime(struct timespec *ts) {
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
/*
* This returns real time, not CPU time. See http://stackoverflow.com/a/6725161
* Possible solution: http://stackoverflow.com/a/11659289
*/
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts->tv_sec = mts.tv_sec;
ts->tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_THREAD_CPUTIME_ID, ts);
#endif
}
/* /*
Usage: Usage:
Create your parser (i.e., const HParser*), and an array of test cases Create your parser (i.e., const HParser*), and an array of test cases
@ -85,14 +109,14 @@ HBenchmarkResults *h_benchmark__m(HAllocator* mm__, HParser* parser, HParserTest
// TODO: replace this with a posix timer-based benchmark. (cf. timerfd_create, timer_create, setitimer) // TODO: replace this with a posix timer-based benchmark. (cf. timerfd_create, timer_create, setitimer)
int count = 1, cur; int count = 1, cur;
struct timespec ts_start, ts_end; struct timespec ts_start, ts_end;
long long time_diff; int64_t time_diff;
do { do {
count *= 2; // Yes, this means that the first run will run the function twice. This is fine, as we want multiple runs anyway. count *= 2; // Yes, this means that the first run will run the function twice. This is fine, as we want multiple runs anyway.
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts_start); h_benchmark_clock_gettime(&ts_start);
for (cur = 0; cur < count; cur++) { for (cur = 0; cur < count; cur++) {
h_parse_result_free(h_parse(parser, tc->input, tc->length)); h_parse_result_free(h_parse(parser, tc->input, tc->length));
} }
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts_end); h_benchmark_clock_gettime(&ts_end);
// time_diff is in ns // time_diff is in ns
time_diff = (ts_end.tv_sec - ts_start.tv_sec) * 1000000000 + (ts_end.tv_nsec - ts_start.tv_nsec); time_diff = (ts_end.tv_sec - ts_start.tv_sec) * 1000000000 + (ts_end.tv_nsec - ts_start.tv_nsec);
@ -106,11 +130,11 @@ HBenchmarkResults *h_benchmark__m(HAllocator* mm__, HParser* parser, HParserTest
void h_benchmark_report(FILE* stream, HBenchmarkResults* result) { void h_benchmark_report(FILE* stream, HBenchmarkResults* result) {
for (size_t i=0; i<result->len; ++i) { for (size_t i=0; i<result->len; ++i) {
fprintf(stream, "Backend %ld ... \n", i); fprintf(stream, "Backend %zd ... \n", i);
for (size_t j=0; j<result->results[i].n_testcases; ++j) { for (size_t j=0; j<result->results[i].n_testcases; ++j) {
if(result->results[i].cases == NULL) if(result->results[i].cases == NULL)
continue; continue;
fprintf(stream, "Case %ld: %ld ns/parse\n", j, result->results[i].cases[j].parse_time); fprintf(stream, "Case %zd: %zd ns/parse\n", j, result->results[i].cases[j].parse_time);
} }
} }
} }

View file

@ -26,12 +26,12 @@
#define LDB(range,i) (((i)>>LSB(range))&((1<<(MSB(range)-LSB(range)+1))-1)) #define LDB(range,i) (((i)>>LSB(range))&((1<<(MSB(range)-LSB(range)+1))-1))
long long h_read_bits(HInputStream* state, int count, char signed_p) { int64_t h_read_bits(HInputStream* state, int count, char signed_p) {
// BUG: Does not // BUG: Does not
long long out = 0; int64_t out = 0;
int offset = 0; int offset = 0;
int final_shift = 0; int final_shift = 0;
long long msb = ((signed_p ? 1LL:0) << (count - 1)); // 0 if unsigned, else 1 << (nbits - 1) int64_t msb = ((signed_p ? 1LL:0) << (count - 1)); // 0 if unsigned, else 1 << (nbits - 1)
// overflow check... // overflow check...

View file

@ -1,3 +1,4 @@
#include <stdint.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include "hammer.h" #include "hammer.h"
@ -43,7 +44,7 @@ static void h_bit_writer_reserve(HBitWriter* w, size_t nbits) {
} }
void h_bit_writer_put(HBitWriter* w, unsigned long long data, size_t nbits) { void h_bit_writer_put(HBitWriter* w, uint64_t data, size_t nbits) {
assert(nbits > 0); // Less than or equal to zero makes complete nonsense assert(nbits > 0); // Less than or equal to zero makes complete nonsense
// expand size... // expand size...

View file

@ -17,7 +17,9 @@
#ifndef HAMMER_HAMMER__H #ifndef HAMMER_HAMMER__H
#define HAMMER_HAMMER__H #define HAMMER_HAMMER__H
#ifndef HAMMER_INTERNAL__NO_STDARG_H
#include <stdarg.h> #include <stdarg.h>
#endif // HAMMER_INTERNAL__NO_STDARG_H
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include "allocator.h" #include "allocator.h"
@ -97,7 +99,7 @@ typedef struct HParsedToken_ {
*/ */
typedef struct HParseResult_ { typedef struct HParseResult_ {
const HParsedToken *ast; const HParsedToken *ast;
long long bit_length; int64_t bit_length;
HArena * arena; HArena * arena;
} HParseResult; } HParseResult;
@ -180,6 +182,7 @@ typedef struct HBenchmarkResults_ {
rtype_t name(__VA_ARGS__) attr; \ rtype_t name(__VA_ARGS__) attr; \
rtype_t name##__m(HAllocator* mm__, __VA_ARGS__) attr rtype_t name##__m(HAllocator* mm__, __VA_ARGS__) attr
#ifndef HAMMER_INTERNAL__NO_STDARG_H
#define HAMMER_FN_DECL_VARARGS(rtype_t, name, ...) \ #define HAMMER_FN_DECL_VARARGS(rtype_t, name, ...) \
rtype_t name(__VA_ARGS__, ...); \ rtype_t name(__VA_ARGS__, ...); \
rtype_t name##__m(HAllocator* mm__, __VA_ARGS__, ...); \ rtype_t name##__m(HAllocator* mm__, __VA_ARGS__, ...); \
@ -196,7 +199,20 @@ typedef struct HBenchmarkResults_ {
rtype_t name##__v(__VA_ARGS__, va_list ap); \ rtype_t name##__v(__VA_ARGS__, va_list ap); \
rtype_t name##__a(void *args[]); \ rtype_t name##__a(void *args[]); \
rtype_t name##__ma(HAllocator *mm__, void *args[]) rtype_t name##__ma(HAllocator *mm__, void *args[])
#else
#define HAMMER_FN_DECL_VARARGS(rtype_t, name, ...) \
rtype_t name(__VA_ARGS__, ...); \
rtype_t name##__m(HAllocator* mm__, __VA_ARGS__, ...); \
rtype_t name##__a(void *args[]); \
rtype_t name##__ma(HAllocator *mm__, void *args[])
// Note: this drops the attributes on the floor for the __v versions
#define HAMMER_FN_DECL_VARARGS_ATTR(attr, rtype_t, name, ...) \
rtype_t name(__VA_ARGS__, ...) attr; \
rtype_t name##__m(HAllocator* mm__, __VA_ARGS__, ...) attr; \
rtype_t name##__a(void *args[]); \
rtype_t name##__ma(HAllocator *mm__, void *args[])
#endif // HAMMER_INTERNAL__NO_STDARG_H
// }}} // }}}
@ -571,7 +587,7 @@ HAMMER_FN_DECL(void, h_parse_result_free, HParseResult *result);
* Format token into a compact unambiguous form. Useful for parser test cases. * Format token into a compact unambiguous form. Useful for parser test cases.
* Caller is responsible for freeing the result. * Caller is responsible for freeing the result.
*/ */
HAMMER_FN_DECL(char*, h_write_result_unamb, const HParsedToken* tok); char* h_write_result_unamb(const HParsedToken* tok);
/** /**
* Format token to the given output stream. Indent starting at * Format token to the given output stream. Indent starting at
* [indent] spaces, with [delta] spaces between levels. * [indent] spaces, with [delta] spaces between levels.
@ -595,7 +611,7 @@ HBitWriter *h_bit_writer_new(HAllocator* mm__);
/** /**
* TODO: Document me * TODO: Document me
*/ */
void h_bit_writer_put(HBitWriter* w, unsigned long long data, size_t nbits); void h_bit_writer_put(HBitWriter* w, uint64_t data, size_t nbits);
/** /**
* TODO: Document me * TODO: Document me

View file

@ -17,6 +17,7 @@
#ifndef HAMMER_INTERNAL__H #ifndef HAMMER_INTERNAL__H
#define HAMMER_INTERNAL__H #define HAMMER_INTERNAL__H
#include <stdint.h>
#include <assert.h> #include <assert.h>
#include <err.h> #include <err.h>
#include <string.h> #include <string.h>
@ -285,7 +286,7 @@ extern HParserBackendVTable h__glr_backend_vtable;
// TODO(thequux): Set symbol visibility for these functions so that they aren't exported. // TODO(thequux): Set symbol visibility for these functions so that they aren't exported.
long long h_read_bits(HInputStream* state, int count, char signed_p); int64_t h_read_bits(HInputStream* state, int count, char signed_p);
// need to decide if we want to make this public. // need to decide if we want to make this public.
HParseResult* h_do_parse(const HParser* parser, HParseState *state); HParseResult* h_do_parse(const HParser* parser, HParseState *state);
void put_cached(HParseState *ps, const HParser *p, HParseResult *cached); void put_cached(HParseState *ps, const HParser *p, HParseResult *cached);

View file

@ -1,3 +1,4 @@
#include <stdint.h>
#include <assert.h> #include <assert.h>
#include "parser_internal.h" #include "parser_internal.h"

View file

@ -21,6 +21,7 @@
#include "hammer.h" #include "hammer.h"
#include "internal.h" #include "internal.h"
#include <stdlib.h> #include <stdlib.h>
#include <inttypes.h>
typedef struct pp_state { typedef struct pp_state {
int delta; int delta;
@ -49,13 +50,13 @@ void h_pprint(FILE* stream, const HParsedToken* tok, int indent, int delta) {
break; break;
case TT_SINT: case TT_SINT:
if (tok->sint < 0) if (tok->sint < 0)
fprintf(stream, "%*ss -%#lx\n", indent, "", -tok->sint); fprintf(stream, "%*ss -%#" PRIx64 "\n", indent, "", -tok->sint);
else else
fprintf(stream, "%*ss %#lx\n", indent, "", tok->sint); fprintf(stream, "%*ss %#" PRIx64 "\n", indent, "", tok->sint);
break; break;
case TT_UINT: case TT_UINT:
fprintf(stream, "%*su %#lx\n", indent, "", tok->uint); fprintf(stream, "%*su %#" PRIx64 "\n", indent, "", tok->uint);
break; break;
case TT_SEQUENCE: { case TT_SEQUENCE: {
fprintf(stream, "%*s[\n", indent, ""); fprintf(stream, "%*s[\n", indent, "");
@ -80,14 +81,13 @@ void h_pprint(FILE* stream, const HParsedToken* tok, int indent, int delta) {
struct result_buf { struct result_buf {
char* output; char* output;
HAllocator *mm__;
size_t len; size_t len;
size_t capacity; size_t capacity;
}; };
static inline void ensure_capacity(struct result_buf *buf, int amt) { static inline void ensure_capacity(struct result_buf *buf, int amt) {
while (buf->len + amt >= buf->capacity) while (buf->len + amt >= buf->capacity)
buf->output = buf->mm__->realloc(buf->mm__, buf->output, buf->capacity *= 2); buf->output = realloc(buf->output, buf->capacity *= 2);
} }
static inline void append_buf(struct result_buf *buf, const char* input, int len) { static inline void append_buf(struct result_buf *buf, const char* input, int len) {
@ -128,14 +128,14 @@ static void unamb_sub(const HParsedToken* tok, struct result_buf *buf) {
break; break;
case TT_SINT: case TT_SINT:
if (tok->sint < 0) if (tok->sint < 0)
len = asprintf(&tmpbuf, "s-%#lx", -tok->sint); len = asprintf(&tmpbuf, "s-%#" PRIx64, -tok->sint);
else else
len = asprintf(&tmpbuf, "s%#lx", tok->sint); len = asprintf(&tmpbuf, "s%#" PRIx64, tok->sint);
append_buf(buf, tmpbuf, len); append_buf(buf, tmpbuf, len);
free(tmpbuf); free(tmpbuf);
break; break;
case TT_UINT: case TT_UINT:
len = asprintf(&tmpbuf, "u%#lx", tok->uint); len = asprintf(&tmpbuf, "u%#" PRIx64, tok->uint);
append_buf(buf, tmpbuf, len); append_buf(buf, tmpbuf, len);
free(tmpbuf); free(tmpbuf);
break; break;
@ -160,13 +160,9 @@ static void unamb_sub(const HParsedToken* tok, struct result_buf *buf) {
char* h_write_result_unamb(const HParsedToken* tok) { char* h_write_result_unamb(const HParsedToken* tok) {
return h_write_result_unamb__m(&system_allocator, tok);
}
char* h_write_result_unamb__m(HAllocator* mm__, const HParsedToken* tok) {
struct result_buf buf = { struct result_buf buf = {
.output = mm__->alloc(mm__, 16), .output = malloc(16),
.len = 0, .len = 0,
.mm__ = mm__,
.capacity = 16 .capacity = 16
}; };
unamb_sub(tok, &buf); unamb_sub(tok, &buf);

View file

@ -1,3 +1,4 @@
#include <stdint.h>
#include <glib.h> #include <glib.h>
#include "hammer.h" #include "hammer.h"
#include "internal.h" #include "internal.h"
@ -15,44 +16,44 @@
static void test_bitreader_ints(void) { static void test_bitreader_ints(void) {
HInputStream is = MK_INPUT_STREAM("\xFF\xFF\xFF\xFE\x00\x00\x00\x00", 8, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN); HInputStream is = MK_INPUT_STREAM("\xFF\xFF\xFF\xFE\x00\x00\x00\x00", 8, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN);
g_check_cmplong(h_read_bits(&is, 64, true), ==, -0x200000000); g_check_cmp_int64(h_read_bits(&is, 64, true), ==, -0x200000000);
} }
static void test_bitreader_be(void) { static void test_bitreader_be(void) {
HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN); HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN);
g_check_cmpint(h_read_bits(&is, 3, false), ==, 0x03); g_check_cmp_int32(h_read_bits(&is, 3, false), ==, 0x03);
g_check_cmpint(h_read_bits(&is, 8, false), ==, 0x52); g_check_cmp_int32(h_read_bits(&is, 8, false), ==, 0x52);
g_check_cmpint(h_read_bits(&is, 5, false), ==, 0x1A); g_check_cmp_int32(h_read_bits(&is, 5, false), ==, 0x1A);
} }
static void test_bitreader_le(void) { static void test_bitreader_le(void) {
HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_LITTLE_ENDIAN | BYTE_LITTLE_ENDIAN); HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_LITTLE_ENDIAN | BYTE_LITTLE_ENDIAN);
g_check_cmpint(h_read_bits(&is, 3, false), ==, 0x02); g_check_cmp_int32(h_read_bits(&is, 3, false), ==, 0x02);
g_check_cmpint(h_read_bits(&is, 8, false), ==, 0x4D); g_check_cmp_int32(h_read_bits(&is, 8, false), ==, 0x4D);
g_check_cmpint(h_read_bits(&is, 5, false), ==, 0x0B); g_check_cmp_int32(h_read_bits(&is, 5, false), ==, 0x0B);
} }
static void test_largebits_be(void) { static void test_largebits_be(void) {
HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN); HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN);
g_check_cmpint(h_read_bits(&is, 11, false), ==, 0x352); g_check_cmp_int32(h_read_bits(&is, 11, false), ==, 0x352);
g_check_cmpint(h_read_bits(&is, 5, false), ==, 0x1A); g_check_cmp_int32(h_read_bits(&is, 5, false), ==, 0x1A);
} }
static void test_largebits_le(void) { static void test_largebits_le(void) {
HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_LITTLE_ENDIAN | BYTE_LITTLE_ENDIAN); HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_LITTLE_ENDIAN | BYTE_LITTLE_ENDIAN);
g_check_cmpint(h_read_bits(&is, 11, false), ==, 0x26A); g_check_cmp_int32(h_read_bits(&is, 11, false), ==, 0x26A);
g_check_cmpint(h_read_bits(&is, 5, false), ==, 0x0B); g_check_cmp_int32(h_read_bits(&is, 5, false), ==, 0x0B);
} }
static void test_offset_largebits_be(void) { static void test_offset_largebits_be(void) {
HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN); HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN);
g_check_cmpint(h_read_bits(&is, 5, false), ==, 0xD); g_check_cmp_int32(h_read_bits(&is, 5, false), ==, 0xD);
g_check_cmpint(h_read_bits(&is, 11, false), ==, 0x25A); g_check_cmp_int32(h_read_bits(&is, 11, false), ==, 0x25A);
} }
static void test_offset_largebits_le(void) { static void test_offset_largebits_le(void) {
HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_LITTLE_ENDIAN | BYTE_LITTLE_ENDIAN); HInputStream is = MK_INPUT_STREAM("\x6A\x5A", 2, BIT_LITTLE_ENDIAN | BYTE_LITTLE_ENDIAN);
g_check_cmpint(h_read_bits(&is, 5, false), ==, 0xA); g_check_cmp_int32(h_read_bits(&is, 5, false), ==, 0xA);
g_check_cmpint(h_read_bits(&is, 11, false), ==, 0x2D3); g_check_cmp_int32(h_read_bits(&is, 11, false), ==, 0x2D3);
} }

View file

@ -1,10 +1,11 @@
#include <glib.h> #include <glib.h>
#include <stdint.h>
#include "hammer.h" #include "hammer.h"
#include "internal.h" #include "internal.h"
#include "test_suite.h" #include "test_suite.h"
typedef struct { typedef struct {
unsigned long long data; uint64_t data;
size_t nbits; size_t nbits;
} bitwriter_test_elem; // should end with {0,0} } bitwriter_test_elem; // should end with {0,0}
@ -29,7 +30,7 @@ void run_bitwriter_test(bitwriter_test_elem data[], char flags) {
}; };
for (i = 0; data[i].nbits; i++) { for (i = 0; data[i].nbits; i++) {
g_check_cmpulonglong ((unsigned long long)h_read_bits(&input, data[i].nbits, FALSE), ==, data[i].data); g_check_cmp_uint64((uint64_t)h_read_bits(&input, data[i].nbits, FALSE), ==, data[i].data);
} }
} }

View file

@ -3,12 +3,12 @@
#include "hammer.h" #include "hammer.h"
static void test_tt_user(void) { static void test_tt_user(void) {
g_check_cmpint(TT_USER, >, TT_NONE); g_check_cmp_int32(TT_USER, >, TT_NONE);
g_check_cmpint(TT_USER, >, TT_BYTES); g_check_cmp_int32(TT_USER, >, TT_BYTES);
g_check_cmpint(TT_USER, >, TT_SINT); g_check_cmp_int32(TT_USER, >, TT_SINT);
g_check_cmpint(TT_USER, >, TT_UINT); g_check_cmp_int32(TT_USER, >, TT_UINT);
g_check_cmpint(TT_USER, >, TT_SEQUENCE); g_check_cmp_int32(TT_USER, >, TT_SEQUENCE);
g_check_cmpint(TT_USER, >, TT_ERR); g_check_cmp_int32(TT_USER, >, TT_ERR);
} }
void register_misc_tests(void) { void register_misc_tests(void) {

View file

@ -8,21 +8,21 @@
static void test_token(gconstpointer backend) { static void test_token(gconstpointer backend) {
const HParser *token_ = h_token((const uint8_t*)"95\xa2", 3); const HParser *token_ = h_token((const uint8_t*)"95\xa2", 3);
g_check_parse_ok(token_, (HParserBackend)GPOINTER_TO_INT(backend), "95\xa2", 3, "<39.35.a2>"); g_check_parse_match(token_, (HParserBackend)GPOINTER_TO_INT(backend), "95\xa2", 3, "<39.35.a2>");
g_check_parse_failed(token_, (HParserBackend)GPOINTER_TO_INT(backend), "95", 2); g_check_parse_failed(token_, (HParserBackend)GPOINTER_TO_INT(backend), "95", 2);
} }
static void test_ch(gconstpointer backend) { static void test_ch(gconstpointer backend) {
const HParser *ch_ = h_ch(0xa2); const HParser *ch_ = h_ch(0xa2);
g_check_parse_ok(ch_, (HParserBackend)GPOINTER_TO_INT(backend), "\xa2", 1, "u0xa2"); g_check_parse_match(ch_, (HParserBackend)GPOINTER_TO_INT(backend), "\xa2", 1, "u0xa2");
g_check_parse_failed(ch_, (HParserBackend)GPOINTER_TO_INT(backend), "\xa3", 1); g_check_parse_failed(ch_, (HParserBackend)GPOINTER_TO_INT(backend), "\xa3", 1);
} }
static void test_ch_range(gconstpointer backend) { static void test_ch_range(gconstpointer backend) {
const HParser *range_ = h_ch_range('a', 'c'); const HParser *range_ = h_ch_range('a', 'c');
g_check_parse_ok(range_, (HParserBackend)GPOINTER_TO_INT(backend), "b", 1, "u0x62"); g_check_parse_match(range_, (HParserBackend)GPOINTER_TO_INT(backend), "b", 1, "u0x62");
g_check_parse_failed(range_, (HParserBackend)GPOINTER_TO_INT(backend), "d", 1); g_check_parse_failed(range_, (HParserBackend)GPOINTER_TO_INT(backend), "d", 1);
} }
@ -30,62 +30,62 @@ static void test_ch_range(gconstpointer backend) {
static void test_int64(gconstpointer backend) { static void test_int64(gconstpointer backend) {
const HParser *int64_ = h_int64(); const HParser *int64_ = h_int64();
g_check_parse_ok(int64_, (HParserBackend)GPOINTER_TO_INT(backend), "\xff\xff\xff\xfe\x00\x00\x00\x00", 8, "s-0x200000000"); g_check_parse_match(int64_, (HParserBackend)GPOINTER_TO_INT(backend), "\xff\xff\xff\xfe\x00\x00\x00\x00", 8, "s-0x200000000");
g_check_parse_failed(int64_, (HParserBackend)GPOINTER_TO_INT(backend), "\xff\xff\xff\xfe\x00\x00\x00", 7); g_check_parse_failed(int64_, (HParserBackend)GPOINTER_TO_INT(backend), "\xff\xff\xff\xfe\x00\x00\x00", 7);
} }
static void test_int32(gconstpointer backend) { static void test_int32(gconstpointer backend) {
const HParser *int32_ = h_int32(); const HParser *int32_ = h_int32();
g_check_parse_ok(int32_, (HParserBackend)GPOINTER_TO_INT(backend), "\xff\xfe\x00\x00", 4, "s-0x20000"); g_check_parse_match(int32_, (HParserBackend)GPOINTER_TO_INT(backend), "\xff\xfe\x00\x00", 4, "s-0x20000");
g_check_parse_failed(int32_, (HParserBackend)GPOINTER_TO_INT(backend), "\xff\xfe\x00", 3); g_check_parse_failed(int32_, (HParserBackend)GPOINTER_TO_INT(backend), "\xff\xfe\x00", 3);
g_check_parse_ok(int32_, (HParserBackend)GPOINTER_TO_INT(backend), "\x00\x02\x00\x00", 4, "s0x20000"); g_check_parse_match(int32_, (HParserBackend)GPOINTER_TO_INT(backend), "\x00\x02\x00\x00", 4, "s0x20000");
g_check_parse_failed(int32_, (HParserBackend)GPOINTER_TO_INT(backend), "\x00\x02\x00", 3); g_check_parse_failed(int32_, (HParserBackend)GPOINTER_TO_INT(backend), "\x00\x02\x00", 3);
} }
static void test_int16(gconstpointer backend) { static void test_int16(gconstpointer backend) {
const HParser *int16_ = h_int16(); const HParser *int16_ = h_int16();
g_check_parse_ok(int16_, (HParserBackend)GPOINTER_TO_INT(backend), "\xfe\x00", 2, "s-0x200"); g_check_parse_match(int16_, (HParserBackend)GPOINTER_TO_INT(backend), "\xfe\x00", 2, "s-0x200");
g_check_parse_failed(int16_, (HParserBackend)GPOINTER_TO_INT(backend), "\xfe", 1); g_check_parse_failed(int16_, (HParserBackend)GPOINTER_TO_INT(backend), "\xfe", 1);
g_check_parse_ok(int16_, (HParserBackend)GPOINTER_TO_INT(backend), "\x02\x00", 2, "s0x200"); g_check_parse_match(int16_, (HParserBackend)GPOINTER_TO_INT(backend), "\x02\x00", 2, "s0x200");
g_check_parse_failed(int16_, (HParserBackend)GPOINTER_TO_INT(backend), "\x02", 1); g_check_parse_failed(int16_, (HParserBackend)GPOINTER_TO_INT(backend), "\x02", 1);
} }
static void test_int8(gconstpointer backend) { static void test_int8(gconstpointer backend) {
const HParser *int8_ = h_int8(); const HParser *int8_ = h_int8();
g_check_parse_ok(int8_, (HParserBackend)GPOINTER_TO_INT(backend), "\x88", 1, "s-0x78"); g_check_parse_match(int8_, (HParserBackend)GPOINTER_TO_INT(backend), "\x88", 1, "s-0x78");
g_check_parse_failed(int8_, (HParserBackend)GPOINTER_TO_INT(backend), "", 0); g_check_parse_failed(int8_, (HParserBackend)GPOINTER_TO_INT(backend), "", 0);
} }
static void test_uint64(gconstpointer backend) { static void test_uint64(gconstpointer backend) {
const HParser *uint64_ = h_uint64(); const HParser *uint64_ = h_uint64();
g_check_parse_ok(uint64_, (HParserBackend)GPOINTER_TO_INT(backend), "\x00\x00\x00\x02\x00\x00\x00\x00", 8, "u0x200000000"); g_check_parse_match(uint64_, (HParserBackend)GPOINTER_TO_INT(backend), "\x00\x00\x00\x02\x00\x00\x00\x00", 8, "u0x200000000");
g_check_parse_failed(uint64_, (HParserBackend)GPOINTER_TO_INT(backend), "\x00\x00\x00\x02\x00\x00\x00", 7); g_check_parse_failed(uint64_, (HParserBackend)GPOINTER_TO_INT(backend), "\x00\x00\x00\x02\x00\x00\x00", 7);
} }
static void test_uint32(gconstpointer backend) { static void test_uint32(gconstpointer backend) {
const HParser *uint32_ = h_uint32(); const HParser *uint32_ = h_uint32();
g_check_parse_ok(uint32_, (HParserBackend)GPOINTER_TO_INT(backend), "\x00\x02\x00\x00", 4, "u0x20000"); g_check_parse_match(uint32_, (HParserBackend)GPOINTER_TO_INT(backend), "\x00\x02\x00\x00", 4, "u0x20000");
g_check_parse_failed(uint32_, (HParserBackend)GPOINTER_TO_INT(backend), "\x00\x02\x00", 3); g_check_parse_failed(uint32_, (HParserBackend)GPOINTER_TO_INT(backend), "\x00\x02\x00", 3);
} }
static void test_uint16(gconstpointer backend) { static void test_uint16(gconstpointer backend) {
const HParser *uint16_ = h_uint16(); const HParser *uint16_ = h_uint16();
g_check_parse_ok(uint16_, (HParserBackend)GPOINTER_TO_INT(backend), "\x02\x00", 2, "u0x200"); g_check_parse_match(uint16_, (HParserBackend)GPOINTER_TO_INT(backend), "\x02\x00", 2, "u0x200");
g_check_parse_failed(uint16_, (HParserBackend)GPOINTER_TO_INT(backend), "\x02", 1); g_check_parse_failed(uint16_, (HParserBackend)GPOINTER_TO_INT(backend), "\x02", 1);
} }
static void test_uint8(gconstpointer backend) { static void test_uint8(gconstpointer backend) {
const HParser *uint8_ = h_uint8(); const HParser *uint8_ = h_uint8();
g_check_parse_ok(uint8_, (HParserBackend)GPOINTER_TO_INT(backend), "\x78", 1, "u0x78"); g_check_parse_match(uint8_, (HParserBackend)GPOINTER_TO_INT(backend), "\x78", 1, "u0x78");
g_check_parse_failed(uint8_, (HParserBackend)GPOINTER_TO_INT(backend), "", 0); g_check_parse_failed(uint8_, (HParserBackend)GPOINTER_TO_INT(backend), "", 0);
} }
//@MARK_END //@MARK_END
@ -93,7 +93,7 @@ static void test_uint8(gconstpointer backend) {
static void test_int_range(gconstpointer backend) { static void test_int_range(gconstpointer backend) {
const HParser *int_range_ = h_int_range(h_uint8(), 3, 10); const HParser *int_range_ = h_int_range(h_uint8(), 3, 10);
g_check_parse_ok(int_range_, (HParserBackend)GPOINTER_TO_INT(backend), "\x05", 1, "u0x5"); g_check_parse_match(int_range_, (HParserBackend)GPOINTER_TO_INT(backend), "\x05", 1, "u0x5");
g_check_parse_failed(int_range_, (HParserBackend)GPOINTER_TO_INT(backend), "\xb", 1); g_check_parse_failed(int_range_, (HParserBackend)GPOINTER_TO_INT(backend), "\xb", 1);
} }
@ -101,14 +101,14 @@ static void test_int_range(gconstpointer backend) {
static void test_float64(gconstpointer backend) { static void test_float64(gconstpointer backend) {
const HParser *float64_ = h_float64(); const HParser *float64_ = h_float64();
g_check_parse_ok(float64_, (HParserBackend)GPOINTER_TO_INT(backend), "\x3f\xf0\x00\x00\x00\x00\x00\x00", 8, 1.0); g_check_parse_match(float64_, (HParserBackend)GPOINTER_TO_INT(backend), "\x3f\xf0\x00\x00\x00\x00\x00\x00", 8, 1.0);
g_check_parse_failed(float64_, (HParserBackend)GPOINTER_TO_INT(backend), "\x3f\xf0\x00\x00\x00\x00\x00", 7); g_check_parse_failed(float64_, (HParserBackend)GPOINTER_TO_INT(backend), "\x3f\xf0\x00\x00\x00\x00\x00", 7);
} }
static void test_float32(gconstpointer backend) { static void test_float32(gconstpointer backend) {
const HParser *float32_ = h_float32(); const HParser *float32_ = h_float32();
g_check_parse_ok(float32_, (HParserBackend)GPOINTER_TO_INT(backend), "\x3f\x80\x00\x00", 4, 1.0); g_check_parse_match(float32_, (HParserBackend)GPOINTER_TO_INT(backend), "\x3f\x80\x00\x00", 4, 1.0);
g_check_parse_failed(float32_, (HParserBackend)GPOINTER_TO_INT(backend), "\x3f\x80\x00"); g_check_parse_failed(float32_, (HParserBackend)GPOINTER_TO_INT(backend), "\x3f\x80\x00");
} }
#endif #endif
@ -118,21 +118,21 @@ static void test_whitespace(gconstpointer backend) {
const HParser *whitespace_ = h_whitespace(h_ch('a')); const HParser *whitespace_ = h_whitespace(h_ch('a'));
const HParser *whitespace_end = h_whitespace(h_end_p()); const HParser *whitespace_end = h_whitespace(h_end_p());
g_check_parse_ok(whitespace_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "u0x61"); g_check_parse_match(whitespace_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "u0x61");
g_check_parse_ok(whitespace_, (HParserBackend)GPOINTER_TO_INT(backend), " a", 2, "u0x61"); g_check_parse_match(whitespace_, (HParserBackend)GPOINTER_TO_INT(backend), " a", 2, "u0x61");
g_check_parse_ok(whitespace_, (HParserBackend)GPOINTER_TO_INT(backend), " a", 3, "u0x61"); g_check_parse_match(whitespace_, (HParserBackend)GPOINTER_TO_INT(backend), " a", 3, "u0x61");
g_check_parse_ok(whitespace_, (HParserBackend)GPOINTER_TO_INT(backend), "\ta", 2, "u0x61"); g_check_parse_match(whitespace_, (HParserBackend)GPOINTER_TO_INT(backend), "\ta", 2, "u0x61");
g_check_parse_failed(whitespace_, (HParserBackend)GPOINTER_TO_INT(backend), "_a", 2); g_check_parse_failed(whitespace_, (HParserBackend)GPOINTER_TO_INT(backend), "_a", 2);
g_check_parse_ok(whitespace_end, (HParserBackend)GPOINTER_TO_INT(backend), "", 0, "NULL"); g_check_parse_match(whitespace_end, (HParserBackend)GPOINTER_TO_INT(backend), "", 0, "NULL");
g_check_parse_ok(whitespace_end, (HParserBackend)GPOINTER_TO_INT(backend)," ", 2, "NULL"); g_check_parse_match(whitespace_end, (HParserBackend)GPOINTER_TO_INT(backend)," ", 2, "NULL");
g_check_parse_failed(whitespace_end, (HParserBackend)GPOINTER_TO_INT(backend)," x", 3); g_check_parse_failed(whitespace_end, (HParserBackend)GPOINTER_TO_INT(backend)," x", 3);
} }
static void test_left(gconstpointer backend) { static void test_left(gconstpointer backend) {
const HParser *left_ = h_left(h_ch('a'), h_ch(' ')); const HParser *left_ = h_left(h_ch('a'), h_ch(' '));
g_check_parse_ok(left_, (HParserBackend)GPOINTER_TO_INT(backend), "a ", 2, "u0x61"); g_check_parse_match(left_, (HParserBackend)GPOINTER_TO_INT(backend), "a ", 2, "u0x61");
g_check_parse_failed(left_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1); g_check_parse_failed(left_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1);
g_check_parse_failed(left_, (HParserBackend)GPOINTER_TO_INT(backend), " ", 1); g_check_parse_failed(left_, (HParserBackend)GPOINTER_TO_INT(backend), " ", 1);
g_check_parse_failed(left_, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2); g_check_parse_failed(left_, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2);
@ -141,7 +141,7 @@ static void test_left(gconstpointer backend) {
static void test_right(gconstpointer backend) { static void test_right(gconstpointer backend) {
const HParser *right_ = h_right(h_ch(' '), h_ch('a')); const HParser *right_ = h_right(h_ch(' '), h_ch('a'));
g_check_parse_ok(right_, (HParserBackend)GPOINTER_TO_INT(backend), " a", 2, "u0x61"); g_check_parse_match(right_, (HParserBackend)GPOINTER_TO_INT(backend), " a", 2, "u0x61");
g_check_parse_failed(right_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1); g_check_parse_failed(right_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1);
g_check_parse_failed(right_, (HParserBackend)GPOINTER_TO_INT(backend), " ", 1); g_check_parse_failed(right_, (HParserBackend)GPOINTER_TO_INT(backend), " ", 1);
g_check_parse_failed(right_, (HParserBackend)GPOINTER_TO_INT(backend), "ba", 2); g_check_parse_failed(right_, (HParserBackend)GPOINTER_TO_INT(backend), "ba", 2);
@ -150,7 +150,7 @@ static void test_right(gconstpointer backend) {
static void test_middle(gconstpointer backend) { static void test_middle(gconstpointer backend) {
const HParser *middle_ = h_middle(h_ch(' '), h_ch('a'), h_ch(' ')); const HParser *middle_ = h_middle(h_ch(' '), h_ch('a'), h_ch(' '));
g_check_parse_ok(middle_, (HParserBackend)GPOINTER_TO_INT(backend), " a ", 3, "u0x61"); g_check_parse_match(middle_, (HParserBackend)GPOINTER_TO_INT(backend), " a ", 3, "u0x61");
g_check_parse_failed(middle_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1); g_check_parse_failed(middle_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1);
g_check_parse_failed(middle_, (HParserBackend)GPOINTER_TO_INT(backend), " ", 1); g_check_parse_failed(middle_, (HParserBackend)GPOINTER_TO_INT(backend), " ", 1);
g_check_parse_failed(middle_, (HParserBackend)GPOINTER_TO_INT(backend), " a", 2); g_check_parse_failed(middle_, (HParserBackend)GPOINTER_TO_INT(backend), " a", 2);
@ -204,15 +204,15 @@ static void test_action(gconstpointer backend) {
NULL), NULL),
upcase); upcase);
g_check_parse_ok(action_, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2, "(u0x41 u0x42)"); g_check_parse_match(action_, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2, "(u0x41 u0x42)");
g_check_parse_ok(action_, (HParserBackend)GPOINTER_TO_INT(backend), "AB", 2, "(u0x41 u0x42)"); g_check_parse_match(action_, (HParserBackend)GPOINTER_TO_INT(backend), "AB", 2, "(u0x41 u0x42)");
g_check_parse_failed(action_, (HParserBackend)GPOINTER_TO_INT(backend), "XX", 2); g_check_parse_failed(action_, (HParserBackend)GPOINTER_TO_INT(backend), "XX", 2);
} }
static void test_in(gconstpointer backend) { static void test_in(gconstpointer backend) {
uint8_t options[3] = { 'a', 'b', 'c' }; uint8_t options[3] = { 'a', 'b', 'c' };
const HParser *in_ = h_in(options, 3); const HParser *in_ = h_in(options, 3);
g_check_parse_ok(in_, (HParserBackend)GPOINTER_TO_INT(backend), "b", 1, "u0x62"); g_check_parse_match(in_, (HParserBackend)GPOINTER_TO_INT(backend), "b", 1, "u0x62");
g_check_parse_failed(in_, (HParserBackend)GPOINTER_TO_INT(backend), "d", 1); g_check_parse_failed(in_, (HParserBackend)GPOINTER_TO_INT(backend), "d", 1);
} }
@ -220,14 +220,14 @@ static void test_in(gconstpointer backend) {
static void test_not_in(gconstpointer backend) { static void test_not_in(gconstpointer backend) {
uint8_t options[3] = { 'a', 'b', 'c' }; uint8_t options[3] = { 'a', 'b', 'c' };
const HParser *not_in_ = h_not_in(options, 3); const HParser *not_in_ = h_not_in(options, 3);
g_check_parse_ok(not_in_, (HParserBackend)GPOINTER_TO_INT(backend), "d", 1, "u0x64"); g_check_parse_match(not_in_, (HParserBackend)GPOINTER_TO_INT(backend), "d", 1, "u0x64");
g_check_parse_failed(not_in_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1); g_check_parse_failed(not_in_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1);
} }
static void test_end_p(gconstpointer backend) { static void test_end_p(gconstpointer backend) {
const HParser *end_p_ = h_sequence(h_ch('a'), h_end_p(), NULL); const HParser *end_p_ = h_sequence(h_ch('a'), h_end_p(), NULL);
g_check_parse_ok(end_p_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "(u0x61)"); g_check_parse_match(end_p_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "(u0x61)");
g_check_parse_failed(end_p_, (HParserBackend)GPOINTER_TO_INT(backend), "aa", 2); g_check_parse_failed(end_p_, (HParserBackend)GPOINTER_TO_INT(backend), "aa", 2);
} }
@ -240,19 +240,19 @@ static void test_sequence(gconstpointer backend) {
const HParser *sequence_1 = h_sequence(h_ch('a'), h_ch('b'), NULL); const HParser *sequence_1 = h_sequence(h_ch('a'), h_ch('b'), NULL);
const HParser *sequence_2 = h_sequence(h_ch('a'), h_whitespace(h_ch('b')), NULL); const HParser *sequence_2 = h_sequence(h_ch('a'), h_whitespace(h_ch('b')), NULL);
g_check_parse_ok(sequence_1, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2, "(u0x61 u0x62)"); g_check_parse_match(sequence_1, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2, "(u0x61 u0x62)");
g_check_parse_failed(sequence_1, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1); g_check_parse_failed(sequence_1, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1);
g_check_parse_failed(sequence_1, (HParserBackend)GPOINTER_TO_INT(backend), "b", 1); g_check_parse_failed(sequence_1, (HParserBackend)GPOINTER_TO_INT(backend), "b", 1);
g_check_parse_ok(sequence_2, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2, "(u0x61 u0x62)"); g_check_parse_match(sequence_2, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2, "(u0x61 u0x62)");
g_check_parse_ok(sequence_2, (HParserBackend)GPOINTER_TO_INT(backend), "a b", 3, "(u0x61 u0x62)"); g_check_parse_match(sequence_2, (HParserBackend)GPOINTER_TO_INT(backend), "a b", 3, "(u0x61 u0x62)");
g_check_parse_ok(sequence_2, (HParserBackend)GPOINTER_TO_INT(backend), "a b", 4, "(u0x61 u0x62)"); g_check_parse_match(sequence_2, (HParserBackend)GPOINTER_TO_INT(backend), "a b", 4, "(u0x61 u0x62)");
} }
static void test_choice(gconstpointer backend) { static void test_choice(gconstpointer backend) {
const HParser *choice_ = h_choice(h_ch('a'), h_ch('b'), NULL); const HParser *choice_ = h_choice(h_ch('a'), h_ch('b'), NULL);
g_check_parse_ok(choice_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "u0x61"); g_check_parse_match(choice_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "u0x61");
g_check_parse_ok(choice_, (HParserBackend)GPOINTER_TO_INT(backend), "b", 1, "u0x62"); g_check_parse_match(choice_, (HParserBackend)GPOINTER_TO_INT(backend), "b", 1, "u0x62");
g_check_parse_failed(choice_, (HParserBackend)GPOINTER_TO_INT(backend), "c", 1); g_check_parse_failed(choice_, (HParserBackend)GPOINTER_TO_INT(backend), "c", 1);
} }
@ -260,24 +260,24 @@ static void test_butnot(gconstpointer backend) {
const HParser *butnot_1 = h_butnot(h_ch('a'), h_token((const uint8_t*)"ab", 2)); const HParser *butnot_1 = h_butnot(h_ch('a'), h_token((const uint8_t*)"ab", 2));
const HParser *butnot_2 = h_butnot(h_ch_range('0', '9'), h_ch('6')); const HParser *butnot_2 = h_butnot(h_ch_range('0', '9'), h_ch('6'));
g_check_parse_ok(butnot_1, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "u0x61"); g_check_parse_match(butnot_1, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "u0x61");
g_check_parse_failed(butnot_1, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2); g_check_parse_failed(butnot_1, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2);
g_check_parse_ok(butnot_1, (HParserBackend)GPOINTER_TO_INT(backend), "aa", 2, "u0x61"); g_check_parse_match(butnot_1, (HParserBackend)GPOINTER_TO_INT(backend), "aa", 2, "u0x61");
g_check_parse_failed(butnot_2, (HParserBackend)GPOINTER_TO_INT(backend), "6", 1); g_check_parse_failed(butnot_2, (HParserBackend)GPOINTER_TO_INT(backend), "6", 1);
} }
static void test_difference(gconstpointer backend) { static void test_difference(gconstpointer backend) {
const HParser *difference_ = h_difference(h_token((const uint8_t*)"ab", 2), h_ch('a')); const HParser *difference_ = h_difference(h_token((const uint8_t*)"ab", 2), h_ch('a'));
g_check_parse_ok(difference_, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2, "<61.62>"); g_check_parse_match(difference_, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2, "<61.62>");
g_check_parse_failed(difference_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1); g_check_parse_failed(difference_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1);
} }
static void test_xor(gconstpointer backend) { static void test_xor(gconstpointer backend) {
const HParser *xor_ = h_xor(h_ch_range('0', '6'), h_ch_range('5', '9')); const HParser *xor_ = h_xor(h_ch_range('0', '6'), h_ch_range('5', '9'));
g_check_parse_ok(xor_, (HParserBackend)GPOINTER_TO_INT(backend), "0", 1, "u0x30"); g_check_parse_match(xor_, (HParserBackend)GPOINTER_TO_INT(backend), "0", 1, "u0x30");
g_check_parse_ok(xor_, (HParserBackend)GPOINTER_TO_INT(backend), "9", 1, "u0x39"); g_check_parse_match(xor_, (HParserBackend)GPOINTER_TO_INT(backend), "9", 1, "u0x39");
g_check_parse_failed(xor_, (HParserBackend)GPOINTER_TO_INT(backend), "5", 1); g_check_parse_failed(xor_, (HParserBackend)GPOINTER_TO_INT(backend), "5", 1);
g_check_parse_failed(xor_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1); g_check_parse_failed(xor_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1);
} }
@ -285,26 +285,26 @@ static void test_xor(gconstpointer backend) {
static void test_many(gconstpointer backend) { static void test_many(gconstpointer backend) {
const HParser *many_ = h_many(h_choice(h_ch('a'), h_ch('b'), NULL)); const HParser *many_ = h_many(h_choice(h_ch('a'), h_ch('b'), NULL));
g_check_parse_ok(many_, (HParserBackend)GPOINTER_TO_INT(backend), "", 0, "()"); g_check_parse_match(many_, (HParserBackend)GPOINTER_TO_INT(backend), "", 0, "()");
g_check_parse_ok(many_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "(u0x61)"); g_check_parse_match(many_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "(u0x61)");
// g_check_parse_ok(many_, (HParserBackend)GPOINTER_TO_INT(backend), "adef", 4, "(u0x61)"); // g_check_parse_match(many_, (HParserBackend)GPOINTER_TO_INT(backend), "adef", 4, "(u0x61)");
g_check_parse_ok(many_, (HParserBackend)GPOINTER_TO_INT(backend), "b", 1, "(u0x62)"); g_check_parse_match(many_, (HParserBackend)GPOINTER_TO_INT(backend), "b", 1, "(u0x62)");
// g_check_parse_ok(many_, (HParserBackend)GPOINTER_TO_INT(backend), "bdef", 4, "(u0x62)"); // g_check_parse_match(many_, (HParserBackend)GPOINTER_TO_INT(backend), "bdef", 4, "(u0x62)");
g_check_parse_ok(many_, (HParserBackend)GPOINTER_TO_INT(backend), "aabbaba", 7, "(u0x61 u0x61 u0x62 u0x62 u0x61 u0x62 u0x61)"); g_check_parse_match(many_, (HParserBackend)GPOINTER_TO_INT(backend), "aabbaba", 7, "(u0x61 u0x61 u0x62 u0x62 u0x61 u0x62 u0x61)");
// g_check_parse_ok(many_, (HParserBackend)GPOINTER_TO_INT(backend), "aabbabadef", 10, "(u0x61 u0x61 u0x62 u0x62 u0x61 u0x62 u0x61)"); // g_check_parse_match(many_, (HParserBackend)GPOINTER_TO_INT(backend), "aabbabadef", 10, "(u0x61 u0x61 u0x62 u0x62 u0x61 u0x62 u0x61)");
// g_check_parse_ok(many_, (HParserBackend)GPOINTER_TO_INT(backend), "daabbabadef", 11, "()"); // g_check_parse_match(many_, (HParserBackend)GPOINTER_TO_INT(backend), "daabbabadef", 11, "()");
} }
static void test_many1(gconstpointer backend) { static void test_many1(gconstpointer backend) {
const HParser *many1_ = h_many1(h_choice(h_ch('a'), h_ch('b'), NULL)); const HParser *many1_ = h_many1(h_choice(h_ch('a'), h_ch('b'), NULL));
g_check_parse_failed(many1_, (HParserBackend)GPOINTER_TO_INT(backend), "", 0); g_check_parse_failed(many1_, (HParserBackend)GPOINTER_TO_INT(backend), "", 0);
g_check_parse_ok(many1_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "(u0x61)"); g_check_parse_match(many1_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "(u0x61)");
// g_check_parse_ok(many1_, (HParserBackend)GPOINTER_TO_INT(backend), "adef", 4, "(u0x61)"); // g_check_parse_match(many1_, (HParserBackend)GPOINTER_TO_INT(backend), "adef", 4, "(u0x61)");
g_check_parse_ok(many1_, (HParserBackend)GPOINTER_TO_INT(backend), "b", 1, "(u0x62)"); g_check_parse_match(many1_, (HParserBackend)GPOINTER_TO_INT(backend), "b", 1, "(u0x62)");
// g_check_parse_ok(many1_, (HParserBackend)GPOINTER_TO_INT(backend), "bdef", 4, "(u0x62)"); // g_check_parse_match(many1_, (HParserBackend)GPOINTER_TO_INT(backend), "bdef", 4, "(u0x62)");
g_check_parse_ok(many1_, (HParserBackend)GPOINTER_TO_INT(backend), "aabbaba", 7, "(u0x61 u0x61 u0x62 u0x62 u0x61 u0x62 u0x61)"); g_check_parse_match(many1_, (HParserBackend)GPOINTER_TO_INT(backend), "aabbaba", 7, "(u0x61 u0x61 u0x62 u0x62 u0x61 u0x62 u0x61)");
// g_check_parse_ok(many1_, (HParserBackend)GPOINTER_TO_INT(backend), "aabbabadef", 10, "(u0x61 u0x61 u0x62 u0x62 u0x61 u0x62 u0x61)"); // g_check_parse_match(many1_, (HParserBackend)GPOINTER_TO_INT(backend), "aabbabadef", 10, "(u0x61 u0x61 u0x62 u0x62 u0x61 u0x62 u0x61)");
g_check_parse_failed(many1_, (HParserBackend)GPOINTER_TO_INT(backend), "daabbabadef", 11); g_check_parse_failed(many1_, (HParserBackend)GPOINTER_TO_INT(backend), "daabbabadef", 11);
} }
@ -312,16 +312,16 @@ static void test_repeat_n(gconstpointer backend) {
const HParser *repeat_n_ = h_repeat_n(h_choice(h_ch('a'), h_ch('b'), NULL), 2); const HParser *repeat_n_ = h_repeat_n(h_choice(h_ch('a'), h_ch('b'), NULL), 2);
g_check_parse_failed(repeat_n_, (HParserBackend)GPOINTER_TO_INT(backend), "adef", 4); g_check_parse_failed(repeat_n_, (HParserBackend)GPOINTER_TO_INT(backend), "adef", 4);
g_check_parse_ok(repeat_n_, (HParserBackend)GPOINTER_TO_INT(backend), "abdef", 5, "(u0x61 u0x62)"); g_check_parse_match(repeat_n_, (HParserBackend)GPOINTER_TO_INT(backend), "abdef", 5, "(u0x61 u0x62)");
g_check_parse_failed(repeat_n_, (HParserBackend)GPOINTER_TO_INT(backend), "dabdef", 6); g_check_parse_failed(repeat_n_, (HParserBackend)GPOINTER_TO_INT(backend), "dabdef", 6);
} }
static void test_optional(gconstpointer backend) { static void test_optional(gconstpointer backend) {
const HParser *optional_ = h_sequence(h_ch('a'), h_optional(h_choice(h_ch('b'), h_ch('c'), NULL)), h_ch('d'), NULL); const HParser *optional_ = h_sequence(h_ch('a'), h_optional(h_choice(h_ch('b'), h_ch('c'), NULL)), h_ch('d'), NULL);
g_check_parse_ok(optional_, (HParserBackend)GPOINTER_TO_INT(backend), "abd", 3, "(u0x61 u0x62 u0x64)"); g_check_parse_match(optional_, (HParserBackend)GPOINTER_TO_INT(backend), "abd", 3, "(u0x61 u0x62 u0x64)");
g_check_parse_ok(optional_, (HParserBackend)GPOINTER_TO_INT(backend), "acd", 3, "(u0x61 u0x63 u0x64)"); g_check_parse_match(optional_, (HParserBackend)GPOINTER_TO_INT(backend), "acd", 3, "(u0x61 u0x63 u0x64)");
g_check_parse_ok(optional_, (HParserBackend)GPOINTER_TO_INT(backend), "ad", 2, "(u0x61 null u0x64)"); g_check_parse_match(optional_, (HParserBackend)GPOINTER_TO_INT(backend), "ad", 2, "(u0x61 null u0x64)");
g_check_parse_failed(optional_, (HParserBackend)GPOINTER_TO_INT(backend), "aed", 3); g_check_parse_failed(optional_, (HParserBackend)GPOINTER_TO_INT(backend), "aed", 3);
g_check_parse_failed(optional_, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2); g_check_parse_failed(optional_, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2);
g_check_parse_failed(optional_, (HParserBackend)GPOINTER_TO_INT(backend), "ac", 2); g_check_parse_failed(optional_, (HParserBackend)GPOINTER_TO_INT(backend), "ac", 2);
@ -330,27 +330,27 @@ static void test_optional(gconstpointer backend) {
static void test_ignore(gconstpointer backend) { static void test_ignore(gconstpointer backend) {
const HParser *ignore_ = h_sequence(h_ch('a'), h_ignore(h_ch('b')), h_ch('c'), NULL); const HParser *ignore_ = h_sequence(h_ch('a'), h_ignore(h_ch('b')), h_ch('c'), NULL);
g_check_parse_ok(ignore_, (HParserBackend)GPOINTER_TO_INT(backend), "abc", 3, "(u0x61 u0x63)"); g_check_parse_match(ignore_, (HParserBackend)GPOINTER_TO_INT(backend), "abc", 3, "(u0x61 u0x63)");
g_check_parse_failed(ignore_, (HParserBackend)GPOINTER_TO_INT(backend), "ac", 2); g_check_parse_failed(ignore_, (HParserBackend)GPOINTER_TO_INT(backend), "ac", 2);
} }
static void test_sepBy(gconstpointer backend) { static void test_sepBy(gconstpointer backend) {
const HParser *sepBy_ = h_sepBy(h_choice(h_ch('1'), h_ch('2'), h_ch('3'), NULL), h_ch(',')); const HParser *sepBy_ = h_sepBy(h_choice(h_ch('1'), h_ch('2'), h_ch('3'), NULL), h_ch(','));
g_check_parse_ok(sepBy_, (HParserBackend)GPOINTER_TO_INT(backend), "1,2,3", 5, "(u0x31 u0x32 u0x33)"); g_check_parse_match(sepBy_, (HParserBackend)GPOINTER_TO_INT(backend), "1,2,3", 5, "(u0x31 u0x32 u0x33)");
g_check_parse_ok(sepBy_, (HParserBackend)GPOINTER_TO_INT(backend), "1,3,2", 5, "(u0x31 u0x33 u0x32)"); g_check_parse_match(sepBy_, (HParserBackend)GPOINTER_TO_INT(backend), "1,3,2", 5, "(u0x31 u0x33 u0x32)");
g_check_parse_ok(sepBy_, (HParserBackend)GPOINTER_TO_INT(backend), "1,3", 3, "(u0x31 u0x33)"); g_check_parse_match(sepBy_, (HParserBackend)GPOINTER_TO_INT(backend), "1,3", 3, "(u0x31 u0x33)");
g_check_parse_ok(sepBy_, (HParserBackend)GPOINTER_TO_INT(backend), "3", 1, "(u0x33)"); g_check_parse_match(sepBy_, (HParserBackend)GPOINTER_TO_INT(backend), "3", 1, "(u0x33)");
g_check_parse_ok(sepBy_, (HParserBackend)GPOINTER_TO_INT(backend), "", 0, "()"); g_check_parse_match(sepBy_, (HParserBackend)GPOINTER_TO_INT(backend), "", 0, "()");
} }
static void test_sepBy1(gconstpointer backend) { static void test_sepBy1(gconstpointer backend) {
const HParser *sepBy1_ = h_sepBy1(h_choice(h_ch('1'), h_ch('2'), h_ch('3'), NULL), h_ch(',')); const HParser *sepBy1_ = h_sepBy1(h_choice(h_ch('1'), h_ch('2'), h_ch('3'), NULL), h_ch(','));
g_check_parse_ok(sepBy1_, (HParserBackend)GPOINTER_TO_INT(backend), "1,2,3", 5, "(u0x31 u0x32 u0x33)"); g_check_parse_match(sepBy1_, (HParserBackend)GPOINTER_TO_INT(backend), "1,2,3", 5, "(u0x31 u0x32 u0x33)");
g_check_parse_ok(sepBy1_, (HParserBackend)GPOINTER_TO_INT(backend), "1,3,2", 5, "(u0x31 u0x33 u0x32)"); g_check_parse_match(sepBy1_, (HParserBackend)GPOINTER_TO_INT(backend), "1,3,2", 5, "(u0x31 u0x33 u0x32)");
g_check_parse_ok(sepBy1_, (HParserBackend)GPOINTER_TO_INT(backend), "1,3", 3, "(u0x31 u0x33)"); g_check_parse_match(sepBy1_, (HParserBackend)GPOINTER_TO_INT(backend), "1,3", 3, "(u0x31 u0x33)");
g_check_parse_ok(sepBy1_, (HParserBackend)GPOINTER_TO_INT(backend), "3", 1, "(u0x33)"); g_check_parse_match(sepBy1_, (HParserBackend)GPOINTER_TO_INT(backend), "3", 1, "(u0x33)");
g_check_parse_failed(sepBy1_, (HParserBackend)GPOINTER_TO_INT(backend), "", 0); g_check_parse_failed(sepBy1_, (HParserBackend)GPOINTER_TO_INT(backend), "", 0);
} }
@ -359,9 +359,9 @@ static void test_epsilon_p(gconstpointer backend) {
const HParser *epsilon_p_2 = h_sequence(h_epsilon_p(), h_ch('a'), NULL); const HParser *epsilon_p_2 = h_sequence(h_epsilon_p(), h_ch('a'), NULL);
const HParser *epsilon_p_3 = h_sequence(h_ch('a'), h_epsilon_p(), NULL); const HParser *epsilon_p_3 = h_sequence(h_ch('a'), h_epsilon_p(), NULL);
g_check_parse_ok(epsilon_p_1, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2, "(u0x61 u0x62)"); g_check_parse_match(epsilon_p_1, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2, "(u0x61 u0x62)");
g_check_parse_ok(epsilon_p_2, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "(u0x61)"); g_check_parse_match(epsilon_p_2, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "(u0x61)");
g_check_parse_ok(epsilon_p_3, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "(u0x61)"); g_check_parse_match(epsilon_p_3, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "(u0x61)");
} }
bool validate_test_ab(HParseResult *p) { bool validate_test_ab(HParseResult *p) {
@ -378,8 +378,8 @@ static void test_attr_bool(gconstpointer backend) {
const HParser *ab_ = h_attr_bool(h_many1(h_choice(h_ch('a'), h_ch('b'), NULL)), const HParser *ab_ = h_attr_bool(h_many1(h_choice(h_ch('a'), h_ch('b'), NULL)),
validate_test_ab); validate_test_ab);
g_check_parse_ok(ab_, (HParserBackend)GPOINTER_TO_INT(backend), "aa", 2, "(u0x61 u0x61)"); g_check_parse_match(ab_, (HParserBackend)GPOINTER_TO_INT(backend), "aa", 2, "(u0x61 u0x61)");
g_check_parse_ok(ab_, (HParserBackend)GPOINTER_TO_INT(backend), "bb", 2, "(u0x62 u0x62)"); g_check_parse_match(ab_, (HParserBackend)GPOINTER_TO_INT(backend), "bb", 2, "(u0x62 u0x62)");
g_check_parse_failed(ab_, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2); g_check_parse_failed(ab_, (HParserBackend)GPOINTER_TO_INT(backend), "ab", 2);
} }
@ -388,9 +388,9 @@ static void test_and(gconstpointer backend) {
const HParser *and_2 = h_sequence(h_and(h_ch('0')), h_ch('1'), NULL); const HParser *and_2 = h_sequence(h_and(h_ch('0')), h_ch('1'), NULL);
const HParser *and_3 = h_sequence(h_ch('1'), h_and(h_ch('2')), NULL); const HParser *and_3 = h_sequence(h_ch('1'), h_and(h_ch('2')), NULL);
g_check_parse_ok(and_1, (HParserBackend)GPOINTER_TO_INT(backend), "0", 1, "(u0x30)"); g_check_parse_match(and_1, (HParserBackend)GPOINTER_TO_INT(backend), "0", 1, "(u0x30)");
g_check_parse_failed(and_2, (HParserBackend)GPOINTER_TO_INT(backend), "0", 1); g_check_parse_failed(and_2, (HParserBackend)GPOINTER_TO_INT(backend), "0", 1);
g_check_parse_ok(and_3, (HParserBackend)GPOINTER_TO_INT(backend), "12", 2, "(u0x31)"); g_check_parse_match(and_3, (HParserBackend)GPOINTER_TO_INT(backend), "12", 2, "(u0x31)");
} }
static void test_not(gconstpointer backend) { static void test_not(gconstpointer backend) {
@ -400,10 +400,10 @@ static void test_not(gconstpointer backend) {
h_token((const uint8_t*)"++", 2), h_token((const uint8_t*)"++", 2),
NULL), h_ch('b'), NULL); NULL), h_ch('b'), NULL);
g_check_parse_ok(not_1, (HParserBackend)GPOINTER_TO_INT(backend), "a+b", 3, "(u0x61 u0x2b u0x62)"); g_check_parse_match(not_1, (HParserBackend)GPOINTER_TO_INT(backend), "a+b", 3, "(u0x61 u0x2b u0x62)");
g_check_parse_failed(not_1, (HParserBackend)GPOINTER_TO_INT(backend), "a++b", 4); g_check_parse_failed(not_1, (HParserBackend)GPOINTER_TO_INT(backend), "a++b", 4);
g_check_parse_ok(not_2, (HParserBackend)GPOINTER_TO_INT(backend), "a+b", 3, "(u0x61 (u0x2b) u0x62)"); g_check_parse_match(not_2, (HParserBackend)GPOINTER_TO_INT(backend), "a+b", 3, "(u0x61 (u0x2b) u0x62)");
g_check_parse_ok(not_2, (HParserBackend)GPOINTER_TO_INT(backend), "a++b", 4, "(u0x61 <2b.2b> u0x62)"); g_check_parse_match(not_2, (HParserBackend)GPOINTER_TO_INT(backend), "a++b", 4, "(u0x61 <2b.2b> u0x62)");
} }
static void test_leftrec(gconstpointer backend) { static void test_leftrec(gconstpointer backend) {
@ -423,9 +423,9 @@ static void test_rightrec(gconstpointer backend) {
HParser *rr_ = h_indirect(); HParser *rr_ = h_indirect();
h_bind_indirect(rr_, h_choice(h_sequence(a_, rr_, NULL), h_epsilon_p(), NULL)); h_bind_indirect(rr_, h_choice(h_sequence(a_, rr_, NULL), h_epsilon_p(), NULL));
g_check_parse_ok(rr_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "(u0x61)"); g_check_parse_match(rr_, (HParserBackend)GPOINTER_TO_INT(backend), "a", 1, "(u0x61)");
g_check_parse_ok(rr_, (HParserBackend)GPOINTER_TO_INT(backend), "aa", 2, "(u0x61 (u0x61))"); g_check_parse_match(rr_, (HParserBackend)GPOINTER_TO_INT(backend), "aa", 2, "(u0x61 (u0x61))");
g_check_parse_ok(rr_, (HParserBackend)GPOINTER_TO_INT(backend), "aaa", 3, "(u0x61 (u0x61 (u0x61)))"); g_check_parse_match(rr_, (HParserBackend)GPOINTER_TO_INT(backend), "aaa", 3, "(u0x61 (u0x61 (u0x61)))");
} }
static void test_ambiguous(gconstpointer backend) { static void test_ambiguous(gconstpointer backend) {
@ -435,9 +435,9 @@ static void test_ambiguous(gconstpointer backend) {
h_bind_indirect(E_, h_choice(h_sequence(E_, p_, E_, NULL), d_, NULL)); h_bind_indirect(E_, h_choice(h_sequence(E_, p_, E_, NULL), d_, NULL));
HParser *expr_ = h_action(E_, h_act_flatten); HParser *expr_ = h_action(E_, h_act_flatten);
g_check_parse_ok(expr_, (HParserBackend)GPOINTER_TO_INT(backend), "d", 1, "(u0x64)"); g_check_parse_match(expr_, (HParserBackend)GPOINTER_TO_INT(backend), "d", 1, "(u0x64)");
g_check_parse_ok(expr_, (HParserBackend)GPOINTER_TO_INT(backend), "d+d", 3, "(u0x64 u0x2b u0x64)"); g_check_parse_match(expr_, (HParserBackend)GPOINTER_TO_INT(backend), "d+d", 3, "(u0x64 u0x2b u0x64)");
g_check_parse_ok(expr_, (HParserBackend)GPOINTER_TO_INT(backend), "d+d+d", 5, "(u0x64 u0x2b u0x64 u0x2b u0x64)"); g_check_parse_match(expr_, (HParserBackend)GPOINTER_TO_INT(backend), "d+d+d", 5, "(u0x64 u0x2b u0x64 u0x2b u0x64)");
g_check_parse_failed(expr_, (HParserBackend)GPOINTER_TO_INT(backend), "d+", 2); g_check_parse_failed(expr_, (HParserBackend)GPOINTER_TO_INT(backend), "d+", 2);
} }

View file

@ -17,7 +17,9 @@
#ifndef HAMMER_TEST_SUITE__H #ifndef HAMMER_TEST_SUITE__H
#define HAMMER_TEST_SUITE__H #define HAMMER_TEST_SUITE__H
#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <inttypes.h>
// Equivalent to g_assert_*, but not using g_assert... // Equivalent to g_assert_*, but not using g_assert...
#define g_check_inttype(fmt, typ, n1, op, n2) do { \ #define g_check_inttype(fmt, typ, n1, op, n2) do { \
@ -96,7 +98,28 @@
} \ } \
} while(0) } while(0)
#define g_check_parse_ok(parser, backend, input, inp_len, result) do { \ #define g_check_parse_ok(parser, backend, input, inp_len) do { \
int skip = h_compile((HParser *)(parser), (HParserBackend) backend, NULL); \
if(skip) { \
g_test_message("Backend not applicable, skipping test"); \
break; \
} \
HParseResult *res = h_parse(parser, (const uint8_t*)input, inp_len); \
if (!res) { \
g_test_message("Parse failed on line %d", __LINE__); \
g_test_fail(); \
} else { \
HArenaStats stats; \
h_allocator_stats(res->arena, &stats); \
g_test_message("Parse used %zd bytes, wasted %zd bytes. " \
"Inefficiency: %5f%%", \
stats.used, stats.wasted, \
stats.wasted * 100. / (stats.used+stats.wasted)); \
h_delete_arena(res->arena); \
} \
} while(0)
#define g_check_parse_match(parser, backend, input, inp_len, result) do { \
int skip = h_compile((HParser *)(parser), (HParserBackend) backend, NULL); \ int skip = h_compile((HParser *)(parser), (HParserBackend) backend, NULL); \
if(skip) { \ if(skip) { \
g_test_message("Backend not applicable, skipping test"); \ g_test_message("Backend not applicable, skipping test"); \
@ -109,7 +132,7 @@
} else { \ } else { \
char* cres = h_write_result_unamb(res->ast); \ char* cres = h_write_result_unamb(res->ast); \
g_check_string(cres, ==, result); \ g_check_string(cres, ==, result); \
system_allocator.free(&system_allocator, cres); \ free(cres); \
HArenaStats stats; \ HArenaStats stats; \
h_allocator_stats(res->arena, &stats); \ h_allocator_stats(res->arena, &stats); \
g_test_message("Parse used %zd bytes, wasted %zd bytes. " \ g_test_message("Parse used %zd bytes, wasted %zd bytes. " \
@ -138,7 +161,7 @@
size_t expected = n; \ size_t expected = n; \
size_t actual = (table)->used; \ size_t actual = (table)->used; \
if(actual != expected) { \ if(actual != expected) { \
g_test_message("Check failed: table size should have been %lu, but was %lu", \ g_test_message("Check failed: table size should have been %zu, but was %zu", \
expected, actual); \ expected, actual); \
g_test_fail(); \ g_test_fail(); \
} \ } \
@ -161,6 +184,7 @@
} while(0) } while(0)
// This stuff needs to be made internal-only; it has no use in user-level test suites
#define g_check_terminal(grammar, parser) \ #define g_check_terminal(grammar, parser) \
g_check_hashtable_absent(grammar->nts, h_desugar(&system_allocator, NULL, parser)) g_check_hashtable_absent(grammar->nts, h_desugar(&system_allocator, NULL, parser))
@ -188,12 +212,10 @@
#define g_check_cmpint(n1, op, n2) g_check_inttype("%d", int, n1, op, n2) #define g_check_cmp_int32(n1, op, n2) g_check_inttype("%d", int32_t, n1, op, n2)
#define g_check_cmplong(n1, op, n2) g_check_inttype("%ld", long, n1, op, n2) #define g_check_cmp_int64(n1, op, n2) g_check_inttype("%" PRId64, int64_t, n1, op, n2)
#define g_check_cmplonglong(n1, op, n2) g_check_inttype("%lld", long long, n1, op, n2) #define g_check_cmp_uint32(n1, op, n2) g_check_inttype("%u", uint32_t, n1, op, n2)
#define g_check_cmpuint(n1, op, n2) g_check_inttype("%u", unsigned int, n1, op, n2) #define g_check_cmp_uint64(n1, op, n2) g_check_inttype("%" PRIu64, uint64_t, n1, op, n2)
#define g_check_cmpulong(n1, op, n2) g_check_inttype("%lu", unsigned long, n1, op, n2)
#define g_check_cmpulonglong(n1, op, n2) g_check_inttype("%llu", unsigned long long, n1, op, n2)
#define g_check_cmpfloat(n1, op, n2) g_check_inttype("%g", float, n1, op, n2) #define g_check_cmpfloat(n1, op, n2) g_check_inttype("%g", float, n1, op, n2)
#define g_check_cmpdouble(n1, op, n2) g_check_inttype("%g", double, n1, op, n2) #define g_check_cmpdouble(n1, op, n2) g_check_inttype("%g", double, n1, op, n2)