From ea5c3baa7fdeb6612d587bec9ad8aebb6ab033c4 Mon Sep 17 00:00:00 2001 From: "jordi.saludes" Date: Fri, 18 Jun 2010 11:50:05 +0000 Subject: [PATCH] Added methods to PGFModule. --- contrib/py-bindings/gfmodule.c | 40 +++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/contrib/py-bindings/gfmodule.c b/contrib/py-bindings/gfmodule.c index 25a9cb636..92129d4a5 100644 --- a/contrib/py-bindings/gfmodule.c +++ b/contrib/py-bindings/gfmodule.c @@ -81,6 +81,16 @@ NEWGF(Tree,GF_Tree,TreeType,"gf.tree","gf tree") DEALLOCFN(PGF_dealloc, PGFModule, gf_freePGF, "freePGF") +static PyObject* +pgf_repr(PGFModule *self) { + GF_Language lang = gf_abstractName(self->obj); + const char* abs = gf_showLanguage(lang); + gf_freeLanguage(lang); + return PyString_FromFormat("", abs, self->obj); +} + + + static gfType* startCategory(PyObject *self, PyObject *noarg) { @@ -91,6 +101,20 @@ startCategory(PyObject *self, PyObject *noarg) return cat; } +static PyObject* +languages(PGFModule* self) +{ + PyObject *langs = PyList_New(0); + GF_Language *p = gf_languages(self->obj); + while (*p) { + Lang* l = (Lang*)LangType.tp_new(&LangType,NULL,NULL); + l->obj = *(p++); + PyList_Append(langs, (PyObject*)l); + Py_DECREF(l); //?? + } + return langs; +} + static PyObject* linearize(PGFModule *self, PyObject *args) { @@ -105,6 +129,16 @@ linearize(PGFModule *self, PyObject *args) return PyString_FromString(c_lin); } +static Lang* +abstractName(PGFModule* self) +{ + Lang* abs = (Lang*)LangType.tp_new(&LangType,NULL,NULL); + if (!checkType(self,&PGFType)) return NULL; + abs->obj = gf_abstractName(self->obj); + return abs; +} + + static PyObject* parse(PyObject *self, PyObject *args, PyObject *kws) { @@ -163,6 +197,8 @@ static PyMethodDef pgf_methods[] = { {"parse", (PyCFunction)parse, METH_VARARGS|METH_KEYWORDS,"Parse a string."}, {"lin", (PyCFunction)linearize, METH_VARARGS,"Linearize tree."}, {"startcat", (PyCFunction)startCategory, METH_NOARGS,"Get the start category."}, + {"abstract", (PyCFunction)abstractName, METH_NOARGS,"Get the module abstract name."}, + {"languages", (PyCFunction)languages, METH_NOARGS,"Get the module languages."}, {NULL, NULL, 0, NULL} /* Sentinel */ }; @@ -195,8 +231,6 @@ DEALLOCFN(gfType_dealloc, gfType, gf_freeType, "freeType") REPRCB(gfType_repr, gfType, gf_showType) - - /* expression type: methods, destructor */ DEALLOCFN(expr_dealloc, Expr, gf_freeExpr, "freeExpr") @@ -234,7 +268,7 @@ initgf(void) if (PyType_Ready(&t) < 0) return; PGFType.tp_methods = pgf_methods; - READYTYPE(PGFType,NULL,PGF_dealloc) + READYTYPE(PGFType, pgf_repr, PGF_dealloc) READYTYPE(LangType, lang_repr, Lang_dealloc) READYTYPE(gfTypeType, gfType_repr, gfType_dealloc) READYTYPE(ExprType, expr_repr, expr_dealloc)