Added CId and categories to py-bindings.

This commit is contained in:
jordi.saludes
2010-06-18 15:33:08 +00:00
parent 33eebe698d
commit 7bee8ce0c7
2 changed files with 57 additions and 10 deletions

View File

@@ -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 */
@@ -101,6 +117,20 @@ startCategory(PyObject *self, PyObject *noarg)
return cat;
}
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)
{
@@ -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)

View File

@@ -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)