mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-23 11:42:49 -06:00
Added linearization to py-bindings.
This commit is contained in:
@@ -58,9 +58,9 @@ cbid(t *self) { \
|
||||
/* utilities */
|
||||
|
||||
int
|
||||
checkType(PyObject* obj, PyTypeObject* tp)
|
||||
checkType(void* obj, PyTypeObject* tp)
|
||||
{
|
||||
int isRight = PyObject_TypeCheck(obj, tp);
|
||||
int isRight = PyObject_TypeCheck((PyObject*)obj, tp);
|
||||
if (!isRight)
|
||||
PyErr_Format(PyExc_TypeError, "Expected a %s", tp->tp_doc);
|
||||
return isRight;
|
||||
@@ -90,6 +90,20 @@ startCategory(PyObject *self, PyObject *noarg)
|
||||
return cat;
|
||||
}
|
||||
|
||||
static PyObject*
|
||||
linearize(PGFModule *self, PyObject *args)
|
||||
{
|
||||
Lang *lang;
|
||||
Tree *tree;
|
||||
if (!checkType(self,&PGFType)) return NULL;
|
||||
if (!PyArg_ParseTuple(args, "OO", &lang, &tree))
|
||||
return NULL;
|
||||
if (!checkType(lang,&LangType)) return NULL;
|
||||
if (!checkType(tree,&TreeType)) return NULL;
|
||||
char* c_lin = gf_linearize(self->obj, lang->obj, tree->obj);
|
||||
return PyString_FromString(c_lin);
|
||||
}
|
||||
|
||||
static PyObject*
|
||||
parse(PyObject *self, PyObject *args, PyObject *kws)
|
||||
{
|
||||
@@ -100,7 +114,7 @@ parse(PyObject *self, PyObject *args, PyObject *kws)
|
||||
char *lexed;
|
||||
static char *kwlist[] = {"lexed", "lang", "cat", NULL};
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kws, "sO|O", kwlist,
|
||||
&lexed, &lang_pyob, &cat_pyob))
|
||||
&lexed, &lang_pyob, &cat_pyob))
|
||||
return NULL;
|
||||
if (!checkType(self, &PGFType)) return NULL;
|
||||
if (!checkType(lang_pyob, &LangType)) return NULL;
|
||||
@@ -146,6 +160,7 @@ readPGF(PyObject *self, PyObject *args)
|
||||
|
||||
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."},
|
||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user