mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-21 10:49:33 -06:00
change the API to allow different grammar revisions
This commit is contained in:
@@ -107,26 +107,12 @@ typedef struct {
|
||||
Namespace<PgfAbsCat> cats;
|
||||
} PgfAbstr;
|
||||
|
||||
struct PGF_INTERNAL_DECL PgfPGFRoot {
|
||||
typedef struct PGF_INTERNAL_DECL {
|
||||
uint16_t major_version;
|
||||
uint16_t minor_version;
|
||||
Namespace<PgfFlag> gflags;
|
||||
PgfAbstr abstract;
|
||||
//PgfConcrs* concretes;
|
||||
};
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wattributes"
|
||||
|
||||
struct PgfPGF : DB {
|
||||
PGF_INTERNAL_DECL PgfPGF(const char* fpath, int flags, int mode)
|
||||
: DB(fpath, flags, mode)
|
||||
{ };
|
||||
|
||||
PGF_INTERNAL_DECL ~PgfPGF()
|
||||
{ };
|
||||
};
|
||||
|
||||
#pragma GCC diagnostic pop
|
||||
} PgfPGF;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
#include "data.h"
|
||||
|
||||
PGF_INTERNAL __thread unsigned char* current_base __attribute__((tls_model("initial-exec"))) = NULL;
|
||||
PGF_INTERNAL __thread DB* current_db __attribute__((tls_model("initial-exec"))) = NULL;
|
||||
PGF_INTERNAL __thread PgfDB* current_db __attribute__((tls_model("initial-exec"))) = NULL;
|
||||
PGF_INTERNAL __thread DB_scope *last_db_scope __attribute__((tls_model("initial-exec"))) = NULL;
|
||||
|
||||
#ifndef DEFAULT_TOP_PAD
|
||||
@@ -269,7 +269,7 @@ struct malloc_state
|
||||
object root_offset;
|
||||
};
|
||||
|
||||
DB::DB(const char* pathname, int flags, int mode) {
|
||||
PgfDB::PgfDB(const char* pathname, int flags, int mode) {
|
||||
size_t file_size;
|
||||
bool is_new = false;
|
||||
|
||||
@@ -313,7 +313,7 @@ DB::DB(const char* pathname, int flags, int mode) {
|
||||
}
|
||||
}
|
||||
|
||||
DB::~DB() {
|
||||
PgfDB::~PgfDB() {
|
||||
if (ms != NULL) {
|
||||
size_t size =
|
||||
ms->top + chunksize(ptr(ms,ms->top)) + sizeof(size_t);
|
||||
@@ -327,7 +327,7 @@ DB::~DB() {
|
||||
pthread_rwlock_destroy(&rwlock);
|
||||
}
|
||||
|
||||
void DB::sync()
|
||||
void PgfDB::sync()
|
||||
{
|
||||
malloc_state *ms = current_db->ms;
|
||||
size_t size =
|
||||
@@ -338,16 +338,16 @@ void DB::sync()
|
||||
throw std::system_error(errno, std::generic_category());
|
||||
}
|
||||
|
||||
object DB::get_root_internal() {
|
||||
object PgfDB::get_root_internal() {
|
||||
return ms->root_offset;
|
||||
}
|
||||
|
||||
void DB::set_root_internal(object root_offset) {
|
||||
void PgfDB::set_root_internal(object root_offset) {
|
||||
ms->root_offset = root_offset;
|
||||
}
|
||||
|
||||
void
|
||||
DB::init_state(size_t size)
|
||||
PgfDB::init_state(size_t size)
|
||||
{
|
||||
/* Init fastbins */
|
||||
ms->have_fastchunks = false;
|
||||
@@ -483,7 +483,7 @@ static void malloc_consolidate(malloc_state *ms)
|
||||
}
|
||||
|
||||
object
|
||||
DB::malloc_internal(size_t bytes)
|
||||
PgfDB::malloc_internal(size_t bytes)
|
||||
{
|
||||
unsigned int idx; /* associated bin index */
|
||||
mbin* bin; /* associated bin */
|
||||
@@ -856,7 +856,7 @@ DB::malloc_internal(size_t bytes)
|
||||
}
|
||||
|
||||
void
|
||||
DB::free_internal(object o)
|
||||
PgfDB::free_internal(object o)
|
||||
{
|
||||
size_t size; /* its size */
|
||||
object *fb; /* associated fastbin */
|
||||
@@ -948,7 +948,7 @@ DB::free_internal(object o)
|
||||
}
|
||||
|
||||
|
||||
DB_scope::DB_scope(DB *db, DB_scope_mode tp)
|
||||
DB_scope::DB_scope(PgfDB *db, DB_scope_mode tp)
|
||||
{
|
||||
int res =
|
||||
(tp == READER_SCOPE) ? pthread_rwlock_rdlock(&db->rwlock)
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
|
||||
#define MALLOC_ALIGN_MASK (2*sizeof(size_t) - 1)
|
||||
|
||||
class DB;
|
||||
class PgfDB;
|
||||
|
||||
extern PGF_INTERNAL_DECL __thread unsigned char* current_base __attribute__((tls_model("initial-exec")));
|
||||
extern PGF_INTERNAL_DECL __thread DB* current_db __attribute__((tls_model("initial-exec")));
|
||||
extern PGF_INTERNAL_DECL __thread PgfDB* current_db __attribute__((tls_model("initial-exec")));
|
||||
|
||||
struct malloc_state;
|
||||
|
||||
@@ -14,7 +14,7 @@ template<class A> class PGF_INTERNAL_DECL ref {
|
||||
private:
|
||||
object offset;
|
||||
|
||||
friend class DB;
|
||||
friend class PgfDB;
|
||||
|
||||
public:
|
||||
ref<A>() { }
|
||||
@@ -54,7 +54,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class PGF_INTERNAL_DECL DB {
|
||||
class PgfDB {
|
||||
private:
|
||||
int fd;
|
||||
malloc_state* ms;
|
||||
@@ -64,8 +64,8 @@ private:
|
||||
friend class PgfReader;
|
||||
|
||||
public:
|
||||
DB(const char* pathname, int flags, int mode);
|
||||
~DB();
|
||||
PgfDB(const char* pathname, int flags, int mode);
|
||||
~PgfDB();
|
||||
|
||||
template<class A>
|
||||
static ref<A> malloc() {
|
||||
@@ -112,11 +112,11 @@ enum DB_scope_mode {READER_SCOPE, WRITER_SCOPE};
|
||||
|
||||
class PGF_INTERNAL_DECL DB_scope {
|
||||
public:
|
||||
DB_scope(DB *db, DB_scope_mode type);
|
||||
DB_scope(PgfDB *db, DB_scope_mode type);
|
||||
~DB_scope();
|
||||
|
||||
private:
|
||||
DB* save_db;
|
||||
PgfDB* save_db;
|
||||
DB_scope* next_scope;
|
||||
};
|
||||
|
||||
|
||||
@@ -111,7 +111,7 @@ PgfType PgfDBMarshaller::match_type(PgfUnmarshaller *u, PgfType ty)
|
||||
PgfExpr PgfDBUnmarshaller::eabs(PgfBindType bind_type, PgfText *name, PgfExpr body)
|
||||
{
|
||||
ref<PgfExprAbs> eabs =
|
||||
DB::malloc<PgfExprAbs>(sizeof(PgfExprAbs)+name->size+1);
|
||||
PgfDB::malloc<PgfExprAbs>(sizeof(PgfExprAbs)+name->size+1);
|
||||
eabs->bind_type = bind_type;
|
||||
eabs->body = m->match_expr(this, body);
|
||||
memcpy(&eabs->name, name, sizeof(PgfText)+name->size+1);
|
||||
@@ -120,7 +120,7 @@ PgfExpr PgfDBUnmarshaller::eabs(PgfBindType bind_type, PgfText *name, PgfExpr bo
|
||||
|
||||
PgfExpr PgfDBUnmarshaller::eapp(PgfExpr fun, PgfExpr arg)
|
||||
{
|
||||
ref<PgfExprApp> eapp = DB::malloc<PgfExprApp>();
|
||||
ref<PgfExprApp> eapp = PgfDB::malloc<PgfExprApp>();
|
||||
eapp->fun = m->match_expr(this, fun);
|
||||
eapp->arg = m->match_expr(this, arg);
|
||||
return ref<PgfExprApp>::tagged(eapp);
|
||||
@@ -128,14 +128,14 @@ PgfExpr PgfDBUnmarshaller::eapp(PgfExpr fun, PgfExpr arg)
|
||||
|
||||
PgfExpr PgfDBUnmarshaller::elit(PgfLiteral lit)
|
||||
{
|
||||
ref<PgfExprLit> elit = DB::malloc<PgfExprLit>();
|
||||
ref<PgfExprLit> elit = PgfDB::malloc<PgfExprLit>();
|
||||
elit->lit = m->match_lit(this, lit);
|
||||
return ref<PgfExprLit>::tagged(elit);
|
||||
}
|
||||
|
||||
PgfExpr PgfDBUnmarshaller::emeta(PgfMetaId meta_id)
|
||||
{
|
||||
ref<PgfExprMeta> emeta = DB::malloc<PgfExprMeta>();
|
||||
ref<PgfExprMeta> emeta = PgfDB::malloc<PgfExprMeta>();
|
||||
emeta->id = meta_id;
|
||||
return ref<PgfExprMeta>::tagged(emeta);
|
||||
}
|
||||
@@ -143,21 +143,21 @@ PgfExpr PgfDBUnmarshaller::emeta(PgfMetaId meta_id)
|
||||
PgfExpr PgfDBUnmarshaller::efun(PgfText *name)
|
||||
{
|
||||
ref<PgfExprFun> efun =
|
||||
DB::malloc<PgfExprFun>(sizeof(PgfExprFun)+name->size+1);
|
||||
PgfDB::malloc<PgfExprFun>(sizeof(PgfExprFun)+name->size+1);
|
||||
memcpy(&efun->name, name, sizeof(PgfText)+name->size+1);
|
||||
return ref<PgfExprFun>::tagged(efun);
|
||||
}
|
||||
|
||||
PgfExpr PgfDBUnmarshaller::evar(int index)
|
||||
{
|
||||
ref<PgfExprVar> evar = DB::malloc<PgfExprVar>();
|
||||
ref<PgfExprVar> evar = PgfDB::malloc<PgfExprVar>();
|
||||
evar->var = index;
|
||||
return ref<PgfExprVar>::tagged(evar);
|
||||
}
|
||||
|
||||
PgfExpr PgfDBUnmarshaller::etyped(PgfExpr expr, PgfType ty)
|
||||
{
|
||||
ref<PgfExprTyped> etyped = DB::malloc<PgfExprTyped>();
|
||||
ref<PgfExprTyped> etyped = PgfDB::malloc<PgfExprTyped>();
|
||||
etyped->expr = m->match_expr(this, expr);
|
||||
etyped->type = m->match_type(this, ty);
|
||||
return ref<PgfExprTyped>::tagged(etyped);
|
||||
@@ -173,7 +173,7 @@ PgfExpr PgfDBUnmarshaller::eimplarg(PgfExpr expr)
|
||||
PgfLiteral PgfDBUnmarshaller::lint(size_t size, uintmax_t *val)
|
||||
{
|
||||
ref<PgfLiteralInt> lit_int =
|
||||
DB::malloc<PgfLiteralInt>(sizeof(PgfLiteralInt)+size*sizeof(uintmax_t));
|
||||
PgfDB::malloc<PgfLiteralInt>(sizeof(PgfLiteralInt)+size*sizeof(uintmax_t));
|
||||
lit_int->size = size;
|
||||
memcpy(&lit_int->val, val, size*sizeof(uintmax_t));
|
||||
return ref<PgfLiteralInt>::tagged(lit_int);
|
||||
@@ -181,7 +181,7 @@ PgfLiteral PgfDBUnmarshaller::lint(size_t size, uintmax_t *val)
|
||||
|
||||
PgfLiteral PgfDBUnmarshaller::lflt(double val)
|
||||
{
|
||||
ref<PgfLiteralFlt> lit_flt = DB::malloc<PgfLiteralFlt>();
|
||||
ref<PgfLiteralFlt> lit_flt = PgfDB::malloc<PgfLiteralFlt>();
|
||||
lit_flt->val = val;
|
||||
return ref<PgfLiteralFlt>::tagged(lit_flt);
|
||||
}
|
||||
@@ -189,7 +189,7 @@ PgfLiteral PgfDBUnmarshaller::lflt(double val)
|
||||
PgfLiteral PgfDBUnmarshaller::lstr(PgfText *val)
|
||||
{
|
||||
ref<PgfLiteralStr> lit_str =
|
||||
DB::malloc<PgfLiteralStr>(sizeof(PgfLiteralStr)+val->size+1);
|
||||
PgfDB::malloc<PgfLiteralStr>(sizeof(PgfLiteralStr)+val->size+1);
|
||||
memcpy(&lit_str->val, val, sizeof(PgfText)+val->size+1);
|
||||
return ref<PgfLiteralStr>::tagged(lit_str);
|
||||
}
|
||||
@@ -199,13 +199,13 @@ PgfType PgfDBUnmarshaller::dtyp(int n_hypos, PgfTypeHypo *hypos,
|
||||
int n_exprs, PgfExpr *exprs)
|
||||
{
|
||||
ref<PgfDTyp> ty =
|
||||
DB::malloc<PgfDTyp>(sizeof(PgfDTyp)+cat->size+1);
|
||||
PgfDB::malloc<PgfDTyp>(sizeof(PgfDTyp)+cat->size+1);
|
||||
memcpy(&ty->name, cat, sizeof(PgfText)+cat->size+1);
|
||||
ty->hypos = vector_new<PgfHypo>(n_hypos);
|
||||
for (size_t i = 0; i < n_hypos; i++) {
|
||||
ref<PgfHypo> hypo = vector_elem(ty->hypos,i);
|
||||
hypo->bind_type = hypos[i].bind_type;
|
||||
hypo->cid = DB::malloc<PgfText>(sizeof(PgfText)+hypos[i].cid->size+1);
|
||||
hypo->cid = PgfDB::malloc<PgfText>(sizeof(PgfText)+hypos[i].cid->size+1);
|
||||
memcpy(&hypo->cid->text, hypos[i].cid, sizeof(PgfText)+hypos[i].cid->size+1);
|
||||
hypo->type = m->match_type(this, hypos[i].type);
|
||||
}
|
||||
@@ -219,7 +219,7 @@ PgfType PgfDBUnmarshaller::dtyp(int n_hypos, PgfTypeHypo *hypos,
|
||||
|
||||
void PgfDBUnmarshaller::free_ref(object x)
|
||||
{
|
||||
DB::free(ref<void>::untagged(x));
|
||||
PgfDB::free(ref<void>::untagged(x));
|
||||
}
|
||||
|
||||
PgfExprParser::PgfExprParser(PgfText *input, PgfUnmarshaller *unmarshaller)
|
||||
|
||||
@@ -339,7 +339,7 @@ void namespace_release(Namespace<V> node)
|
||||
if (!(--node->ref_count)) {
|
||||
namespace_release(node->left);
|
||||
namespace_release(node->right);
|
||||
DB::free(node);
|
||||
PgfDB::free(node);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,15 +13,16 @@ pgf_exn_clear(PgfExn* err)
|
||||
}
|
||||
|
||||
PGF_API
|
||||
PgfPGF *pgf_read_pgf(const char* fpath,
|
||||
PgfExn* err)
|
||||
PgfDB *pgf_read_pgf(const char* fpath,
|
||||
PgfRevision *revision,
|
||||
PgfExn* err)
|
||||
{
|
||||
PgfPGF *pgf = NULL;
|
||||
PgfDB *db = NULL;
|
||||
|
||||
pgf_exn_clear(err);
|
||||
|
||||
try {
|
||||
pgf = new PgfPGF(NULL, 0, 0);
|
||||
db = new PgfDB(NULL, 0, 0);
|
||||
|
||||
std::ifstream in(fpath, std::ios::binary);
|
||||
if (in.fail()) {
|
||||
@@ -29,15 +30,16 @@ PgfPGF *pgf_read_pgf(const char* fpath,
|
||||
}
|
||||
|
||||
{
|
||||
DB_scope scope(pgf, WRITER_SCOPE);
|
||||
DB_scope scope(db, WRITER_SCOPE);
|
||||
|
||||
PgfReader rdr(&in);
|
||||
ref<PgfPGFRoot> pgf_root = rdr.read_pgf();
|
||||
ref<PgfPGF> pgf = rdr.read_pgf();
|
||||
|
||||
pgf->set_root(pgf_root);
|
||||
PgfDB::set_root(pgf);
|
||||
*revision = pgf.as_object();
|
||||
}
|
||||
|
||||
return pgf;
|
||||
return db;
|
||||
} catch (std::system_error& e) {
|
||||
err->type = PGF_EXN_SYSTEM_ERROR;
|
||||
err->code = e.code().value();
|
||||
@@ -46,22 +48,23 @@ PgfPGF *pgf_read_pgf(const char* fpath,
|
||||
err->msg = strdup(e.what());
|
||||
}
|
||||
|
||||
if (pgf != NULL)
|
||||
delete pgf;
|
||||
if (db != NULL)
|
||||
delete db;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PGF_API
|
||||
PgfPGF *pgf_boot_ngf(const char* pgf_path, const char* ngf_path,
|
||||
PgfExn* err)
|
||||
PgfDB *pgf_boot_ngf(const char* pgf_path, const char* ngf_path,
|
||||
PgfRevision *revision,
|
||||
PgfExn* err)
|
||||
{
|
||||
PgfPGF *pgf = NULL;
|
||||
PgfDB *db = NULL;
|
||||
|
||||
pgf_exn_clear(err);
|
||||
|
||||
try {
|
||||
pgf = new PgfPGF(ngf_path, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
|
||||
db = new PgfDB(ngf_path, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
|
||||
|
||||
std::ifstream in(pgf_path, std::ios::binary);
|
||||
if (in.fail()) {
|
||||
@@ -69,17 +72,18 @@ PgfPGF *pgf_boot_ngf(const char* pgf_path, const char* ngf_path,
|
||||
}
|
||||
|
||||
{
|
||||
DB_scope scope(pgf, WRITER_SCOPE);
|
||||
DB_scope scope(db, WRITER_SCOPE);
|
||||
|
||||
PgfReader rdr(&in);
|
||||
ref<PgfPGFRoot> pgf_root = rdr.read_pgf();
|
||||
ref<PgfPGF> pgf = rdr.read_pgf();
|
||||
|
||||
pgf->set_root(pgf_root);
|
||||
db->set_root<PgfPGF>(pgf);
|
||||
*revision = pgf.as_object();
|
||||
|
||||
DB::sync();
|
||||
PgfDB::sync();
|
||||
}
|
||||
|
||||
return pgf;
|
||||
return db;
|
||||
} catch (std::system_error& e) {
|
||||
err->type = PGF_EXN_SYSTEM_ERROR;
|
||||
err->code = e.code().value();
|
||||
@@ -88,8 +92,8 @@ PgfPGF *pgf_boot_ngf(const char* pgf_path, const char* ngf_path,
|
||||
err->msg = strdup(e.what());
|
||||
}
|
||||
|
||||
if (pgf != NULL) {
|
||||
delete pgf;
|
||||
if (db != NULL) {
|
||||
delete db;
|
||||
remove(ngf_path);
|
||||
}
|
||||
|
||||
@@ -97,36 +101,40 @@ PgfPGF *pgf_boot_ngf(const char* pgf_path, const char* ngf_path,
|
||||
}
|
||||
|
||||
PGF_API
|
||||
PgfPGF *pgf_read_ngf(const char *fpath,
|
||||
PgfDB *pgf_read_ngf(const char *fpath,
|
||||
PgfRevision *revision,
|
||||
PgfExn* err)
|
||||
{
|
||||
PgfPGF *pgf = NULL;
|
||||
PgfDB *db = NULL;
|
||||
|
||||
pgf_exn_clear(err);
|
||||
|
||||
bool is_new = false;
|
||||
try {
|
||||
pgf = new PgfPGF(fpath, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
|
||||
db = new PgfDB(fpath, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
|
||||
|
||||
{
|
||||
DB_scope scope(pgf, WRITER_SCOPE);
|
||||
DB_scope scope(db, WRITER_SCOPE);
|
||||
|
||||
if (DB::get_root<PgfPGFRoot>() == 0) {
|
||||
if (PgfDB::get_root<PgfPGF>() == 0) {
|
||||
is_new = true;
|
||||
ref<PgfPGFRoot> root = DB::malloc<PgfPGFRoot>();
|
||||
root->major_version = 2;
|
||||
root->minor_version = 0;
|
||||
root->gflags = 0;
|
||||
root->abstract.name = DB::malloc<PgfText>();
|
||||
root->abstract.name->size = 0;
|
||||
root->abstract.aflags = 0;
|
||||
root->abstract.funs = 0;
|
||||
root->abstract.cats = 0;
|
||||
DB::set_root<PgfPGFRoot>(root);
|
||||
ref<PgfPGF> pgf = PgfDB::malloc<PgfPGF>();
|
||||
pgf->major_version = 2;
|
||||
pgf->minor_version = 0;
|
||||
pgf->gflags = 0;
|
||||
pgf->abstract.name = PgfDB::malloc<PgfText>();
|
||||
pgf->abstract.name->size = 0;
|
||||
pgf->abstract.aflags = 0;
|
||||
pgf->abstract.funs = 0;
|
||||
pgf->abstract.cats = 0;
|
||||
PgfDB::set_root<PgfPGF>(pgf);
|
||||
*revision = pgf.as_object();
|
||||
} else {
|
||||
*revision = PgfDB::get_root<PgfPGF>().as_object();
|
||||
}
|
||||
}
|
||||
|
||||
return pgf;
|
||||
return db;
|
||||
} catch (std::system_error& e) {
|
||||
err->type = PGF_EXN_SYSTEM_ERROR;
|
||||
err->code = e.code().value();
|
||||
@@ -135,8 +143,8 @@ PgfPGF *pgf_read_ngf(const char *fpath,
|
||||
err->msg = strdup(e.what());
|
||||
}
|
||||
|
||||
if (pgf != NULL) {
|
||||
delete pgf;
|
||||
if (db != NULL) {
|
||||
delete db;
|
||||
if (is_new)
|
||||
remove(fpath);
|
||||
}
|
||||
@@ -145,32 +153,42 @@ PgfPGF *pgf_read_ngf(const char *fpath,
|
||||
}
|
||||
|
||||
PGF_API
|
||||
void pgf_free(PgfPGF *pgf)
|
||||
void pgf_free(PgfDB *db)
|
||||
{
|
||||
delete db;
|
||||
}
|
||||
|
||||
PGF_API_DECL
|
||||
void pgf_free_revision(PgfDB *pgf, PgfRevision revision)
|
||||
{
|
||||
delete pgf;
|
||||
}
|
||||
|
||||
PGF_API
|
||||
PgfText *pgf_abstract_name(PgfPGF* pgf)
|
||||
PgfText *pgf_abstract_name(PgfDB *db, PgfRevision revision)
|
||||
{
|
||||
DB_scope scope(pgf, READER_SCOPE);
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = revision;
|
||||
|
||||
return textdup(&(*pgf->get_root<PgfPGFRoot>()->abstract.name));
|
||||
return textdup(&(*pgf->abstract.name));
|
||||
}
|
||||
|
||||
PGF_API
|
||||
void pgf_iter_categories(PgfPGF *pgf, PgfItor *itor, PgfExn *err)
|
||||
void pgf_iter_categories(PgfDB *db, PgfRevision revision,
|
||||
PgfItor *itor, PgfExn *err)
|
||||
{
|
||||
DB_scope scope(pgf, READER_SCOPE);
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = revision;
|
||||
|
||||
err->type = PGF_EXN_NONE;
|
||||
namespace_iter(pgf->get_root<PgfPGFRoot>()->abstract.cats, itor, err);
|
||||
namespace_iter(pgf->abstract.cats, itor, err);
|
||||
}
|
||||
|
||||
PGF_API
|
||||
PgfType pgf_start_cat(PgfPGF *pgf, PgfUnmarshaller *u)
|
||||
PgfType pgf_start_cat(PgfDB *db, PgfRevision revision,
|
||||
PgfUnmarshaller *u)
|
||||
{
|
||||
DB_scope scope(pgf, READER_SCOPE);
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = revision;
|
||||
|
||||
PgfText *startcat = (PgfText *)
|
||||
alloca(sizeof(PgfText)+9);
|
||||
@@ -178,7 +196,7 @@ PgfType pgf_start_cat(PgfPGF *pgf, PgfUnmarshaller *u)
|
||||
strcpy(startcat->text, "startcat");
|
||||
|
||||
ref<PgfFlag> flag =
|
||||
namespace_lookup(pgf->get_root<PgfPGFRoot>()->abstract.aflags, startcat);
|
||||
namespace_lookup(pgf->abstract.aflags, startcat);
|
||||
|
||||
if (flag != 0) {
|
||||
switch (ref<PgfLiteral>::get_tag(flag->value)) {
|
||||
@@ -202,12 +220,14 @@ PgfType pgf_start_cat(PgfPGF *pgf, PgfUnmarshaller *u)
|
||||
}
|
||||
|
||||
PGF_API
|
||||
PgfTypeHypo *pgf_category_context(PgfPGF *pgf, PgfText *catname, size_t *n_hypos, PgfUnmarshaller *u)
|
||||
PgfTypeHypo *pgf_category_context(PgfDB *db, PgfRevision revision,
|
||||
PgfText *catname, size_t *n_hypos, PgfUnmarshaller *u)
|
||||
{
|
||||
DB_scope scope(pgf, READER_SCOPE);
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = revision;
|
||||
|
||||
ref<PgfAbsCat> abscat =
|
||||
namespace_lookup(pgf->get_root<PgfPGFRoot>()->abstract.cats, catname);
|
||||
namespace_lookup(pgf->abstract.cats, catname);
|
||||
if (abscat == 0) {
|
||||
*n_hypos = 0;
|
||||
return NULL;
|
||||
@@ -228,12 +248,14 @@ PgfTypeHypo *pgf_category_context(PgfPGF *pgf, PgfText *catname, size_t *n_hypos
|
||||
}
|
||||
|
||||
PGF_API
|
||||
prob_t pgf_category_prob(PgfPGF *pgf, PgfText *catname)
|
||||
prob_t pgf_category_prob(PgfDB *db, PgfRevision revision,
|
||||
PgfText *catname)
|
||||
{
|
||||
DB_scope scope(pgf, READER_SCOPE);
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = revision;
|
||||
|
||||
ref<PgfAbsCat> abscat =
|
||||
namespace_lookup(pgf->get_root<PgfPGFRoot>()->abstract.cats, catname);
|
||||
namespace_lookup(pgf->abstract.cats, catname);
|
||||
if (abscat == 0) {
|
||||
return 0;
|
||||
}
|
||||
@@ -242,12 +264,14 @@ prob_t pgf_category_prob(PgfPGF *pgf, PgfText *catname)
|
||||
}
|
||||
|
||||
PGF_API
|
||||
void pgf_iter_functions(PgfPGF *pgf, PgfItor *itor, PgfExn *err)
|
||||
void pgf_iter_functions(PgfDB *db, PgfRevision revision,
|
||||
PgfItor *itor, PgfExn *err)
|
||||
{
|
||||
DB_scope scope(pgf, READER_SCOPE);
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = revision;
|
||||
|
||||
err->type = PGF_EXN_NONE;
|
||||
namespace_iter(pgf->get_root<PgfPGFRoot>()->abstract.funs, itor, err);
|
||||
namespace_iter(pgf->abstract.funs, itor, err);
|
||||
}
|
||||
|
||||
struct PgfItorHelper : PgfItor
|
||||
@@ -267,9 +291,11 @@ void iter_by_cat_helper(PgfItor *itor, PgfText *key, void *value,
|
||||
}
|
||||
|
||||
PGF_API
|
||||
void pgf_iter_functions_by_cat(PgfPGF *pgf, PgfText *cat, PgfItor *itor, PgfExn *err)
|
||||
void pgf_iter_functions_by_cat(PgfDB *db, PgfRevision revision,
|
||||
PgfText *cat, PgfItor *itor, PgfExn *err)
|
||||
{
|
||||
DB_scope scope(pgf, READER_SCOPE);
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = revision;
|
||||
|
||||
PgfItorHelper helper;
|
||||
helper.fn = iter_by_cat_helper;
|
||||
@@ -277,16 +303,18 @@ void pgf_iter_functions_by_cat(PgfPGF *pgf, PgfText *cat, PgfItor *itor, PgfExn
|
||||
helper.itor = itor;
|
||||
|
||||
err->type = PGF_EXN_NONE;
|
||||
namespace_iter(pgf->get_root<PgfPGFRoot>()->abstract.funs, &helper, err);
|
||||
namespace_iter(pgf->abstract.funs, &helper, err);
|
||||
}
|
||||
|
||||
PGF_API
|
||||
PgfType pgf_function_type(PgfPGF *pgf, PgfText *funname, PgfUnmarshaller *u)
|
||||
PgfType pgf_function_type(PgfDB *db, PgfRevision revision,
|
||||
PgfText *funname, PgfUnmarshaller *u)
|
||||
{
|
||||
DB_scope scope(pgf, READER_SCOPE);
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = revision;
|
||||
|
||||
ref<PgfAbsFun> absfun =
|
||||
namespace_lookup(pgf->get_root<PgfPGFRoot>()->abstract.funs, funname);
|
||||
namespace_lookup(pgf->abstract.funs, funname);
|
||||
if (absfun == 0)
|
||||
return 0;
|
||||
|
||||
@@ -294,12 +322,14 @@ PgfType pgf_function_type(PgfPGF *pgf, PgfText *funname, PgfUnmarshaller *u)
|
||||
}
|
||||
|
||||
PGF_API
|
||||
int pgf_function_is_constructor(PgfPGF *pgf, PgfText *funname)
|
||||
int pgf_function_is_constructor(PgfDB *db, PgfRevision revision,
|
||||
PgfText *funname)
|
||||
{
|
||||
DB_scope scope(pgf, READER_SCOPE);
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = revision;
|
||||
|
||||
ref<PgfAbsFun> absfun =
|
||||
namespace_lookup(pgf->get_root<PgfPGFRoot>()->abstract.funs, funname);
|
||||
namespace_lookup(pgf->abstract.funs, funname);
|
||||
if (absfun == 0)
|
||||
return false;
|
||||
|
||||
@@ -307,12 +337,14 @@ int pgf_function_is_constructor(PgfPGF *pgf, PgfText *funname)
|
||||
}
|
||||
|
||||
PGF_API
|
||||
prob_t pgf_function_prob(PgfPGF *pgf, PgfText *funname)
|
||||
prob_t pgf_function_prob(PgfDB *db, PgfRevision revision,
|
||||
PgfText *funname)
|
||||
{
|
||||
DB_scope scope(pgf, READER_SCOPE);
|
||||
DB_scope scope(db, READER_SCOPE);
|
||||
ref<PgfPGF> pgf = revision;
|
||||
|
||||
ref<PgfAbsFun> absfun =
|
||||
namespace_lookup(pgf->get_root<PgfPGFRoot>()->abstract.funs, funname);
|
||||
namespace_lookup(pgf->abstract.funs, funname);
|
||||
if (absfun == 0)
|
||||
return INFINITY;
|
||||
|
||||
@@ -364,16 +396,18 @@ PgfType pgf_read_type(PgfText *input, PgfUnmarshaller *u)
|
||||
}
|
||||
|
||||
PGF_API
|
||||
void pgf_create_function(PgfPGF *pgf, PgfText *name,
|
||||
void pgf_create_function(PgfDB *db, PgfRevision revision,
|
||||
PgfText *name,
|
||||
PgfType ty, prob_t prob,
|
||||
PgfMarshaller *m)
|
||||
PgfMarshaller *m,
|
||||
PgfExn *err)
|
||||
{
|
||||
DB_scope scope(pgf, WRITER_SCOPE);
|
||||
DB_scope scope(db, WRITER_SCOPE);
|
||||
|
||||
PgfDBUnmarshaller u(m);
|
||||
|
||||
ref<PgfPGFRoot> root = pgf->get_root<PgfPGFRoot>();
|
||||
ref<PgfAbsFun> absfun = DB::malloc<PgfAbsFun>(sizeof(PgfAbsFun)+name->size+1);
|
||||
ref<PgfPGF> pgf = revision;
|
||||
ref<PgfAbsFun> absfun = PgfDB::malloc<PgfAbsFun>(sizeof(PgfAbsFun)+name->size+1);
|
||||
absfun->type = m->match_type(&u, ty);
|
||||
absfun->arity = 0;
|
||||
absfun->defns = 0;
|
||||
@@ -384,7 +418,7 @@ void pgf_create_function(PgfPGF *pgf, PgfText *name,
|
||||
memcpy(&absfun->name, name, sizeof(PgfText)+name->size+1);
|
||||
|
||||
Namespace<PgfAbsFun> nmsp =
|
||||
namespace_insert(root->abstract.funs, absfun);
|
||||
namespace_release(root->abstract.funs);
|
||||
root->abstract.funs = nmsp;
|
||||
namespace_insert(pgf->abstract.funs, absfun);
|
||||
namespace_release(pgf->abstract.funs);
|
||||
pgf->abstract.funs = nmsp;
|
||||
}
|
||||
|
||||
@@ -210,62 +210,80 @@ struct PgfMarshaller {
|
||||
|
||||
typedef float prob_t;
|
||||
|
||||
typedef struct PgfPGF PgfPGF;
|
||||
typedef struct PgfDB PgfDB;
|
||||
typedef object PgfRevision;
|
||||
|
||||
/* Reads a PGF file and keeps it in memory. */
|
||||
/* Reads a PGF file and builds the database in memory.
|
||||
* If successful, *revision will contain the initial revision of
|
||||
* the grammar. */
|
||||
PGF_API_DECL
|
||||
PgfPGF *pgf_read_pgf(const char* fpath,
|
||||
PgfExn* err);
|
||||
PgfDB *pgf_read_pgf(const char* fpath, PgfRevision *revision,
|
||||
PgfExn* err);
|
||||
|
||||
/* Reads a PGF file and stores the unpacked data in an NGF file
|
||||
* ready to be shared with other process, or used for quick startup.
|
||||
* ready to be shared with other process, or used for quick re-start.
|
||||
* The NGF file is platform dependent and should not be copied
|
||||
* between machines. */
|
||||
PGF_API_DECL
|
||||
PgfPGF *pgf_boot_ngf(const char* pgf_path, const char* ngf_path,
|
||||
PgfExn* err);
|
||||
PgfDB *pgf_boot_ngf(const char* pgf_path, const char* ngf_path,
|
||||
PgfRevision *revision,
|
||||
PgfExn* err);
|
||||
|
||||
/* Tries to read the grammar from an already booted NGF file.
|
||||
* If the file does not exist then a new one is created, and the
|
||||
* grammar is set to be empty. It can later be populated with
|
||||
* rules dynamically. */
|
||||
* rules dynamically. The default grammar revision is stored
|
||||
* in *revision. */
|
||||
PGF_API_DECL
|
||||
PgfPGF *pgf_read_ngf(const char* fpath,
|
||||
PgfExn* err);
|
||||
PgfDB *pgf_read_ngf(const char* fpath,
|
||||
PgfRevision *revision,
|
||||
PgfExn* err);
|
||||
|
||||
/* Release the grammar when it is no longer needed. */
|
||||
/* Release the database when it is no longer needed. */
|
||||
PGF_API_DECL
|
||||
void pgf_free(PgfPGF *pgf);
|
||||
void pgf_free(PgfDB *pgf);
|
||||
|
||||
PGF_API_DECL
|
||||
PgfText *pgf_abstract_name(PgfPGF *pgf);
|
||||
void pgf_free_revision(PgfDB *pgf, PgfRevision revision);
|
||||
|
||||
PGF_API_DECL
|
||||
void pgf_iter_categories(PgfPGF *pgf, PgfItor *itor, PgfExn *err);
|
||||
PgfText *pgf_abstract_name(PgfDB *db, PgfRevision revision);
|
||||
|
||||
PGF_API_DECL
|
||||
PgfType pgf_start_cat(PgfPGF *pgf, PgfUnmarshaller *u);
|
||||
void pgf_iter_categories(PgfDB *db, PgfRevision revision,
|
||||
PgfItor *itor, PgfExn *err);
|
||||
|
||||
PGF_API_DECL
|
||||
PgfTypeHypo *pgf_category_context(PgfPGF *pgf, PgfText *catname, size_t *n_hypos, PgfUnmarshaller *u);
|
||||
PgfType pgf_start_cat(PgfDB *db, PgfRevision revision,
|
||||
PgfUnmarshaller *u);
|
||||
|
||||
PGF_API_DECL
|
||||
prob_t pgf_category_prob(PgfPGF* pgf, PgfText *catname);
|
||||
PgfTypeHypo *pgf_category_context(PgfDB *db, PgfRevision revision,
|
||||
PgfText *catname, size_t *n_hypos, PgfUnmarshaller *u);
|
||||
|
||||
PGF_API_DECL
|
||||
void pgf_iter_functions(PgfPGF *pgf, PgfItor *itor, PgfExn *err);
|
||||
prob_t pgf_category_prob(PgfDB *db, PgfRevision revision,
|
||||
PgfText *catname);
|
||||
|
||||
PGF_API_DECL
|
||||
void pgf_iter_functions_by_cat(PgfPGF *pgf, PgfText *cat, PgfItor *itor, PgfExn *err);
|
||||
void pgf_iter_functions(PgfDB *db, PgfRevision revision,
|
||||
PgfItor *itor, PgfExn *err);
|
||||
|
||||
PGF_API_DECL
|
||||
PgfType pgf_function_type(PgfPGF *pgf, PgfText *funname, PgfUnmarshaller *u);
|
||||
void pgf_iter_functions_by_cat(PgfDB *db, PgfRevision revision,
|
||||
PgfText *cat, PgfItor *itor, PgfExn *err);
|
||||
|
||||
PGF_API_DECL
|
||||
int pgf_function_is_constructor(PgfPGF *pgf, PgfText *funname);
|
||||
PgfType pgf_function_type(PgfDB *db, PgfRevision revision,
|
||||
PgfText *funname, PgfUnmarshaller *u);
|
||||
|
||||
PGF_API_DECL
|
||||
prob_t pgf_function_prob(PgfPGF *pgf, PgfText *funname);
|
||||
int pgf_function_is_constructor(PgfDB *db, PgfRevision revision,
|
||||
PgfText *funname);
|
||||
|
||||
PGF_API_DECL
|
||||
prob_t pgf_function_prob(PgfDB *db, PgfRevision revision,
|
||||
PgfText *funname);
|
||||
|
||||
typedef struct PgfPrintContext PgfPrintContext;
|
||||
|
||||
@@ -291,8 +309,10 @@ PGF_API_DECL
|
||||
PgfType pgf_read_type(PgfText *input, PgfUnmarshaller *u);
|
||||
|
||||
PGF_API_DECL
|
||||
void pgf_create_function(PgfPGF *pgf, PgfText *name,
|
||||
void pgf_create_function(PgfDB *db, PgfRevision revision,
|
||||
PgfText *name,
|
||||
PgfType ty, prob_t prob,
|
||||
PgfMarshaller *m);
|
||||
PgfMarshaller *m,
|
||||
PgfExn *err);
|
||||
|
||||
#endif // PGF_H_
|
||||
|
||||
@@ -204,7 +204,7 @@ PgfLiteral PgfReader::read_literal()
|
||||
}
|
||||
case PgfLiteralInt::tag: {
|
||||
ref<PgfLiteralInt> lit_int =
|
||||
DB::malloc<PgfLiteralInt>(sizeof(PgfLiteralInt)+sizeof(uintmax_t));
|
||||
PgfDB::malloc<PgfLiteralInt>(sizeof(PgfLiteralInt)+sizeof(uintmax_t));
|
||||
lit_int->size = 1;
|
||||
lit_int->val[0] = read_int();
|
||||
lit = ref<PgfLiteralInt>::tagged(lit_int);
|
||||
@@ -245,20 +245,20 @@ PgfExpr PgfReader::read_expr()
|
||||
break;
|
||||
}
|
||||
case PgfExprApp::tag: {
|
||||
ref<PgfExprApp> eapp = DB::malloc<PgfExprApp>();
|
||||
ref<PgfExprApp> eapp = PgfDB::malloc<PgfExprApp>();
|
||||
eapp->fun = read_expr();
|
||||
eapp->arg = read_expr();
|
||||
expr = ref<PgfExprApp>::tagged(eapp);
|
||||
break;
|
||||
}
|
||||
case PgfExprLit::tag: {
|
||||
ref<PgfExprLit> elit = DB::malloc<PgfExprLit>();
|
||||
ref<PgfExprLit> elit = PgfDB::malloc<PgfExprLit>();
|
||||
elit->lit = read_literal();
|
||||
expr = ref<PgfExprLit>::tagged(elit);
|
||||
break;
|
||||
}
|
||||
case PgfExprMeta::tag: {
|
||||
ref<PgfExprMeta> emeta = DB::malloc<PgfExprMeta>();
|
||||
ref<PgfExprMeta> emeta = PgfDB::malloc<PgfExprMeta>();
|
||||
emeta->id = read_int();
|
||||
expr = ref<PgfExprMeta>::tagged(emeta);
|
||||
break;
|
||||
@@ -269,13 +269,13 @@ PgfExpr PgfReader::read_expr()
|
||||
break;
|
||||
}
|
||||
case PgfExprVar::tag: {
|
||||
ref<PgfExprVar> evar = DB::malloc<PgfExprVar>();
|
||||
ref<PgfExprVar> evar = PgfDB::malloc<PgfExprVar>();
|
||||
evar->var = read_int();
|
||||
expr = ref<PgfExprVar>::tagged(evar);
|
||||
break;
|
||||
}
|
||||
case PgfExprTyped::tag: {
|
||||
ref<PgfExprTyped> etyped = DB::malloc<PgfExprTyped>();
|
||||
ref<PgfExprTyped> etyped = PgfDB::malloc<PgfExprTyped>();
|
||||
etyped->expr = read_expr();
|
||||
etyped->type = read_type();
|
||||
expr = ref<PgfExprTyped>::tagged(etyped);
|
||||
@@ -339,24 +339,24 @@ PgfPatt PgfReader::read_patt()
|
||||
break;
|
||||
}
|
||||
case PgfPattWild::tag: {
|
||||
ref<PgfPattWild> pwild = DB::malloc<PgfPattWild>();
|
||||
ref<PgfPattWild> pwild = PgfDB::malloc<PgfPattWild>();
|
||||
patt = ref<PgfPattWild>::tagged(pwild);
|
||||
break;
|
||||
}
|
||||
case PgfPattLit::tag: {
|
||||
ref<PgfPattLit> plit = DB::malloc<PgfPattLit>();
|
||||
ref<PgfPattLit> plit = PgfDB::malloc<PgfPattLit>();
|
||||
plit->lit = read_literal();
|
||||
patt = ref<PgfPattLit>::tagged(plit);
|
||||
break;
|
||||
}
|
||||
case PgfPattImplArg::tag: {
|
||||
ref<PgfPattImplArg> pimpl = DB::malloc<PgfPattImplArg>();
|
||||
ref<PgfPattImplArg> pimpl = PgfDB::malloc<PgfPattImplArg>();
|
||||
pimpl->patt = read_patt();
|
||||
patt = ref<PgfPattImplArg>::tagged(pimpl);
|
||||
break;
|
||||
}
|
||||
case PgfPattTilde::tag: {
|
||||
ref<PgfPattTilde> ptilde = DB::malloc<PgfPattTilde>();
|
||||
ref<PgfPattTilde> ptilde = PgfDB::malloc<PgfPattTilde>();
|
||||
ptilde->expr = read_expr();
|
||||
patt = ref<PgfPattTilde>::tagged(ptilde);
|
||||
break;
|
||||
@@ -425,9 +425,9 @@ void PgfReader::read_abstract(ref<PgfAbstr> abstract)
|
||||
abstract->cats = read_namespace<PgfAbsCat>(&PgfReader::read_abscat);
|
||||
}
|
||||
|
||||
ref<PgfPGFRoot> PgfReader::read_pgf()
|
||||
ref<PgfPGF> PgfReader::read_pgf()
|
||||
{
|
||||
ref<PgfPGFRoot> pgf = DB::malloc<PgfPGFRoot>();
|
||||
ref<PgfPGF> pgf = PgfDB::malloc<PgfPGF>();
|
||||
|
||||
pgf->major_version = read_u16be();
|
||||
pgf->minor_version = read_u16be();
|
||||
|
||||
@@ -70,7 +70,7 @@ public:
|
||||
ref<PgfAbsCat> read_abscat();
|
||||
void read_abstract(ref<PgfAbstr> abstract);
|
||||
|
||||
ref<PgfPGFRoot> read_pgf();
|
||||
ref<PgfPGF> read_pgf();
|
||||
|
||||
private:
|
||||
std::istream *in;
|
||||
|
||||
@@ -10,7 +10,7 @@ struct PgfVector {
|
||||
template <class A> inline
|
||||
ref<PgfVector<A>> vector_new(size_t len)
|
||||
{
|
||||
ref<PgfVector<A>> res = DB::malloc<PgfVector<A>>(sizeof(PgfVector<A>)+len*sizeof(A));
|
||||
ref<PgfVector<A>> res = PgfDB::malloc<PgfVector<A>>(sizeof(PgfVector<A>)+len*sizeof(A));
|
||||
res->len = len;
|
||||
return res;
|
||||
}
|
||||
@@ -18,7 +18,7 @@ ref<PgfVector<A>> vector_new(size_t len)
|
||||
template <class C, class A> inline
|
||||
ref<C> vector_new(PgfVector<A> C::* field, size_t len)
|
||||
{
|
||||
ref<C> res = DB::malloc<C>(((size_t) &(((C*) NULL)->*field))+sizeof(PgfVector<A>)+len*sizeof(A));
|
||||
ref<C> res = PgfDB::malloc<C>(((size_t) &(((C*) NULL)->*field))+sizeof(PgfVector<A>)+len*sizeof(A));
|
||||
(res->*field).len = len;
|
||||
return res;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user