summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrasimir Angelov <kr.angelov@gmail.com>2017-09-26 09:16:48 +0200
committerKrasimir Angelov <kr.angelov@gmail.com>2017-09-26 09:16:48 +0200
commit06ec6b3e92503bba3a0b0ed216bbeca3452639df (patch)
tree1b00db61e80cc90a02cc1e656bfea1887711a823
parent404154d16b2fea65e05395e4326ee9c105339e41 (diff)
added a method in Python for pretty printing a grammar
-rw-r--r--src/runtime/python/pypgf.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/src/runtime/python/pypgf.c b/src/runtime/python/pypgf.c
index 732c14e69..93dd0d034 100644
--- a/src/runtime/python/pypgf.c
+++ b/src/runtime/python/pypgf.c
@@ -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*/