diff --git a/src/runtime/c/pgf/data.h b/src/runtime/c/pgf/data.h index 0dae42491..163d45d95 100644 --- a/src/runtime/c/pgf/data.h +++ b/src/runtime/c/pgf/data.h @@ -128,7 +128,6 @@ typedef struct { extern GU_DECLARE_TYPE(PgfCncCat, abstract); -typedef GuString PgfToken; typedef GuSeq PgfTokens; bool diff --git a/src/runtime/c/pgf/linearizer.c b/src/runtime/c/pgf/linearizer.c index 0465b6e1c..9a57de5fc 100644 --- a/src/runtime/c/pgf/linearizer.c +++ b/src/runtime/c/pgf/linearizer.c @@ -677,6 +677,35 @@ pgf_lzr_linearize_simple(PgfConcr* concr, PgfCncTree ctree, pgf_lzr_linearize(concr, ctree, lin_idx, &flin.funcs); } +void +pgf_lzr_linearize_table(PgfConcr* concr, PgfCncTree ctree, + size_t* n_lins, GuString** labels) +{ + static GuString s_label = "s"; + + GuVariantInfo cti = gu_variant_open(ctree); + + switch (cti.tag) { + case PGF_CNC_TREE_APP: { + PgfCncTreeApp* fapp = cti.data; + PgfCncCat* cnccat = + gu_map_get(concr->cnccats, fapp->fun->absfun->type->cid, PgfCncCat*); + *n_lins = cnccat->n_lins; + *labels = cnccat->labels; + break; + } + case PGF_CNC_TREE_LIT: + case PGF_CNC_TREE_CHUNKS: { + *n_lins = 1; + *labels = &s_label; + break; + } + default: + gu_impossible(); + } + +} + GuString pgf_get_tokens(PgfSymbols* syms, uint16_t sym_idx, GuPool* pool) { diff --git a/src/runtime/c/pgf/linearizer.h b/src/runtime/c/pgf/linearizer.h index 1cc244575..65c02c48b 100644 --- a/src/runtime/c/pgf/linearizer.h +++ b/src/runtime/c/pgf/linearizer.h @@ -71,6 +71,11 @@ pgf_lzr_linearize(PgfConcr* concr, PgfCncTree ctree, size_t lin_idx, void pgf_lzr_linearize_simple(PgfConcr* concr, PgfCncTree ctree, size_t lin_idx, GuOut* out, GuExn* err); + + +void +pgf_lzr_linearize_table(PgfConcr* concr, PgfCncTree ctree, + size_t* n_lins, GuString** labels); #endif #ifdef PGF_PARSER_H_ diff --git a/src/runtime/c/pgf/pgf.h b/src/runtime/c/pgf/pgf.h index b586eb7b0..9ec63c4d6 100644 --- a/src/runtime/c/pgf/pgf.h +++ b/src/runtime/c/pgf/pgf.h @@ -35,6 +35,7 @@ typedef GuString PgfCId; extern GU_DECLARE_TYPE(PgfCId, typedef); +typedef GuString PgfToken; extern GU_DECLARE_TYPE(PgfExn, abstract); extern GU_DECLARE_TYPE(PgfParseError, abstract); diff --git a/src/runtime/java/jpgf.c b/src/runtime/java/jpgf.c index bf62d8c64..fb5b53bff 100644 --- a/src/runtime/java/jpgf.c +++ b/src/runtime/java/jpgf.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -350,6 +351,71 @@ Java_org_grammaticalframework_pgf_Concr_linearize(JNIEnv* env, jobject self, job return jstr; } +JNIEXPORT jobject JNICALL +Java_org_grammaticalframework_pgf_Concr_tabularLinearize(JNIEnv* env, jobject self, jobject jexpr) +{ + jclass map_class = (*env)->FindClass(env, "java/util/HashMap"); + if (!map_class) + return NULL; + jmethodID constrId = (*env)->GetMethodID(env, map_class, "", "()V"); + if (!constrId) + return NULL; + jobject table = (*env)->NewObject(env, map_class, constrId); + if (!table) + return NULL; + + jmethodID put_method = (*env)->GetMethodID(env, map_class, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); + if (!put_method) + return NULL; + + PgfConcr* concr = get_ref(env, self); + + GuPool* tmp_pool = gu_local_pool(); + GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool); + + GuEnum* cts = + pgf_lzr_concretize(concr, + gu_variant_from_ptr((void*) get_ref(env, jexpr)), + tmp_pool); + PgfCncTree ctree = gu_next(cts, PgfCncTree, tmp_pool); + if (gu_variant_is_null(ctree)) { + gu_pool_free(tmp_pool); + return NULL; + } + + size_t n_lins; + GuString* labels; + pgf_lzr_linearize_table(concr, ctree, &n_lins, &labels); + + for (size_t lin_idx = 0; lin_idx < n_lins; lin_idx++) { + GuStringBuf* sbuf = gu_string_buf(tmp_pool); + GuOut* out = gu_string_buf_out(sbuf); + + pgf_lzr_linearize_simple(concr, ctree, lin_idx, out, err); + + jstring jstr = NULL; + if (gu_ok(err)) { + GuString str = gu_string_buf_freeze(sbuf, tmp_pool); + jstr = gu2j_string(env, str); + } else { + gu_exn_clear(err); + } + + jstring jname = gu2j_string(env, labels[lin_idx]); + + (*env)->CallObjectMethod(env, table, put_method, jname, jstr); + + (*env)->DeleteLocalRef(env, jname); + + if (jstr != NULL) + (*env)->DeleteLocalRef(env, jstr); + } + + gu_pool_free(tmp_pool); + + return table; +} + typedef struct { PgfMorphoCallback fn; jobject analyses; diff --git a/src/runtime/java/org/grammaticalframework/pgf/Concr.java b/src/runtime/java/org/grammaticalframework/pgf/Concr.java index a824b740f..81f696aaf 100644 --- a/src/runtime/java/org/grammaticalframework/pgf/Concr.java +++ b/src/runtime/java/org/grammaticalframework/pgf/Concr.java @@ -21,6 +21,8 @@ public class Concr { public native String linearize(Expr expr); + public native Map tabularLinearize(Expr expr); + public native List lookupMorpho(String sentence); //////////////////////////////////////////////////////////////////