readExpr in the Java binding

This commit is contained in:
kr.angelov
2013-10-11 08:10:04 +00:00
parent a0e6576e45
commit f7fb6fa5bc
2 changed files with 30 additions and 0 deletions

View File

@@ -421,6 +421,34 @@ Java_org_grammaticalframework_pgf_Expr_showExpr(JNIEnv* env, jclass clazz, jlong
return jstr;
}
JNIEXPORT jobject JNICALL
Java_org_grammaticalframework_pgf_Expr_readExpr(JNIEnv* env, jclass clazz, jstring s)
{
GuPool* pool = gu_new_pool();
GuPool* tmp_pool = gu_local_pool();
GuString buf = j2gu_string(env, s, tmp_pool);
GuIn* in = gu_data_in((uint8_t*) buf, strlen(buf), tmp_pool);
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
PgfExpr e = pgf_read_expr(in, pool, err);
if (!gu_ok(err) || gu_variant_is_null(e)) {
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The expression cannot be parsed");
gu_pool_free(tmp_pool);
gu_pool_free(pool);
return NULL;
}
gu_pool_free(tmp_pool);
jclass pool_class = (*env)->FindClass(env, "org/grammaticalframework/pgf/Pool");
jmethodID pool_constrId = (*env)->GetMethodID(env, pool_class, "<init>", "(J)V");
jobject jpool = (*env)->NewObject(env, pool_class, pool_constrId, p2l(pool));
jmethodID constrId = (*env)->GetMethodID(env, clazz, "<init>", "(Lorg/grammaticalframework/pgf/Pool;Lorg/grammaticalframework/pgf/PGF;J)V");
return (*env)->NewObject(env, clazz, constrId, jpool, NULL, p2l(gu_variant_to_ptr(e)));
}
JNIEXPORT jobject JNICALL
Java_org_grammaticalframework_pgf_Generator_generateAll(JNIEnv* env, jclass clazz, jobject jpgf, jstring jstartCat)
{

View File

@@ -15,5 +15,7 @@ public class Expr {
return showExpr(ref);
}
public static native Expr readExpr(String s) throws PGFError;
private static native String showExpr(long ref);
}