summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjordi.saludes <jordi.saludes@upc.edu>2010-06-12 22:19:27 +0000
committerjordi.saludes <jordi.saludes@upc.edu>2010-06-12 22:19:27 +0000
commitcd0e3831879ef266af5676946e251cda211cc826 (patch)
treee0a292760386903e33feaf49b4ed0103e40c6a7d
parentb8a022e3a86b42bcd49cc130fea9b8cf0ef885a6 (diff)
Type methods for pgf type.
-rw-r--r--contrib/py-bindings/gfmodule.c49
-rw-r--r--contrib/py-bindings/script.py10
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