2013-11-16 07:56:47 +01:00
|
|
|
%module hammer
|
2013-11-19 22:59:14 -06:00
|
|
|
%nodefaultctor;
|
2013-11-23 19:53:58 -06:00
|
|
|
|
2013-11-19 02:41:45 -06:00
|
|
|
%nodefaultctor;
|
2013-11-16 07:56:47 +01:00
|
|
|
|
2013-11-17 15:55:38 -06:00
|
|
|
%include "stdint.i"
|
2013-11-18 17:19:46 -06:00
|
|
|
|
|
|
|
|
#if defined(SWIGPYTHON)
|
2013-11-19 00:18:25 -06:00
|
|
|
%ignore HCountedArray_;
|
2013-11-22 19:42:02 -06:00
|
|
|
%apply (char *STRING, size_t LENGTH) {(uint8_t* str, size_t len)}
|
|
|
|
|
%apply (uint8_t* str, size_t len) {(const uint8_t* input, size_t length)}
|
|
|
|
|
%apply (uint8_t* str, size_t len) {(const uint8_t* str, const size_t len)}
|
|
|
|
|
%apply (uint8_t* str, size_t len) {(const uint8_t* charset, size_t length)}
|
2013-11-23 19:53:58 -06:00
|
|
|
|
|
|
|
|
|
2013-11-25 02:49:02 -06:00
|
|
|
%rename("_%s") "";
|
|
|
|
|
// %rename(_h_ch) h_ch;
|
2013-11-23 19:53:58 -06:00
|
|
|
|
|
|
|
|
%inline {
|
|
|
|
|
static PyObject *_helper_Placeholder = NULL, *_helper_ParseError = NULL;
|
|
|
|
|
|
2013-11-25 02:49:02 -06:00
|
|
|
static void register_helpers(PyObject* parse_error, PyObject *placeholder) {
|
2013-11-23 19:53:58 -06:00
|
|
|
_helper_ParseError = parse_error;
|
|
|
|
|
_helper_Placeholder = placeholder;
|
|
|
|
|
}
|
2013-11-18 21:14:44 -06:00
|
|
|
}
|
2013-11-23 19:53:58 -06:00
|
|
|
|
|
|
|
|
%pythoncode %{
|
|
|
|
|
class Placeholder(object):
|
|
|
|
|
"""The python equivalent of TT_NONE"""
|
|
|
|
|
def __str__(self):
|
|
|
|
|
return "Placeholder"
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return "Placeholder"
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
|
return type(self) == type(other)
|
|
|
|
|
class ParseError(Exception):
|
|
|
|
|
"""The parse failed; the message may have more information"""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
_hammer._register_helpers(ParseError,
|
|
|
|
|
Placeholder)
|
|
|
|
|
%}
|
|
|
|
|
|
2013-11-19 02:41:45 -06:00
|
|
|
%typemap(in) void*[] {
|
|
|
|
|
if (PyList_Check($input)) {
|
|
|
|
|
Py_INCREF($input);
|
|
|
|
|
int size = PyList_Size($input);
|
|
|
|
|
int i = 0;
|
|
|
|
|
int res = 0;
|
2013-11-22 19:42:02 -06:00
|
|
|
$1 = (void**)malloc((size+1)*sizeof(HParser*));
|
2013-11-19 02:41:45 -06:00
|
|
|
for (i=0; i<size; i++) {
|
|
|
|
|
PyObject *o = PyList_GetItem($input, i);
|
|
|
|
|
res = SWIG_ConvertPtr(o, &($1[i]), SWIGTYPE_p_HParser_, 0 | 0);
|
|
|
|
|
if (!SWIG_IsOK(res)) {
|
|
|
|
|
SWIG_exception_fail(SWIG_ArgError(res), "that wasn't an HParser" );
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-22 19:42:02 -06:00
|
|
|
$1[size] = NULL;
|
2013-11-19 02:41:45 -06:00
|
|
|
} else {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError, "__a functions take lists of parsers as their argument");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-18 21:50:28 -06:00
|
|
|
%typemap(in) uint8_t {
|
|
|
|
|
if (PyInt_Check($input)) {
|
|
|
|
|
$1 = PyInt_AsLong($input);
|
|
|
|
|
}
|
|
|
|
|
else if (!PyString_Check($input)) {
|
|
|
|
|
PyErr_SetString(PyExc_ValueError, "Expecting a string");
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
|
|
|
|
$1 = *(uint8_t*)PyString_AsString($input);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
%typemap(out) HBytes* {
|
|
|
|
|
$result = PyString_FromStringAndSize((char*)$1->token, $1->len);
|
|
|
|
|
}
|
2013-11-19 00:18:25 -06:00
|
|
|
%typemap(out) struct HCountedArray_* {
|
|
|
|
|
int i;
|
|
|
|
|
$result = PyList_New($1->used);
|
|
|
|
|
for (i=0; i<$1->used; i++) {
|
|
|
|
|
HParsedToken *t = $1->elements[i];
|
|
|
|
|
PyObject *o = SWIG_NewPointerObj(SWIG_as_voidptr(t), SWIGTYPE_p_HParsedToken_, 0 | 0);
|
|
|
|
|
PyList_SetItem($result, i, o);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-23 16:40:57 -06:00
|
|
|
%typemap(out) struct HParseResult_* {
|
|
|
|
|
if ($1 == NULL) {
|
|
|
|
|
// TODO: raise parse failure
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
$result = Py_None;
|
|
|
|
|
} else {
|
|
|
|
|
$result = hpt_to_python($1->ast);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-23 19:53:58 -06:00
|
|
|
%typemap(newfree) struct HParseResult_* {
|
|
|
|
|
h_parse_result_free($input);
|
|
|
|
|
}
|
2013-11-23 16:40:57 -06:00
|
|
|
%inline %{
|
|
|
|
|
static int h_tt_python;
|
|
|
|
|
%}
|
|
|
|
|
%init %{
|
|
|
|
|
h_tt_python = h_allocate_token_type("com.upstandinghackers.hammer.python");
|
|
|
|
|
%}
|
|
|
|
|
|
2013-11-23 19:53:58 -06:00
|
|
|
|
|
|
|
|
|
2013-11-24 18:43:14 -06:00
|
|
|
|
|
|
|
|
%typemap(in) (HPredicate pred, void* user_data) {
|
2013-11-23 16:40:57 -06:00
|
|
|
Py_INCREF($input);
|
|
|
|
|
$2 = $input;
|
|
|
|
|
$1 = call_predicate;
|
|
|
|
|
}
|
2013-11-24 18:43:14 -06:00
|
|
|
|
2013-11-23 16:40:57 -06:00
|
|
|
%typemap(in) (const HAction a, void* user_data) {
|
|
|
|
|
Py_INCREF($input);
|
|
|
|
|
$2 = $input;
|
|
|
|
|
$1 = call_action;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-24 18:43:14 -06:00
|
|
|
%inline %{
|
|
|
|
|
|
2013-11-23 16:40:57 -06:00
|
|
|
struct HParsedToken_;
|
|
|
|
|
struct HParseResult_;
|
|
|
|
|
static PyObject* hpt_to_python(const struct HParsedToken_ *token);
|
|
|
|
|
|
|
|
|
|
static struct HParsedToken_* call_action(const struct HParseResult_ *p, void* user_data);
|
2013-11-24 18:43:14 -06:00
|
|
|
static int call_predicate(const struct HParseResult_ *p, void* user_data);
|
|
|
|
|
%}
|
2013-11-18 17:19:46 -06:00
|
|
|
#else
|
2013-11-18 21:14:44 -06:00
|
|
|
#warning no uint8_t* typemaps defined
|
2013-11-18 17:19:46 -06:00
|
|
|
#endif
|
2013-11-17 15:55:38 -06:00
|
|
|
|
2013-11-17 17:57:43 -06:00
|
|
|
//%include "typemaps.i"
|
2013-11-19 22:59:14 -06:00
|
|
|
|
|
|
|
|
#if defined(SWIGPHP)
|
|
|
|
|
%ignore HCountedArray_;
|
2013-11-24 20:12:54 -06:00
|
|
|
|
|
|
|
|
%inline %{
|
|
|
|
|
static int h_tt_php;
|
|
|
|
|
%}
|
|
|
|
|
|
|
|
|
|
%init %{
|
|
|
|
|
h_tt_php = h_allocate_token_type("com.upstandinghackers.hammer.php");
|
|
|
|
|
%}
|
|
|
|
|
|
|
|
|
|
%inline {
|
|
|
|
|
struct HParsedToken_;
|
|
|
|
|
struct HParseResult_;
|
|
|
|
|
static zval* hpt_to_php(const struct HParsedToken_ *token);
|
|
|
|
|
|
|
|
|
|
//static struct HParsedToken_* call_action(const struct HParseResult_ *p, void* user_data);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-20 03:29:10 -06:00
|
|
|
%typemap(in) (const uint8_t* str, const size_t len) {
|
|
|
|
|
$1 = (uint8_t*)(*$input)->value.str.val;
|
|
|
|
|
$2 = (*$input)->value.str.len;
|
2013-11-19 22:59:14 -06:00
|
|
|
}
|
2013-11-24 19:26:14 -06:00
|
|
|
|
2013-11-20 18:45:02 -06:00
|
|
|
//%typemap(out) (const uint8_t* str, const size_t len) {
|
|
|
|
|
// RETVAL_STRINGL((char*)$1, $2, 1);
|
|
|
|
|
// }
|
2013-11-20 12:40:28 -06:00
|
|
|
%apply (const uint8_t* str, const size_t len) { (const uint8_t* input, size_t length) }
|
2013-11-24 19:26:14 -06:00
|
|
|
|
2013-11-19 22:59:14 -06:00
|
|
|
%typemap(in) void*[] {
|
2013-11-24 22:58:25 -06:00
|
|
|
if (IS_ARRAY == Z_TYPE_PP($input)) {
|
|
|
|
|
zval **data;
|
|
|
|
|
HashTable *arr = Z_ARRVAL_PP($input);
|
|
|
|
|
HashPosition pointer;
|
|
|
|
|
int size = zend_hash_num_elements(arr);
|
|
|
|
|
int i = 0;
|
|
|
|
|
int res = 0;
|
|
|
|
|
$1 = (void**)malloc((size+1)*sizeof(HParser*));
|
|
|
|
|
for (zend_hash_internal_pointer_reset_ex(arr, &pointer);
|
|
|
|
|
zend_hash_get_current_data_ex(arr, (void**)&data, &pointer);
|
|
|
|
|
zend_hash_move_forward_ex(arr, &pointer), i++) {
|
|
|
|
|
res = SWIG_ConvertPtr(*data, &($1[i]), SWIGTYPE_p_HParser_, 0 | 0);
|
|
|
|
|
if (!SWIG_IsOk(res)) {
|
|
|
|
|
// TODO do we not *have* SWIG_TypeError?
|
|
|
|
|
SWIG_exception_fail(res, "that wasn't an HParser");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// FIXME raise some error
|
|
|
|
|
$1 = NULL;
|
|
|
|
|
}
|
2013-11-19 22:59:14 -06:00
|
|
|
}
|
2013-11-24 19:26:14 -06:00
|
|
|
|
2013-11-19 22:59:14 -06:00
|
|
|
%typemap(in) uint8_t {
|
2013-11-24 20:23:49 -06:00
|
|
|
if (IS_LONG == Z_TYPE_PP($input)) {
|
|
|
|
|
$1 = Z_LVAL_PP($input);
|
|
|
|
|
} else if (IS_STRING != Z_TYPE_PP($input)) {
|
|
|
|
|
// FIXME raise some error
|
|
|
|
|
} else {
|
|
|
|
|
$1 = *(uint8_t*)Z_STRVAL_PP($input);
|
|
|
|
|
}
|
2013-11-19 22:59:14 -06:00
|
|
|
}
|
2013-11-24 19:26:14 -06:00
|
|
|
|
2013-11-19 22:59:14 -06:00
|
|
|
%typemap(out) HBytes* {
|
2013-11-20 18:45:02 -06:00
|
|
|
RETVAL_STRINGL((char*)$1->token, $1->len, 1);
|
2013-11-19 22:59:14 -06:00
|
|
|
}
|
2013-11-24 19:26:14 -06:00
|
|
|
|
2013-11-24 22:58:25 -06:00
|
|
|
/* TODO do we need this anymore?
|
2013-11-19 22:59:14 -06:00
|
|
|
%typemap(out) struct HCountedArray_* {
|
|
|
|
|
|
|
|
|
|
}
|
2013-11-24 22:58:25 -06:00
|
|
|
*/
|
2013-11-24 19:26:14 -06:00
|
|
|
%typemap(out) struct HParseResult_* {
|
|
|
|
|
if ($1 == NULL) {
|
|
|
|
|
// TODO: raise parse failure
|
|
|
|
|
RETVAL_NULL();
|
|
|
|
|
} else {
|
2013-11-24 20:12:54 -06:00
|
|
|
if ($1->ast == NULL) {
|
|
|
|
|
RETVAL_NULL();
|
|
|
|
|
} else {
|
|
|
|
|
switch($1->ast->token_type) {
|
|
|
|
|
case TT_NONE:
|
|
|
|
|
RETVAL_NULL();
|
|
|
|
|
break;
|
|
|
|
|
case TT_BYTES:
|
|
|
|
|
RETVAL_STRINGL((char*)$1->ast->token_data.bytes.token, $1->ast->token_data.bytes.len, 1);
|
|
|
|
|
break;
|
|
|
|
|
case TT_SINT:
|
|
|
|
|
RETVAL_LONG($1->ast->token_data.sint);
|
|
|
|
|
break;
|
|
|
|
|
case TT_UINT:
|
|
|
|
|
RETVAL_LONG($1->ast->token_data.uint);
|
|
|
|
|
break;
|
|
|
|
|
case TT_SEQUENCE:
|
|
|
|
|
array_init($result);
|
|
|
|
|
for (int i=0; i < $1->ast->token_data.seq->used; i++) {
|
|
|
|
|
add_next_index_zval($result, hpt_to_php($1->ast->token_data.seq->elements[i]));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
/* if (token->token_type == h_tt_php) { */
|
|
|
|
|
/* ZEND_REGISTER_RESOURCE(return_value, token->token_data.user, le_swig__p_void); // it's a void*, what else could I do with it? */
|
|
|
|
|
/* return return_value; */
|
|
|
|
|
/* } else { */
|
|
|
|
|
/* // I guess that's a python thing */
|
|
|
|
|
/* //return (zval*)SWIG_NewPointerObj((void*)token, SWIGTYPE_p_HParsedToken_, 0 | 0); */
|
|
|
|
|
/* // TODO: support registry */
|
|
|
|
|
/* } */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-24 19:26:14 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 22:59:14 -06:00
|
|
|
#else
|
|
|
|
|
#warning no Hammer typemaps defined
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-11-17 15:55:38 -06:00
|
|
|
// All the include paths are relative to the build, i.e., ../../. If you need to build these manually (i.e., not with scons), keep that in mind.
|
2013-11-16 20:24:05 +01:00
|
|
|
%{
|
|
|
|
|
#include "allocator.h"
|
|
|
|
|
#include "hammer.h"
|
2013-12-03 21:29:02 +01:00
|
|
|
#ifndef SWIGPERL
|
|
|
|
|
// Perl's embed.h conflicts with err.h, which internal.h includes. Ugh.
|
2013-11-16 20:24:05 +01:00
|
|
|
#include "internal.h"
|
2013-12-03 21:29:02 +01:00
|
|
|
#endif
|
2013-11-24 18:43:14 -06:00
|
|
|
#include "glue.h"
|
2013-11-16 20:24:05 +01:00
|
|
|
%}
|
|
|
|
|
%include "allocator.h"
|
|
|
|
|
%include "hammer.h"
|
|
|
|
|
|
2013-11-23 16:40:57 -06:00
|
|
|
%extend HArena_ {
|
|
|
|
|
~HArena_() {
|
|
|
|
|
h_delete_arena($self);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
%extend HParseResult_ {
|
|
|
|
|
~HParseResult_() {
|
|
|
|
|
h_parse_result_free($self);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
%newobject h_parse;
|
|
|
|
|
%delobject h_parse_result_free;
|
|
|
|
|
%newobject h_new_arena;
|
|
|
|
|
%delobject h_delete_arena;
|
|
|
|
|
|
|
|
|
|
#ifdef SWIGPYTHON
|
|
|
|
|
%inline {
|
|
|
|
|
static PyObject* hpt_to_python(const HParsedToken *token) {
|
|
|
|
|
// Caller holds a reference to returned object
|
|
|
|
|
PyObject *ret;
|
|
|
|
|
if (token == NULL) {
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
}
|
|
|
|
|
switch (token->token_type) {
|
|
|
|
|
case TT_NONE:
|
2013-11-23 19:53:58 -06:00
|
|
|
return PyObject_CallFunctionObjArgs(_helper_Placeholder, NULL);
|
2013-11-23 16:40:57 -06:00
|
|
|
break;
|
|
|
|
|
case TT_BYTES:
|
|
|
|
|
return PyString_FromStringAndSize((char*)token->token_data.bytes.token, token->token_data.bytes.len);
|
|
|
|
|
case TT_SINT:
|
|
|
|
|
// TODO: return PyINT if appropriate
|
|
|
|
|
return PyLong_FromLong(token->token_data.sint);
|
|
|
|
|
case TT_UINT:
|
|
|
|
|
// TODO: return PyINT if appropriate
|
|
|
|
|
return PyLong_FromUnsignedLong(token->token_data.uint);
|
|
|
|
|
case TT_SEQUENCE:
|
|
|
|
|
ret = PyTuple_New(token->token_data.seq->used);
|
|
|
|
|
for (int i = 0; i < token->token_data.seq->used; i++) {
|
|
|
|
|
PyTuple_SET_ITEM(ret, i, hpt_to_python(token->token_data.seq->elements[i]));
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
default:
|
|
|
|
|
if (token->token_type == h_tt_python) {
|
|
|
|
|
ret = (PyObject*)token->token_data.user;
|
|
|
|
|
Py_INCREF(ret);
|
|
|
|
|
return ret;
|
|
|
|
|
} else {
|
|
|
|
|
return SWIG_NewPointerObj((void*)token, SWIGTYPE_p_HParsedToken_, 0 | 0);
|
|
|
|
|
// TODO: support registry
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
static struct HParsedToken_* call_action(const struct HParseResult_ *p, void* user_data) {
|
|
|
|
|
PyObject *callable = user_data;
|
|
|
|
|
PyObject *ret = PyObject_CallFunctionObjArgs(callable,
|
|
|
|
|
hpt_to_python(p->ast),
|
|
|
|
|
NULL);
|
|
|
|
|
if (ret == NULL) {
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
assert(ret != NULL);
|
|
|
|
|
}
|
|
|
|
|
// TODO: add reference to ret to parse-local data
|
2013-11-24 18:43:14 -06:00
|
|
|
// For now, just hold onto reference
|
2013-11-23 16:40:57 -06:00
|
|
|
HParsedToken *tok = h_make(p->arena, h_tt_python, ret);
|
|
|
|
|
return tok;
|
|
|
|
|
}
|
2013-11-23 19:53:58 -06:00
|
|
|
|
2013-11-24 18:43:14 -06:00
|
|
|
static int call_predicate(const struct HParseResult_ *p, void* user_data) {
|
|
|
|
|
PyObject *callable = user_data;
|
|
|
|
|
PyObject *ret = PyObject_CallFunctionObjArgs(callable,
|
|
|
|
|
hpt_to_python(p->ast),
|
|
|
|
|
NULL);
|
|
|
|
|
int rret = 0;
|
|
|
|
|
if (ret == NULL) {
|
|
|
|
|
// TODO: throw exception
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
assert(ret != NULL);
|
|
|
|
|
}
|
|
|
|
|
// TODO: add reference to ret to parse-local data
|
|
|
|
|
rret = PyObject_IsTrue(ret);
|
|
|
|
|
Py_DECREF(ret);
|
|
|
|
|
return rret;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-23 16:40:57 -06:00
|
|
|
}
|
2013-11-23 19:53:58 -06:00
|
|
|
|
2013-11-25 02:49:02 -06:00
|
|
|
%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)
|
2013-11-23 19:53:58 -06:00
|
|
|
|
2013-11-25 02:49:02 -06:00
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%}
|
2013-11-24 18:43:14 -06:00
|
|
|
|
2013-11-23 16:40:57 -06:00
|
|
|
#endif
|
2013-11-17 17:57:43 -06:00
|
|
|
//%apply const char* { const uint8_t* }
|
|
|
|
|
|
2013-11-24 19:26:14 -06:00
|
|
|
#if defined(SWIGPHP)
|
|
|
|
|
%inline {
|
|
|
|
|
static zval* hpt_to_php(const HParsedToken *token) {
|
|
|
|
|
zval *ret;
|
|
|
|
|
ALLOC_INIT_ZVAL(ret);
|
|
|
|
|
if (token == NULL) {
|
|
|
|
|
ZVAL_NULL(ret);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
switch (token->token_type) {
|
|
|
|
|
case TT_NONE:
|
|
|
|
|
ZVAL_NULL(ret);
|
|
|
|
|
return ret;
|
|
|
|
|
case TT_BYTES:
|
|
|
|
|
ZVAL_STRINGL(ret, (char*)token->token_data.bytes.token, token->token_data.bytes.len, 1);
|
2013-11-24 20:12:54 -06:00
|
|
|
printf("Being returned from hpt_to_php: %s\n", Z_STRVAL_P(ret));
|
2013-11-24 19:26:14 -06:00
|
|
|
return ret;
|
|
|
|
|
case TT_SINT:
|
|
|
|
|
ZVAL_LONG(ret, token->token_data.sint);
|
|
|
|
|
return ret;
|
|
|
|
|
case TT_UINT:
|
|
|
|
|
ZVAL_LONG(ret, token->token_data.uint);
|
|
|
|
|
return ret;
|
|
|
|
|
case TT_SEQUENCE:
|
|
|
|
|
array_init(ret);
|
|
|
|
|
for (int i=0; i < token->token_data.seq->used; i++) {
|
2013-11-24 20:12:54 -06:00
|
|
|
add_next_index_zval(ret, hpt_to_php(token->token_data.seq->elements[i]));
|
2013-11-24 19:26:14 -06:00
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
default:
|
|
|
|
|
/* if (token->token_type == h_tt_php) { */
|
2013-11-24 20:12:54 -06:00
|
|
|
/* ZEND_REGISTER_RESOURCE(return_value, token->token_data.user, le_swig__p_void); // it's a void*, wh
|
|
|
|
|
/* return return_value; */
|
2013-11-24 19:26:14 -06:00
|
|
|
/* } else { */
|
2013-11-24 20:12:54 -06:00
|
|
|
/* // I guess that's a python thing */
|
|
|
|
|
/* //return (zval*)SWIG_NewPointerObj((void*)token, SWIGTYPE_p_HParsedToken_, 0 | 0); */
|
|
|
|
|
/* // TODO: support registry */
|
2013-11-24 19:26:14 -06:00
|
|
|
/* } */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|