Add getCategories and getFunctions to Python bindings, but don't know how to handle errors?

This commit is contained in:
John J. Camilleri
2021-08-30 22:26:22 +02:00
parent 50e54d131b
commit b7bd5a4561
2 changed files with 124 additions and 95 deletions

View File

@@ -39,7 +39,7 @@ typedef struct {
PyObject_HEAD PyObject_HEAD
PgfPGF* pgf; PgfPGF* pgf;
} PGFObject; } PGFObject;
// #if 0
// typedef struct { // typedef struct {
// PyObject_HEAD // PyObject_HEAD
// PyObject* master; // PyObject* master;
@@ -2814,20 +2814,20 @@ typedef struct {
// 0, /*tp_alloc */ // 0, /*tp_alloc */
// (newfunc)Concr_new, /*tp_new */ // (newfunc)Concr_new, /*tp_new */
// }; // };
// #endif
static void static void
PGF_dealloc(PGFObject* self) PGF_dealloc(PGFObject* self)
{ {
pgf_free(self->pgf); pgf_free(self->pgf);
Py_TYPE(self)->tp_free((PyObject*)self); Py_TYPE(self)->tp_free((PyObject*)self);
} }
// #if 0
// typedef struct { typedef struct {
// GuMapItor fn; PgfItor fn;
// PGFObject* grammar; PGFObject* grammar;
// void* collection; void* collection;
// } PyPGFClosure; } PyPGFClosure;
//
// static void // static void
// pgf_collect_langs_seq(GuMapItor* fn, const void* key, void* value, GuExn* err) // pgf_collect_langs_seq(GuMapItor* fn, const void* key, void* value, GuExn* err)
// { // {
@@ -2836,7 +2836,7 @@ PGF_dealloc(PGFObject* self)
// //
// gu_buf_push((GuBuf*) clo->collection, PgfConcr*, concr); // gu_buf_push((GuBuf*) clo->collection, PgfConcr*, concr);
// } // }
// #endif
static PyObject* static PyObject*
PGF_repr(PGFObject *self) PGF_repr(PGFObject *self)
{ {
@@ -2869,7 +2869,7 @@ PGF_getAbstractName(PGFObject *self, void *closure)
PgfText* txt = pgf_abstract_name(self->pgf); PgfText* txt = pgf_abstract_name(self->pgf);
return PyString_FromString(txt->text); return PyString_FromString(txt->text);
} }
// #if 0
// static void // static void
// pgf_collect_langs_dict(GuMapItor* fn, const void* key, void* value, GuExn* err) // pgf_collect_langs_dict(GuMapItor* fn, const void* key, void* value, GuExn* err)
// { // {
@@ -2956,31 +2956,55 @@ PGF_getAbstractName(PGFObject *self, void *closure)
// end: // end:
// Py_XDECREF(py_name); // Py_XDECREF(py_name);
// } // }
//
// static PyObject* static void
// PGF_getCategories(PGFObject *self, void *closure) pgf_collect_cats(PgfItor* fn, PgfText* key, void* value)
// { {
// PyObject* categories = PyList_New(0); PgfText* name = key;
// if (categories == NULL) PyPGFClosure* clo = (PyPGFClosure*) fn;
// return NULL;
// PyObject* py_name = NULL;
// GuPool* tmp_pool = gu_local_pool();
// py_name = PyString_FromString(name->text);
// // Create an exception frame that catches all errors. if (py_name == NULL) {
// GuExn* err = gu_new_exn(tmp_pool); // gu_raise(err, PgfExn);
// goto end;
// PyPGFClosure clo = { { pgf_collect_cats }, self, categories }; }
// pgf_iter_categories(self->pgf, &clo.fn, err);
// if (!gu_ok(err)) { if (PyList_Append((PyObject*) clo->collection, py_name) != 0) {
// Py_DECREF(categories); // gu_raise(err, PgfExn);
// gu_pool_free(tmp_pool); goto end;
// return NULL; }
// }
// end:
// gu_pool_free(tmp_pool); Py_XDECREF(py_name);
// return categories; }
// }
// static PyObject*
PGF_getCategories(PGFObject *self, void *closure)
{
PyObject* categories = PyList_New(0);
if (categories == NULL)
return NULL;
// GuPool* tmp_pool = gu_local_pool();
//
// Create an exception frame that catches all errors.
// GuExn* err = gu_new_exn(tmp_pool);
PyPGFClosure clo = { { pgf_collect_cats }, self, categories };
pgf_iter_categories(self->pgf, &clo.fn);
// if (!gu_ok(err)) {
// Py_DECREF(categories);
// gu_pool_free(tmp_pool);
// return NULL;
// }
//
// gu_pool_free(tmp_pool);
return categories;
}
// static TypeObject* // static TypeObject*
// PGF_getStartCat(PGFObject *self, void *closure) // PGF_getStartCat(PGFObject *self, void *closure)
// { // {
@@ -3000,54 +3024,54 @@ PGF_getAbstractName(PGFObject *self, void *closure)
// //
// return pytype; // return pytype;
// } // }
//
// static void static void
// pgf_collect_funs(GuMapItor* fn, const void* key, void* value, GuExn* err) pgf_collect_funs(PgfItor* fn, const void* key, void* value)
// { {
// PgfCId name = (PgfCId) key; PgfText* name = key;
// PyPGFClosure* clo = (PyPGFClosure*) fn; PyPGFClosure* clo = (PyPGFClosure*) fn;
//
// PyObject* py_name = NULL; PyObject* py_name = NULL;
//
// py_name = PyString_FromString(name); py_name = PyString_FromString(name->text);
// if (py_name == NULL) { if (py_name == NULL) {
// gu_raise(err, PgfExn); // gu_raise(err, PgfExn);
// goto end; goto end;
// } }
//
// if (PyList_Append((PyObject*) clo->collection, py_name) != 0) { if (PyList_Append((PyObject*) clo->collection, py_name) != 0) {
// gu_raise(err, PgfExn); // gu_raise(err, PgfExn);
// goto end; goto end;
// } }
//
// end: end:
// Py_XDECREF(py_name); Py_XDECREF(py_name);
// } }
//
// static PyObject* static PyObject*
// PGF_getFunctions(PGFObject *self, void *closure) PGF_getFunctions(PGFObject *self, void *closure)
// { {
// PyObject* functions = PyList_New(0); PyObject* functions = PyList_New(0);
// if (functions == NULL) if (functions == NULL)
// return NULL; return NULL;
//
// GuPool* tmp_pool = gu_local_pool(); // GuPool* tmp_pool = gu_local_pool();
//
// // Create an exception frame that catches all errors. // Create an exception frame that catches all errors.
// GuExn* err = gu_new_exn(tmp_pool); // GuExn* err = gu_new_exn(tmp_pool);
//
// PyPGFClosure clo = { { pgf_collect_funs }, self, functions }; PyPGFClosure clo = { { pgf_collect_funs }, self, functions };
// pgf_iter_functions(self->pgf, &clo.fn, err); pgf_iter_functions(self->pgf, &clo.fn);
// if (!gu_ok(err)) { // if (!gu_ok(err)) {
// Py_DECREF(functions); // Py_DECREF(functions);
// gu_pool_free(tmp_pool); // gu_pool_free(tmp_pool);
// return NULL; // return NULL;
// } // }
// //
// gu_pool_free(tmp_pool); // gu_pool_free(tmp_pool);
// return functions; return functions;
// } }
//
// static PyObject* // static PyObject*
// PGF_functionsByCat(PGFObject* self, PyObject *args) // PGF_functionsByCat(PGFObject* self, PyObject *args)
// { // {
@@ -3398,7 +3422,7 @@ PGF_getAbstractName(PGFObject *self, void *closure)
// Py_INCREF(m); // Py_INCREF(m);
// return m; // return m;
// } // }
// #endif
static PyGetSetDef PGF_getseters[] = { static PyGetSetDef PGF_getseters[] = {
{"abstractName", {"abstractName",
(getter)PGF_getAbstractName, NULL, (getter)PGF_getAbstractName, NULL,
@@ -3408,18 +3432,18 @@ static PyGetSetDef PGF_getseters[] = {
// (getter)PGF_getLanguages, NULL, // (getter)PGF_getLanguages, NULL,
// "a map containing all concrete languages in the grammar", // "a map containing all concrete languages in the grammar",
// NULL}, // NULL},
// {"categories", {"categories",
// (getter)PGF_getCategories, NULL, (getter)PGF_getCategories, NULL,
// "a list containing all categories in the grammar", "a list containing all categories in the grammar",
// NULL}, NULL},
// {"startCat", // {"startCat",
// (getter)PGF_getStartCat, NULL, // (getter)PGF_getStartCat, NULL,
// "the start category for the grammar", // "the start category for the grammar",
// NULL}, // NULL},
// {"functions", {"functions",
// (getter)PGF_getFunctions, NULL, (getter)PGF_getFunctions, NULL,
// "a list containing all functions in the grammar", "a list containing all functions in the grammar",
// NULL}, NULL},
{NULL} /* Sentinel */ {NULL} /* Sentinel */
}; };
@@ -3585,7 +3609,6 @@ pgf_readNGF(PyObject *self, PyObject *args)
return py_pgf; return py_pgf;
} }
// #if 0
// static ExprObject* // static ExprObject*
// pgf_readExpr(PyObject *self, PyObject *args) { // pgf_readExpr(PyObject *self, PyObject *args) {
// Py_ssize_t len; // Py_ssize_t len;
@@ -3645,7 +3668,6 @@ pgf_readNGF(PyObject *self, PyObject *args)
// gu_pool_free(tmp_pool); // gu_pool_free(tmp_pool);
// return pytype; // return pytype;
// } // }
// #endif
static PyMethodDef module_methods[] = { static PyMethodDef module_methods[] = {
{"readPGF", (void*)pgf_readPGF, METH_VARARGS, {"readPGF", (void*)pgf_readPGF, METH_VARARGS,

View File

@@ -68,3 +68,10 @@ def test_readNGF(NGF):
def test_abstractName(PGF): def test_abstractName(PGF):
assert PGF.abstractName == "basic" assert PGF.abstractName == "basic"
def test_categories(PGF):
assert PGF.categories == ["Float","Int","N","P","S","String"]
def test_functions(PGF):
assert PGF.functions == ["c","ind","s","z"]