diff options
Diffstat (limited to 'src/runtime/python')
| -rw-r--r-- | src/runtime/python/pypgf.c | 104 | ||||
| -rw-r--r-- | src/runtime/python/setup.py | 8 |
2 files changed, 78 insertions, 34 deletions
diff --git a/src/runtime/python/pypgf.c b/src/runtime/python/pypgf.c index 07d534db5..eebaa2781 100644 --- a/src/runtime/python/pypgf.c +++ b/src/runtime/python/pypgf.c @@ -1412,7 +1412,7 @@ pypgf_literal_callback_match(PgfLiteralCallback* self, PgfConcr* concr, ExprObject* pyexpr; #if PY_MAJOR_VERSION >= 3 - size_t chars; + int chars; if (!PyArg_ParseTuple(result, "Ofi", &pyexpr, &ep->prob, &chars)) return NULL; *poffset = unicode_to_utf8_offset(sentence, chars); @@ -1421,37 +1421,7 @@ pypgf_literal_callback_match(PgfLiteralCallback* self, PgfConcr* concr, return NULL; #endif - ep->expr = pyexpr->expr; - - { - // This is an uggly hack. We first show the expression ep->expr - // and then we read it back but in out_pool. The whole purpose - // of this is to copy the expression from the temporary pool - // that was created in the Java binding to the parser pool. - // There should be a real copying function or even better - // there must be a way to avoid copying at all. - - 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_expr(ep->expr, NULL, 0, out, err); - - GuIn* in = gu_data_in((uint8_t*) gu_string_buf_data(sbuf), - gu_string_buf_length(sbuf), - tmp_pool); - - ep->expr = pgf_read_expr(in, out_pool, tmp_pool, err); - if (!gu_ok(err) || gu_variant_is_null(ep->expr)) { - PyErr_SetString(PGFError, "The expression cannot be parsed"); - gu_pool_free(tmp_pool); - return NULL; - } - - gu_pool_free(tmp_pool); - } + ep->expr = pgf_clone_expr(pyexpr->expr, out_pool); Py_DECREF(result); @@ -2108,6 +2078,58 @@ static PyTypeObject pgf_BracketType = { }; typedef struct { + PyObject_HEAD +} BINDObject; + +static PyObject * +BIND_repr(BINDObject *self) +{ + return PyString_FromString("&+"); +} + +static PyTypeObject pgf_BINDType = { + PyVarObject_HEAD_INIT(NULL, 0) + //0, /*ob_size*/ + "pgf.BIND", /*tp_name*/ + sizeof(BINDObject), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + 0, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + (reprfunc) BIND_repr, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ + "a marker for BIND in a bracketed string", /*tp_doc*/ + 0, /*tp_traverse */ + 0, /*tp_clear */ + 0, /*tp_richcompare */ + 0, /*tp_weaklistoffset */ + 0, /*tp_iter */ + 0, /*tp_iternext */ + 0, /*tp_methods */ + 0, /*tp_members */ + 0, /*tp_getset */ + 0, /*tp_base */ + 0, /*tp_dict */ + 0, /*tp_descr_get */ + 0, /*tp_descr_set */ + 0, /*tp_dictoffset */ + 0, /*tp_init */ + 0, /*tp_alloc */ + 0, /*tp_new */ +}; + +typedef struct { PgfLinFuncs* funcs; GuBuf* stack; PyObject* list; @@ -2159,6 +2181,16 @@ pgf_bracket_lzn_end_phrase(PgfLinFuncs** funcs, PgfCId cat, int fid, GuString an } static void +pgf_bracket_lzn_symbol_bind(PgfLinFuncs** funcs) +{ + PgfBracketLznState* state = gu_container(funcs, PgfBracketLznState, funcs); + + PyObject* bind = pgf_BINDType.tp_alloc(&pgf_BINDType, 0); + PyList_Append(state->list, bind); + Py_DECREF(bind); +} + +static void pgf_bracket_lzn_symbol_meta(PgfLinFuncs** funcs, PgfMetaId meta_id) { pgf_bracket_lzn_symbol_token(funcs, "?"); @@ -2169,7 +2201,7 @@ static PgfLinFuncs pgf_bracket_lin_funcs = { .begin_phrase = pgf_bracket_lzn_begin_phrase, .end_phrase = pgf_bracket_lzn_end_phrase, .symbol_ne = NULL, - .symbol_bind = NULL, + .symbol_bind = pgf_bracket_lzn_symbol_bind, .symbol_capit = NULL, .symbol_meta = pgf_bracket_lzn_symbol_meta }; @@ -3589,6 +3621,9 @@ MOD_INIT(pgf) if (PyType_Ready(&pgf_BracketType) < 0) return MOD_ERROR_VAL; + if (PyType_Ready(&pgf_BINDType) < 0) + return MOD_ERROR_VAL; + if (PyType_Ready(&pgf_ExprType) < 0) return MOD_ERROR_VAL; @@ -3635,5 +3670,8 @@ MOD_INIT(pgf) PyModule_AddObject(m, "Bracket", (PyObject *) &pgf_BracketType); Py_INCREF(&pgf_BracketType); + PyModule_AddObject(m, "BIND", (PyObject *) &pgf_BINDType); + Py_INCREF(&pgf_BINDType); + return MOD_SUCCESS_VAL(m); } diff --git a/src/runtime/python/setup.py b/src/runtime/python/setup.py index 19459b411..fdc2fe8c5 100644 --- a/src/runtime/python/setup.py +++ b/src/runtime/python/setup.py @@ -17,7 +17,13 @@ pgf_module = Extension('pgf', setup (name = 'pgf', version = '1.0', - description = 'A binding to the PGF engine', + description = 'Python bindings to the Grammatical Framework\'s PGF runtime', + long_description="""\ +Grammatical Framework (GF) is a programming language for multilingual grammar applications. +This package provides Python bindings to GF runtime, which allows you to \ +parse and generate text using GF grammars compiled into the PGF format. +""", + url='https://www.grammaticalframework.org/', author='Krasimir Angelov', author_email='kr.angelov@gmail.com', license='BSD', |
