mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-12 06:19:33 -06:00
throw away the long obsolete runtime type information in the C runtime
This commit is contained in:
@@ -25,7 +25,6 @@ guinclude_HEADERS = \
|
||||
gu/str.h \
|
||||
gu/string.h \
|
||||
gu/sysdeps.h \
|
||||
gu/type.h \
|
||||
gu/ucs.h \
|
||||
gu/utf8.h \
|
||||
gu/variant.h
|
||||
@@ -57,7 +56,6 @@ libgu_la_SOURCES = \
|
||||
gu/prime.c \
|
||||
gu/str.c \
|
||||
gu/string.c \
|
||||
gu/type.c \
|
||||
gu/utf8.c \
|
||||
gu/ucs.c \
|
||||
gu/variant.c
|
||||
|
||||
@@ -24,8 +24,6 @@ unsigned gu_ceil2e(unsigned u)
|
||||
return u;
|
||||
}
|
||||
|
||||
GU_DEFINE_TYPE(GuIntDecodeExn, abstract, _);
|
||||
|
||||
double
|
||||
gu_decode_double(uint64_t u)
|
||||
{
|
||||
|
||||
@@ -103,9 +103,6 @@ gu_tagged(void* ptr, size_t tag) {
|
||||
}
|
||||
|
||||
#include <gu/exn.h>
|
||||
#include <gu/type.h>
|
||||
|
||||
extern GU_DECLARE_TYPE(GuIntDecodeExn, abstract);
|
||||
|
||||
#define GU_DECODE_2C_(u_, t_, umax_, posmax_, tmin_, err_) \
|
||||
(((u_) <= (posmax_)) \
|
||||
|
||||
@@ -3,12 +3,10 @@
|
||||
|
||||
|
||||
GuExn*
|
||||
gu_new_exn(GuExn* parent, GuKind* catch, GuPool* pool)
|
||||
gu_new_exn(GuPool* pool)
|
||||
{
|
||||
GuExn* exn = gu_new(GuExn, pool);
|
||||
exn->state = GU_EXN_OK;
|
||||
exn->parent = parent;
|
||||
exn->catch = catch;
|
||||
exn->caught = NULL;
|
||||
exn->data.pool = pool;
|
||||
exn->data.data = NULL;
|
||||
@@ -20,6 +18,12 @@ gu_exn_is_raised(GuExn* err) {
|
||||
return err && (err->state == GU_EXN_RAISED);
|
||||
}
|
||||
|
||||
bool
|
||||
gu_exn_caught_(GuExn* err, const char* type)
|
||||
{
|
||||
return (err->caught && strcmp(err->caught, type) == 0);
|
||||
}
|
||||
|
||||
void
|
||||
gu_exn_block(GuExn* err)
|
||||
{
|
||||
@@ -37,24 +41,11 @@ gu_exn_unblock(GuExn* err)
|
||||
}
|
||||
|
||||
GuExnData*
|
||||
gu_exn_raise_debug_(GuExn* base, GuType* type,
|
||||
const char* filename, const char* func, int lineno)
|
||||
gu_exn_raise_debug_(GuExn* err, const char* type,
|
||||
const char* filename, const char* func, int lineno)
|
||||
{
|
||||
gu_require(type);
|
||||
|
||||
// TODO: log the error, once there's a system for dumping
|
||||
// error objects.
|
||||
|
||||
GuExn* err = base;
|
||||
|
||||
while (err && !(err->catch && gu_type_has_kind(type, err->catch))) {
|
||||
err->state = GU_EXN_RAISED;
|
||||
err = err->parent;
|
||||
}
|
||||
if (!err) {
|
||||
gu_abort_(GU_ASSERT_ASSERTION, filename, func, lineno,
|
||||
"Unexpected error raised");
|
||||
}
|
||||
GuExnState old_state = err->state;
|
||||
err->state = GU_EXN_RAISED;
|
||||
if (old_state == GU_EXN_OK) {
|
||||
@@ -63,23 +54,18 @@ gu_exn_raise_debug_(GuExn* base, GuType* type,
|
||||
return &err->data;
|
||||
}
|
||||
}
|
||||
|
||||
// Exceptian had already been raised, possibly blocked, or no
|
||||
// exception value is required.
|
||||
return NULL;
|
||||
}
|
||||
|
||||
GuExnData*
|
||||
gu_exn_raise_(GuExn* base, GuType* type)
|
||||
gu_exn_raise_(GuExn* base, const char* type)
|
||||
{
|
||||
return gu_exn_raise_debug_(base, type, NULL, NULL, -1);
|
||||
}
|
||||
|
||||
GuType*
|
||||
gu_exn_caught(GuExn* err)
|
||||
{
|
||||
return err->caught;
|
||||
}
|
||||
|
||||
void
|
||||
gu_raise_errno(GuExn* err)
|
||||
{
|
||||
@@ -90,5 +76,3 @@ gu_raise_errno(GuExn* err)
|
||||
err_data->data = gu_errno;
|
||||
}
|
||||
}
|
||||
|
||||
GU_DEFINE_TYPE(GuErrno, signed, _);
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#define GU_EXN_H_
|
||||
|
||||
#include <gu/mem.h>
|
||||
#include <gu/type.h>
|
||||
|
||||
/** @file
|
||||
*
|
||||
@@ -47,9 +46,7 @@ struct GuExnData
|
||||
struct GuExn {
|
||||
/// @privatesection
|
||||
GuExnState state;
|
||||
GuExn* parent;
|
||||
GuKind* catch;
|
||||
GuType* caught;
|
||||
const char* caught;
|
||||
GuExnData data;
|
||||
};
|
||||
|
||||
@@ -59,10 +56,8 @@ struct GuExn {
|
||||
|
||||
|
||||
/// Allocate a new local exception frame.
|
||||
#define gu_exn(parent_, catch_, pool_) &(GuExn){ \
|
||||
#define gu_exn(pool_) &(GuExn){ \
|
||||
.state = GU_EXN_OK, \
|
||||
.parent = parent_, \
|
||||
.catch = gu_kind(catch_), \
|
||||
.caught = NULL, \
|
||||
.data = {.pool = pool_, .data = NULL} \
|
||||
}
|
||||
@@ -70,8 +65,7 @@ struct GuExn {
|
||||
|
||||
/// Allocate a new exception frame.
|
||||
GuExn*
|
||||
gu_new_exn(GuExn* parent, GuKind* catch_kind, GuPool* pool);
|
||||
|
||||
gu_new_exn(GuPool* pool);
|
||||
|
||||
|
||||
bool
|
||||
@@ -83,10 +77,11 @@ gu_exn_clear(GuExn* err) {
|
||||
err->state = GU_EXN_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
GuType*
|
||||
gu_exn_caught(GuExn* err);
|
||||
#define gu_exn_caught(err, type) \
|
||||
(err->caught && strcmp(err->caught, #type) == 0)
|
||||
|
||||
bool
|
||||
gu_exn_caught_(GuExn* err, const char* type);
|
||||
|
||||
static inline const void*
|
||||
gu_exn_caught_data(GuExn* err)
|
||||
@@ -104,11 +99,11 @@ gu_exn_unblock(GuExn* err);
|
||||
|
||||
//@private
|
||||
GuExnData*
|
||||
gu_exn_raise_(GuExn* err, GuType* type);
|
||||
gu_exn_raise_(GuExn* err, const char* type);
|
||||
|
||||
//@private
|
||||
GuExnData*
|
||||
gu_exn_raise_debug_(GuExn* err, GuType* type,
|
||||
gu_exn_raise_debug_(GuExn* err, const char* type,
|
||||
const char* filename, const char* func, int lineno);
|
||||
|
||||
#ifdef NDEBUG
|
||||
@@ -122,7 +117,7 @@ gu_exn_raise_debug_(GuExn* err, GuType* type,
|
||||
|
||||
/// Raise an exception.
|
||||
#define gu_raise(exn, T) \
|
||||
gu_exn_raise(exn, gu_type(T))
|
||||
gu_exn_raise(exn, #T)
|
||||
/**<
|
||||
* @param exn The current exception frame.
|
||||
*
|
||||
@@ -168,8 +163,6 @@ gu_ok(GuExn* exn) {
|
||||
|
||||
typedef int GuErrno;
|
||||
|
||||
extern GU_DECLARE_TYPE(GuErrno, signed);
|
||||
|
||||
void
|
||||
gu_raise_errno(GuExn* err);
|
||||
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
#include <gu/bits.h>
|
||||
#include <math.h>
|
||||
|
||||
GU_DEFINE_TYPE(GuEOF, abstract, _);
|
||||
|
||||
|
||||
static bool
|
||||
gu_in_is_buffering(GuIn* in)
|
||||
@@ -258,7 +256,7 @@ gu_in_fini(GuFinalizer* fin)
|
||||
{
|
||||
GuIn* in = gu_container(fin, GuIn, fini);
|
||||
GuPool* pool = gu_local_pool();
|
||||
GuExn* err = gu_exn(NULL, type, pool);
|
||||
GuExn* err = gu_exn(pool);
|
||||
gu_in_end_buffering(in, err);
|
||||
gu_pool_free(pool);
|
||||
}
|
||||
|
||||
@@ -132,8 +132,4 @@ GuIn*
|
||||
gu_data_in(const uint8_t* buf, size_t size, GuPool* pool);
|
||||
|
||||
|
||||
extern GU_DECLARE_TYPE(GuEOF, abstract);
|
||||
|
||||
#include <gu/type.h>
|
||||
|
||||
#endif // GU_IN_H_
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include <gu/defs.h>
|
||||
#include <gu/mem.h>
|
||||
#include <gu/type.h>
|
||||
#include <gu/map.h>
|
||||
#include <gu/assert.h>
|
||||
#include <gu/prime.h>
|
||||
@@ -432,19 +431,3 @@ gu_make_map(size_t key_size, GuHasher* hasher,
|
||||
return map;
|
||||
}
|
||||
|
||||
GuMap*
|
||||
gu_map_type_make(GuMapType* mtype, GuPool* pool)
|
||||
{
|
||||
size_t key_size = 0;
|
||||
if (mtype->hasher && mtype->hasher != gu_addr_hasher) {
|
||||
key_size = gu_type_size(mtype->key_type);
|
||||
}
|
||||
size_t value_size = gu_type_size(mtype->value_type);
|
||||
return gu_make_map(key_size, mtype->hasher,
|
||||
value_size, mtype->default_value, pool);
|
||||
}
|
||||
|
||||
GU_DEFINE_KIND(GuMap, abstract);
|
||||
// GU_DEFINE_KIND(GuIntMap, GuMap);
|
||||
|
||||
|
||||
|
||||
@@ -81,45 +81,4 @@ typedef GuMap GuIntMap;
|
||||
#define gu_new_int_map(VAL_T, DEFAULT, POOL) \
|
||||
gu_new_map(int, gu_int_hasher, VAL_T, DEFAULT, POOL)
|
||||
|
||||
|
||||
#if defined(GU_TYPE_H_) && !defined(GU_MAP_H_TYPE_)
|
||||
#define GU_MAP_H_TYPE_
|
||||
|
||||
extern GU_DECLARE_KIND(GuMap);
|
||||
|
||||
typedef const struct GuMapType GuMapType, GuType_GuMap;
|
||||
|
||||
struct GuMapType {
|
||||
GuType_abstract abstract_base;
|
||||
GuHasher* hasher;
|
||||
GuType* key_type;
|
||||
GuType* value_type;
|
||||
const void* default_value;
|
||||
};
|
||||
|
||||
GuMap*
|
||||
gu_map_type_make(GuMapType* mtype, GuPool* pool);
|
||||
|
||||
#define gu_map_type_new(MAP_T, POOL) \
|
||||
gu_map_type_make(gu_type_cast(gu_type(MAP_T), GuMap), (POOL))
|
||||
|
||||
#define GU_TYPE_INIT_GuMap(k_, t_, kt_, h_, vt_, dv_) \
|
||||
{ \
|
||||
.abstract_base = GU_TYPE_INIT_abstract(k_, t_, _), \
|
||||
.hasher = h_, \
|
||||
.key_type = kt_, \
|
||||
.value_type = vt_, \
|
||||
.default_value = dv_ \
|
||||
}
|
||||
|
||||
#define gu_type__GuIntMap gu_type__GuMap
|
||||
|
||||
typedef GuType_GuMap GuType_GuIntMap;
|
||||
|
||||
#define GU_TYPE_INIT_GuIntMap(KIND, MAP_T, VAL_T, DEFAULT) \
|
||||
GU_TYPE_INIT_GuMap(KIND, MAP_T, gu_type(int), gu_int_hasher, \
|
||||
VAL_T, DEFAULT)
|
||||
|
||||
#endif
|
||||
|
||||
#endif // GU_MAP_H_
|
||||
|
||||
@@ -59,7 +59,7 @@ gu_out_fini(GuFinalizer* self)
|
||||
GuOut* out = gu_container(self, GuOut, fini);
|
||||
if (gu_out_is_buffering(out)) {
|
||||
GuPool* pool = gu_local_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), pool);
|
||||
GuExn* err = gu_new_exn(pool);
|
||||
gu_out_end_buf(out, err);
|
||||
gu_pool_free(pool);
|
||||
}
|
||||
|
||||
@@ -465,8 +465,3 @@ gu_buf_out(GuBuf* buf, GuPool* pool)
|
||||
bout->buf = buf;
|
||||
return gu_new_out(&bout->stream, pool);
|
||||
}
|
||||
|
||||
#include <gu/type.h>
|
||||
|
||||
GU_DEFINE_KIND(GuSeq, GuOpaque);
|
||||
GU_DEFINE_KIND(GuBuf, abstract);
|
||||
|
||||
@@ -59,8 +59,6 @@ GuHasher gu_str_hasher[1] = {
|
||||
}
|
||||
};
|
||||
|
||||
GU_DEFINE_TYPE(GuStr, repr, _);
|
||||
|
||||
char*
|
||||
gu_vasprintf(const char* fmt, va_list args, GuPool* pool)
|
||||
{
|
||||
|
||||
@@ -18,10 +18,6 @@ gu_str_eq(GuStr s1, GuStr s2);
|
||||
|
||||
extern GuHasher gu_str_hasher[1];
|
||||
|
||||
#include <gu/type.h>
|
||||
|
||||
extern GU_DECLARE_TYPE(GuStr, repr);
|
||||
|
||||
char* gu_vasprintf(const char* fmt, va_list args, GuPool* pool);
|
||||
|
||||
char* gu_asprintf(GuPool* pool, const char* fmt, ...);
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#include <gu/type.h>
|
||||
#include <gu/out.h>
|
||||
#include <gu/seq.h>
|
||||
#include <gu/map.h>
|
||||
@@ -230,7 +229,3 @@ GuHasher gu_string_hasher[1] = {
|
||||
.hash = gu_string_hasher_hash
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
GU_DEFINE_KIND(GuString, pointer);
|
||||
GU_DEFINE_KIND(GuStringMap, GuMap);
|
||||
|
||||
@@ -66,28 +66,6 @@ gu_string_hash(GuHash h, GuString s);
|
||||
extern GuHasher gu_string_hasher[1];
|
||||
#endif
|
||||
|
||||
#ifdef GU_TYPE_H_
|
||||
# ifndef GU_STRING_H_TYPE_
|
||||
# define GU_STRING_H_TYPE_
|
||||
|
||||
extern GU_DECLARE_KIND(GuString);
|
||||
# endif
|
||||
|
||||
# if defined(GU_MAP_H_TYPE_) && !defined(GU_STRING_H_MAP_TYPE_)
|
||||
# define GU_STRING_H_MAP_TYPE_
|
||||
|
||||
extern GU_DECLARE_KIND(GuStringMap);
|
||||
typedef GuType_GuMap GuType_GuStringMap;
|
||||
|
||||
#define GU_TYPE_INIT_GuStringMap(KIND, MAP_T, VAL_T, DEFAULT) \
|
||||
GU_TYPE_INIT_GuMap(KIND, MAP_T, \
|
||||
gu_type(GuString), gu_string_hasher, \
|
||||
VAL_T, DEFAULT)
|
||||
|
||||
# endif
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(GU_SEQ_H_) && !defined(GU_STRING_H_SEQ_)
|
||||
#define GU_STRING_H_SEQ_
|
||||
|
||||
|
||||
@@ -1,228 +0,0 @@
|
||||
|
||||
#include <gu/type.h>
|
||||
#include <gu/assert.h>
|
||||
#include <gu/map.h>
|
||||
|
||||
GuKind GU_TYPE_IDENT(type)[1] = {{ .super = NULL }};
|
||||
|
||||
GU_DEFINE_KIND(alias, type);
|
||||
GU_DEFINE_KIND(typedef, alias);
|
||||
GU_DEFINE_KIND(referenced, alias);
|
||||
|
||||
GU_DEFINE_KIND(repr, type);
|
||||
GU_DEFINE_KIND(GuOpaque, repr);
|
||||
|
||||
GU_DEFINE_KIND(abstract, type);
|
||||
|
||||
GU_DEFINE_KIND(struct, repr);
|
||||
|
||||
GU_DEFINE_KIND(pointer, repr);
|
||||
GU_DEFINE_KIND(reference, pointer);
|
||||
GU_DEFINE_KIND(shared, pointer);
|
||||
|
||||
GU_DEFINE_KIND(primitive, repr);
|
||||
|
||||
// sizeof(void) is illegal, so do this manually
|
||||
GuPrimType GU_TYPE_IDENT(void)[1] = {{
|
||||
.repr_base = {
|
||||
.type_base = {
|
||||
.kind_base = {
|
||||
.super = gu_kind(primitive),
|
||||
},
|
||||
},
|
||||
.size = 0,
|
||||
.align = 1,
|
||||
},
|
||||
.name = "void",
|
||||
}};
|
||||
|
||||
GU_DEFINE_KIND(integer, primitive);
|
||||
GU_DEFINE_TYPE(char, integer, _);
|
||||
|
||||
GU_DEFINE_KIND(signed, integer);
|
||||
GU_DEFINE_TYPE(int, signed, _);
|
||||
GU_DEFINE_TYPE(int8_t, signed, _);
|
||||
GU_DEFINE_TYPE(int16_t, signed, _);
|
||||
GU_DEFINE_TYPE(int32_t, signed, _);
|
||||
GU_DEFINE_TYPE(int64_t, signed, _);
|
||||
GU_DEFINE_TYPE(intptr_t, signed, _);
|
||||
GU_DEFINE_TYPE(intmax_t, signed, _);
|
||||
|
||||
GU_DEFINE_KIND(unsigned, integer);
|
||||
GU_DEFINE_TYPE(uint8_t, unsigned, _);
|
||||
GU_DEFINE_TYPE(uint16_t, unsigned, _);
|
||||
GU_DEFINE_TYPE(uint32_t, unsigned, _);
|
||||
GU_DEFINE_TYPE(uint64_t, unsigned, _);
|
||||
GU_DEFINE_TYPE(uintmax_t, unsigned, _);
|
||||
GU_DEFINE_TYPE(size_t, unsigned, _);
|
||||
|
||||
GU_DEFINE_TYPE(GuLength, unsigned, _);
|
||||
|
||||
GU_DEFINE_KIND(GuFloating, primitive);
|
||||
GU_DEFINE_TYPE(float, GuFloating, _);
|
||||
GU_DEFINE_TYPE(double, GuFloating, _);
|
||||
GU_DEFINE_TYPE(GuLongDouble, GuFloating, _);
|
||||
|
||||
|
||||
GU_DEFINE_KIND(enum, repr);
|
||||
|
||||
bool gu_type_has_kind(GuType* type, GuKind* kind)
|
||||
{
|
||||
GuKind* k = (GuKind*)type;
|
||||
while (k != NULL) {
|
||||
if (k == kind) {
|
||||
return true;
|
||||
}
|
||||
k = k->super;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
struct GuTypeMap {
|
||||
GuMap* map;
|
||||
};
|
||||
|
||||
static void
|
||||
gu_type_map_init(GuTypeMap* tmap, GuTypeTable* table)
|
||||
{
|
||||
for (int i = 0; i < table->parents.len; i++) {
|
||||
gu_type_map_init(tmap, table->parents.elems[i]);
|
||||
}
|
||||
for (int i = 0; i < table->entries.len; i++) {
|
||||
GuTypeTableEntry* e = &table->entries.elems[i];
|
||||
gu_map_put(tmap->map, e->kind, void*, e->val);
|
||||
}
|
||||
}
|
||||
|
||||
GuTypeMap*
|
||||
gu_new_type_map(GuTypeTable* table, GuPool* pool)
|
||||
{
|
||||
GuTypeMap* tmap = gu_new(GuTypeMap, pool);
|
||||
tmap->map = gu_new_map(GuKind, NULL, void*, &gu_null, pool);
|
||||
gu_type_map_init(tmap, table);
|
||||
return tmap;
|
||||
}
|
||||
|
||||
bool
|
||||
gu_struct_has_flex(GuStructRepr* srepr)
|
||||
{
|
||||
for (int i = 0; i < srepr->members.len; i++) {
|
||||
if (srepr->members.elems[i].is_flex) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void*
|
||||
gu_type_map_get(GuTypeMap* tmap, GuType* type)
|
||||
{
|
||||
GuKind* kind = (GuKind*)type;
|
||||
while (kind != NULL) {
|
||||
void* val = gu_map_get(tmap->map, kind, void*);
|
||||
if (val != NULL) {
|
||||
return val;
|
||||
}
|
||||
kind = kind->super;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const void*
|
||||
gu_type_dyn_cast(GuType* type, GuKind* kind)
|
||||
{
|
||||
if (gu_type_has_kind(type, kind)) {
|
||||
return type;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
const void*
|
||||
gu_type_check_cast(GuType* type, GuKind* kind)
|
||||
{
|
||||
gu_assert(gu_type_has_kind(type, kind));
|
||||
return type;
|
||||
}
|
||||
|
||||
GuTypeRepr*
|
||||
gu_type_repr(GuType* type)
|
||||
{
|
||||
GuTypeAlias* alias;
|
||||
while ((alias = gu_type_try_cast(type, alias))) {
|
||||
type = alias->type;
|
||||
}
|
||||
return gu_type_try_cast(type, repr);
|
||||
}
|
||||
|
||||
size_t
|
||||
gu_type_size(GuType* type)
|
||||
{
|
||||
GuTypeRepr* repr = gu_type_repr(type);
|
||||
return repr ? repr->size : 0;
|
||||
}
|
||||
|
||||
GuEnumConstant*
|
||||
gu_enum_value(GuEnumType* etype, const void* enump)
|
||||
{
|
||||
size_t esize = etype->repr_base.size;
|
||||
#define CHECK_ENUM_TYPE(t_) do { \
|
||||
if (esize == sizeof(t_)) { \
|
||||
t_ c = *(const t_*)enump; \
|
||||
for (int i = 0; i < etype->constants.len; i++) { \
|
||||
GuEnumConstant* cp = &etype->constants.elems[i]; \
|
||||
t_ d = *(const t_*)cp->enum_value; \
|
||||
if (c == d) { \
|
||||
return cp; \
|
||||
} \
|
||||
} \
|
||||
return NULL; \
|
||||
} \
|
||||
} while (false)
|
||||
|
||||
CHECK_ENUM_TYPE(int);
|
||||
CHECK_ENUM_TYPE(char);
|
||||
CHECK_ENUM_TYPE(short);
|
||||
CHECK_ENUM_TYPE(long);
|
||||
CHECK_ENUM_TYPE(long long);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void*
|
||||
gu_type_malloc(GuType* type, GuPool* pool)
|
||||
{
|
||||
GuTypeRepr* repr = gu_type_repr(type);
|
||||
gu_assert(repr);
|
||||
return gu_malloc_aligned(pool, repr->size, repr->align);
|
||||
}
|
||||
|
||||
#if 0
|
||||
|
||||
typedef const struct GuPtrConvFns GuPtrConvFns;
|
||||
|
||||
struct GuPtrConvFns {
|
||||
void* (*get)(const void* pp);
|
||||
void (*set)(void** pp, void* p);
|
||||
};
|
||||
|
||||
#define GU_TYPE_PTR_DEFINE_GETSET(name_, t_) \
|
||||
static void* gu_type_##name_##_ptr_get(const void* pp) { \
|
||||
return *(t_* const*) pp; \
|
||||
} \
|
||||
\
|
||||
static void gu_type_##name_##_ptr_set(void* pp, void* p) { \
|
||||
*(t_**) pp = p; \
|
||||
} \
|
||||
static GuPtrConvFns gu_ptr_conv_##name_ = { \
|
||||
.get = gu_type_##name_##_ptr_get, \
|
||||
.set = gu_type_##name_##_ptr_set \
|
||||
}
|
||||
|
||||
GU_TYPE_PTR_DEFINE_GETSET(void, void);
|
||||
GU_TYPE_PTR_DEFINE_GETSET(struct, GuStruct);
|
||||
GU_TYPE_PTR_DEFINE_GETSET(int, int);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -1,474 +0,0 @@
|
||||
|
||||
#ifndef GU_TYPE_H_
|
||||
#define GU_TYPE_H_
|
||||
|
||||
#include <gu/defs.h>
|
||||
|
||||
//
|
||||
// kind
|
||||
//
|
||||
|
||||
typedef const struct GuKind GuKind;
|
||||
|
||||
struct GuKind {
|
||||
GuKind* super;
|
||||
};
|
||||
|
||||
// Use GU_PASTE here so k_ can be preprocessor-expanded
|
||||
#define GU_TYPE_IDENT(k_) GU_PASTE(gu_type__,k_)
|
||||
|
||||
#define gu_kind(k_) ((GuKind*)GU_TYPE_IDENT(k_))
|
||||
|
||||
#define GU_DECLARE_KIND(k_) \
|
||||
GuKind GU_TYPE_IDENT(k_)[1]
|
||||
|
||||
extern GU_DECLARE_KIND(kind);
|
||||
|
||||
#define GU_DEFINE_KIND(k_, super_k_) \
|
||||
GuKind GU_TYPE_IDENT(k_)[1] = {{ .super = gu_kind(super_k_) }}
|
||||
|
||||
//
|
||||
// type
|
||||
//
|
||||
|
||||
typedef const struct GuType GuType;
|
||||
|
||||
struct GuType {
|
||||
GuKind kind_base;
|
||||
};
|
||||
|
||||
typedef GuType GuType_type;
|
||||
|
||||
extern GU_DECLARE_KIND(type);
|
||||
|
||||
#define GU_TYPE_INIT_type(k_, t_, _) { .kind_base = { .super = gu_kind(k_) } }
|
||||
|
||||
#define gu_type(t_) ((GuType*)gu_kind(t_))
|
||||
|
||||
|
||||
#define GU_KIND_TYPE(k_) GU_PASTE(GuType_,k_)
|
||||
|
||||
// This cannot be used indirectly, since we don't want to pp-expand k_.
|
||||
// We must inline the body into other macros.
|
||||
#define GU_TYPE_INIT(k_, ...) \
|
||||
GU_TYPE_INIT_##k_(k_, __VA_ARGS__)
|
||||
|
||||
//#define GU_TYPE_LIT(k_, ...)
|
||||
// ((GuType*)(GuType_##k_[]){GU_TYPE_INIT(k_, __VA_ARGS__)})
|
||||
#define GU_TYPE_LIT(k_, ...) \
|
||||
((GuType*)&(GU_KIND_TYPE(k_)) GU_TYPE_INIT_##k_(k_, __VA_ARGS__))
|
||||
|
||||
#define GU_DECLARE_TYPE(t_, k_) \
|
||||
GU_KIND_TYPE(k_) GU_TYPE_IDENT(t_)[1]
|
||||
|
||||
//#define GU_DEFINE_TYPE(t_, k_, ...)
|
||||
// GuType_##k_ GU_TYPE_IDENT(t_) = GU_TYPE_INIT(k_, t_, __VA_ARGS__)
|
||||
#define GU_DEFINE_TYPE(t_, k_, ...) \
|
||||
GU_KIND_TYPE(k_) GU_TYPE_IDENT(t_)[1] = \
|
||||
{ GU_TYPE_INIT_##k_(k_, t_, __VA_ARGS__) }
|
||||
|
||||
#define GU_DEFINE_TYPE_ALIAS(t1_, t2_) \
|
||||
static GuType* const GU_TYPE_IDENT(t1_) = gu_type(t2_)
|
||||
|
||||
|
||||
//
|
||||
// abstract
|
||||
//
|
||||
|
||||
typedef GuType GuType_abstract;
|
||||
|
||||
#define GU_TYPE_INIT_abstract(k_, t_, _) \
|
||||
GU_TYPE_INIT_type(k_, t_, _)
|
||||
|
||||
extern GU_DECLARE_KIND(abstract);
|
||||
|
||||
|
||||
//
|
||||
// repr
|
||||
//
|
||||
|
||||
typedef struct GuTypeRepr GuTypeRepr, GuType_repr;
|
||||
|
||||
struct GuTypeRepr {
|
||||
GuType type_base;
|
||||
uint16_t size;
|
||||
uint16_t align;
|
||||
};
|
||||
|
||||
#define GU_TYPE_INIT_repr(k_, t_, _) { \
|
||||
.type_base = GU_TYPE_INIT_type(k_, t_, _), \
|
||||
.size = sizeof(t_), \
|
||||
.align = gu_alignof(t_) \
|
||||
}
|
||||
|
||||
extern GU_DECLARE_KIND(repr);
|
||||
|
||||
|
||||
|
||||
//
|
||||
// GuOpaque
|
||||
//
|
||||
|
||||
typedef GuType_repr GuType_GuOpaque;
|
||||
|
||||
#define GU_TYPE_INIT_GuOpaque GU_TYPE_INIT_repr
|
||||
|
||||
extern GU_DECLARE_KIND(GuOpaque);
|
||||
|
||||
//
|
||||
// pointer
|
||||
//
|
||||
|
||||
typedef const struct GuPointerType GuPointerType, GuType_pointer;
|
||||
|
||||
struct GuPointerType {
|
||||
GuType_repr repr_base;
|
||||
GuType* pointed_type;
|
||||
};
|
||||
|
||||
#define GU_TYPE_INIT_pointer(k_, t_, pointed_) \
|
||||
{ \
|
||||
.repr_base = GU_TYPE_INIT_repr(k_, t_, _), \
|
||||
.pointed_type = pointed_ \
|
||||
}
|
||||
|
||||
|
||||
extern GU_DECLARE_KIND(pointer);
|
||||
|
||||
#define gu_ptr_type(t_) \
|
||||
GU_TYPE_LIT(pointer, t_*, gu_type(t_))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//
|
||||
// alias
|
||||
//
|
||||
|
||||
|
||||
typedef const struct GuTypeAlias GuTypeAlias, GuType_alias;
|
||||
|
||||
struct GuTypeAlias {
|
||||
GuType type_base;
|
||||
GuType* type;
|
||||
};
|
||||
|
||||
#define GU_TYPE_INIT_alias(k_, t_, type_) { \
|
||||
.type_base = GU_TYPE_INIT_type(k_, t_, _), \
|
||||
.type = type_ \
|
||||
}
|
||||
|
||||
extern GU_DECLARE_KIND(alias);
|
||||
|
||||
//
|
||||
// typedef
|
||||
//
|
||||
|
||||
typedef const struct GuTypeDef GuTypeDef, GuType_typedef;
|
||||
|
||||
struct GuTypeDef {
|
||||
GuType_alias alias_base;
|
||||
const char* name;
|
||||
};
|
||||
|
||||
#define GU_TYPE_INIT_typedef(k_, t_, type_) { \
|
||||
.alias_base = GU_TYPE_INIT_alias(k_, t_, type_), \
|
||||
.name = #t_, \
|
||||
}
|
||||
|
||||
extern GU_DECLARE_KIND(typedef);
|
||||
|
||||
#define GU_DEFINE_TYPEDEF_X(t_, dk_, k_, ...) \
|
||||
GU_DEFINE_TYPE(t_, dk_, GU_TYPE_LIT(k_, t_, __VA_ARGS__))
|
||||
|
||||
#define GU_DEFINE_TYPEDEF(t_, ...) \
|
||||
GU_DEFINE_TYPEDEF_X(t_, typedef, __VA_ARGS__)
|
||||
|
||||
|
||||
|
||||
//
|
||||
// referenced
|
||||
//
|
||||
|
||||
extern GU_DECLARE_KIND(referenced);
|
||||
|
||||
typedef GuType_alias GuType_referenced;
|
||||
|
||||
#define GU_TYPE_INIT_referenced GU_TYPE_INIT_alias
|
||||
|
||||
|
||||
|
||||
#include <gu/mem.h>
|
||||
|
||||
//
|
||||
// struct
|
||||
//
|
||||
|
||||
typedef const struct GuStructRepr GuStructRepr, GuType_struct;
|
||||
|
||||
typedef const struct GuMember GuMember;
|
||||
|
||||
struct GuMember {
|
||||
ptrdiff_t offset;
|
||||
const char* name;
|
||||
GuType* type;
|
||||
bool is_flex;
|
||||
};
|
||||
|
||||
struct GuStructRepr {
|
||||
GuType_repr repr_base;
|
||||
const char* name;
|
||||
struct {
|
||||
int len;
|
||||
GuMember* elems;
|
||||
} members;
|
||||
};
|
||||
|
||||
extern GU_DECLARE_KIND(struct);
|
||||
|
||||
#define GU_MEMBER_AUX_(struct_, member_, type_, is_flex_) \
|
||||
{ \
|
||||
.offset = offsetof(struct_, member_), \
|
||||
.name = #member_, \
|
||||
.type = type_, \
|
||||
.is_flex = is_flex_, \
|
||||
}
|
||||
|
||||
#define GU_MEMBER_V(struct_, member_, type_) \
|
||||
GU_MEMBER_AUX_(struct_, member_, type_, false)
|
||||
|
||||
#define GU_MEMBER(s_, m_, t_) \
|
||||
GU_MEMBER_V(s_, m_, gu_type(t_))
|
||||
|
||||
#define GU_MEMBER_P(s_, m_, t_) \
|
||||
GU_MEMBER_V(s_, m_, gu_ptr_type(t_))
|
||||
|
||||
#define GU_MEMBER_S(s_, m_, t_) \
|
||||
GU_MEMBER_V(s_, m_, gu_shared_ptr_type(t_))
|
||||
|
||||
#define GU_FLEX_MEMBER_V(struct_, member_, type_) \
|
||||
GU_MEMBER_AUX_(struct_, member_, type_, true)
|
||||
|
||||
#define GU_FLEX_MEMBER(s_, m_, t_) \
|
||||
GU_FLEX_MEMBER_V(s_, m_, gu_type(t_))
|
||||
|
||||
#define GU_FLEX_MEMBER_P(s_, m_, t_) \
|
||||
GU_FLEX_MEMBER_V(s_, m_, gu_ptr_type(t_))
|
||||
|
||||
|
||||
#define GU_TYPE_INIT_struct(k_, t_, ...) { \
|
||||
.repr_base = GU_TYPE_INIT_repr(k_, t_, _), \
|
||||
.name = #t_, \
|
||||
.members = { \
|
||||
.len = GU_ARRAY_LEN(GuMember,GU_ID({__VA_ARGS__})), \
|
||||
.elems = ((GuMember[]){__VA_ARGS__}) \
|
||||
} \
|
||||
}
|
||||
|
||||
bool
|
||||
gu_struct_has_flex(GuStructRepr* srepr);
|
||||
|
||||
|
||||
//
|
||||
// reference
|
||||
//
|
||||
|
||||
typedef GuType_pointer GuType_reference;
|
||||
|
||||
#define GU_TYPE_INIT_reference GU_TYPE_INIT_pointer
|
||||
|
||||
extern GU_DECLARE_KIND(reference);
|
||||
|
||||
|
||||
//
|
||||
// shared
|
||||
//
|
||||
|
||||
typedef GuType_pointer GuType_shared;
|
||||
|
||||
#define GU_TYPE_INIT_shared GU_TYPE_INIT_pointer
|
||||
|
||||
extern GU_DECLARE_KIND(shared);
|
||||
|
||||
#define gu_shared_ptr_type(t_) \
|
||||
GU_TYPE_LIT(shared, t_*, gu_type(t_))
|
||||
|
||||
//
|
||||
// primitives
|
||||
//
|
||||
|
||||
typedef const struct GuPrimType GuPrimType, GuType_primitive;
|
||||
|
||||
struct GuPrimType {
|
||||
GuType_repr repr_base;
|
||||
const char* name;
|
||||
};
|
||||
|
||||
#define GU_TYPE_INIT_primitive(k_, t_, _) { \
|
||||
.repr_base = GU_TYPE_INIT_repr(k_, t_, _), \
|
||||
.name = #t_ \
|
||||
}
|
||||
|
||||
extern GU_DECLARE_KIND(primitive);
|
||||
extern GU_DECLARE_TYPE(void, primitive);
|
||||
|
||||
#define GU_TYPE_INIT_integer GU_TYPE_INIT_primitive
|
||||
typedef GuType_primitive GuType_integer;
|
||||
extern GU_DECLARE_KIND(integer);
|
||||
extern GU_DECLARE_TYPE(char, integer);
|
||||
|
||||
#define GU_TYPE_INIT_signed GU_TYPE_INIT_integer
|
||||
typedef GuType_integer GuType_signed;
|
||||
extern GU_DECLARE_KIND(signed);
|
||||
extern GU_DECLARE_TYPE(int, signed);
|
||||
extern GU_DECLARE_TYPE(int8_t, signed);
|
||||
extern GU_DECLARE_TYPE(int16_t, signed);
|
||||
extern GU_DECLARE_TYPE(int32_t, signed);
|
||||
extern GU_DECLARE_TYPE(int64_t, signed);
|
||||
extern GU_DECLARE_TYPE(intptr_t, signed);
|
||||
extern GU_DECLARE_TYPE(intmax_t, signed);
|
||||
|
||||
#define GU_TYPE_INIT_unsigned GU_TYPE_INIT_integer
|
||||
typedef GuType_integer GuType_unsigned;
|
||||
extern GU_DECLARE_KIND(unsigned);
|
||||
extern GU_DECLARE_TYPE(uint8_t, unsigned);
|
||||
extern GU_DECLARE_TYPE(uint16_t, unsigned);
|
||||
extern GU_DECLARE_TYPE(uint32_t, unsigned);
|
||||
extern GU_DECLARE_TYPE(uint64_t, unsigned);
|
||||
extern GU_DECLARE_TYPE(uintmax_t, unsigned);
|
||||
extern GU_DECLARE_TYPE(size_t, unsigned);
|
||||
|
||||
typedef size_t GuLength;
|
||||
extern GU_DECLARE_TYPE(GuLength, unsigned); // TODO: get rid
|
||||
|
||||
|
||||
#define GU_TYPE_INIT_GuFloating GU_TYPE_INIT_primitive
|
||||
typedef GuType_primitive GuType_GuFloating;
|
||||
extern GU_DECLARE_KIND(GuFloating);
|
||||
extern GU_DECLARE_TYPE(float, GuFloating);
|
||||
extern GU_DECLARE_TYPE(double, GuFloating);
|
||||
typedef long double GuLongDouble;
|
||||
extern GU_DECLARE_TYPE(GuLongDouble, GuFloating);
|
||||
|
||||
|
||||
|
||||
//
|
||||
// enum
|
||||
//
|
||||
|
||||
extern GU_DECLARE_KIND(enum);
|
||||
|
||||
typedef const struct GuEnumConstant GuEnumConstant;
|
||||
|
||||
struct GuEnumConstant {
|
||||
const char* name;
|
||||
int64_t value;
|
||||
const void* enum_value;
|
||||
};
|
||||
|
||||
typedef const struct GuEnumType GuEnumType, GuType_enum;
|
||||
|
||||
struct GuEnumType {
|
||||
GuType_repr repr_base;
|
||||
struct {
|
||||
int len;
|
||||
GuEnumConstant* elems;
|
||||
} constants;
|
||||
};
|
||||
|
||||
#define GU_ENUM_C(t_, x) { \
|
||||
.name = #x, \
|
||||
.value = x, \
|
||||
.enum_value = (const t_[1]){ x } \
|
||||
}
|
||||
|
||||
#define GU_TYPE_INIT_enum(k_, t_, ...) { \
|
||||
.repr_base = GU_TYPE_INIT_repr(k_, t_, _), \
|
||||
.constants = { \
|
||||
.len = GU_ARRAY_LEN(GuEnumConstant,GU_ID({__VA_ARGS__})), \
|
||||
.elems = ((GuEnumConstant[]){__VA_ARGS__}) \
|
||||
} \
|
||||
}
|
||||
|
||||
GuEnumConstant*
|
||||
gu_enum_value(GuEnumType* etype, const void* enump);
|
||||
|
||||
|
||||
|
||||
|
||||
bool gu_type_has_kind(const GuType* type, const GuKind* kind);
|
||||
|
||||
|
||||
|
||||
|
||||
typedef const struct GuTypeTableEntry GuTypeTableEntry;
|
||||
|
||||
struct GuTypeTableEntry {
|
||||
GuKind* kind;
|
||||
void* val;
|
||||
};
|
||||
|
||||
typedef const struct GuTypeTable GuTypeTable;
|
||||
|
||||
struct GuTypeTable {
|
||||
struct {
|
||||
int len;
|
||||
GuTypeTable** elems;
|
||||
} parents;
|
||||
struct {
|
||||
int len;
|
||||
GuTypeTableEntry* elems;
|
||||
} entries;
|
||||
};
|
||||
|
||||
#define GU_TYPETABLE(parents_, ...) { \
|
||||
.parents = parents_, \
|
||||
.entries = { \
|
||||
.len = GU_ARRAY_LEN(GuTypeTableEntry,GU_ID({__VA_ARGS__})), \
|
||||
.elems = ((GuTypeTableEntry[]){__VA_ARGS__}) \
|
||||
} \
|
||||
}
|
||||
|
||||
typedef struct GuTypeMap GuTypeMap;
|
||||
|
||||
GuTypeMap*
|
||||
gu_new_type_map(GuTypeTable* table, GuPool* pool);
|
||||
|
||||
void*
|
||||
gu_type_map_get(GuTypeMap* tmap, GuType* type);
|
||||
|
||||
size_t
|
||||
gu_type_size(GuType* type);
|
||||
|
||||
GuTypeRepr*
|
||||
gu_type_repr(GuType* type);
|
||||
|
||||
const void*
|
||||
gu_type_check_cast(GuType* t, GuKind* k);
|
||||
|
||||
const void*
|
||||
gu_type_dyn_cast(GuType* t, GuKind* k);
|
||||
|
||||
#define gu_type_try_cast(type_, k_) \
|
||||
((GU_KIND_TYPE(k_)*)gu_type_dyn_cast(type_, gu_kind(k_)))
|
||||
|
||||
#ifndef NDEBUG
|
||||
#define gu_type_cast(type_, k_) \
|
||||
((GU_KIND_TYPE(k_)*)gu_type_check_cast(type_, gu_kind(k_)))
|
||||
#else
|
||||
#define gu_type_cast(type_, k_) \
|
||||
((GU_KIND_TYPE(k_)*)(type_))
|
||||
#endif
|
||||
|
||||
void* gu_type_malloc(GuType* type, GuPool* pool);
|
||||
|
||||
#if 0
|
||||
void* gu_type_ptr_get(GuType* type, const void* pp);
|
||||
void gu_type_ptr_set(GuType* type, void* pp, void* p);
|
||||
#endif
|
||||
|
||||
|
||||
#endif // GU_TYPE_H_
|
||||
@@ -1,8 +1,6 @@
|
||||
#include <gu/ucs.h>
|
||||
#include <gu/assert.h>
|
||||
|
||||
GU_DEFINE_TYPE(GuUCSExn, abstract, _);
|
||||
|
||||
bool
|
||||
gu_char_is_valid(char c)
|
||||
{
|
||||
|
||||
@@ -36,6 +36,4 @@ gu_str_to_ucs(const char* cbuf, size_t len, GuUCS* ubuf, GuExn* err);
|
||||
size_t
|
||||
gu_ucs_to_str(const GuUCS* ubuf, size_t len, char* cbuf, GuExn* err);
|
||||
|
||||
extern GU_DECLARE_TYPE(GuUCSExn, abstract);
|
||||
|
||||
#endif // GU_ISO10646_H_
|
||||
|
||||
@@ -49,9 +49,9 @@ gu_in_utf8_(GuIn* in, GuExn* err)
|
||||
uint8_t buf[3];
|
||||
// If reading the extra bytes causes EOF, it is an encoding
|
||||
// error, not a legitimate end of character stream.
|
||||
GuExn* tmp_err = gu_exn(err, GuEOF, NULL);
|
||||
gu_in_bytes(in, buf, len, tmp_err);
|
||||
if (tmp_err->caught) {
|
||||
gu_in_bytes(in, buf, len, err);
|
||||
if (gu_exn_caught(err, GuEOF)) {
|
||||
gu_exn_clear(err);
|
||||
goto fail;
|
||||
}
|
||||
if (!gu_ok(err)) {
|
||||
@@ -151,9 +151,9 @@ gu_in_utf8_buf(uint8_t** buf, GuIn* in, GuExn* err)
|
||||
}
|
||||
// If reading the extra bytes causes EOF, it is an encoding
|
||||
// error, not a legitimate end of character stream.
|
||||
GuExn* tmp_err = gu_exn(err, GuEOF, NULL);
|
||||
gu_in_bytes(in, p, len, tmp_err);
|
||||
if (tmp_err->caught) {
|
||||
gu_in_bytes(in, p, len, err);
|
||||
if (gu_exn_caught(err, GuEOF)) {
|
||||
gu_exn_clear(err);
|
||||
goto fail;
|
||||
}
|
||||
if (!gu_ok(err)) {
|
||||
|
||||
@@ -109,6 +109,3 @@ gu_variant_intval(GuVariant variant)
|
||||
}
|
||||
|
||||
const GuVariant gu_null_variant = { (GuWord) NULL };
|
||||
|
||||
GU_DEFINE_KIND(GuVariant, repr);
|
||||
GU_DEFINE_KIND(GuVariantAsPtr, repr);
|
||||
|
||||
@@ -27,7 +27,6 @@
|
||||
|
||||
#include <gu/defs.h>
|
||||
#include <gu/mem.h>
|
||||
#include <gu/type.h>
|
||||
|
||||
/** @name Variants
|
||||
* @{
|
||||
@@ -105,63 +104,4 @@ gu_variant_is_null(GuVariant v) {
|
||||
return ((void*)v == NULL);
|
||||
}
|
||||
|
||||
|
||||
// variant
|
||||
|
||||
typedef const struct GuConstructor GuConstructor;
|
||||
|
||||
struct GuConstructor {
|
||||
int c_tag;
|
||||
const char* c_name;
|
||||
const GuType* type;
|
||||
};
|
||||
|
||||
#define GU_CONSTRUCTOR_V(ctag, c_type) { \
|
||||
.c_tag = ctag, \
|
||||
.c_name = #ctag, \
|
||||
.type = c_type \
|
||||
}
|
||||
|
||||
#define GU_CONSTRUCTOR(ctag, t_) \
|
||||
GU_CONSTRUCTOR_V(ctag, gu_type(t_))
|
||||
|
||||
#define GU_CONSTRUCTOR_P(ctag, t_) \
|
||||
GU_CONSTRUCTOR_V(ctag, gu_ptr_type(t_))
|
||||
|
||||
#define GU_CONSTRUCTOR_S(ctag, t_, ...) \
|
||||
GU_CONSTRUCTOR_V(ctag, GU_TYPE_LIT(struct, t_, __VA_ARGS__))
|
||||
|
||||
#define GU_CONSTRUCTOR_S1(ctag, t_, mem1_, type1_) \
|
||||
GU_CONSTRUCTOR_S(ctag, t_, \
|
||||
GU_MEMBER(t_, mem1_, type1_))
|
||||
|
||||
#define GU_CONSTRUCTOR_S2(ctag, t_, mem1_, type1_, mem2_, type2_) \
|
||||
GU_CONSTRUCTOR_S(ctag, t_, \
|
||||
GU_MEMBER(t_, mem1_, type1_), \
|
||||
GU_MEMBER(t_, mem2_, type2_))
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
int len;
|
||||
GuConstructor* elems;
|
||||
} GuConstructors;
|
||||
|
||||
typedef const struct GuVariantType GuVariantType, GuType_GuVariant;
|
||||
|
||||
struct GuVariantType {
|
||||
GuType_repr repr_base;
|
||||
GuConstructors ctors;
|
||||
};
|
||||
|
||||
#define GU_TYPE_INIT_GuVariant(k_, t_, ...) { \
|
||||
.repr_base = GU_TYPE_INIT_repr(k_, GuVariant, _), \
|
||||
.ctors = { \
|
||||
.len = GU_ARRAY_LEN(GuConstructor,GU_ID({__VA_ARGS__})), \
|
||||
.elems = ((GuConstructor[]){__VA_ARGS__}) \
|
||||
} \
|
||||
}
|
||||
|
||||
extern GU_DECLARE_KIND(GuVariant);
|
||||
|
||||
#endif // GU_VARIANT_H_
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "data.h"
|
||||
#include <gu/type.h>
|
||||
#include <gu/variant.h>
|
||||
#include <gu/assert.h>
|
||||
#include <math.h>
|
||||
@@ -22,18 +21,6 @@ pgf_tokens_equal(PgfTokens* t1, PgfTokens* t2)
|
||||
return true;
|
||||
}
|
||||
|
||||
GU_DEFINE_TYPE(PgfCId, typedef, gu_type(GuString));
|
||||
|
||||
#define gu_type__PgfCIdMap gu_type__GuStringMap
|
||||
typedef GuType_GuStringMap GuType_PgfCIdMap;
|
||||
#define GU_TYPE_INIT_PgfCIdMap GU_TYPE_INIT_GuStringMap
|
||||
|
||||
GU_DEFINE_TYPE(PgfCCat, abstract);
|
||||
|
||||
GU_DEFINE_TYPE(PgfCncCat, abstract);
|
||||
|
||||
GU_DEFINE_TYPE(PgfDummyVariant, GuVariant);
|
||||
|
||||
static int
|
||||
pgf_flag_cmp_fn(GuOrder* self, const void* p1, const void* p2)
|
||||
{
|
||||
@@ -43,20 +30,6 @@ pgf_flag_cmp_fn(GuOrder* self, const void* p1, const void* p2)
|
||||
|
||||
GuOrder pgf_flag_order[1] = { { pgf_flag_cmp_fn } };
|
||||
|
||||
|
||||
GU_DEFINE_TYPE(PgfProductionSeq, abstract);
|
||||
GU_DEFINE_TYPE(PgfProductionBuf, abstract);
|
||||
|
||||
static prob_t inf_prob = INFINITY;
|
||||
|
||||
GU_DEFINE_TYPE(prob_t, GuFloating, _);
|
||||
|
||||
GU_DEFINE_TYPE(PgfMetaChildMap, GuMap,
|
||||
gu_type(PgfAbsCat), NULL,
|
||||
gu_type(prob_t), &inf_prob);
|
||||
|
||||
GU_DEFINE_TYPE(PgfAbsCat, abstract);
|
||||
|
||||
static int
|
||||
pgf_abscat_cmp_fn(GuOrder* self, const void* p1, const void* p2)
|
||||
{
|
||||
@@ -75,12 +48,6 @@ pgf_absfun_cmp_fn(GuOrder* self, const void* p1, const void* p2)
|
||||
|
||||
GuOrder pgf_absfun_order[1] = { { pgf_absfun_cmp_fn } };
|
||||
|
||||
|
||||
static GuString empty_string = "";
|
||||
|
||||
GU_DEFINE_TYPE(
|
||||
PgfPrintNames, PgfCIdMap, gu_type(GuString), &empty_string);
|
||||
|
||||
static int
|
||||
pgf_concr_cmp_fn(GuOrder* self, const void* p1, const void* p2)
|
||||
{
|
||||
|
||||
@@ -4,12 +4,10 @@
|
||||
#include <gu/variant.h>
|
||||
#include <gu/map.h>
|
||||
#include <gu/string.h>
|
||||
#include <gu/type.h>
|
||||
#include <gu/seq.h>
|
||||
#include <pgf/pgf.h>
|
||||
|
||||
typedef struct PgfCCat PgfCCat;
|
||||
extern GU_DECLARE_TYPE(PgfCCat, abstract);
|
||||
|
||||
typedef GuSeq PgfCCats;
|
||||
|
||||
@@ -41,7 +39,7 @@ typedef enum {
|
||||
|
||||
typedef struct {
|
||||
PgfCId ctor;
|
||||
GuLength n_args;
|
||||
size_t n_args;
|
||||
PgfPatt args[];
|
||||
} PgfPattApp;
|
||||
|
||||
@@ -70,7 +68,7 @@ typedef struct {
|
||||
|
||||
typedef struct {
|
||||
PgfExpr body;
|
||||
GuLength n_patts;
|
||||
size_t n_patts;
|
||||
PgfPatt patts[];
|
||||
} PgfEquation;
|
||||
|
||||
@@ -99,7 +97,6 @@ typedef GuSeq PgfAbsFuns;
|
||||
extern GuOrder pgf_absfun_order[1];
|
||||
|
||||
typedef GuMap PgfMetaChildMap;
|
||||
extern GU_DECLARE_TYPE(PgfMetaChildMap, GuMap);
|
||||
|
||||
typedef struct {
|
||||
PgfCId name;
|
||||
@@ -110,8 +107,6 @@ typedef struct {
|
||||
void* predicate;
|
||||
} PgfAbsCat;
|
||||
|
||||
extern GU_DECLARE_TYPE(PgfAbsCat, abstract);
|
||||
|
||||
typedef GuSeq PgfAbsCats;
|
||||
|
||||
extern GuOrder pgf_abscat_order[1];
|
||||
@@ -171,8 +166,6 @@ typedef struct {
|
||||
* represents. */
|
||||
} PgfCncCat;
|
||||
|
||||
extern GU_DECLARE_TYPE(PgfCncCat, abstract);
|
||||
|
||||
typedef GuSeq PgfTokens;
|
||||
|
||||
bool
|
||||
@@ -192,18 +185,14 @@ typedef struct {
|
||||
typedef struct PgfItemConts PgfItemConts;
|
||||
|
||||
typedef PgfCIdMap PgfPrintNames;
|
||||
extern GU_DECLARE_TYPE(PgfPrintNames, GuStringMap);
|
||||
|
||||
typedef GuStringMap PgfCncFunOverloadMap;
|
||||
extern GU_DECLARE_TYPE(PgfCncFunOverloadMap, GuStringMap);
|
||||
|
||||
typedef GuMap PgfCncOverloadMap;
|
||||
extern GU_DECLARE_TYPE(PgfCncOverloadMap, GuMap);
|
||||
|
||||
typedef struct PgfItem PgfItem;
|
||||
|
||||
typedef GuMap PgfCallbacksMap;
|
||||
extern GU_DECLARE_TYPE(PgfCallbacksMap, GuMap);
|
||||
|
||||
typedef GuVariant PgfSymbol;
|
||||
|
||||
@@ -237,7 +226,7 @@ typedef struct PgfSymbolKP
|
||||
/**< Default form that this symbol takes if none of of the
|
||||
* variant forms is triggered. */
|
||||
|
||||
GuLength n_forms;
|
||||
size_t n_forms;
|
||||
PgfAlternative forms[];
|
||||
/**< Variant forms whose choise depends on the following
|
||||
* symbol. */
|
||||
@@ -262,7 +251,7 @@ typedef struct {
|
||||
PgfAbsFun* absfun;
|
||||
PgfExprProb *ep;
|
||||
int funid;
|
||||
GuLength n_lins;
|
||||
size_t n_lins;
|
||||
PgfSequence* lins[];
|
||||
} PgfCncFun;
|
||||
|
||||
@@ -331,7 +320,6 @@ typedef struct {
|
||||
} PgfProductionMeta;
|
||||
|
||||
typedef GuSeq PgfProductionSeq;
|
||||
extern GU_DECLARE_TYPE(PgfProductionSeq, abstract);
|
||||
|
||||
typedef struct {
|
||||
PgfCCat* ccat;
|
||||
|
||||
@@ -73,57 +73,6 @@ pgf_expr_unapply(PgfExpr expr, GuPool* pool)
|
||||
return appl;
|
||||
}
|
||||
|
||||
GU_DEFINE_TYPE(PgfBindType, enum,
|
||||
GU_ENUM_C(PgfBindType, PGF_BIND_TYPE_EXPLICIT),
|
||||
GU_ENUM_C(PgfBindType, PGF_BIND_TYPE_IMPLICIT));
|
||||
|
||||
GU_DEFINE_TYPE(PgfLiteral, GuVariant,
|
||||
GU_CONSTRUCTOR_S(PGF_LITERAL_STR, PgfLiteralStr,
|
||||
GU_MEMBER(PgfLiteralStr, val, GuString)),
|
||||
GU_CONSTRUCTOR_S(PGF_LITERAL_INT, PgfLiteralInt,
|
||||
GU_MEMBER(PgfLiteralInt, val, int)),
|
||||
GU_CONSTRUCTOR_S(PGF_LITERAL_FLT, PgfLiteralFlt,
|
||||
GU_MEMBER(PgfLiteralFlt, val, double)));
|
||||
|
||||
GU_DECLARE_TYPE(PgfType, struct);
|
||||
|
||||
GU_DEFINE_TYPE(PgfHypo, struct,
|
||||
GU_MEMBER(PgfHypo, bind_type, PgfBindType),
|
||||
GU_MEMBER(PgfHypo, cid, PgfCId),
|
||||
GU_MEMBER_P(PgfHypo, type, PgfType));
|
||||
|
||||
GU_DEFINE_TYPE(
|
||||
PgfExpr, GuVariant,
|
||||
GU_CONSTRUCTOR_S(
|
||||
PGF_EXPR_ABS, PgfExprAbs,
|
||||
GU_MEMBER(PgfExprAbs, bind_type, PgfBindType),
|
||||
GU_MEMBER(PgfExprAbs, id, GuString),
|
||||
GU_MEMBER(PgfExprAbs, body, PgfExpr)),
|
||||
GU_CONSTRUCTOR_S(
|
||||
PGF_EXPR_APP, PgfExprApp,
|
||||
GU_MEMBER(PgfExprApp, fun, PgfExpr),
|
||||
GU_MEMBER(PgfExprApp, arg, PgfExpr)),
|
||||
GU_CONSTRUCTOR_S(
|
||||
PGF_EXPR_LIT, PgfExprLit,
|
||||
GU_MEMBER(PgfExprLit, lit, PgfLiteral)),
|
||||
GU_CONSTRUCTOR_S(
|
||||
PGF_EXPR_META, PgfExprMeta,
|
||||
GU_MEMBER(PgfExprMeta, id, int)),
|
||||
GU_CONSTRUCTOR_S(
|
||||
PGF_EXPR_FUN, PgfExprFun,
|
||||
GU_MEMBER(PgfExprFun, fun, GuString)),
|
||||
GU_CONSTRUCTOR_S(
|
||||
PGF_EXPR_VAR, PgfExprVar,
|
||||
GU_MEMBER(PgfExprVar, var, int)),
|
||||
GU_CONSTRUCTOR_S(
|
||||
PGF_EXPR_TYPED, PgfExprTyped,
|
||||
GU_MEMBER(PgfExprTyped, expr, PgfExpr),
|
||||
GU_MEMBER_P(PgfExprTyped, type, PgfType)),
|
||||
GU_CONSTRUCTOR_S(
|
||||
PGF_EXPR_IMPL_ARG, PgfExprImplArg,
|
||||
GU_MEMBER(PgfExprImplArg, expr, PgfExpr)));
|
||||
|
||||
|
||||
typedef struct PgfExprParser PgfExprParser;
|
||||
|
||||
typedef enum {
|
||||
|
||||
@@ -12,8 +12,6 @@
|
||||
/// An abstract syntax tree
|
||||
typedef GuVariant PgfExpr;
|
||||
|
||||
extern GU_DECLARE_TYPE(PgfExpr, GuVariant);
|
||||
|
||||
typedef struct PgfHypo PgfHypo;
|
||||
typedef struct PgfType PgfType;
|
||||
|
||||
@@ -128,8 +126,6 @@ typedef struct {
|
||||
PgfExpr expr;
|
||||
} PgfExprProb;
|
||||
|
||||
extern GU_DECLARE_TYPE(PgfExprProb, struct);
|
||||
|
||||
int
|
||||
pgf_expr_arity(PgfExpr expr);
|
||||
|
||||
|
||||
@@ -126,7 +126,7 @@ pgf_jit_predicate(PgfReader* rdr, PgfAbstr* abstr,
|
||||
#ifdef PGF_JIT_DEBUG
|
||||
GuPool* tmp_pool = gu_new_pool();
|
||||
GuOut* out = gu_file_out(stderr, tmp_pool);
|
||||
GuExn* err = gu_exn(NULL, type, tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
|
||||
gu_string_write(abscat->name, out, err);
|
||||
gu_puts(":\n", out, err);
|
||||
@@ -637,7 +637,7 @@ pgf_jit_function(PgfReader* rdr, PgfAbstr* abstr,
|
||||
#ifdef PGF_JIT_DEBUG
|
||||
GuPool* tmp_pool = gu_new_pool();
|
||||
GuOut* out = gu_file_out(stderr, tmp_pool);
|
||||
GuExn* err = gu_exn(NULL, type, tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
|
||||
gu_string_write(absfun->name, out, err);
|
||||
gu_puts(":\n", out, err);
|
||||
@@ -873,7 +873,7 @@ pgf_jit_function(PgfReader* rdr, PgfAbstr* abstr,
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
GuLength len = pgf_read_len(rdr);
|
||||
size_t len = pgf_read_len(rdr);
|
||||
uint8_t* buf = alloca(len*6+1);
|
||||
uint8_t* p = buf;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
|
||||
@@ -13,13 +13,6 @@
|
||||
|
||||
//#define PGF_LINEARIZER_DEBUG
|
||||
|
||||
GU_DEFINE_TYPE(PgfCncOverloadMap, GuMap,
|
||||
gu_type(PgfCCat), NULL,
|
||||
gu_ptr_type(GuBuf), &gu_null_struct);
|
||||
|
||||
GU_DEFINE_TYPE(PgfCncFunOverloadMap, GuStringMap, gu_ptr_type(PgfCncOverloadMap),
|
||||
&gu_null_struct);
|
||||
|
||||
static void
|
||||
pgf_lzr_add_overl_entry(PgfCncOverloadMap* overl_table,
|
||||
PgfCCat* ccat, void* entry,
|
||||
@@ -48,7 +41,7 @@ pgf_lzr_index(PgfConcr* concr,
|
||||
gu_map_get(concr->fun_indices, papply->fun->absfun->name,
|
||||
PgfCncOverloadMap*);
|
||||
if (!overl_table) {
|
||||
overl_table = gu_map_type_new(PgfCncOverloadMap, pool);
|
||||
overl_table = gu_new_addr_map(PgfCCat*, GuBuf*, &gu_null_struct, pool);
|
||||
gu_map_put(concr->fun_indices,
|
||||
papply->fun->absfun->name, PgfCncOverloadMap*, overl_table);
|
||||
}
|
||||
@@ -100,7 +93,7 @@ typedef struct {
|
||||
size_t n_vars;
|
||||
PgfPrintContext* context;
|
||||
|
||||
GuLength n_args;
|
||||
size_t n_args;
|
||||
PgfCncTree args[];
|
||||
} PgfCncTreeChunks;
|
||||
|
||||
@@ -463,7 +456,7 @@ pgf_cnc_resolve(PgfCnc* cnc,
|
||||
}
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_new_exn(tmp_pool);
|
||||
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
|
||||
GuOut* out = gu_string_buf_out(sbuf);
|
||||
|
||||
@@ -600,7 +593,7 @@ pgf_cnc_tree_enum_next(GuEnum* self, void* to, GuPool* pool)
|
||||
#ifdef PGF_LINEARIZER_DEBUG
|
||||
GuPool* tmp_pool = gu_new_pool();
|
||||
GuOut* out = gu_file_out(stderr, tmp_pool);
|
||||
GuExn* err = gu_exn(NULL, type, tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
if (gu_variant_is_null(*toc))
|
||||
gu_puts("*nil*\n", out, err);
|
||||
else {
|
||||
@@ -1034,8 +1027,6 @@ struct PgfSimpleLin {
|
||||
GuExn* err;
|
||||
};
|
||||
|
||||
GU_DEFINE_TYPE(PgfLinNonExist, abstract, _);
|
||||
|
||||
static void
|
||||
pgf_file_lzn_put_space(PgfSimpleLin* flin)
|
||||
{
|
||||
@@ -1145,7 +1136,7 @@ GuString
|
||||
pgf_get_tokens(PgfSymbols* syms, uint16_t sym_idx, GuPool* pool)
|
||||
{
|
||||
GuPool* tmp_pool = gu_new_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_new_exn(tmp_pool);
|
||||
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
|
||||
GuOut* out = gu_string_buf_out(sbuf);
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#ifndef PGF_LINEARIZER_H_
|
||||
#define PGF_LINEARIZER_H_
|
||||
|
||||
#include <gu/type.h>
|
||||
#include <gu/enum.h>
|
||||
|
||||
/// Linearization of abstract syntax trees.
|
||||
@@ -32,8 +31,6 @@ pgf_lzr_concretize(PgfConcr* concr, PgfExpr expr, GuExn* err, GuPool* pool);
|
||||
typedef struct {
|
||||
} PgfLinNonExist;
|
||||
|
||||
extern GU_DECLARE_TYPE(PgfLinNonExist, abstract);
|
||||
|
||||
PgfCncTree
|
||||
pgf_lzr_wrap_linref(PgfCncTree ctree, GuPool* pool);
|
||||
|
||||
|
||||
@@ -4,12 +4,6 @@
|
||||
#include <pgf/literals.h>
|
||||
#include <wctype.h>
|
||||
|
||||
GU_DEFINE_TYPE(PgfLiteralCallback, struct);
|
||||
|
||||
GU_DEFINE_TYPE(PgfCallbacksMap, GuMap,
|
||||
gu_type(PgfCncCat), NULL,
|
||||
gu_ptr_type(PgfLiteralCallback), &gu_null_struct);
|
||||
|
||||
|
||||
static PgfExprProb*
|
||||
pgf_match_string_lit(PgfLiteralCallback* self,
|
||||
@@ -185,7 +179,7 @@ pgf_match_name_lit(PgfLiteralCallback* self,
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuStringBuf *sbuf = gu_string_buf(tmp_pool);
|
||||
GuOut* out = gu_string_buf_out(sbuf);
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_new_exn(tmp_pool);
|
||||
|
||||
size_t offset = *poffset;
|
||||
|
||||
@@ -258,8 +252,8 @@ pgf_new_callbacks_map(PgfConcr* concr, GuPool *pool)
|
||||
PgfCCat* ccat;
|
||||
|
||||
PgfCallbacksMap* callbacks =
|
||||
gu_map_type_new(PgfCallbacksMap, pool);
|
||||
|
||||
gu_new_addr_map(PgfCncCat*, PgfLiteralCallback*, &gu_null_struct, pool);
|
||||
|
||||
fid = -1;
|
||||
ccat = gu_map_get(concr->ccats, &fid, PgfCCat*);
|
||||
if (ccat != NULL)
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
//#define PGF_RESULT_DEBUG
|
||||
|
||||
typedef GuBuf PgfItemBuf;
|
||||
static GU_DEFINE_TYPE(PgfItemBuf, abstract, _);
|
||||
|
||||
typedef struct PgfParseState PgfParseState;
|
||||
|
||||
@@ -26,20 +25,9 @@ struct PgfItemConts {
|
||||
int ref_count; // how many items point to this cont?
|
||||
};
|
||||
|
||||
static GU_DEFINE_TYPE(PgfItemConts, abstract, _);
|
||||
|
||||
typedef GuSeq PgfItemContss;
|
||||
static GU_DEFINE_TYPE(PgfItemContss, abstract);
|
||||
|
||||
typedef GuMap PgfContsMap;
|
||||
static GU_DEFINE_TYPE(PgfContsMap, GuMap,
|
||||
gu_type(PgfCCat), NULL,
|
||||
gu_ptr_type(PgfItemContss), &gu_null_struct);
|
||||
|
||||
typedef GuMap PgfGenCatMap;
|
||||
static GU_DEFINE_TYPE(PgfGenCatMap, GuMap,
|
||||
gu_type(PgfItemConts), NULL,
|
||||
gu_ptr_type(PgfCCat), &gu_null_struct);
|
||||
|
||||
typedef GuBuf PgfCCatBuf;
|
||||
|
||||
@@ -1045,7 +1033,7 @@ pgf_parsing_complete(PgfParsing* ps, PgfItem* item, PgfExprProb *ep)
|
||||
#ifdef PGF_PARSER_DEBUG
|
||||
GuPool* tmp_pool = gu_new_pool();
|
||||
GuOut* out = gu_file_out(stderr, tmp_pool);
|
||||
GuExn* err = gu_exn(NULL, type, tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
if (tmp_ccat == NULL) {
|
||||
gu_printf(out, err, "[");
|
||||
pgf_print_range(item->conts->state, ps->before, out, err);
|
||||
@@ -1293,8 +1281,8 @@ pgf_new_parse_state(PgfParsing* ps, size_t start_offset, BIND_TYPE bind_type)
|
||||
state->next = *pstate;
|
||||
state->agenda = gu_new_buf(PgfItem*, ps->pool);
|
||||
state->meta_item = NULL;
|
||||
state->generated_cats = gu_map_type_new(PgfGenCatMap, ps->pool);
|
||||
state->conts_map = gu_map_type_new(PgfContsMap, ps->pool);
|
||||
state->generated_cats = gu_new_addr_map(PgfItemConts*, PgfCCat*, &gu_null_struct, ps->pool);
|
||||
state->conts_map = gu_new_addr_map(PgfCCat*, PgfItemContss*, &gu_null_struct, ps->pool);
|
||||
state->needs_bind = (bind_type == BIND_NONE) &&
|
||||
(start_offset == end_offset);
|
||||
state->start_offset = start_offset;
|
||||
@@ -1742,7 +1730,7 @@ pgf_parsing_item(PgfParsing* ps, PgfItem* item)
|
||||
#ifdef PGF_PARSER_DEBUG
|
||||
GuPool* tmp_pool = gu_new_pool();
|
||||
GuOut* out = gu_file_out(stderr, tmp_pool);
|
||||
GuExn* err = gu_exn(NULL, type, tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
pgf_print_item(item, ps->before, out, err, tmp_pool);
|
||||
gu_pool_free(tmp_pool);
|
||||
#endif
|
||||
@@ -2230,7 +2218,7 @@ pgf_parse_result_next(PgfParsing* ps)
|
||||
GuPool* tmp_pool = gu_new_pool();
|
||||
GuOut* out = gu_file_out(stderr, tmp_pool);
|
||||
GuWriter* wtr = gu_new_utf8_writer(out, tmp_pool);
|
||||
GuExn* err = gu_exn(NULL, type, tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
pgf_print_expr_state0(st, wtr, err, tmp_pool);
|
||||
gu_pool_free(tmp_pool);
|
||||
#endif
|
||||
@@ -2316,8 +2304,6 @@ pgf_parsing_last_token(PgfParsing* ps, GuPool* pool)
|
||||
return tok;
|
||||
}
|
||||
|
||||
GU_DEFINE_TYPE(PgfParseError, abstract, _);
|
||||
|
||||
GuEnum*
|
||||
pgf_parse(PgfConcr* concr, PgfCId cat, GuString sentence,
|
||||
GuExn* err,
|
||||
|
||||
@@ -132,7 +132,7 @@ pgf_parseval(PgfConcr* concr, PgfExpr expr, PgfCId cat,
|
||||
{
|
||||
GuPool* pool = gu_new_pool();
|
||||
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), pool);
|
||||
GuExn* err = gu_new_exn(pool);
|
||||
|
||||
GuEnum* en_lins1 =
|
||||
pgf_lzr_concretize(concr, expr, err, pool);
|
||||
@@ -153,7 +153,7 @@ pgf_parseval(PgfConcr* concr, PgfExpr expr, PgfCId cat,
|
||||
PgfMetricsLznState state;
|
||||
state.bind = true;
|
||||
state.out = gu_string_buf_out(sbuf);
|
||||
state.err = gu_new_exn(NULL, gu_kind(type), pool);
|
||||
state.err = gu_new_exn(pool);
|
||||
state.funcs = &pgf_metrics_lin_funcs1;
|
||||
state.pos = 0;
|
||||
state.marks = gu_new_buf(int, pool);
|
||||
|
||||
@@ -10,8 +10,6 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
GU_DEFINE_TYPE(PgfExn, abstract, _);
|
||||
|
||||
PgfPGF*
|
||||
pgf_read(const char* fpath,
|
||||
GuPool* pool, GuExn* err)
|
||||
@@ -47,7 +45,7 @@ pgf_iter_languages(PgfPGF* pgf, GuMapItor* itor, GuExn* err)
|
||||
size_t n_concrs = gu_seq_length(pgf->concretes);
|
||||
for (size_t i = 0; i < n_concrs; i++) {
|
||||
PgfConcr* concr = gu_seq_index(pgf->concretes, PgfConcr, i);
|
||||
itor->fn(itor, concr->name, concr, err);
|
||||
itor->fn(itor, concr->name, &concr, err);
|
||||
if (!gu_ok(err))
|
||||
break;
|
||||
}
|
||||
@@ -71,7 +69,7 @@ pgf_iter_categories(PgfPGF* pgf, GuMapItor* itor, GuExn* err)
|
||||
size_t n_cats = gu_seq_length(pgf->abstract.cats);
|
||||
for (size_t i = 0; i < n_cats; i++) {
|
||||
PgfAbsCat* cat = gu_seq_index(pgf->abstract.cats, PgfAbsCat, i);
|
||||
itor->fn(itor, cat->name, cat, err);
|
||||
itor->fn(itor, cat->name, &cat, err);
|
||||
if (!gu_ok(err))
|
||||
break;
|
||||
}
|
||||
@@ -123,7 +121,7 @@ pgf_iter_functions(PgfPGF* pgf, GuMapItor* itor, GuExn* err)
|
||||
size_t n_funs = gu_seq_length(pgf->abstract.funs);
|
||||
for (size_t i = 0; i < n_funs; i++) {
|
||||
PgfAbsFun* fun = gu_seq_index(pgf->abstract.funs, PgfAbsFun, i);
|
||||
itor->fn(itor, fun->name, fun, err);
|
||||
itor->fn(itor, fun->name, &fun, err);
|
||||
if (!gu_ok(err))
|
||||
break;
|
||||
}
|
||||
@@ -138,7 +136,7 @@ pgf_iter_functions_by_cat(PgfPGF* pgf, PgfCId catname,
|
||||
PgfAbsFun* fun = gu_seq_index(pgf->abstract.funs, PgfAbsFun, i);
|
||||
|
||||
if (strcmp(fun->type->cid, catname) == 0) {
|
||||
itor->fn(itor, fun->name, fun, err);
|
||||
itor->fn(itor, fun->name, &fun, err);
|
||||
if (!gu_ok(err))
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -9,14 +9,9 @@
|
||||
|
||||
|
||||
typedef GuString PgfCId;
|
||||
extern GU_DECLARE_TYPE(PgfCId, typedef);
|
||||
|
||||
typedef GuString PgfToken;
|
||||
|
||||
extern GU_DECLARE_TYPE(PgfExn, abstract);
|
||||
extern GU_DECLARE_TYPE(PgfParseError, abstract);
|
||||
extern GU_DECLARE_TYPE(PgfTypeError, abstract);
|
||||
|
||||
typedef struct PgfPGF PgfPGF;
|
||||
|
||||
typedef struct PgfConcr PgfConcr;
|
||||
|
||||
@@ -25,14 +25,9 @@
|
||||
typedef struct PgfReadTagExn PgfReadTagExn;
|
||||
|
||||
struct PgfReadTagExn {
|
||||
GuType* type;
|
||||
int tag;
|
||||
};
|
||||
|
||||
static GU_DEFINE_TYPE(PgfReadTagExn, abstract, _);
|
||||
|
||||
static GU_DEFINE_TYPE(PgfReadExn, abstract, _);
|
||||
|
||||
uint8_t
|
||||
pgf_read_tag(PgfReader* rdr)
|
||||
{
|
||||
@@ -73,7 +68,6 @@ pgf_read_len(PgfReader* rdr)
|
||||
GuExnData* err_data = gu_raise(rdr->err, PgfReadTagExn);
|
||||
if (err_data) {
|
||||
PgfReadTagExn* rtag = gu_new(PgfReadTagExn, err_data->pool);
|
||||
rtag->type = gu_type(GuLength);
|
||||
rtag->tag = len;
|
||||
err_data->data = rtag;
|
||||
}
|
||||
@@ -93,7 +87,7 @@ pgf_read_cid(PgfReader* rdr, GuPool* pool)
|
||||
GuString
|
||||
pgf_read_string(PgfReader* rdr)
|
||||
{
|
||||
GuLength len = pgf_read_len(rdr);
|
||||
size_t len = pgf_read_len(rdr);
|
||||
return gu_string_read(len, rdr->opool, rdr->in, rdr->err);
|
||||
}
|
||||
|
||||
@@ -117,7 +111,7 @@ pgf_read_literal(PgfReader* rdr)
|
||||
uint8_t tag = pgf_read_tag(rdr);
|
||||
switch (tag) {
|
||||
case PGF_LITERAL_STR: {
|
||||
GuLength len = pgf_read_len(rdr);
|
||||
size_t len = pgf_read_len(rdr);
|
||||
uint8_t* buf = alloca(len*6+1);
|
||||
uint8_t* p = buf;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
@@ -159,7 +153,7 @@ pgf_read_literal(PgfReader* rdr)
|
||||
static PgfFlags*
|
||||
pgf_read_flags(PgfReader* rdr)
|
||||
{
|
||||
GuLength n_flags = pgf_read_len(rdr);
|
||||
size_t n_flags = pgf_read_len(rdr);
|
||||
gu_return_on_exn(rdr->err, NULL);
|
||||
|
||||
PgfFlags* flags = gu_new_seq(PgfFlag, n_flags, rdr->opool);
|
||||
@@ -447,20 +441,20 @@ pgf_read_absfun(PgfReader* rdr, PgfAbstr* abstr, PgfAbsFun* absfun)
|
||||
}
|
||||
break;
|
||||
case 1: {
|
||||
GuLength length = pgf_read_len(rdr);
|
||||
size_t length = pgf_read_len(rdr);
|
||||
gu_return_on_exn(rdr->err, NULL);
|
||||
|
||||
absfun->defns = gu_new_seq(PgfEquation*, length, rdr->opool);
|
||||
PgfEquation** data = gu_seq_data(absfun->defns);
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
GuLength n_patts = pgf_read_len(rdr);
|
||||
size_t n_patts = pgf_read_len(rdr);
|
||||
gu_return_on_exn(rdr->err, NULL);
|
||||
|
||||
PgfEquation *equ =
|
||||
gu_malloc(rdr->opool,
|
||||
sizeof(PgfEquation)+sizeof(PgfPatt)*n_patts);
|
||||
equ->n_patts = n_patts;
|
||||
for (GuLength j = 0; j < n_patts; j++) {
|
||||
for (size_t j = 0; j < n_patts; j++) {
|
||||
equ->patts[j] = pgf_read_patt(rdr);
|
||||
gu_return_on_exn(rdr->err, NULL);
|
||||
}
|
||||
@@ -470,7 +464,7 @@ pgf_read_absfun(PgfReader* rdr, PgfAbstr* abstr, PgfAbsFun* absfun)
|
||||
data[i] = equ;
|
||||
}
|
||||
|
||||
// pgf_jit_function(rdr, abstr, absfun);
|
||||
// pgf_jit_function(rdr, abstr, absfun);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@@ -571,11 +565,7 @@ pgf_read_abstract(PgfReader* rdr, PgfAbstr* abstract)
|
||||
static PgfCIdMap*
|
||||
pgf_read_printnames(PgfReader* rdr)
|
||||
{
|
||||
GuMapType* map_type = (GuMapType*)
|
||||
GU_TYPE_LIT(GuStringMap, _,
|
||||
gu_type(GuString),
|
||||
&"");
|
||||
PgfCIdMap* printnames = gu_map_type_make(map_type, rdr->opool);
|
||||
PgfCIdMap* printnames = gu_new_string_map(GuString, &"", rdr->opool);
|
||||
|
||||
size_t len = pgf_read_len(rdr);
|
||||
gu_return_on_exn(rdr->err, NULL);
|
||||
@@ -661,7 +651,7 @@ pgf_read_symbol(PgfReader* rdr)
|
||||
break;
|
||||
}
|
||||
case PGF_SYMBOL_KS: {
|
||||
GuLength len = pgf_read_len(rdr);
|
||||
size_t len = pgf_read_len(rdr);
|
||||
uint8_t* buf = alloca(len*6+1);
|
||||
uint8_t* p = buf;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
@@ -990,7 +980,7 @@ pgf_read_ccats(PgfReader* rdr, PgfConcr* concr)
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
PgfCCat* ccat = pgf_read_fid(rdr, concr);
|
||||
|
||||
GuLength n_prods = pgf_read_len(rdr);
|
||||
size_t n_prods = pgf_read_len(rdr);
|
||||
gu_return_on_exn(rdr->err,);
|
||||
|
||||
ccat->prods = gu_new_seq(PgfProduction, n_prods, rdr->opool);
|
||||
@@ -1056,11 +1046,7 @@ pgf_read_cnccat(PgfReader* rdr, PgfAbstr* abstr, PgfConcr* concr, PgfCId name)
|
||||
static PgfCIdMap*
|
||||
pgf_read_cnccats(PgfReader* rdr, PgfAbstr* abstr, PgfConcr* concr)
|
||||
{
|
||||
GuMapType* map_type = (GuMapType*)
|
||||
GU_TYPE_LIT(GuStringMap, _,
|
||||
gu_ptr_type(PgfCncCat),
|
||||
&gu_null_struct);
|
||||
PgfCIdMap* cnccats = gu_map_type_make(map_type, rdr->opool);
|
||||
PgfCIdMap* cnccats = gu_new_string_map(PgfCncCat, &gu_null_struct, rdr->opool);
|
||||
|
||||
size_t len = pgf_read_len(rdr);
|
||||
gu_return_on_exn(rdr->err, NULL);
|
||||
@@ -1149,8 +1135,8 @@ pgf_read_concrete_content(PgfReader* rdr, PgfConcr* concr)
|
||||
|
||||
concr->ccats =
|
||||
gu_new_int_map(PgfCCat*, &gu_null_struct, rdr->opool);
|
||||
concr->fun_indices = gu_map_type_new(PgfCncFunOverloadMap, rdr->opool);
|
||||
concr->coerce_idx = gu_map_type_new(PgfCncOverloadMap, rdr->opool);
|
||||
concr->fun_indices = gu_new_string_map(PgfCncOverloadMap*, &gu_null_struct, rdr->opool);
|
||||
concr->coerce_idx = gu_new_addr_map(PgfCCat*, GuBuf*, &gu_null_struct, rdr->opool);
|
||||
pgf_read_lindefs(rdr, concr);
|
||||
pgf_read_linrefs(rdr, concr);
|
||||
pgf_read_ccats(rdr, concr);
|
||||
|
||||
@@ -58,11 +58,7 @@ typedef struct {
|
||||
size_t choice;
|
||||
} PgfCombine2State;
|
||||
|
||||
static GU_DEFINE_TYPE(PgfAnswers, abstract);
|
||||
|
||||
typedef GuStringMap PgfAbswersMap;
|
||||
static GU_DEFINE_TYPE(PgfAbswersMap, GuStringMap, gu_ptr_type(PgfAnswers),
|
||||
&gu_null_struct);
|
||||
|
||||
struct PgfReasoner {
|
||||
GuPool* pool;
|
||||
@@ -355,7 +351,7 @@ pgf_reasoner_next(PgfReasoner* rs)
|
||||
GuPool* tmp_pool = gu_new_pool();
|
||||
GuOut* out = gu_file_out(stderr, tmp_pool);
|
||||
GuWriter* wtr = gu_new_utf8_writer(out, tmp_pool);
|
||||
GuExn* err = gu_exn(NULL, type, tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
st->print(st, wtr, err, tmp_pool);
|
||||
gu_pool_free(tmp_pool);
|
||||
}
|
||||
@@ -388,7 +384,8 @@ pgf_generate_all(PgfPGF* pgf, PgfCId cat, GuPool* pool)
|
||||
rs->pool = pool;
|
||||
rs->tmp_pool = gu_new_pool(),
|
||||
rs->abstract = &pgf->abstract,
|
||||
rs->table = gu_map_type_new(PgfAbswersMap, rs->tmp_pool),
|
||||
rs->table = gu_new_string_map(PgfAnswers*, &gu_null_struct, rs->tmp_pool),
|
||||
|
||||
rs->pqueue = gu_new_buf(PgfReasonerState*, rs->tmp_pool);
|
||||
rs->exprs = gu_new_buf(PgfExprProb*, rs->tmp_pool);
|
||||
rs->en.next = pgf_reasoner_enum_next;
|
||||
|
||||
@@ -76,8 +76,6 @@ pgf_cfty2ty(PgfTypeChecker* checker, PgfCFType cf_ty)
|
||||
return ty;
|
||||
}
|
||||
|
||||
GU_DEFINE_TYPE(PgfTypeError, abstract, _);
|
||||
|
||||
static PgfPrintContext*
|
||||
pgf_tc_mk_print_context(PgfTypeChecker* checker, PgfContext* ctxt)
|
||||
{
|
||||
@@ -104,7 +102,7 @@ pgf_tc_err_cannot_infer(PgfTypeChecker* checker, PgfContext* ctxt, PgfExpr e)
|
||||
{
|
||||
GuStringBuf* sbuf = gu_string_buf(checker->tmp_pool);
|
||||
GuOut* out = gu_string_buf_out(sbuf);
|
||||
GuExn* err = gu_exn(NULL, type, checker->tmp_pool);
|
||||
GuExn* err = gu_exn(checker->tmp_pool);
|
||||
|
||||
gu_puts("Cannot infer the type of expression ", out, err);
|
||||
pgf_print_expr(e, pgf_tc_mk_print_context(checker, ctxt), 0, out, err);
|
||||
@@ -120,7 +118,7 @@ pgf_tc_err_exp_fun_type_1(PgfTypeChecker* checker, PgfContext* ctxt,
|
||||
{
|
||||
GuStringBuf* sbuf = gu_string_buf(checker->tmp_pool);
|
||||
GuOut* out = gu_string_buf_out(sbuf);
|
||||
GuExn* err = gu_exn(NULL, type, checker->tmp_pool);
|
||||
GuExn* err = gu_exn(checker->tmp_pool);
|
||||
|
||||
PgfPrintContext* pctxt = pgf_tc_mk_print_context(checker, ctxt);
|
||||
gu_puts("The expression ", out, err);
|
||||
@@ -140,7 +138,7 @@ pgf_tc_err_exp_fun_type_2(PgfTypeChecker* checker, PgfContext* ctxt,
|
||||
{
|
||||
GuStringBuf* sbuf = gu_string_buf(checker->tmp_pool);
|
||||
GuOut* out = gu_string_buf_out(sbuf);
|
||||
GuExn* err = gu_exn(NULL, type, checker->tmp_pool);
|
||||
GuExn* err = gu_exn(checker->tmp_pool);
|
||||
|
||||
PgfPrintContext* pctxt = pgf_tc_mk_print_context(checker, ctxt);
|
||||
gu_puts("A function type is expected for the expression ", out, err);
|
||||
@@ -173,7 +171,7 @@ pgf_tc_err_type_mismatch(PgfTypeChecker* checker,
|
||||
{
|
||||
GuStringBuf* sbuf = gu_string_buf(checker->tmp_pool);
|
||||
GuOut* out = gu_string_buf_out(sbuf);
|
||||
GuExn* err = gu_exn(NULL, type, checker->tmp_pool);
|
||||
GuExn* err = gu_exn(checker->tmp_pool);
|
||||
|
||||
PgfPrintContext* pctxt = pgf_tc_mk_print_context(checker, ctxt);
|
||||
gu_puts("The expected type of the expression ", out, err);
|
||||
|
||||
@@ -36,7 +36,7 @@ int main(int argc, char* argv[]) {
|
||||
}
|
||||
|
||||
// Create an exception frame that catches all errors.
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), pool);
|
||||
GuExn* err = gu_new_exn(pool);
|
||||
|
||||
|
||||
clock_t start = clock();
|
||||
@@ -109,7 +109,7 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
clock_t start = clock();
|
||||
|
||||
GuExn* parse_err = gu_new_exn(NULL, gu_kind(type), ppool);
|
||||
GuExn* parse_err = gu_new_exn(ppool);
|
||||
GuEnum* result = pgf_parse_with_heuristics(concr, cat, line, heuristics, parse_err, ppool, ppool);
|
||||
|
||||
PgfExprProb* ep = NULL;
|
||||
|
||||
@@ -7,8 +7,6 @@
|
||||
#include <locale.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
GU_DECLARE_TYPE(PgfAbstr, struct);
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
// Set the character locale, so we can produce proper output.
|
||||
setlocale(LC_CTYPE, "");
|
||||
@@ -20,7 +18,7 @@ int main(int argc, char* argv[]) {
|
||||
char* filename = argv[1];
|
||||
|
||||
GuPool* pool = gu_new_pool();
|
||||
GuExn* err = gu_exn(NULL, type, pool);
|
||||
GuExn* err = gu_exn(pool);
|
||||
PgfPGF* pgf = pgf_read(filename, pool, err);
|
||||
int status = 0;
|
||||
if (!gu_ok(err)) {
|
||||
|
||||
@@ -36,7 +36,7 @@ print_result(PgfExprProb* ep, PgfConcr* to_concr,
|
||||
// sequence of strings.
|
||||
pgf_lzr_linearize_simple(to_concr, ctree, 0, out, err, ppool);
|
||||
|
||||
if (gu_exn_caught(err) == gu_type(PgfLinNonExist)) {
|
||||
if (gu_exn_caught(err, PgfLinNonExist)) {
|
||||
// encountered nonExist. Unfortunately there
|
||||
// might be some output printed already. The
|
||||
// right solution should be to use GuStringBuf.
|
||||
@@ -66,7 +66,7 @@ int main(int argc, char* argv[]) {
|
||||
GuString to_lang = argv[4];
|
||||
|
||||
// Create an exception frame that catches all errors.
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), pool);
|
||||
GuExn* err = gu_new_exn(pool);
|
||||
|
||||
// Read the PGF grammar.
|
||||
PgfPGF* pgf = pgf_read(filename, pool, err);
|
||||
@@ -152,15 +152,15 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
clock_t start = clock();
|
||||
|
||||
GuExn* parse_err = gu_new_exn(NULL, gu_kind(type), ppool);
|
||||
GuExn* parse_err = gu_new_exn(ppool);
|
||||
result =
|
||||
pgf_parse(from_concr, cat, line, parse_err, ppool, ppool);
|
||||
if (!gu_ok(parse_err)) {
|
||||
if (gu_exn_caught(parse_err) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(parse_err, PgfExn)) {
|
||||
GuString msg = gu_exn_caught_data(parse_err);
|
||||
gu_string_write(msg, out, err);
|
||||
gu_putc('\n', out, err);
|
||||
} else if (gu_exn_caught(parse_err) == gu_type(PgfParseError)) {
|
||||
} else if (gu_exn_caught(parse_err, PgfParseError)) {
|
||||
gu_puts("Unexpected token: \"", out, err);
|
||||
GuString tok = gu_exn_caught_data(parse_err);
|
||||
gu_string_write(tok, out, err);
|
||||
|
||||
@@ -52,12 +52,12 @@ readPGF fpath =
|
||||
do pool <- gu_new_pool
|
||||
pgf <- withCString fpath $ \c_fpath ->
|
||||
withGuPool $ \tmpPl -> do
|
||||
exn <- gu_new_exn nullPtr gu_type__type tmpPl
|
||||
exn <- gu_new_exn tmpPl
|
||||
pgf <- pgf_read c_fpath pool exn
|
||||
failed <- gu_exn_is_raised exn
|
||||
if failed
|
||||
then do ty <- gu_exn_caught exn
|
||||
if ty == gu_type__GuErrno
|
||||
then do is_errno <- gu_exn_caught exn gu_exn_type_GuErrno
|
||||
if is_errno
|
||||
then do perrno <- (#peek GuExn, data.data) exn
|
||||
errno <- peek perrno
|
||||
gu_pool_free pool
|
||||
@@ -110,12 +110,12 @@ loadConcr c fpath =
|
||||
withGuPool $ \tmpPl -> do
|
||||
file <- fopen c_fpath c_mode
|
||||
inp <- gu_file_in file tmpPl
|
||||
exn <- gu_new_exn nullPtr gu_type__type tmpPl
|
||||
exn <- gu_new_exn tmpPl
|
||||
pgf_concrete_load (concr c) inp exn
|
||||
failed <- gu_exn_is_raised exn
|
||||
if failed
|
||||
then do ty <- gu_exn_caught exn
|
||||
if ty == gu_type__GuErrno
|
||||
then do is_errno <- gu_exn_caught exn gu_exn_type_GuErrno
|
||||
if is_errno
|
||||
then do perrno <- (#peek GuExn, data.data) exn
|
||||
errno <- peek perrno
|
||||
ioError (errnoToIOError "loadConcr" (Errno errno) Nothing (Just fpath))
|
||||
@@ -158,7 +158,7 @@ readExpr str =
|
||||
withGuPool $ \tmpPl ->
|
||||
withCString str $ \c_str ->
|
||||
do guin <- gu_string_in c_str tmpPl
|
||||
exn <- gu_new_exn nullPtr gu_type__type tmpPl
|
||||
exn <- gu_new_exn tmpPl
|
||||
c_expr <- pgf_read_expr guin exprPl exn
|
||||
status <- gu_exn_is_raised exn
|
||||
if (not status && c_expr /= nullPtr)
|
||||
@@ -173,7 +173,7 @@ showExpr e =
|
||||
withGuPool $ \tmpPl ->
|
||||
do (sb,out) <- newOut tmpPl
|
||||
let printCtxt = nullPtr
|
||||
exn <- gu_new_exn nullPtr gu_type__type tmpPl
|
||||
exn <- gu_new_exn tmpPl
|
||||
pgf_print_expr (expr e) printCtxt 1 out exn
|
||||
s <- gu_string_buf_freeze sb tmpPl
|
||||
peekCString s
|
||||
@@ -235,28 +235,29 @@ parse lang cat sent =
|
||||
unsafePerformIO $
|
||||
do parsePl <- gu_new_pool
|
||||
exprPl <- gu_new_pool
|
||||
exn <- gu_new_exn nullPtr gu_type__type parsePl
|
||||
exn <- gu_new_exn parsePl
|
||||
enum <- withCString cat $ \cat ->
|
||||
withCString sent $ \sent ->
|
||||
pgf_parse (concr lang) cat sent exn parsePl exprPl
|
||||
failed <- gu_exn_is_raised exn
|
||||
if failed
|
||||
then do ty <- gu_exn_caught exn
|
||||
if ty == gu_type__PgfParseError
|
||||
then do is_parse_error <- gu_exn_caught exn gu_exn_type_PgfParseError
|
||||
if is_parse_error
|
||||
then do c_tok <- (#peek GuExn, data.data) exn
|
||||
tok <- peekCString c_tok
|
||||
gu_pool_free parsePl
|
||||
gu_pool_free exprPl
|
||||
return (Left tok)
|
||||
else if ty == gu_type__PgfExn
|
||||
then do c_msg <- (#peek GuExn, data.data) exn
|
||||
msg <- peekCString c_msg
|
||||
gu_pool_free parsePl
|
||||
gu_pool_free exprPl
|
||||
throwIO (PGFError msg)
|
||||
else do gu_pool_free parsePl
|
||||
gu_pool_free exprPl
|
||||
throwIO (PGFError "Parsing failed")
|
||||
else do is_exn <- gu_exn_caught exn gu_exn_type_PgfExn
|
||||
if is_exn
|
||||
then do c_msg <- (#peek GuExn, data.data) exn
|
||||
msg <- peekCString c_msg
|
||||
gu_pool_free parsePl
|
||||
gu_pool_free exprPl
|
||||
throwIO (PGFError msg)
|
||||
else do gu_pool_free parsePl
|
||||
gu_pool_free exprPl
|
||||
throwIO (PGFError "Parsing failed")
|
||||
else do parseFPl <- newForeignPtr gu_pool_finalizer parsePl
|
||||
exprFPl <- newForeignPtr gu_pool_finalizer exprPl
|
||||
exprs <- fromPgfExprEnum enum parseFPl (lang,exprFPl)
|
||||
@@ -271,12 +272,12 @@ addLiteral lang cat match =
|
||||
predict <- wrapLiteralPredictCallback predict_callback
|
||||
(#poke PgfLiteralCallback, match) callback match
|
||||
(#poke PgfLiteralCallback, predict) callback predict
|
||||
exn <- gu_new_exn nullPtr gu_type__type tmp_pool
|
||||
exn <- gu_new_exn tmp_pool
|
||||
pgf_concr_add_literal (concr lang) ccat callback exn
|
||||
failed <- gu_exn_is_raised exn
|
||||
if failed
|
||||
then do ty <- gu_exn_caught exn
|
||||
if ty == gu_type__PgfExn
|
||||
then do is_exn <- gu_exn_caught exn gu_exn_type_PgfExn
|
||||
if is_exn
|
||||
then do c_msg <- (#peek GuExn, data.data) exn
|
||||
msg <- peekCString c_msg
|
||||
throwIO (PGFError msg)
|
||||
@@ -295,7 +296,7 @@ addLiteral lang cat match =
|
||||
|
||||
-- here we copy the expression to out_pool
|
||||
c_e <- withGuPool $ \tmpPl -> do
|
||||
exn <- gu_new_exn nullPtr gu_type__type tmpPl
|
||||
exn <- gu_new_exn tmpPl
|
||||
|
||||
(sb,out) <- newOut tmpPl
|
||||
let printCtxt = nullPtr
|
||||
@@ -323,18 +324,19 @@ linearize :: Concr -> Expr -> String
|
||||
linearize lang e = unsafePerformIO $
|
||||
withGuPool $ \pl ->
|
||||
do (sb,out) <- newOut pl
|
||||
exn <- gu_new_exn nullPtr gu_type__type pl
|
||||
exn <- gu_new_exn pl
|
||||
pgf_linearize (concr lang) (expr e) out exn
|
||||
failed <- gu_exn_is_raised exn
|
||||
if failed
|
||||
then do ty <- gu_exn_caught exn
|
||||
if ty == gu_type__PgfLinNonExist
|
||||
then do is_nonexist <- gu_exn_caught exn gu_exn_type_PgfLinNonExist
|
||||
if is_nonexist
|
||||
then return ""
|
||||
else if ty == gu_type__PgfExn
|
||||
then do c_msg <- (#peek GuExn, data.data) exn
|
||||
msg <- peekCString c_msg
|
||||
throwIO (PGFError msg)
|
||||
else throwIO (PGFError "The abstract tree cannot be linearized")
|
||||
else do is_exn <- gu_exn_caught exn gu_exn_type_PgfExn
|
||||
if is_exn
|
||||
then do c_msg <- (#peek GuExn, data.data) exn
|
||||
msg <- peekCString c_msg
|
||||
throwIO (PGFError msg)
|
||||
else throwIO (PGFError "The abstract tree cannot be linearized")
|
||||
else do lin <- gu_string_buf_freeze sb pl
|
||||
peekCString lin
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{-# LANGUAGE ForeignFunctionInterface #-}
|
||||
{-# LANGUAGE ForeignFunctionInterface, MagicHash #-}
|
||||
|
||||
module PGF2.FFI where
|
||||
|
||||
@@ -7,6 +7,7 @@ import Foreign.C.String
|
||||
import Foreign.Ptr
|
||||
import Foreign.ForeignPtr
|
||||
import Control.Exception
|
||||
import GHC.Ptr
|
||||
|
||||
------------------------------------------------------------------
|
||||
-- libgu API
|
||||
@@ -37,28 +38,21 @@ foreign import ccall "gu/mem.h &gu_pool_free"
|
||||
gu_pool_finalizer :: FinalizerPtr GuPool
|
||||
|
||||
foreign import ccall "gu/exn.h gu_new_exn"
|
||||
gu_new_exn :: Ptr GuExn -> Ptr GuKind -> Ptr GuPool -> IO (Ptr GuExn)
|
||||
gu_new_exn :: Ptr GuPool -> IO (Ptr GuExn)
|
||||
|
||||
foreign import ccall "gu/exn.h gu_exn_is_raised"
|
||||
gu_exn_is_raised :: Ptr GuExn -> IO Bool
|
||||
|
||||
foreign import ccall "gu/exn.h gu_exn_caught"
|
||||
gu_exn_caught :: Ptr GuExn -> IO (Ptr GuType)
|
||||
foreign import ccall "gu/exn.h gu_exn_caught_"
|
||||
gu_exn_caught :: Ptr GuExn -> CString -> IO Bool
|
||||
|
||||
foreign import ccall "gu/type.h &gu_type__type"
|
||||
gu_type__type :: Ptr GuKind
|
||||
gu_exn_type_GuErrno = Ptr "GuErrno"# :: CString
|
||||
|
||||
foreign import ccall "gu/type.h &gu_type__GuErrno"
|
||||
gu_type__GuErrno :: Ptr GuType
|
||||
gu_exn_type_PgfLinNonExist = Ptr "PgfLinNonExist"# :: CString
|
||||
|
||||
foreign import ccall "gu/type.h &gu_type__PgfLinNonExist"
|
||||
gu_type__PgfLinNonExist :: Ptr GuType
|
||||
gu_exn_type_PgfExn = Ptr "PgfExn"# :: CString
|
||||
|
||||
foreign import ccall "gu/type.h &gu_type__PgfExn"
|
||||
gu_type__PgfExn :: Ptr GuType
|
||||
|
||||
foreign import ccall "gu/type.h &gu_type__PgfParseError"
|
||||
gu_type__PgfParseError :: Ptr GuType
|
||||
gu_exn_type_PgfParseError = Ptr "PgfParseError"# :: CString
|
||||
|
||||
foreign import ccall "gu/string.h gu_string_in"
|
||||
gu_string_in :: CString -> Ptr GuPool -> IO (Ptr GuIn)
|
||||
|
||||
@@ -111,7 +111,7 @@ Java_org_grammaticalframework_pgf_PGF_readPGF__Ljava_lang_String_2(JNIEnv *env,
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
|
||||
// Create an exception frame that catches all errors.
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
|
||||
const char *fpath = (*env)->GetStringUTFChars(env, s, 0);
|
||||
|
||||
@@ -121,7 +121,7 @@ Java_org_grammaticalframework_pgf_PGF_readPGF__Ljava_lang_String_2(JNIEnv *env,
|
||||
(*env)->ReleaseStringUTFChars(env, s, fpath);
|
||||
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(GuErrno)) {
|
||||
if (gu_exn_caught(err, GuErrno)) {
|
||||
throw_jstring_exception(env, "java/io/FileNotFoundException", s);
|
||||
} else {
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", "The grammar cannot be loaded");
|
||||
@@ -220,7 +220,7 @@ Java_org_grammaticalframework_pgf_PGF_readPGF__Ljava_io_InputStream_2(JNIEnv *en
|
||||
GuIn* in = gu_new_in(jstream, tmp_pool);
|
||||
|
||||
// Create an exception frame that catches all errors.
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
|
||||
// Read the PGF grammar.
|
||||
PgfReader* rdr = pgf_new_reader(in, pool, tmp_pool, err);
|
||||
@@ -315,7 +315,7 @@ Java_org_grammaticalframework_pgf_PGF_getLanguages(JNIEnv* env, jobject self)
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
|
||||
// Create an exception frame that catches all errors.
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
|
||||
JPGFClosure clo = { { pgf_collect_langs }, env, self, languages };
|
||||
pgf_iter_languages(pgf, &clo.fn, err);
|
||||
@@ -350,11 +350,11 @@ Java_org_grammaticalframework_pgf_Concr_load__Ljava_io_InputStream_2(JNIEnv* env
|
||||
GuIn* in = gu_new_in(jstream, tmp_pool);
|
||||
|
||||
// Create an exception frame that catches all errors.
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
|
||||
pgf_concrete_load(get_ref(env, self), in, err);
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg);
|
||||
} else {
|
||||
@@ -380,16 +380,16 @@ Java_org_grammaticalframework_pgf_Parser_parse
|
||||
|
||||
GuString startCat = j2gu_string(env, jstartCat, pool);
|
||||
GuString s = j2gu_string(env, js, pool);
|
||||
GuExn* parse_err = gu_new_exn(NULL, gu_kind(type), pool);
|
||||
GuExn* parse_err = gu_new_exn(pool);
|
||||
|
||||
GuEnum* res =
|
||||
pgf_parse(get_ref(env, concr), startCat, s, parse_err, pool, out_pool);
|
||||
|
||||
if (!gu_ok(parse_err)) {
|
||||
if (gu_exn_caught(parse_err) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(parse_err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(parse_err);
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg);
|
||||
} else if (gu_exn_caught(parse_err) == gu_type(PgfParseError)) {
|
||||
} else if (gu_exn_caught(parse_err, PgfParseError)) {
|
||||
GuString tok = (GuString) gu_exn_caught_data(parse_err);
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/ParseError", tok);
|
||||
}
|
||||
@@ -417,16 +417,16 @@ Java_org_grammaticalframework_pgf_Completer_complete(JNIEnv* env, jclass clazz,
|
||||
GuString startCat = j2gu_string(env, jstartCat, pool);
|
||||
GuString s = j2gu_string(env, js, pool);
|
||||
GuString prefix = j2gu_string(env, jprefix, pool);
|
||||
GuExn* parse_err = gu_new_exn(NULL, gu_kind(type), pool);
|
||||
GuExn* parse_err = gu_new_exn(pool);
|
||||
|
||||
GuEnum* res =
|
||||
pgf_complete(get_ref(env, jconcr), startCat, s, prefix, parse_err, pool);
|
||||
|
||||
if (!gu_ok(parse_err)) {
|
||||
if (gu_exn_caught(parse_err) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(parse_err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(parse_err);
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg);
|
||||
} else if (gu_exn_caught(parse_err) == gu_type(PgfParseError)) {
|
||||
} else if (gu_exn_caught(parse_err, PgfParseError)) {
|
||||
GuString tok = (GuString) gu_exn_caught_data(parse_err);
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/ParseError", tok);
|
||||
}
|
||||
@@ -481,16 +481,16 @@ JNIEXPORT jstring JNICALL
|
||||
Java_org_grammaticalframework_pgf_Concr_linearize(JNIEnv* env, jobject self, jobject jexpr)
|
||||
{
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
|
||||
GuOut* out = gu_string_buf_out(sbuf);
|
||||
|
||||
pgf_linearize(get_ref(env, self), gu_variant_from_ptr((void*) get_ref(env, jexpr)), out, err);
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(PgfLinNonExist)) {
|
||||
if (gu_exn_caught(err, PgfLinNonExist)) {
|
||||
gu_pool_free(tmp_pool);
|
||||
return NULL;
|
||||
} else if (gu_exn_caught(err) == gu_type(PgfExn)) {
|
||||
} else if (gu_exn_caught(err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg);
|
||||
} else {
|
||||
@@ -528,7 +528,7 @@ Java_org_grammaticalframework_pgf_Concr_tabularLinearize(JNIEnv* env, jobject se
|
||||
PgfConcr* concr = get_ref(env, self);
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
|
||||
GuEnum* cts =
|
||||
pgf_lzr_concretize(concr,
|
||||
@@ -536,7 +536,7 @@ Java_org_grammaticalframework_pgf_Concr_tabularLinearize(JNIEnv* env, jobject se
|
||||
err,
|
||||
tmp_pool);
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg);
|
||||
} else {
|
||||
@@ -677,14 +677,14 @@ Java_org_grammaticalframework_pgf_Concr_bracketedLinearize(JNIEnv* env, jobject
|
||||
return NULL;
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
|
||||
PgfConcr* concr = get_ref(env, self);
|
||||
|
||||
GuEnum* cts =
|
||||
pgf_lzr_concretize(concr, gu_variant_from_ptr((void*) get_ref(env, jexpr)), err, tmp_pool);
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg);
|
||||
} else {
|
||||
@@ -775,13 +775,13 @@ Java_org_grammaticalframework_pgf_Concr_lookupMorpho(JNIEnv* env, jobject self,
|
||||
|
||||
GuPool* tmp_pool = gu_new_pool();
|
||||
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
|
||||
JMorphoCallback callback = { { jpgf_collect_morpho }, analyses, 0, env, addId, an_class, an_constrId };
|
||||
pgf_lookup_morpho(get_ref(env, self), j2gu_string(env, sentence, tmp_pool),
|
||||
&callback.fn, err);
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg);
|
||||
} else {
|
||||
@@ -800,12 +800,12 @@ Java_org_grammaticalframework_pgf_Lexicon_lookupWordPrefix
|
||||
(JNIEnv* env, jclass clazz, jobject jconcr, jstring prefix)
|
||||
{
|
||||
GuPool* pool = gu_new_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), pool);
|
||||
GuExn* err = gu_new_exn(pool);
|
||||
|
||||
GuEnum* en = pgf_lookup_word_prefix(get_ref(env, jconcr), j2gu_string(env, prefix, pool),
|
||||
pool, err);
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg);
|
||||
} else {
|
||||
@@ -843,12 +843,12 @@ Java_org_grammaticalframework_pgf_FullFormIterator_fetchFullFormEntry
|
||||
jmethodID an_constrId = (*env)->GetMethodID(env, an_class, "<init>", "(Ljava/lang/String;Ljava/lang/String;D)V");
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
|
||||
JMorphoCallback callback = { { jpgf_collect_morpho }, analyses, 0, env, addId, an_class, an_constrId };
|
||||
pgf_fullform_get_analyses(entry, &callback.fn, err);
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg);
|
||||
} else {
|
||||
@@ -936,7 +936,7 @@ jpgf_literal_callback_match(PgfLiteralCallback* self,
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
|
||||
GuOut* out = gu_string_buf_out(sbuf);
|
||||
|
||||
@@ -1025,11 +1025,11 @@ Java_org_grammaticalframework_pgf_Concr_addLiteral(JNIEnv* env, jobject self, js
|
||||
gu_pool_finally(pool, &callback->fin);
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
pgf_concr_add_literal(concr, j2gu_string(env, jcat, tmp_pool), &callback->callback, err);
|
||||
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
throw_string_exception(env, "org/grammaticalframework/pgf/PGFError", msg);
|
||||
} else {
|
||||
@@ -1057,7 +1057,7 @@ Java_org_grammaticalframework_pgf_Expr_showExpr(JNIEnv* env, jclass clazz, jlong
|
||||
{
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
|
||||
GuOut* out = gu_string_buf_out(sbuf);
|
||||
|
||||
@@ -1078,7 +1078,7 @@ Java_org_grammaticalframework_pgf_Expr_readExpr(JNIEnv* env, jclass clazz, jstri
|
||||
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);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
|
||||
PgfExpr e = pgf_read_expr(in, pool, err);
|
||||
if (!gu_ok(err) || gu_variant_is_null(e)) {
|
||||
|
||||
@@ -105,7 +105,7 @@ Expr_repr(ExprObject *self)
|
||||
{
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
|
||||
GuOut* out = gu_string_buf_out(sbuf);
|
||||
|
||||
@@ -771,7 +771,7 @@ Type_repr(TypeObject *self)
|
||||
{
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
|
||||
GuOut* out = gu_string_buf_out(sbuf);
|
||||
|
||||
@@ -1227,7 +1227,7 @@ Concr_parse(ConcrObject* self, PyObject *args, PyObject *keywds)
|
||||
pyres->counter = 0;
|
||||
pyres->fetch = Iter_fetch_expr;
|
||||
|
||||
GuExn* parse_err = gu_new_exn(NULL, gu_kind(type), pyres->pool);
|
||||
GuExn* parse_err = gu_exn(pyres->pool);
|
||||
|
||||
pyres->res =
|
||||
pgf_parse_with_heuristics(self->concr, catname, sentence,
|
||||
@@ -1235,10 +1235,10 @@ Concr_parse(ConcrObject* self, PyObject *args, PyObject *keywds)
|
||||
pyres->pool, out_pool);
|
||||
|
||||
if (!gu_ok(parse_err)) {
|
||||
if (gu_exn_caught(parse_err) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(parse_err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(parse_err);
|
||||
PyErr_SetString(PGFError, msg);
|
||||
} else if (gu_exn_caught(parse_err) == gu_type(PgfParseError)) {
|
||||
} else if (gu_exn_caught(parse_err, PgfParseError)) {
|
||||
GuString tok = (GuString) gu_exn_caught_data(parse_err);
|
||||
PyObject* py_tok = PyString_FromString(tok);
|
||||
PyObject_SetAttrString(ParseError, "token", py_tok);
|
||||
@@ -1285,7 +1285,7 @@ Concr_complete(ConcrObject* self, PyObject *args, PyObject *keywds)
|
||||
|
||||
GuPool *tmp_pool = gu_local_pool();
|
||||
|
||||
GuExn* parse_err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* parse_err = gu_new_exn(tmp_pool);
|
||||
|
||||
pyres->res =
|
||||
pgf_complete(self->concr, catname, sentence, prefix, parse_err, pyres->pool);
|
||||
@@ -1294,10 +1294,10 @@ Concr_complete(ConcrObject* self, PyObject *args, PyObject *keywds)
|
||||
Py_DECREF(pyres);
|
||||
pyres = NULL;
|
||||
|
||||
if (gu_exn_caught(parse_err) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(parse_err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(parse_err);
|
||||
PyErr_SetString(PGFError, msg);
|
||||
} else if (gu_exn_caught(parse_err) == gu_type(PgfParseError)) {
|
||||
} else if (gu_exn_caught(parse_err, PgfParseError)) {
|
||||
GuString tok = (GuString) gu_exn_caught_data(parse_err);
|
||||
PyObject* py_tok = PyString_FromString(tok);
|
||||
PyObject_SetAttrString(ParseError, "token", py_tok);
|
||||
@@ -1371,7 +1371,7 @@ pypgf_literal_callback_match(PgfLiteralCallback* self,
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
|
||||
GuOut* out = gu_string_buf_out(sbuf);
|
||||
|
||||
@@ -1431,11 +1431,11 @@ Concr_addLiteral(ConcrObject* self, PyObject *args) {
|
||||
gu_pool_finally(pool, &callback->fin);
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
pgf_concr_add_literal(self->concr, cat, &callback->callback, err);
|
||||
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
PyErr_SetString(PGFError, msg);
|
||||
} else {
|
||||
@@ -1455,15 +1455,15 @@ Concr_linearize(ConcrObject* self, PyObject *args)
|
||||
return NULL;
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
|
||||
GuOut* out = gu_string_buf_out(sbuf);
|
||||
|
||||
pgf_linearize(self->concr, pyexpr->expr, out, err);
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(PgfLinNonExist))
|
||||
if (gu_exn_caught(err, PgfLinNonExist))
|
||||
Py_RETURN_NONE;
|
||||
else if (gu_exn_caught(err) == gu_type(PgfExn)) {
|
||||
else if (gu_exn_caught(err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
PyErr_SetString(PGFError, msg);
|
||||
return NULL;
|
||||
@@ -1492,7 +1492,7 @@ Concr_tabularLinearize(ConcrObject* self, PyObject *args)
|
||||
return NULL;
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
|
||||
GuEnum* cts =
|
||||
pgf_lzr_concretize(self->concr,
|
||||
@@ -1500,9 +1500,9 @@ Concr_tabularLinearize(ConcrObject* self, PyObject *args)
|
||||
err,
|
||||
tmp_pool);
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(PgfLinNonExist))
|
||||
if (gu_exn_caught(err, PgfLinNonExist))
|
||||
Py_RETURN_NONE;
|
||||
else if (gu_exn_caught(err) == gu_type(PgfExn)) {
|
||||
else if (gu_exn_caught(err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
PyErr_SetString(PGFError, msg);
|
||||
return NULL;
|
||||
@@ -1739,12 +1739,12 @@ Concr_bracketedLinearize(ConcrObject* self, PyObject *args)
|
||||
return NULL;
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
|
||||
GuEnum* cts =
|
||||
pgf_lzr_concretize(self->concr, pyexpr->expr, err, tmp_pool);
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
PyErr_SetString(PGFError, msg);
|
||||
return NULL;
|
||||
@@ -1807,13 +1807,13 @@ Concr_graphvizParseTree(ConcrObject* self, PyObject *args) {
|
||||
return NULL;
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
|
||||
GuOut* out = gu_string_buf_out(sbuf);
|
||||
|
||||
pgf_graphviz_parse_tree(self->concr, pyexpr->expr, out, err);
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
PyErr_SetString(PGFError, msg);
|
||||
} else {
|
||||
@@ -1862,14 +1862,14 @@ Concr_lookupMorpho(ConcrObject* self, PyObject *args) {
|
||||
return NULL;
|
||||
|
||||
GuPool *tmp_pool = gu_local_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_exn(tmp_pool);
|
||||
|
||||
PyObject* analyses = PyList_New(0);
|
||||
|
||||
PyMorphoCallback callback = { { pypgf_collect_morpho }, analyses };
|
||||
pgf_lookup_morpho(self->concr, sent, &callback.fn, err);
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
PyErr_SetString(PGFError, msg);
|
||||
} else {
|
||||
@@ -1908,7 +1908,7 @@ Iter_fetch_fullform(IterObject* self)
|
||||
goto done;
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_new_exn(tmp_pool);
|
||||
|
||||
PyMorphoCallback callback = { { pypgf_collect_morpho }, py_analyses };
|
||||
pgf_fullform_get_analyses(entry, &callback.fn, err);
|
||||
@@ -1961,7 +1961,7 @@ Concr_load(ConcrObject* self, PyObject *args)
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
|
||||
// Create an exception frame that catches all errors.
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_new_exn(tmp_pool);
|
||||
|
||||
FILE* infile = fopen(fpath, "rb");
|
||||
if (infile == NULL) {
|
||||
@@ -1975,10 +1975,10 @@ Concr_load(ConcrObject* self, PyObject *args)
|
||||
// Read the PGF grammar.
|
||||
pgf_concrete_load(self->concr, in, err);
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(GuErrno)) {
|
||||
if (gu_exn_caught(err, GuErrno)) {
|
||||
errno = *((GuErrno*) gu_exn_caught_data(err));
|
||||
PyErr_SetFromErrnoWithFilename(PyExc_IOError, fpath);
|
||||
} else if (gu_exn_caught(err) == gu_type(PgfExn)) {
|
||||
} else if (gu_exn_caught(err, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(err);
|
||||
PyErr_SetString(PGFError, msg);
|
||||
return NULL;
|
||||
@@ -2175,7 +2175,7 @@ PGF_getLanguages(PGFObject *self, void *closure)
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
|
||||
// Create an exception frame that catches all errors.
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_new_exn(tmp_pool);
|
||||
|
||||
PyPGFClosure clo = { { pgf_collect_langs }, self, languages };
|
||||
pgf_iter_languages(self->pgf, &clo.fn, err);
|
||||
@@ -2226,7 +2226,7 @@ PGF_getCategories(PGFObject *self, void *closure)
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
|
||||
// Create an exception frame that catches all errors.
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_new_exn(tmp_pool);
|
||||
|
||||
PyPGFClosure clo = { { pgf_collect_cats }, self, categories };
|
||||
pgf_iter_categories(self->pgf, &clo.fn, err);
|
||||
@@ -2279,7 +2279,7 @@ PGF_getFunctions(PGFObject *self, void *closure)
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
|
||||
// Create an exception frame that catches all errors.
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_new_exn(tmp_pool);
|
||||
|
||||
PyPGFClosure clo = { { pgf_collect_funs }, self, functions };
|
||||
pgf_iter_functions(self->pgf, &clo.fn, err);
|
||||
@@ -2308,7 +2308,7 @@ PGF_functionsByCat(PGFObject* self, PyObject *args)
|
||||
GuPool *tmp_pool = gu_local_pool();
|
||||
|
||||
// Create an exception frame that catches all errors.
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_new_exn(tmp_pool);
|
||||
|
||||
PyPGFClosure clo = { { pgf_collect_funs }, self, functions };
|
||||
pgf_iter_functions_by_cat(self->pgf, catname, &clo.fn, err);
|
||||
@@ -2400,7 +2400,7 @@ PGF_compute(PGFObject* self, PyObject *args)
|
||||
return NULL;
|
||||
|
||||
GuPool* tmp_pool = gu_new_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_new_exn(tmp_pool);
|
||||
|
||||
py_expr_res->pool = gu_new_pool();
|
||||
py_expr_res->expr = pgf_compute(self->pgf, py_expr->expr, err,
|
||||
@@ -2438,15 +2438,15 @@ PGF_checkExpr(PGFObject* self, PyObject *args)
|
||||
new_pyexpr->master = NULL;
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* exn = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* exn = gu_new_exn(tmp_pool);
|
||||
|
||||
pgf_check_expr(self->pgf, &new_pyexpr->expr, py_type->type,
|
||||
exn, new_pyexpr->pool);
|
||||
if (!gu_ok(exn)) {
|
||||
if (gu_exn_caught(exn) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(exn, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(exn);
|
||||
PyErr_SetString(PGFError, msg);
|
||||
} else if (gu_exn_caught(exn) == gu_type(PgfTypeError)) {
|
||||
} else if (gu_exn_caught(exn, PgfTypeError)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(exn);
|
||||
PyErr_SetString(TypeError, msg);
|
||||
}
|
||||
@@ -2488,16 +2488,16 @@ PGF_inferExpr(PGFObject* self, PyObject *args)
|
||||
Py_INCREF(new_pyexpr);
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* exn = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* exn = gu_new_exn(tmp_pool);
|
||||
|
||||
new_pytype->type =
|
||||
pgf_infer_expr(self->pgf, &new_pyexpr->expr,
|
||||
exn, new_pyexpr->pool);
|
||||
if (!gu_ok(exn)) {
|
||||
if (gu_exn_caught(exn) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(exn, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(exn);
|
||||
PyErr_SetString(PGFError, msg);
|
||||
} else if (gu_exn_caught(exn) == gu_type(PgfTypeError)) {
|
||||
} else if (gu_exn_caught(exn, PgfTypeError)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(exn);
|
||||
PyErr_SetString(TypeError, msg);
|
||||
}
|
||||
@@ -2536,15 +2536,15 @@ PGF_checkType(PGFObject* self, PyObject *args)
|
||||
new_pytype->master = NULL;
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* exn = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* exn = gu_new_exn(tmp_pool);
|
||||
|
||||
pgf_check_type(self->pgf, &new_pytype->type,
|
||||
exn, new_pytype->pool);
|
||||
if (!gu_ok(exn)) {
|
||||
if (gu_exn_caught(exn) == gu_type(PgfExn)) {
|
||||
if (gu_exn_caught(exn, PgfExn)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(exn);
|
||||
PyErr_SetString(PGFError, msg);
|
||||
} else if (gu_exn_caught(exn) == gu_type(PgfTypeError)) {
|
||||
} else if (gu_exn_caught(exn, PgfTypeError)) {
|
||||
GuString msg = (GuString) gu_exn_caught_data(exn);
|
||||
PyErr_SetString(TypeError, msg);
|
||||
}
|
||||
@@ -2565,7 +2565,7 @@ PGF_graphvizAbstractTree(PGFObject* self, PyObject *args) {
|
||||
return NULL;
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_new_exn(tmp_pool);
|
||||
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
|
||||
GuOut* out = gu_string_buf_out(sbuf);
|
||||
|
||||
@@ -2620,7 +2620,7 @@ PGF_embed(PGFObject* self, PyObject *args)
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
|
||||
// Create an exception frame that catches all errors.
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_new_exn(tmp_pool);
|
||||
|
||||
PyPGFClosure clo = { { pgf_embed_funs }, self, m };
|
||||
pgf_iter_functions(self->pgf, &clo.fn, err);
|
||||
@@ -2753,12 +2753,12 @@ pgf_readPGF(PyObject *self, PyObject *args)
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
|
||||
// Create an exception frame that catches all errors.
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_new_exn(tmp_pool);
|
||||
|
||||
// Read the PGF grammar.
|
||||
py_pgf->pgf = pgf_read(fpath, py_pgf->pool, err);
|
||||
if (!gu_ok(err)) {
|
||||
if (gu_exn_caught(err) == gu_type(GuErrno)) {
|
||||
if (gu_exn_caught(err, GuErrno)) {
|
||||
errno = *((GuErrno*) gu_exn_caught_data(err));
|
||||
PyErr_SetFromErrnoWithFilename(PyExc_IOError, fpath);
|
||||
} else {
|
||||
@@ -2786,7 +2786,7 @@ pgf_readExpr(PyObject *self, PyObject *args) {
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuIn* in = gu_data_in(buf, len, tmp_pool);
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_new_exn(tmp_pool);
|
||||
|
||||
pyexpr->pool = gu_new_pool();
|
||||
pyexpr->expr = pgf_read_expr(in, pyexpr->pool, err);
|
||||
@@ -2816,7 +2816,7 @@ pgf_readType(PyObject *self, PyObject *args) {
|
||||
|
||||
GuPool* tmp_pool = gu_local_pool();
|
||||
GuIn* in = gu_data_in(buf, len, tmp_pool);
|
||||
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
|
||||
GuExn* err = gu_new_exn(tmp_pool);
|
||||
|
||||
pytype->pool = gu_new_pool();
|
||||
pytype->type = pgf_read_type(in, pytype->pool, err);
|
||||
|
||||
@@ -5,7 +5,7 @@ include $(CLEAR_VARS)
|
||||
jni_c_files := jpgf.c
|
||||
pgf_c_files := data.c expr.c graphviz.c linearizer.c literals.c parser.c parseval.c pgf.c printer.c reader.c \
|
||||
reasoner.c evaluator.c jit.c typechecker.c
|
||||
gu_c_files := assert.c choice.c exn.c fun.c in.c map.c out.c str.c type.c utf8.c \
|
||||
gu_c_files := assert.c choice.c exn.c fun.c in.c map.c out.c str.c utf8.c \
|
||||
bits.c defs.c enum.c file.c hash.c mem.c prime.c seq.c string.c ucs.c variant.c
|
||||
|
||||
LOCAL_MODULE := jpgf
|
||||
|
||||
Reference in New Issue
Block a user