Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V3 actual consistent naming #1

Merged
merged 6 commits into from
Oct 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 36 additions & 36 deletions bblfsh/pyuast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ typedef struct {
PyObject_HEAD
ContextExt *ctx;
NodeHandle handle;
} NodeExt;
} PyNodeExt;

static PyObject *NodeExt_load(NodeExt *self, PyObject *Py_UNUSED(ignored));
static PyObject *PyNodeExt_load(PyNodeExt *self, PyObject *Py_UNUSED(ignored));

static PyMethodDef NodeExt_methods[] = {
{"load", (PyCFunction) NodeExt_load, METH_NOARGS,
static PyMethodDef PyNodeExt_methods[] = {
{"load", (PyCFunction) PyNodeExt_load, METH_NOARGS,
"Load external node to Python"
},
{nullptr} // Sentinel
};

extern "C"
{
static PyTypeObject NodeExtType = {
static PyTypeObject PyNodeExtType = {
PyVarObject_HEAD_INIT(nullptr, 0)
"pyuast.NodeExt", // tp_name
sizeof(NodeExt), // tp_basicsize
sizeof(PyNodeExt), // tp_basicsize
0, // tp_itemsize
0, // tp_dealloc
0, // tp_print
Expand All @@ -80,7 +80,7 @@ extern "C"
0, // tp_weaklistoffset
0, // tp_iter: __iter()__ method
0, // tp_iternext: next() method
NodeExt_methods, // tp_methods
PyNodeExt_methods, // tp_methods
0, // tp_members
0, // tp_getset
0, // tp_base
Expand Down Expand Up @@ -181,32 +181,32 @@ class ContextExt {
private:
uast::Context<NodeHandle> *ctx;

// toPy allocates a new NodeExt with a specified handle.
// toPy allocates a new PyNodeExt with a specified handle.
// Returns a new reference.
PyObject* toPy(NodeHandle node) {
if (node == 0) Py_RETURN_NONE;

NodeExt *pyObj = PyObject_New(NodeExt, &NodeExtType);
PyNodeExt *pyObj = PyObject_New(PyNodeExt, &PyNodeExtType);
if (!pyObj) return nullptr;

pyObj->ctx = this;
pyObj->handle = node;
return (PyObject*)pyObj;
}

// toHandle casts an object to NodeExt and returns its handle.
// toHandle casts an object to PyNodeExt and returns its handle.
// Borrows the reference.
NodeHandle toHandle(PyObject* obj) {
if (!obj || obj == Py_None) return 0;

if (!PyObject_TypeCheck(obj, &NodeExtType)) {
if (!PyObject_TypeCheck(obj, &PyNodeExtType)) {
const char* err = "unknown node type";
PyErr_SetString(PyExc_NotImplementedError, err);
ctx->SetError(err);
return 0;
}

auto node = (NodeExt*)obj;
auto node = (PyNodeExt*)obj;
return node->handle;
}

Expand Down Expand Up @@ -828,7 +828,7 @@ class Context {
uast::Buffer data = ctx->Encode(toNode(node), format);
return asPyBuffer(data); // TODO: this probably won't deallocate the buffer
}
PyObject* LoadFrom(NodeExt *src) {
PyObject* LoadFrom(PyNodeExt *src) {
auto sctx = src->ctx->ctx;
NodeHandle snode = src->handle;

Expand All @@ -837,7 +837,7 @@ class Context {
}
};

static PyObject *NodeExt_load(NodeExt *self, PyObject *Py_UNUSED(ignored)) {
static PyObject *PyNodeExt_load(PyNodeExt *self, PyObject *Py_UNUSED(ignored)) {
auto ctx = new Context();
PyObject* node = ctx->LoadFrom(self);
delete(ctx);
Expand All @@ -858,54 +858,54 @@ static void PyUastIter_dealloc(PyObject *self) {
typedef struct {
PyObject_HEAD
Context *p;
} PyUast;
} PyContext;

static void PyUast_dealloc(PyObject *self) {
static void PyContext_dealloc(PyObject *self) {
delete(((PyUast *)self)->p);
// TODO: delete self?
}

static PyObject *PyUast_root(PyUast *self, PyObject *Py_UNUSED(ignored)) {
static PyObject *PyContext_root(PyContext *self, PyObject *Py_UNUSED(ignored)) {
return self->p->RootNode();
}

static PyObject *PyUast_filter(PyUast *self, PyObject *args) {
static PyObject *PyContext_filter(PyContext *self, PyObject *args) {
PyObject *node = nullptr;
char *query = nullptr;
if (!PyArg_ParseTuple(args, "Os", &node, &query))
return nullptr;
return self->p->Filter(node, query);
}

static PyObject *PyUast_encode(PyUast *self, PyObject *args) {
static PyObject *PyContext_encode(PyContext *self, PyObject *args) {
PyObject *node = nullptr;
UastFormat format = UAST_BINARY; // TODO: make it a kwarg and enum
if (!PyArg_ParseTuple(args, "Oi", &node, &format))
return nullptr;
return self->p->Encode(node, format);
}

static PyMethodDef PyUast_methods[] = {
{"root", (PyCFunction) PyUast_root, METH_NOARGS,
static PyMethodDef PyContext_methods[] = {
{"root", (PyCFunction) PyContext_root, METH_NOARGS,
"Return the root node attached to this query context"
},
{"filter", (PyCFunction) PyUast_filter, METH_VARARGS,
{"filter", (PyCFunction) PyContext_filter, METH_VARARGS,
"Filter a provided UAST with XPath"
},
{"encode", (PyCFunction) PyUast_encode, METH_VARARGS,
{"encode", (PyCFunction) PyContext_encode, METH_VARARGS,
"Encodes a UAST into a buffer"
},
{nullptr} // Sentinel
};

extern "C"
{
static PyTypeObject PyUastType = {
static PyTypeObject PyContextType = {
PyVarObject_HEAD_INIT(nullptr, 0)
"pyuast.Context", // tp_name
sizeof(PyUast), // tp_basicsize
sizeof(PyContext), // tp_basicsize
0, // tp_itemsize
PyUast_dealloc, // tp_dealloc
PyContext_dealloc, // tp_dealloc
0, // tp_print
0, // tp_getattr
0, // tp_setattr
Expand All @@ -928,7 +928,7 @@ extern "C"
0, // tp_weaklistoffset
0, // tp_iter: __iter()__ method
0, // tp_iternext: next() method
PyUast_methods, // tp_methods
PyContext_methods, // tp_methods
0, // tp_members
0, // tp_getset
0, // tp_base
Expand All @@ -954,17 +954,17 @@ static PyObject *PyUastIter_new(PyObject *self, PyObject *args) {
return nullptr;

// the node can either be external or any other Python object
if (PyObject_TypeCheck(obj, &NodeExtType)) {
if (PyObject_TypeCheck(obj, &PyNodeExtType)) {
// external node -> external iterator
auto node = (NodeExt*)obj;
auto node = (PyNodeExt*)obj;
return node->ctx->Iterate(obj, (TreeOrder)order);
}
// Python object -> create a new context and attach it to an iterator
Context* ctx = new Context();
return ctx->Iterate(obj, (TreeOrder)order, true);
}

static PyObject *PyUastDecode(PyObject *self, PyObject *args) {
static PyObject *PyContextExt_decode(PyObject *self, PyObject *args) {
PyObject *obj = nullptr;
UastFormat format = UAST_BINARY; // TODO: make it a kwarg

Expand All @@ -991,12 +991,12 @@ static PyObject *PyUastDecode(PyObject *self, PyObject *args) {
return (PyObject*)pyU;
}

static PyObject *PyUast_new(PyObject *self, PyObject *args) {
static PyObject *PyContext_new(PyObject *self, PyObject *args) {
if (!PyArg_ParseTuple(args, "")) {
return nullptr;
}

PyUast *pyU = PyObject_New(PyUast, &PyUastType);
PyContext *pyU = PyObject_New(PyContext, &PyContextType);
if (!pyU) {
return nullptr;
}
Expand All @@ -1006,8 +1006,8 @@ static PyObject *PyUast_new(PyObject *self, PyObject *args) {

static PyMethodDef extension_methods[] = {
{"iterator", PyUastIter_new, METH_VARARGS, "Get an iterator over a node"},
{"decode", PyUastDecode, METH_VARARGS, "Decode UAST from a byte array"},
{"uast", PyUast_new, METH_VARARGS, "Creates a new UAST context"},
{"decode", PyContextDecode, METH_VARARGS, "Decode UAST from a byte array"},
{"uast", PyContext_new, METH_VARARGS, "Creates a new UAST context"},
{nullptr, nullptr, 0, nullptr}
};

Expand All @@ -1027,10 +1027,10 @@ PyMODINIT_FUNC
PyInit_pyuast(void)
{
if (PyType_Ready(&PyContextExtType) < 0) return nullptr;
if (PyType_Ready(&NodeExtType) < 0) return nullptr;
if (PyType_Ready(&PyNodeExtType) < 0) return nullptr;
if (PyType_Ready(&PyUastIterExtType) < 0) return nullptr;

if (PyType_Ready(&PyUastType) < 0) return nullptr;
if (PyType_Ready(&PyContextType) < 0) return nullptr;
if (PyType_Ready(&PyUastIterType) < 0) return nullptr;
return PyModule_Create(&module_def);
}