started an official API to the C runtime

This commit is contained in:
kr.angelov
2012-12-12 11:25:58 +00:00
parent 5e091d2e3d
commit aa13090b66
15 changed files with 329 additions and 173 deletions

View File

@@ -217,7 +217,7 @@ GU_DEFINE_TYPE(
&gu_null_struct))));
GU_DEFINE_TYPE(
PgfPrintNames, PgfCIdMap, gu_type(GuString), NULL);
PgfPrintNames, PgfCIdMap, gu_type(GuString), &gu_empty_string);
GU_DEFINE_TYPE(
PgfConcr, struct,

169
src/runtime/c/pgf/pgf.c Normal file
View File

@@ -0,0 +1,169 @@
#include <pgf/pgf.h>
#include <pgf/data.h>
#include <pgf/expr.h>
#include <pgf/reader.h>
#include <gu/file.h>
#include <gu/string.h>
#include <stdio.h>
#include <math.h>
GU_DEFINE_TYPE(PgfExn, abstract, _);
PgfPGF*
pgf_read(const char* fpath,
GuPool* pool, GuExn* err)
{
FILE* infile = fopen(fpath, "r");
if (infile == NULL) {
gu_raise_errno(err);
return NULL;
}
GuPool* tmp_pool = gu_new_pool();
// Create an input stream from the input file
GuIn* in = gu_file_in(infile, tmp_pool);
PgfReader* rdr = pgf_new_reader(in, pool, tmp_pool, err);
PgfPGF* pgf = pgf_read_new(rdr, gu_type(PgfPGF), pool, NULL);
gu_pool_free(tmp_pool);
gu_return_on_exn(err, NULL);
return pgf;
}
void
pgf_load_meta_child_probs(PgfPGF* pgf, const char* fpath,
GuPool* pool, GuExn* err)
{
FILE *fp = fopen(fpath, "r");
if (!fp) {
gu_raise_errno(err);
return;
}
GuPool* tmp_pool = gu_new_pool();
for (;;) {
char cat1_s[21];
char cat2_s[21];
prob_t prob;
if (fscanf(fp, "%20s\t%20s\t%f", cat1_s, cat2_s, &prob) < 3)
break;
prob = - log(prob);
GuString cat1 = gu_str_string(cat1_s, tmp_pool);
PgfCat* abscat1 =
gu_map_get(pgf->abstract.cats, &cat1, PgfCat*);
if (abscat1 == NULL) {
gu_raise(err, PgfExn);
goto close;
}
if (strcmp(cat2_s, "*") == 0) {
abscat1->meta_prob = prob;
} else if (strcmp(cat2_s, "_") == 0) {
abscat1->meta_token_prob = prob;
} else {
GuString cat2 = gu_str_string(cat2_s, tmp_pool);
PgfCat* abscat2 = gu_map_get(pgf->abstract.cats, &cat2, PgfCat*);
if (abscat2 == NULL) {
gu_raise(err, PgfExn);
goto close;
}
if (abscat1->meta_child_probs == NULL) {
abscat1->meta_child_probs =
gu_map_type_new(PgfMetaChildMap, pool);
}
gu_map_put(abscat1->meta_child_probs, abscat2, prob_t, prob);
}
}
close:
gu_pool_free(tmp_pool);
fclose(fp);
}
GuString
pgf_abstract_name(PgfPGF* pgf)
{
return pgf->absname;
}
void
pgf_iter_languages(PgfPGF* pgf, GuMapItor* fn, GuExn* err)
{
gu_map_iter(pgf->concretes, fn, err);
}
PgfConcr*
pgf_get_language(PgfPGF* pgf, PgfCId lang)
{
return gu_map_get(pgf->concretes, &lang, PgfConcr*);
}
void
pgf_iter_categories(PgfPGF* pgf, GuMapItor* fn, GuExn* err)
{
gu_map_iter(pgf->abstract.cats, fn, err);
}
PgfCId
pgf_start_cat(PgfPGF* pgf, GuPool* pool)
{
GuPool* tmp_pool = gu_local_pool();
GuString s = gu_str_string("startcat", tmp_pool);
PgfLiteral lit =
gu_map_get(pgf->abstract.aflags, &s, PgfLiteral);
if (gu_variant_is_null(lit))
return gu_str_string("S", pool);
GuVariantInfo i = gu_variant_open(lit);
switch (i.tag) {
case PGF_LITERAL_STR: {
PgfLiteralStr *lstr = (PgfLiteralStr *) i.data;
return lstr->val;
}
}
return gu_str_string("S", pool);
}
void
pgf_iter_functions(PgfPGF* pgf, GuMapItor* fn, GuExn* err)
{
gu_map_iter(pgf->abstract.funs, fn, err);
}
void
pgf_iter_functions_by_cat(PgfPGF* pgf, PgfCId catname,
GuMapItor* fn, GuExn* err)
{
PgfCat* abscat =
gu_map_get(pgf->abstract.cats, &catname, PgfCat*);
if (abscat == NULL) {
gu_raise(err, PgfExn);
return;
}
for (size_t i = 0; i < abscat->n_functions; i++) {
fn->fn(fn, &abscat->functions[i].fun, NULL, err);
if (!gu_ok(err))
return;
}
}
GuString
pgf_print_name(PgfConcr* concr, PgfCId id)
{
PgfCId name =
gu_map_get(concr->printnames, &id, PgfCId);
if (gu_string_eq(name, gu_empty_string))
name = id;
return name;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2010 University of Helsinki.
* Copyright 2010 University of Gothenburg.
*
* This file is part of libpgf.
*
@@ -27,7 +27,7 @@
#include <gu/exn.h>
#include <gu/mem.h>
#include <gu/in.h>
#include <gu/map.h>
#include <gu/string.h>
@@ -35,20 +35,25 @@ typedef GuString PgfCId;
extern GU_DECLARE_TYPE(PgfCId, typedef);
extern GU_DECLARE_TYPE(PgfExn, abstract);
/// A single lexical token
typedef GuString PgfToken;
typedef GuString PgfToken;
/// @name PGF Grammar objects
/// @{
typedef struct PgfPGF PgfPGF;
typedef struct PgfConcr PgfConcr;
/**< A representation of a PGF grammar.
*/
PgfPGF*
pgf_read(GuIn* in, GuPool* pool, GuExn* err);
pgf_read(const char* fpath,
GuPool* pool, GuExn* err);
/**< Read a grammar from a PGF file.
*
@@ -69,10 +74,34 @@ pgf_read(GuIn* in, GuPool* pool, GuExn* err);
*/
bool
pgf_load_meta_child_probs(PgfPGF*, const char* fpath, GuPool* pool);
void
pgf_load_meta_child_probs(PgfPGF*, const char* fpath,
GuPool* pool, GuExn* err);
typedef struct PgfConcr PgfConcr;
GuString
pgf_abstract_name(PgfPGF*);
void
pgf_iter_languages(PgfPGF*, GuMapItor*, GuExn* err);
PgfConcr*
pgf_get_language(PgfPGF*, PgfCId lang);
void
pgf_iter_categories(PgfPGF* pgf, GuMapItor* fn, GuExn* err);
PgfCId
pgf_start_cat(PgfPGF* pgf, GuPool* pool);
void
pgf_iter_functions(PgfPGF* pgf, GuMapItor* fn, GuExn* err);
void
pgf_iter_functions_by_cat(PgfPGF* pgf, PgfCId catname,
GuMapItor* fn, GuExn* err);
GuString
pgf_print_name(PgfConcr*, PgfCId id);
#include <gu/type.h>
extern GU_DECLARE_TYPE(PgfPGF, struct);

View File

@@ -20,6 +20,7 @@
#include "data.h"
#include "expr.h"
#include "literals.h"
#include "reader.h"
#include <gu/defs.h>
#include <gu/map.h>
#include <gu/seq.h>
@@ -40,8 +41,6 @@
// PgfReader
//
typedef struct PgfReader PgfReader;
struct PgfReader {
GuIn* in;
GuExn* err;
@@ -132,7 +131,7 @@ struct PgfReadNewFn {
size_t* size_out);
};
static void*
void*
pgf_read_new(PgfReader* rdr, GuType* type, GuPool* pool, size_t* size_out)
{
size_t size = 0;
@@ -884,7 +883,7 @@ pgf_read_new_table = GU_TYPETABLE(
PGF_READ_NEW(PgfConcr)
);
static PgfReader*
PgfReader*
pgf_new_reader(GuIn* in, GuPool* opool, GuPool* tmp_pool, GuExn* err)
{
PgfReader* rdr = gu_new(PgfReader, tmp_pool);
@@ -900,65 +899,3 @@ pgf_new_reader(GuIn* in, GuPool* opool, GuPool* tmp_pool, GuExn* err)
rdr->read_new_map = gu_new_type_map(&pgf_read_new_table, tmp_pool);
return rdr;
}
PgfPGF*
pgf_read(GuIn* in, GuPool* pool, GuExn* err)
{
GuPool* tmp_pool = gu_new_pool();
PgfReader* rdr = pgf_new_reader(in, pool, tmp_pool, err);
PgfPGF* pgf = pgf_read_new(rdr, gu_type(PgfPGF), pool, NULL);
gu_pool_free(tmp_pool);
gu_return_on_exn(err, NULL);
return pgf;
}
bool
pgf_load_meta_child_probs(PgfPGF* pgf, const char* fpath, GuPool* pool)
{
FILE *fp = fopen(fpath, "r");
if (!fp)
return false;
GuPool* tmp_pool = gu_new_pool();
for (;;) {
char cat1_s[21];
char cat2_s[21];
prob_t prob;
if (fscanf(fp, "%20s\t%20s\t%f", cat1_s, cat2_s, &prob) < 3)
break;
prob = - log(prob);
GuString cat1 = gu_str_string(cat1_s, tmp_pool);
PgfCat* abscat1 =
gu_map_get(pgf->abstract.cats, &cat1, PgfCat*);
if (abscat1 == NULL)
return false;
if (strcmp(cat2_s, "*") == 0) {
abscat1->meta_prob = prob;
} else if (strcmp(cat2_s, "_") == 0) {
abscat1->meta_token_prob = prob;
} else {
GuString cat2 = gu_str_string(cat2_s, tmp_pool);
PgfCat* abscat2 = gu_map_get(pgf->abstract.cats, &cat2, PgfCat*);
if (abscat2 == NULL)
return false;
if (abscat1->meta_child_probs == NULL) {
abscat1->meta_child_probs =
gu_map_type_new(PgfMetaChildMap, pool);
}
gu_map_put(abscat1->meta_child_probs, abscat2, prob_t, prob);
}
}
gu_pool_free(tmp_pool);
fclose(fp);
return true;
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2012 University of Gothenburg.
*
* This file is part of libpgf.
*
* Libpgf is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Libpgf is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libpgf. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef READER_H_
#define READER_H_
#include <gu/exn.h>
#include <gu/mem.h>
#include <gu/in.h>
typedef struct PgfReader PgfReader;
PgfReader*
pgf_new_reader(GuIn* in, GuPool* opool, GuPool* tmp_pool, GuExn* err);
void*
pgf_read_new(PgfReader* rdr, GuType* type, GuPool* pool, size_t* size_out);
#endif // READER_H_