summaryrefslogtreecommitdiff
path: root/contrib
diff options
context:
space:
mode:
authorjordi.saludes <jordi.saludes@upc.edu>2010-06-18 15:33:08 +0000
committerjordi.saludes <jordi.saludes@upc.edu>2010-06-18 15:33:08 +0000
commit7bee8ce0c7e79ef82387d2a375e172de867dcd1c (patch)
tree154b88dbdc74bdce74a1c845a1d009af7c090d88 /contrib
parent33eebe698d23f326994959c7f33962bc2d26e57f (diff)
Added CId and categories to py-bindings.
Diffstat (limited to 'contrib')
-rw-r--r--contrib/py-bindings/gfmodule.c32
-rw-r--r--contrib/py-bindings/test.py35
2 files changed, 57 insertions, 10 deletions
diff --git a/contrib/py-bindings/gfmodule.c b/contrib/py-bindings/gfmodule.c
index 92129d4a5..34375a85d 100644
--- a/contrib/py-bindings/gfmodule.c
+++ b/contrib/py-bindings/gfmodule.c
@@ -69,12 +69,28 @@ checkType(void* obj, PyTypeObject* tp)
/* new types and declarations */
+NEWGF(CId,GF_CId,CIdType,"gf.cid","c identifier")
NEWGF(Lang,GF_Language,LangType,"gf.lang","language")
NEWGF(gfType,GF_Type,gfTypeType,"gf.type","gf type")
NEWGF(PGFModule,GF_PGF,PGFType,"gf.pgf","PGF module")
NEWGF(Expr,GF_Expr,ExprType,"gf.expr","gf expression")
NEWGF(Tree,GF_Tree,TreeType,"gf.tree","gf tree")
+/* CId methods, constructor and destructor */
+
+
+DEALLOCFN(CId_dealloc, CId, gf_freeCId, "freeCId")
+
+static PyObject*
+CId_repr(CId *self)
+{
+ char* str_cid = gf_showCId(self->obj);
+ PyObject* repr = PyString_FromString(str_cid);
+ free(str_cid);
+ return repr;
+}
+
+
/* PGF methods, constructor and destructor */
@@ -102,6 +118,20 @@ startCategory(PyObject *self, PyObject *noarg)
}
static PyObject*
+categories(PGFModule* self)
+{
+ PyObject* cats = PyList_New(0);
+ GF_CId *p = gf_categories(self->obj);
+ while (*p) {
+ CId* c = (CId*)CIdType.tp_new(&CIdType,NULL,NULL);
+ c->obj = *(p++);
+ PyList_Append(cats, (PyObject*)c);
+ Py_DECREF(c); //?
+ }
+ return cats;
+}
+
+static PyObject*
languages(PGFModule* self)
{
PyObject *langs = PyList_New(0);
@@ -197,6 +227,7 @@ 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."},
+ {"categories", (PyCFunction)categories, METH_NOARGS,"Get all categories."},
{"abstract", (PyCFunction)abstractName, METH_NOARGS,"Get the module abstract name."},
{"languages", (PyCFunction)languages, METH_NOARGS,"Get the module languages."},
{NULL, NULL, 0, NULL} /* Sentinel */
@@ -267,6 +298,7 @@ initgf(void)
t.tp_dealloc = (destructor)tdealloc; \
if (PyType_Ready(&t) < 0) return;
+ READYTYPE(CIdType, CId_repr, CId_dealloc)
PGFType.tp_methods = pgf_methods;
READYTYPE(PGFType, pgf_repr, PGF_dealloc)
READYTYPE(LangType, lang_repr, Lang_dealloc)
diff --git a/contrib/py-bindings/test.py b/contrib/py-bindings/test.py
index 84f934585..3f6919fe8 100644
--- a/contrib/py-bindings/test.py
+++ b/contrib/py-bindings/test.py
@@ -23,22 +23,37 @@ def rmprefix(obj):
# s = `obj`
# m = hexre.match(s)
# return m and s[m.end(0):]
+
+class TestPgfInfo(unittest.TestCase):
+ def pgf(self):
+ return gf.read_pgf(self.path)
+ def setUp(self):
+ self.path = 'Query.pgf'
+ def test_readPgf(self):
+ pgf = self.pgf()
+ self.assertNotEqual(pgf,None)
+ def test_startcat(self):
+ pgf = self.pgf()
+ cat = pgf.startcat()
+ self.assertEqual(rmprefix(cat),'Question')
+ def test_categories(self):
+ pgf = self.pgf()
+ cats = [`c` for c in pgf.categories()]
+ self.failUnless('Float' in cats)
+ self.failUnless('Int' in cats)
+ self.failUnless('String' in cats)
+ def test_createLanguage(self):
+ pgf = self.pgf()
+ for lang in 'QueryEng QuerySpa'.split():
+ l = gf.read_language(lang)
+ self.assertEqual(rmprefix(l),lang)
class TestParsing(unittest.TestCase):
def setUp(self):
self.lexed = samples
self.lang = 'QueryEng'
self.pgf = "Query.pgf"
- def test_createPgf(self):
- q = gf.read_pgf(self.pgf)
- self.assertNotEqual(q,None)
- def test_startcat(self):
- pgf = gf.read_pgf(self.pgf)
- cat = pgf.startcat()
- self.assertEqual(rmprefix(cat),'Question')
- def test_createLanguage(self):
- l = gf.read_language(self.lang)
- self.assertEqual(rmprefix(l),self.lang)
+
def test_parse(self):
s = self.lexed[0]
pgf = gf.read_pgf(self.pgf)