added a method in Python for pretty printing a grammar

This commit is contained in:
Krasimir Angelov
2017-09-26 09:16:48 +02:00
parent 404154d16b
commit 06ec6b3e92

View File

@@ -2619,6 +2619,24 @@ PGF_dealloc(PGFObject* self)
Py_TYPE(self)->tp_free((PyObject*)self);
}
static PyObject *
PGF_repr(PGFObject *self)
{
GuPool* tmp_pool = gu_local_pool();
GuExn* err = gu_exn(tmp_pool);
GuStringBuf* sbuf = gu_new_string_buf(tmp_pool);
GuOut* out = gu_string_buf_out(sbuf);
pgf_print(self->pgf, out, err);
PyObject* pystr = PyString_FromStringAndSize(gu_string_buf_data(sbuf),
gu_string_buf_length(sbuf));
gu_pool_free(tmp_pool);
return pystr;
}
static PyObject*
PGF_getAbstractName(PGFObject *self, void *closure)
{
@@ -3239,7 +3257,7 @@ static PyTypeObject pgf_PGFType = {
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
(reprfunc) PGF_repr, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/