From 9ab679b5d507e8471006eee073cfb5aaf42a3b41 Mon Sep 17 00:00:00 2001 From: "jordi.saludes" Date: Sat, 12 Jun 2010 22:19:27 +0000 Subject: [PATCH] Type methods for pgf type. --- contrib/py-bindings/gfmodule.c | 49 +++++++++++++++++----------------- contrib/py-bindings/script.py | 10 +++---- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/contrib/py-bindings/gfmodule.c b/contrib/py-bindings/gfmodule.c index 867db4a2b..716a68009 100644 --- a/contrib/py-bindings/gfmodule.c +++ b/contrib/py-bindings/gfmodule.c @@ -81,39 +81,38 @@ checkType(PyObject* obj, PyTypeObject* tp) } static gfType* -startCategory(PyObject *self, PyObject *args) +startCategory(PyObject *self, PyObject *noarg) { gfType *cat; - PyObject *pgf_obj; - if (!PyArg_ParseTuple(args,"O",&pgf_obj)) - return NULL; - if (!checkType(pgf_obj, &PGFType)) return NULL; + if (!checkType(self, &PGFType)) return NULL; cat = (gfType*)gfTypeType.tp_new(&gfTypeType,NULL,NULL); - GF_PGF pgf = ((PGFModule*)pgf_obj)->obj; - cat->obj = gf_startCat(pgf); + cat->obj = gf_startCat(((PGFModule*)self)->obj); return cat; } - - static PyObject* -parse(PyObject *self, PyObject *args) +parse(PyObject *self, PyObject *args, PyObject *kws) { - PyObject *pgf_pyob, *lang_pyob, *cat_pyob; + PyObject *lang_pyob, *cat_pyob = NULL; GF_PGF pgf; GF_Language lang; GF_Type cat; char *lexed; - if (!PyArg_ParseTuple(args, "OOOs", &pgf_pyob, &lang_pyob, &cat_pyob, &lexed)) + static char *kwlist[] = {"lexed", "lang", "cat", NULL}; + if (!PyArg_ParseTupleAndKeywords(args, kws, "sO|O", kwlist, + &lexed, &lang_pyob, &cat_pyob)) return NULL; - if (!checkType(pgf_pyob, &PGFType)) return NULL; + if (!checkType(self, &PGFType)) return NULL; if (!checkType(lang_pyob, &LangType)) return NULL; - if (!checkType(cat_pyob, &gfTypeType)) return NULL; - pgf = ((PGFModule*)pgf_pyob)->obj; + if (cat_pyob) { + if (!checkType(cat_pyob, &gfTypeType)) return NULL; + cat = ((gfType*)cat_pyob)->obj; + } else { + cat = startCategory(self,NULL)->obj; + } + pgf = ((PGFModule*)self)->obj; lang = ((Lang*)lang_pyob)->obj; - cat = ((gfType*)cat_pyob)->obj; - GF_Tree *result = gf_parse(pgf, lang, cat, lexed); - GF_Tree *p = result; + GF_Tree *p = gf_parse(pgf, lang, cat, lexed); PyObject *parsed = PyList_New(0); if (*p) { do { @@ -137,16 +136,17 @@ expr_repr(Expr *self) } - - - - - + static PyMethodDef gf_methods[] = { {"read_pgf", (PyCFunction)readPGF, METH_VARARGS,"Read pgf file."}, {"read_language", (PyCFunction)readLang, METH_VARARGS,"Get the language."}, {"startcat", (PyCFunction)startCategory, METH_VARARGS,"Get the start category of a pgf module."}, - {"parse", (PyCFunction)parse, METH_VARARGS,"parse a string."}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +static PyMethodDef pgf_methods[] = { + {"parse", (PyCFunction)parse, METH_VARARGS|METH_KEYWORDS,"Parse a string."}, + {"startcat", (PyCFunction)startCategory, METH_NOARGS,"Get the start category."}, {NULL, NULL, 0, NULL} /* Sentinel */ }; @@ -162,6 +162,7 @@ initgf(void) if (PyType_Ready(&t) < 0) return; PGFType.tp_repr = (reprfunc)PGF_repr; + PGFType.tp_methods = pgf_methods; READYTYPE(PGFType) READYTYPE(LangType) READYTYPE(gfTypeType) diff --git a/contrib/py-bindings/script.py b/contrib/py-bindings/script.py index 671500f16..120d47867 100644 --- a/contrib/py-bindings/script.py +++ b/contrib/py-bindings/script.py @@ -1,9 +1,9 @@ #!/usr/bin/env python -from gf import * -query = read_pgf("Query.pgf") -lang = read_language('QueryEng') -cat = startcat(query) +import gf +query = gf.read_pgf("Query.pgf") +lang = gf.read_language('QueryEng') +print 'start category:',query.startcat() lexed = "is 2 prime" print "Parsing '%s':" % lexed -for e in parse(query, lang, cat, lexed): +for e in query.parse(lexed, lang): print '\t',e