Finished Python bindings

This commit is contained in:
Dan Hirsch 2013-11-25 02:49:02 -06:00
parent 43357bbcda
commit cdc7ea8381
2 changed files with 315 additions and 228 deletions

View file

@ -12,43 +12,13 @@
%apply (uint8_t* str, size_t len) {(const uint8_t* charset, size_t length)}
%rename(_h_ch) h_ch;
%pythoncode %{
def h_ch(ch):
if isinstance(ch, str) or isinstance(ch, unicode):
return h_token(ch)
else:
return _h_ch(ch)
%}
%rename(_h_ch_range) h_ch_range;
%pythoncode %{
def h_ch_range(c1, c2):
dostr = isinstance(c1, str)
dostr2 = isinstance(c2, str)
if isinstance(c1, unicode) or isinstance(c2, unicode):
raise TypeError("ch_range only works on bytes")
if dostr != dostr2:
raise TypeError("Both arguments to ch_range must be the same type")
if dostr:
return h_action(_h_ch_range(c1, c2), chr)
else:
return _h_ch_range(c1, c2)
%}
%rename(_h_in) h_in;
%rename(_h_not_in) h_not_in;
%pythoncode %{
def h_in(charset):
return h_action(_h_in(charset), chr)
def h_not_in(charset):
return h_action(_h_not_in(charset), chr)
%}
%rename("_%s") "";
// %rename(_h_ch) h_ch;
%inline {
static PyObject *_helper_Placeholder = NULL, *_helper_ParseError = NULL;
static void _register_helpers(PyObject* parse_error, PyObject *placeholder) {
static void register_helpers(PyObject* parse_error, PyObject *placeholder) {
_helper_ParseError = parse_error;
_helper_Placeholder = placeholder;
}
@ -260,6 +230,115 @@
}
%rename("%s") "";
%extend HParser_ {
HParseResult* parse(const uint8_t* input, size_t length) {
return h_parse($self, input, length);
}
bool compile(HParserBackend backend) {
return h_compile($self, backend, NULL) == 0;
}
PyObject* __dir__() {
PyObject* ret = PyList_New(2);
PyList_SET_ITEM(ret, 0, PyString_FromString("parse"));
PyList_SET_ITEM(ret, 1, PyString_FromString("compile"));
return ret;
}
}
%pythoncode %{
def action(p, act):
return _h_action(p, act)
def attr_bool(p, pred):
return _h_attr_bool(p, pred)
def ch(ch):
if isinstance(ch, str) or isinstance(ch, unicode):
return token(ch)
else:
return _h_ch(ch)
def ch_range(c1, c2):
dostr = isinstance(c1, str)
dostr2 = isinstance(c2, str)
if isinstance(c1, unicode) or isinstance(c2, unicode):
raise TypeError("ch_range only works on bytes")
if dostr != dostr2:
raise TypeError("Both arguments to ch_range must be the same type")
if dostr:
return action(_h_ch_range(c1, c2), chr)
else:
return _h_ch_range(c1, c2)
def epsilon_p(): return _h_epsilon_p()
def end_p():
return _h_end_p()
def in_(charset):
return action(_h_in(charset), chr)
def not_in(charset):
return action(_h_not_in(charset), chr)
def not_(p): return _h_not(p)
def int_range(p, i1, i2):
return _h_int_range(p, i1, i2)
def token(string):
return _h_token(string)
def whitespace(p):
return _h_whitespace(p)
def xor(p1, p2):
return _h_xor(p1, p2)
def butnot(p1, p2):
return _h_butnot(p1, p2)
def and_(p1):
return _h_and(p1)
def difference(p1, p2):
return _h_difference(p1, p2)
def sepBy(p, sep): return _h_sepBy(p, sep)
def sepBy1(p, sep): return _h_sepBy1(p, sep)
def many(p): return _h_many(p)
def many1(p): return _h_many1(p)
def repeat_n(p, n): return _h_repeat_n(p, n)
def choice(*args): return _h_choice__a(list(args))
def sequence(*args): return _h_sequence__a(list(args))
def optional(p): return _h_optional(p)
def nothing_p(): return _h_nothing_p()
def ignore(p): return _h_ignore(p)
def left(p1, p2): return _h_left(p1, p2)
def middle(p1, p2, p3): return _h_middle(p1, p2, p3)
def right(p1, p2): return _h_right(p1, p2)
class HIndirectParser(_HParser_):
def __init__(self):
# Shoves the guts of an _HParser_ into a HIndirectParser.
tret = _h_indirect()
self.__dict__.clear()
self.__dict__.update(tret.__dict__)
def __dir__(self):
return super(HIndirectParser, self).__dir__() + ['bind']
def bind(self, parser):
_h_bind_indirect(self, parser)
def indirect():
return HIndirectParser()
def bind_indirect(indirect, new_parser):
indirect.bind(new_parser)
def uint8(): return _h_uint8()
def uint16(): return _h_uint16()
def uint32(): return _h_uint32()
def uint64(): return _h_uint64()
def int8(): return _h_int8()
def int16(): return _h_int16()
def int32(): return _h_int32()
def int64(): return _h_int64()
%}
#endif