remove the read and write modules from libgu. this simplifies the i/o layer

This commit is contained in:
kr.angelov
2013-09-05 11:20:39 +00:00
parent d0f527d0dd
commit 83ca6c4f9f
32 changed files with 418 additions and 670 deletions

View File

@@ -4,7 +4,7 @@ include $(CLEAR_VARS)
jni_c_files := ../java/jpgf.c
pgf_c_files := data.c expr.c graphviz.c lexer.c linearizer.c literals.c parser.c parseval.c pgf.c printer.c reader.c reasoner.c
gu_c_files := assert.c choice.c exn.c fun.c in.c list.c map.c out.c read.c str.c type.c utf8.c write.c \
gu_c_files := assert.c choice.c exn.c fun.c in.c list.c map.c out.c str.c type.c utf8.c \
bits.c defs.c enum.c file.c hash.c intern.c log.c mem.c prime.c seq.c string.c ucs.c variant.c
LOCAL_MODULE := jpgf

View File

@@ -27,7 +27,6 @@ guinclude_HEADERS = \
gu/mem.h \
gu/out.h \
gu/prime.h \
gu/read.h \
gu/seq.h \
gu/str.h \
gu/string.h \
@@ -35,12 +34,12 @@ guinclude_HEADERS = \
gu/type.h \
gu/ucs.h \
gu/utf8.h \
gu/variant.h \
gu/write.h
gu/variant.h
pgfincludedir=$(includedir)/pgf
pgfinclude_HEADERS = \
pgf/expr.h \
pgf/reader.h \
pgf/linearizer.h \
pgf/parser.h \
pgf/lexer.h \
@@ -67,12 +66,10 @@ libgu_la_SOURCES = \
gu/mem.c \
gu/out.c \
gu/prime.c \
gu/read.c \
gu/str.c \
gu/string.c \
gu/type.c \
gu/utf8.c \
gu/write.c \
gu/ucs.c \
gu/variant.c

View File

@@ -255,66 +255,23 @@ gu_in_fini(GuFinalizer* fin)
gu_pool_free(pool);
}
GuIn
gu_init_in(GuInStream* stream)
{
return (GuIn) {
.buf_end = NULL,
.buf_curr = 0,
.buf_size = 0,
.stream = stream,
.fini.fn = gu_in_fini
};
}
GuIn*
gu_new_in(GuInStream* stream, GuPool* pool)
{
gu_require(stream != NULL);
GuIn* in = gu_new(GuIn, pool);
*in = gu_init_in(stream);
in->buf_end = NULL;
in->buf_curr = 0;
in->buf_size = 0;
in->stream = stream;
in->fini.fn = gu_in_fini;
return in;
}
typedef struct GuProxyInStream GuProxyInStream;
struct GuProxyInStream {
GuInStream stream;
GuIn* real_in;
};
static const uint8_t*
gu_proxy_in_begin_buffer(GuInStream* self, size_t* sz_out, GuExn* err)
{
GuProxyInStream* pis = gu_container(self, GuProxyInStream, stream);
return gu_in_begin_span(pis->real_in, sz_out, err);
}
static void
gu_proxy_in_end_buffer(GuInStream* self, size_t sz, GuExn* err)
{
GuProxyInStream* pis = gu_container(self, GuProxyInStream, stream);
gu_in_end_span(pis->real_in, sz);
}
static size_t
gu_proxy_in_input(GuInStream* self, uint8_t* dst, size_t sz, GuExn* err)
{
GuProxyInStream* pis = gu_container(self, GuProxyInStream, stream);
return gu_in_some(pis->real_in, dst, sz, err);
}
GuInStream*
gu_in_proxy_stream(GuIn* in, GuPool* pool)
{
GuProxyInStream* ins = gu_new(GuProxyInStream, pool);
ins->stream.begin_buffer = gu_proxy_in_begin_buffer;
ins->stream.end_buffer = gu_proxy_in_end_buffer;
ins->stream.input = gu_proxy_in_input;
ins->real_in = in;
return &ins->stream;
}
enum {
GU_BUFFERED_IN_BUF_SIZE = 4096
};

View File

@@ -25,16 +25,9 @@ struct GuIn {
GuFinalizer fini;
};
GuIn
gu_init_in(GuInStream* stream);
GuIn*
gu_new_in(GuInStream* stream, GuPool* pool);
GuInStream*
gu_in_proxy_stream(GuIn* in, GuPool* pool);
const uint8_t*
gu_in_begin_span(GuIn* in, size_t *sz_out, GuExn* err);

View File

@@ -1,20 +1,6 @@
#include <gu/seq.h>
#include <gu/out.h>
GuOut
gu_init_out(GuOutStream* stream)
{
gu_require(stream != NULL);
GuOut out = {
.buf_end = NULL,
.buf_curr = 0,
.stream = stream,
.fini.fn = NULL
};
return out;
}
#include <gu/utf8.h>
static bool
gu_out_is_buffering(GuOut* out)
@@ -81,8 +67,12 @@ gu_out_fini(GuFinalizer* self)
GuOut*
gu_new_out(GuOutStream* stream, GuPool* pool)
{
gu_require(stream != NULL);
GuOut* out = gu_new(GuOut, pool);
*out = gu_init_out(stream);
out->buf_end = NULL,
out->buf_curr = 0,
out->stream = stream,
out->fini.fn = gu_out_fini;
gu_pool_finally(pool, &out->fini);
return out;
@@ -185,72 +175,11 @@ gu_out_try_u8_(GuOut* restrict out, uint8_t u);
typedef struct GuProxyOutStream GuProxyOutStream;
struct GuProxyOutStream {
GuOutStream stream;
GuOut* real_out;
};
static uint8_t*
gu_proxy_out_buf_begin(GuOutStream* self, size_t req, size_t* sz_out,
GuExn* err)
{
GuProxyOutStream* pos =
gu_container(self, GuProxyOutStream, stream);
return gu_out_begin_span(pos->real_out, req, sz_out, err);
}
static void
gu_proxy_out_buf_end(GuOutStream* self, size_t sz, GuExn* err)
{
GuProxyOutStream* pos =
gu_container(self, GuProxyOutStream, stream);
gu_out_end_span(pos->real_out, sz);
}
static size_t
gu_proxy_out_output(GuOutStream* self, const uint8_t* src, size_t sz,
GuExn* err)
{
GuProxyOutStream* pos =
gu_container(self, GuProxyOutStream, stream);
return gu_out_bytes(pos->real_out, src, sz, err);
}
static void
gu_proxy_out_flush(GuOutStream* self, GuExn* err)
{
GuProxyOutStream* pos =
gu_container(self, GuProxyOutStream, stream);
gu_out_flush(pos->real_out, err);
}
GuOutStream*
gu_out_proxy_stream(GuOut* out, GuPool* pool)
{
GuProxyOutStream* pos = gu_new(GuProxyOutStream, pool);
pos->stream.begin_buf = gu_proxy_out_buf_begin;
pos->stream.end_buf = gu_proxy_out_buf_end;
pos->stream.output = gu_proxy_out_output;
pos->stream.flush = gu_proxy_out_flush;
pos->real_out = out;
return &pos->stream;
}
typedef struct GuBufferedOutStream GuBufferedOutStream;
struct GuBufferedOutStream {
GuProxyOutStream pstream;
GuOutStream stream;
GuOut* real_out;
size_t sz;
uint8_t buf[];
};
@@ -261,7 +190,7 @@ gu_buffered_out_buf_begin(GuOutStream* self, size_t req, size_t* sz_out,
{
(void) (req && err);
GuBufferedOutStream* b =
gu_container(self, GuBufferedOutStream, pstream.stream);
gu_container(self, GuBufferedOutStream, stream);
*sz_out = b->sz;
return b->buf;
}
@@ -270,9 +199,26 @@ static void
gu_buffered_out_buf_end(GuOutStream* self, size_t sz, GuExn* err)
{
GuBufferedOutStream* b =
gu_container(self, GuBufferedOutStream, pstream.stream);
gu_container(self, GuBufferedOutStream, stream);
gu_require(sz <= b->sz);
gu_out_bytes(b->pstream.real_out, b->buf, sz, err);
gu_out_bytes(b->real_out, b->buf, sz, err);
}
static size_t
gu_buffered_out_output(GuOutStream* self, const uint8_t* src, size_t sz,
GuExn* err)
{
GuBufferedOutStream* bos =
gu_container(self, GuBufferedOutStream, stream);
return gu_out_bytes(bos->real_out, src, sz, err);
}
static void
gu_buffered_out_flush(GuOutStream* self, GuExn* err)
{
GuBufferedOutStream* bos =
gu_container(self, GuBufferedOutStream, stream);
gu_out_flush(bos->real_out, err);
}
GuOut*
@@ -280,15 +226,15 @@ gu_new_buffered_out(GuOut* out, size_t sz, GuPool* pool)
{
GuBufferedOutStream* b =
gu_new_flex(pool, GuBufferedOutStream, buf, sz);
b->pstream.stream = (GuOutStream) {
b->stream = (GuOutStream) {
.begin_buf = gu_buffered_out_buf_begin,
.end_buf = gu_buffered_out_buf_end,
.output = gu_proxy_out_output,
.flush = gu_proxy_out_flush
.output = gu_buffered_out_output,
.flush = gu_buffered_out_flush
};
b->pstream.real_out = out;
b->real_out = out;
b->sz = sz;
return gu_new_out(&b->pstream.stream, pool);
return gu_new_out(&b->stream, pool);
}
GuOut*
@@ -301,3 +247,29 @@ gu_out_buffered(GuOut* out, GuPool* pool)
}
extern inline void
gu_putc(char c, GuOut* out, GuExn* err);
void
gu_puts(const char* str, GuOut* out, GuExn* err)
{
gu_str_out_utf8(str, out, err);
}
void
gu_vprintf(const char* fmt, va_list args, GuOut* out, GuExn* err)
{
GuPool* tmp_pool = gu_local_pool();
char* str = gu_vasprintf(fmt, args, tmp_pool);
gu_str_out_utf8(str, out, err);
gu_pool_free(tmp_pool);
}
void
gu_printf(GuOut* out, GuExn* err, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
gu_vprintf(fmt, args, out, err);
va_end(args);
}

View File

@@ -4,6 +4,7 @@
#include <gu/defs.h>
#include <gu/assert.h>
#include <gu/exn.h>
#include <gu/ucs.h>
typedef struct GuOut GuOut;
@@ -27,10 +28,6 @@ struct GuOut {
GuFinalizer fini;
};
GuOut
gu_init_out(GuOutStream* stream);
GuOut*
gu_new_out(GuOutStream* stream, GuPool* pool);
@@ -40,9 +37,6 @@ gu_out_is_buffered(GuOut* out)
return !!out->stream->begin_buf;
}
GuOutStream*
gu_out_proxy_stream(GuOut* out, GuPool* pool);
GuOut*
gu_new_buffered_out(GuOut* out, size_t buf_sz, GuPool* pool);
@@ -163,4 +157,20 @@ gu_out_f64le(GuOut* out, double d, GuExn* err);
void
gu_out_f64be(GuOut* out, double d, GuExn* err);
inline void
gu_putc(char c, GuOut* out, GuExn* err)
{
GuUCS ucs = gu_char_ucs(c);
gu_out_u8(out, (uint8_t) ucs, err);
}
void
gu_puts(const char* str, GuOut* out, GuExn* err);
void
gu_vprintf(const char* fmt, va_list args, GuOut* out, GuExn* err);
void
gu_printf(GuOut* out, GuExn* err, const char* fmt, ...);
#endif // GU_OUT_H_

View File

@@ -1,15 +0,0 @@
#include <gu/read.h>
extern inline GuUCS
gu_read_ucs(GuReader* rdr, GuExn* err);
extern inline char
gu_getc(GuReader* rdr, GuExn* err);
GuReader*
gu_new_utf8_reader(GuIn* utf8_in, GuPool* pool)
{
GuReader* rdr = gu_new(GuReader, pool);
rdr->in_ = gu_init_in(gu_in_proxy_stream(utf8_in, pool));
return rdr;
}

View File

@@ -1,31 +0,0 @@
#ifndef GU_READ_H_
#define GU_READ_H_
#include <gu/in.h>
#include <gu/ucs.h>
#include <gu/utf8.h>
typedef struct GuReader GuReader;
struct GuReader {
GuIn in_;
};
inline GuUCS
gu_read_ucs(GuReader* rdr, GuExn* err)
{
return gu_in_utf8(&rdr->in_, err);
}
inline char
gu_getc(GuReader* rdr, GuExn* err)
{
return gu_in_utf8_char(&rdr->in_, err);
}
GuReader*
gu_new_utf8_reader(GuIn* utf8_in, GuPool* pool);
/**< @todo Implement. */
#endif // GU_READ_H_

View File

@@ -10,7 +10,7 @@ const GuString gu_empty_string = { 1 };
struct GuStringBuf {
GuByteBuf* bbuf;
GuWriter* wtr;
GuOut* out;
};
GuStringBuf*
@@ -18,17 +18,16 @@ gu_string_buf(GuPool* pool)
{
GuBuf* buf = gu_new_buf(uint8_t, pool);
GuOut* out = gu_buf_out(buf, pool);
GuWriter* wtr = gu_new_utf8_writer(out, pool);
GuStringBuf* sbuf = gu_new(GuStringBuf, pool);
sbuf->bbuf = buf;
sbuf->wtr = wtr;
sbuf->out = out;
return sbuf;
}
GuWriter*
gu_string_buf_writer(GuStringBuf* sb)
GuOut*
gu_string_buf_out(GuStringBuf* sb)
{
return sb->wtr;
return sb->out;
}
static GuString
@@ -61,14 +60,14 @@ gu_utf8_string(const uint8_t* buf, size_t sz, GuPool* pool)
GuString
gu_string_buf_freeze(GuStringBuf* sb, GuPool* pool)
{
gu_writer_flush(sb->wtr, NULL);
gu_out_flush(sb->out, NULL);
uint8_t* data = gu_buf_data(sb->bbuf);
size_t len = gu_buf_length(sb->bbuf);
return gu_utf8_string(data, len, pool);
}
GuReader*
gu_string_reader(GuString s, GuPool* pool)
GuIn*
gu_string_in(GuString s, GuPool* pool)
{
GuWord w = s.w_;
uint8_t* buf = NULL;
@@ -85,9 +84,7 @@ gu_string_reader(GuString s, GuPool* pool)
len = (p[0] == 0) ? ((size_t*) p)[-1] : p[0];
buf = &p[1];
}
GuIn* in = gu_data_in(buf, len, pool);
GuReader* rdr = gu_new_utf8_reader(in, pool);
return rdr;
return gu_data_in(buf, len, pool);
}
static bool
@@ -145,7 +142,7 @@ gu_string_copy(GuString string, GuPool* pool)
void
gu_string_write(GuString s, GuWriter* wtr, GuExn* err)
gu_string_write(GuString s, GuOut* out, GuExn* err)
{
GuWord w = s.w_;
uint8_t buf[sizeof(GuWord)];
@@ -165,7 +162,7 @@ gu_string_write(GuString s, GuWriter* wtr, GuExn* err)
sz = (p[0] == 0) ? ((size_t*) p)[-1] : p[0];
src = &p[1];
}
gu_utf8_write(src, sz, wtr, err);
gu_out_bytes(out, src, sz, err);
}
GuString
@@ -173,9 +170,9 @@ gu_format_string_v(const char* fmt, va_list args, GuPool* pool)
{
GuPool* tmp_pool = gu_local_pool();
GuStringBuf* sb = gu_string_buf(tmp_pool);
GuWriter* wtr = gu_string_buf_writer(sb);
gu_vprintf(fmt, args, wtr, NULL);
gu_writer_flush(wtr, NULL);
GuOut* out = gu_string_buf_out(sb);
gu_vprintf(fmt, args, out, NULL);
gu_out_flush(out, NULL);
GuString s = gu_string_buf_freeze(sb, pool);
gu_pool_free(tmp_pool);
return s;

View File

@@ -21,8 +21,8 @@
#define GU_STRING_H_
#include <gu/bits.h>
#include <gu/read.h>
#include <gu/write.h>
#include <gu/in.h>
#include <gu/out.h>
typedef GuOpaque() GuString;
@@ -32,10 +32,10 @@ GuString
gu_string_copy(GuString string, GuPool* pool);
void
gu_string_write(GuString string, GuWriter* wtr, GuExn* err);
gu_string_write(GuString string, GuOut* out, GuExn* err);
GuReader*
gu_string_reader(GuString string, GuPool* pool);
GuIn*
gu_string_in(GuString string, GuPool* pool);
bool
gu_string_is_stable(GuString string);
@@ -48,8 +48,8 @@ typedef struct GuStringBuf GuStringBuf;
GuStringBuf*
gu_string_buf(GuPool* pool);
GuWriter*
gu_string_buf_writer(GuStringBuf* sb);
GuOut*
gu_string_buf_out(GuStringBuf* sb);
GuString
gu_string_buf_freeze(GuStringBuf* sb, GuPool* pool);

View File

@@ -1,53 +0,0 @@
#include <gu/write.h>
size_t
gu_utf32_write(const GuUCS* src, size_t len, GuWriter* wtr, GuExn* err)
{
return gu_utf32_out_utf8(src, len, &wtr->out_, err);
}
void
gu_vprintf(const char* fmt, va_list args, GuWriter* wtr, GuExn* err)
{
GuPool* tmp_pool = gu_local_pool();
char* str = gu_vasprintf(fmt, args, tmp_pool);
gu_puts(str, wtr, err);
gu_pool_free(tmp_pool);
}
void
gu_printf(GuWriter* wtr, GuExn* err, const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
gu_vprintf(fmt, args, wtr, err);
va_end(args);
}
GuWriter*
gu_new_utf8_writer(GuOut* utf8_out, GuPool* pool)
{
GuOutStream* stream = gu_out_proxy_stream(utf8_out, pool);
GuWriter* wtr = gu_new(GuWriter, pool);
wtr->out_ = gu_init_out(stream);
return wtr;
}
extern inline void
gu_ucs_write(GuUCS ucs, GuWriter* wtr, GuExn* err);
extern inline void
gu_writer_flush(GuWriter* wtr, GuExn* err);
extern inline void
gu_putc(char c, GuWriter* wtr, GuExn* err);
extern inline void
gu_puts(const char* str, GuWriter* wtr, GuExn* err);
extern inline size_t
gu_utf8_write(const uint8_t* src, size_t sz, GuWriter* wtr, GuExn* err);

View File

@@ -1,64 +0,0 @@
#ifndef GU_WRITE_H_
#define GU_WRITE_H_
#include <gu/exn.h>
#include <gu/ucs.h>
#include <gu/out.h>
#include <gu/utf8.h>
typedef struct GuWriter GuWriter;
struct GuWriter {
GuOut out_;
};
size_t
gu_utf32_write(const GuUCS* buf, size_t size, GuWriter* wtr, GuExn* err);
inline void
gu_writer_flush(GuWriter* wtr, GuExn* err)
{
gu_out_flush(&wtr->out_, err);
}
inline void
gu_ucs_write(GuUCS ucs, GuWriter* wtr, GuExn* err)
{
gu_out_utf8(ucs, &wtr->out_, err);
}
inline void
gu_putc(char c, GuWriter* wtr, GuExn* err)
{
GuUCS ucs = gu_char_ucs(c);
gu_out_u8(&wtr->out_, (uint8_t) ucs, err);
}
inline void
gu_puts(const char* str, GuWriter* wtr, GuExn* err)
{
gu_str_out_utf8(str, &wtr->out_, err);
}
inline size_t
gu_utf8_write(const uint8_t* src, size_t sz, GuWriter* wtr, GuExn* err)
{
return gu_out_bytes(&wtr->out_, src, sz, err);
}
void
gu_vprintf(const char* fmt, va_list args, GuWriter* wtr, GuExn* err);
void
gu_printf(GuWriter* wtr, GuExn* err, const char* fmt, ...);
//GuWriter
//gu_init_utf8_writer(GuOut* utf8_out);
GuWriter*
gu_new_utf8_writer(GuOut* utf8_out, GuPool* pool);
GuWriter*
gu_make_locale_writer(GuOut* locale_out, GuPool* pool);
#endif // GU_WRITE_H_

View File

@@ -931,25 +931,25 @@ pgf_expr_hash(GuHash h, PgfExpr e)
void
pgf_print_literal(PgfLiteral lit,
GuWriter* wtr, GuExn* err)
GuOut* out, GuExn* err)
{
GuVariantInfo ei = gu_variant_open(lit);
switch (ei.tag) {
case PGF_LITERAL_STR: {
PgfLiteralStr* lit = ei.data;
gu_putc('"', wtr, err);
gu_string_write(lit->val, wtr, err);
gu_putc('"', wtr, err);
gu_putc('"', out, err);
gu_string_write(lit->val, out, err);
gu_putc('"', out, err);
break;
}
case PGF_LITERAL_INT: {
PgfLiteralInt* lit = ei.data;
gu_printf(wtr, err, "%d", lit->val);
gu_printf(out, err, "%d", lit->val);
break;
}
case PGF_LITERAL_FLT: {
PgfLiteralFlt* lit = ei.data;
gu_printf(wtr, err, "%lf", lit->val);
gu_printf(out, err, "%lf", lit->val);
break;
}
default:
@@ -959,7 +959,7 @@ pgf_print_literal(PgfLiteral lit,
void
pgf_print_expr(PgfExpr expr, PgfPrintContext* ctxt, int prec,
GuWriter* wtr, GuExn* err)
GuOut* out, GuExn* err)
{
GuVariantInfo ei = gu_variant_open(expr);
switch (ei.tag) {
@@ -967,20 +967,20 @@ pgf_print_expr(PgfExpr expr, PgfPrintContext* ctxt, int prec,
PgfExprAbs* abs = ei.data;
if (prec > 1) {
gu_puts("(", wtr, err);
gu_puts("(", out, err);
}
gu_putc('\\', wtr, err);
gu_putc('\\', out, err);
PgfPrintContext* new_ctxt = ctxt;
for (;;) {
if (abs->bind_type == PGF_BIND_TYPE_IMPLICIT) {
gu_putc('{', wtr, err);
gu_putc('{', out, err);
}
gu_string_write(abs->id, wtr, err);
gu_string_write(abs->id, out, err);
if (abs->bind_type == PGF_BIND_TYPE_IMPLICIT) {
gu_putc('}', wtr, err);
gu_putc('}', out, err);
}
PgfPrintContext* c = malloc(sizeof(PgfPrintContext));
@@ -991,13 +991,13 @@ pgf_print_expr(PgfExpr expr, PgfPrintContext* ctxt, int prec,
if (gu_variant_tag(abs->body) != PGF_EXPR_ABS)
break;
gu_putc(',', wtr, err);
gu_putc(',', out, err);
abs = gu_variant_data(abs->body);
}
gu_puts(" -> ", wtr, err);
pgf_print_expr(abs->body, new_ctxt, 1, wtr, err);
gu_puts(" -> ", out, err);
pgf_print_expr(abs->body, new_ctxt, 1, out, err);
while (new_ctxt != ctxt) {
PgfPrintContext* next = new_ctxt->next;
@@ -1006,34 +1006,34 @@ pgf_print_expr(PgfExpr expr, PgfPrintContext* ctxt, int prec,
}
if (prec > 1) {
gu_puts(")", wtr, err);
gu_puts(")", out, err);
}
break;
}
case PGF_EXPR_APP: {
PgfExprApp* app = ei.data;
if (prec > 3) {
gu_puts("(", wtr, err);
gu_puts("(", out, err);
}
pgf_print_expr(app->fun, ctxt, 3, wtr, err);
gu_puts(" ", wtr, err);
pgf_print_expr(app->arg, ctxt, 4, wtr, err);
pgf_print_expr(app->fun, ctxt, 3, out, err);
gu_puts(" ", out, err);
pgf_print_expr(app->arg, ctxt, 4, out, err);
if (prec > 3) {
gu_puts(")", wtr, err);
gu_puts(")", out, err);
}
break;
}
case PGF_EXPR_LIT: {
PgfExprLit* lit = ei.data;
pgf_print_literal(lit->lit, wtr, err);
pgf_print_literal(lit->lit, out, err);
break;
}
case PGF_EXPR_META:
gu_putc('?', wtr, err);
gu_putc('?', out, err);
break;
case PGF_EXPR_FUN: {
PgfExprFun* fun = ei.data;
gu_string_write(fun->fun, wtr, err);
gu_string_write(fun->fun, out, err);
break;
}
case PGF_EXPR_VAR: {
@@ -1046,26 +1046,26 @@ pgf_print_expr(PgfExpr expr, PgfPrintContext* ctxt, int prec,
}
if (c == NULL) {
gu_printf(wtr, err, "#%d", evar->var);
gu_printf(out, err, "#%d", evar->var);
} else {
gu_string_write(c->name, wtr, err);
gu_string_write(c->name, out, err);
}
break;
}
case PGF_EXPR_TYPED: {
PgfExprTyped* typed = ei.data;
gu_putc('<', wtr, err);
pgf_print_expr(typed->expr, ctxt, 0, wtr, err);
gu_puts(" : ", wtr, err);
pgf_print_type(typed->type, ctxt, 0, wtr, err);
gu_putc('>', wtr, err);
gu_putc('<', out, err);
pgf_print_expr(typed->expr, ctxt, 0, out, err);
gu_puts(" : ", out, err);
pgf_print_type(typed->type, ctxt, 0, out, err);
gu_putc('>', out, err);
break;
}
case PGF_EXPR_IMPL_ARG: {
PgfExprImplArg* impl = ei.data;
gu_putc('{', wtr, err);
pgf_print_expr(impl->expr, ctxt, 0, wtr, err);
gu_putc('}', wtr, err);
gu_putc('{', out, err);
pgf_print_expr(impl->expr, ctxt, 0, out, err);
gu_putc('}', out, err);
break;
}
default:
@@ -1075,26 +1075,26 @@ pgf_print_expr(PgfExpr expr, PgfPrintContext* ctxt, int prec,
PgfPrintContext*
pgf_print_hypo(PgfHypo *hypo, PgfPrintContext* ctxt, int prec,
GuWriter *wtr, GuExn *err)
GuOut *out, GuExn *err)
{
if (hypo->bind_type == PGF_BIND_TYPE_IMPLICIT) {
gu_puts("({", wtr, err);
gu_string_write(hypo->cid, wtr, err);
gu_puts("} : ", wtr, err);
pgf_print_type(hypo->type, ctxt, 0, wtr, err);
gu_puts(")", wtr, err);
gu_puts("({", out, err);
gu_string_write(hypo->cid, out, err);
gu_puts("} : ", out, err);
pgf_print_type(hypo->type, ctxt, 0, out, err);
gu_puts(")", out, err);
} else {
GuPool* tmp_pool = gu_new_pool();
GuString tmp = gu_str_string("_", tmp_pool);
if (!gu_string_eq(hypo->cid, tmp)) {
gu_puts("(", wtr, err);
gu_string_write(hypo->cid, wtr, err);
gu_puts(" : ", wtr, err);
pgf_print_type(hypo->type, ctxt, 0, wtr, err);
gu_puts(")", wtr, err);
gu_puts("(", out, err);
gu_string_write(hypo->cid, out, err);
gu_puts(" : ", out, err);
pgf_print_type(hypo->type, ctxt, 0, out, err);
gu_puts(")", out, err);
} else {
pgf_print_type(hypo->type, ctxt, prec, wtr, err);
pgf_print_type(hypo->type, ctxt, prec, out, err);
}
gu_pool_free(tmp_pool);
@@ -1108,26 +1108,26 @@ pgf_print_hypo(PgfHypo *hypo, PgfPrintContext* ctxt, int prec,
void
pgf_print_type(PgfType *type, PgfPrintContext* ctxt, int prec,
GuWriter *wtr, GuExn *err)
GuOut *out, GuExn *err)
{
size_t n_hypos = gu_seq_length(type->hypos);
if (n_hypos > 0) {
if (prec > 0) gu_putc('(', wtr, err);
if (prec > 0) gu_putc('(', out, err);
PgfPrintContext* new_ctxt = ctxt;
for (size_t i = 0; i < n_hypos; i++) {
PgfHypo *hypo = gu_seq_index(type->hypos, PgfHypo, i);
new_ctxt = pgf_print_hypo(hypo, new_ctxt, 1, wtr, err);
new_ctxt = pgf_print_hypo(hypo, new_ctxt, 1, out, err);
gu_puts(" -> ", wtr, err);
gu_puts(" -> ", out, err);
}
gu_string_write(type->cid, wtr, err);
gu_string_write(type->cid, out, err);
for (size_t i = 0; i < type->n_exprs; i++) {
gu_puts(" ", wtr, err);
pgf_print_expr(type->exprs[i], new_ctxt, 4, wtr, err);
gu_puts(" ", out, err);
pgf_print_expr(type->exprs[i], new_ctxt, 4, out, err);
}
while (new_ctxt != ctxt) {
@@ -1136,20 +1136,20 @@ pgf_print_type(PgfType *type, PgfPrintContext* ctxt, int prec,
new_ctxt = next;
}
if (prec > 0) gu_putc(')', wtr, err);
if (prec > 0) gu_putc(')', out, err);
} else if (type->n_exprs > 0) {
if (prec > 3) gu_putc('(', wtr, err);
if (prec > 3) gu_putc('(', out, err);
gu_string_write(type->cid, wtr, err);
gu_string_write(type->cid, out, err);
for (size_t i = 0; i < type->n_exprs; i++) {
gu_puts(" ", wtr, err);
pgf_print_expr(type->exprs[i], ctxt, 4, wtr, err);
gu_puts(" ", out, err);
pgf_print_expr(type->exprs[i], ctxt, 4, out, err);
}
if (prec > 3) gu_putc(')', wtr, err);
if (prec > 3) gu_putc(')', out, err);
} else {
gu_string_write(type->cid, wtr, err);
gu_string_write(type->cid, out, err);
}
}

View File

@@ -1,8 +1,8 @@
#ifndef EXPR_H_
#define EXPR_H_
#include <gu/read.h>
#include <gu/write.h>
#include <gu/in.h>
#include <gu/out.h>
#include <gu/variant.h>
#include <gu/seq.h>
@@ -180,18 +180,18 @@ struct PgfPrintContext {
};
void
pgf_print_literal(PgfLiteral lit, GuWriter* wtr, GuExn* err);
pgf_print_literal(PgfLiteral lit, GuOut* out, GuExn* err);
void
pgf_print_expr(PgfExpr expr, PgfPrintContext* ctxt, int prec,
GuWriter* wtr, GuExn* err);
GuOut* out, GuExn* err);
PgfPrintContext*
pgf_print_hypo(PgfHypo *hypo, PgfPrintContext* ctxt, int prec,
GuWriter *wtr, GuExn *err);
GuOut* out, GuExn *err);
void
pgf_print_type(PgfType *type, PgfPrintContext* ctxt, int prec,
GuWriter *wtr, GuExn *err);
GuOut* out, GuExn *err);
#endif /* EXPR_H_ */

View File

@@ -4,7 +4,7 @@
static int
pgf_graphviz_abstract_tree_(PgfExpr expr, int *pid,
GuWriter* wtr, GuExn* err)
GuOut* out, GuExn* err)
{
int id = -1;
@@ -15,52 +15,52 @@ pgf_graphviz_abstract_tree_(PgfExpr expr, int *pid,
break;
case PGF_EXPR_APP: {
PgfExprApp* app = ei.data;
id = pgf_graphviz_abstract_tree_(app->fun, pid, wtr, err);
int arg_id = pgf_graphviz_abstract_tree_(app->arg, pid, wtr, err);
gu_printf(wtr, err, "n%d -- n%d [style = \"solid\"]\n", id, arg_id);
id = pgf_graphviz_abstract_tree_(app->fun, pid, out, err);
int arg_id = pgf_graphviz_abstract_tree_(app->arg, pid, out, err);
gu_printf(out, err, "n%d -- n%d [style = \"solid\"]\n", id, arg_id);
break;
}
case PGF_EXPR_LIT: {
PgfExprLit* lit = ei.data;
id = (*pid)++;
gu_printf(wtr, err, "n%d[label = \"", id);
gu_printf(out, err, "n%d[label = \"", id);
GuVariantInfo ei = gu_variant_open(lit->lit);
switch (ei.tag) {
case PGF_LITERAL_STR: {
PgfLiteralStr* lit = ei.data;
gu_puts("\\\"", wtr, err);
gu_string_write(lit->val, wtr, err);
gu_puts("\\\"", wtr, err);
gu_puts("\\\"", out, err);
gu_string_write(lit->val, out, err);
gu_puts("\\\"", out, err);
break;
}
case PGF_LITERAL_INT: {
PgfLiteralInt* lit = ei.data;
gu_printf(wtr, err, "%d", lit->val);
gu_printf(out, err, "%d", lit->val);
break;
}
case PGF_LITERAL_FLT: {
PgfLiteralFlt* lit = ei.data;
gu_printf(wtr, err, "%lf", lit->val);
gu_printf(out, err, "%lf", lit->val);
break;
}
default:
gu_impossible();
}
gu_puts("\", style = \"solid\", shape = \"plaintext\"]\n", wtr, err);
gu_puts("\", style = \"solid\", shape = \"plaintext\"]\n", out, err);
break;
}
case PGF_EXPR_META:
id = (*pid)++;
gu_printf(wtr, err, "n%d[label = \"?\", style = \"solid\", shape = \"plaintext\"]\n", id);
gu_printf(out, err, "n%d[label = \"?\", style = \"solid\", shape = \"plaintext\"]\n", id);
break;
case PGF_EXPR_FUN: {
PgfExprFun* fun = ei.data;
id = (*pid)++;
gu_printf(wtr, err, "n%d[label = \"", id);
gu_string_write(fun->fun, wtr, err);
gu_puts("\", style = \"solid\", shape = \"plaintext\"]\n", wtr, err);
gu_printf(out, err, "n%d[label = \"", id);
gu_string_write(fun->fun, out, err);
gu_puts("\", style = \"solid\", shape = \"plaintext\"]\n", out, err);
break;
}
case PGF_EXPR_VAR:
@@ -68,12 +68,12 @@ pgf_graphviz_abstract_tree_(PgfExpr expr, int *pid,
break;
case PGF_EXPR_TYPED: {
PgfExprTyped* typed = ei.data;
id = pgf_graphviz_abstract_tree_(typed->expr, pid, wtr, err);
id = pgf_graphviz_abstract_tree_(typed->expr, pid, out, err);
break;
}
case PGF_EXPR_IMPL_ARG: {
PgfExprImplArg* implarg = ei.data;
id = pgf_graphviz_abstract_tree_(implarg->expr, pid, wtr, err);
id = pgf_graphviz_abstract_tree_(implarg->expr, pid, out, err);
break;
}
default:
@@ -84,13 +84,13 @@ pgf_graphviz_abstract_tree_(PgfExpr expr, int *pid,
}
void
pgf_graphviz_abstract_tree(PgfPGF* pgf, PgfExpr expr, GuWriter* wtr, GuExn* err)
pgf_graphviz_abstract_tree(PgfPGF* pgf, PgfExpr expr, GuOut* out, GuExn* err)
{
int id = 0;
gu_puts("graph {\n", wtr, err);
pgf_graphviz_abstract_tree_(expr, &id, wtr, err);
gu_puts("}", wtr, err);
gu_puts("graph {\n", out, err);
pgf_graphviz_abstract_tree_(expr, &id, out, err);
gu_puts("}", out, err);
}
typedef struct PgfParseNode PgfParseNode;
@@ -105,7 +105,7 @@ typedef struct {
PgfLinFuncs* funcs;
GuPool* pool;
GuWriter* wtr;
GuOut* out;
GuExn* err;
PgfParseNode* parent;
@@ -221,43 +221,43 @@ static PgfLinFuncs pgf_bracket_lin_funcs = {
};
static void
pgf_graphviz_parse_level(GuBuf* level, GuWriter* wtr, GuExn* err)
pgf_graphviz_parse_level(GuBuf* level, GuOut* out, GuExn* err)
{
gu_puts("\n subgraph {rank=same;\n", wtr, err);
gu_puts("\n subgraph {rank=same;\n", out, err);
size_t len = gu_buf_length(level);
if (len > 1)
gu_puts(" edge[style=invis]\n", wtr, err);
gu_puts(" edge[style=invis]\n", out, err);
for (size_t i = 0; i < len; i++) {
PgfParseNode* node = gu_buf_get(level, PgfParseNode*, i);
gu_printf(wtr, err, " n%d[label=\"", node->id);
gu_string_write(node->label, wtr, err);
gu_puts("\"]\n", wtr, err);
gu_printf(out, err, " n%d[label=\"", node->id);
gu_string_write(node->label, out, err);
gu_puts("\"]\n", out, err);
}
if (len > 1) {
for (size_t i = 0; i < len; i++) {
PgfParseNode* node = gu_buf_get(level, PgfParseNode*, i);
gu_puts((i == 0) ? " " : " -- ", wtr, err);
gu_printf(wtr, err, "n%d", node->id);
gu_puts((i == 0) ? " " : " -- ", out, err);
gu_printf(out, err, "n%d", node->id);
}
gu_puts("\n", wtr, err);
gu_puts("\n", out, err);
}
gu_puts(" }\n", wtr, err);
gu_puts(" }\n", out, err);
for (size_t i = 0; i < len; i++) {
PgfParseNode* node = gu_buf_get(level, PgfParseNode*, i);
if (node->parent != NULL)
gu_printf(wtr, err, " n%d -- n%d\n", node->parent->id, node->id);
gu_printf(out, err, " n%d -- n%d\n", node->parent->id, node->id);
}
}
void
pgf_graphviz_parse_tree(PgfConcr* concr, PgfExpr expr, GuWriter* wtr, GuExn* err)
pgf_graphviz_parse_tree(PgfConcr* concr, PgfExpr expr, GuOut* out, GuExn* err)
{
GuPool* tmp_pool = gu_local_pool();
@@ -269,13 +269,13 @@ pgf_graphviz_parse_tree(PgfConcr* concr, PgfExpr expr, GuWriter* wtr, GuExn* err
return;
}
gu_puts("graph {\n", wtr, err);
gu_puts(" node[shape=plaintext]\n", wtr, err);
gu_puts("graph {\n", out, err);
gu_puts(" node[shape=plaintext]\n", out, err);
PgfBracketLznState state;
state.funcs = &pgf_bracket_lin_funcs;
state.pool = tmp_pool;
state.wtr = wtr;
state.out = out;
state.err = err;
state.parent = NULL;
@@ -288,11 +288,11 @@ pgf_graphviz_parse_tree(PgfConcr* concr, PgfExpr expr, GuWriter* wtr, GuExn* err
size_t len = gu_buf_length(state.internals);
for (size_t i = 0; i < len; i++) {
GuBuf* level = gu_buf_get(state.internals, GuBuf*, i);
pgf_graphviz_parse_level(level, wtr, err);
pgf_graphviz_parse_level(level, out, err);
}
pgf_graphviz_parse_level(state.leaves, wtr, err);
pgf_graphviz_parse_level(state.leaves, out, err);
gu_puts("}", wtr, err);
gu_puts("}", out, err);
gu_pool_free(tmp_pool);
}

View File

@@ -2,9 +2,9 @@
#define PGF_GRAPHVIZ_H_
void
pgf_graphviz_abstract_tree(PgfPGF* pgf, PgfExpr expr, GuWriter* wtr, GuExn* err);
pgf_graphviz_abstract_tree(PgfPGF* pgf, PgfExpr expr, GuOut* out, GuExn* err);
void
pgf_graphviz_parse_tree(PgfConcr* concr, PgfExpr expr, GuWriter* wtr, GuExn* err);
pgf_graphviz_parse_tree(PgfConcr* concr, PgfExpr expr, GuOut* out, GuExn* err);
#endif

View File

@@ -1,11 +1,12 @@
#include <gu/list.h>
#include <gu/utf8.h>
#include <pgf/pgf.h>
#include <pgf/data.h>
#include <wctype.h>
typedef struct {
PgfLexer base;
GuReader* rdr;
GuIn* in;
GuPool* pool;
GuUCS ucs;
} PgfSimpleLexer;
@@ -13,7 +14,7 @@ typedef struct {
static void
pgf_lexer_read_ucs(PgfSimpleLexer *lexer, GuExn* err)
{
lexer->ucs = gu_read_ucs(lexer->rdr, err);
lexer->ucs = gu_in_utf8(lexer->in, err);
if (gu_exn_is_raised(err)) {
gu_exn_clear(err);
lexer->ucs = ' ';
@@ -27,10 +28,10 @@ pgf_simple_lexer_read_token(PgfLexer *base, GuExn* err)
GuPool* tmp_pool = gu_new_pool();
GuStringBuf* buf = gu_string_buf(tmp_pool);
GuWriter* wtr = gu_string_buf_writer(buf);
GuOut* out = gu_string_buf_out(buf);
while (iswspace(lexer->ucs)) {
lexer->ucs = gu_read_ucs(lexer->rdr, err);
lexer->ucs = gu_in_utf8(lexer->in, err);
if (gu_exn_is_raised(err))
goto stop;
}
@@ -40,7 +41,7 @@ pgf_simple_lexer_read_token(PgfLexer *base, GuExn* err)
lexer->ucs == '_') {
int counter = 0;
do {
gu_ucs_write(lexer->ucs, wtr, err);
gu_out_utf8(lexer->ucs, out, err);
if (gu_exn_is_raised(err))
goto stop;
counter++;
@@ -48,7 +49,7 @@ pgf_simple_lexer_read_token(PgfLexer *base, GuExn* err)
if (lexer->ucs == '.' && counter < 4) {
// perhaps an abreviation
gu_ucs_write(lexer->ucs, wtr, err);
gu_out_utf8(lexer->ucs, out, err);
if (gu_exn_is_raised(err))
goto stop;
counter = 0;
@@ -59,7 +60,7 @@ pgf_simple_lexer_read_token(PgfLexer *base, GuExn* err)
lexer->ucs == '_');
} else if (iswdigit(lexer->ucs) || lexer->ucs == '-') {
if (lexer->ucs == '-') {
gu_ucs_write(lexer->ucs, wtr, err);
gu_out_utf8(lexer->ucs, out, err);
if (gu_exn_is_raised(err))
goto stop;
@@ -69,7 +70,7 @@ pgf_simple_lexer_read_token(PgfLexer *base, GuExn* err)
}
do {
gu_ucs_write(lexer->ucs, wtr, err);
gu_out_utf8(lexer->ucs, out, err);
if (gu_exn_is_raised(err))
goto stop;
@@ -77,20 +78,20 @@ pgf_simple_lexer_read_token(PgfLexer *base, GuExn* err)
} while (iswdigit(lexer->ucs));
if (lexer->ucs == '.') {
gu_ucs_write(lexer->ucs, wtr, err);
gu_out_utf8(lexer->ucs, out, err);
if (gu_exn_is_raised(err))
goto stop;
pgf_lexer_read_ucs(lexer, err);
while (iswdigit(lexer->ucs)) {
gu_ucs_write(lexer->ucs, wtr, err);
gu_out_utf8(lexer->ucs, out, err);
if (gu_exn_is_raised(err))
goto stop;
pgf_lexer_read_ucs(lexer, err);
}
}
} else {
gu_ucs_write(lexer->ucs, wtr, err);
gu_out_utf8(lexer->ucs, out, err);
if (gu_exn_is_raised(err))
goto stop;
pgf_lexer_read_ucs(lexer, err);
@@ -104,12 +105,12 @@ stop:
}
PgfLexer*
pgf_new_simple_lexer(GuReader *rdr, GuPool *pool)
pgf_new_simple_lexer(GuIn *in, GuPool *pool)
{
PgfSimpleLexer* lexer = gu_new(PgfSimpleLexer, pool);
lexer->base.read_token = pgf_simple_lexer_read_token;
lexer->base.tok = gu_empty_string;
lexer->rdr = rdr;
lexer->in = in;
lexer->pool = pool;
lexer->ucs = ' ';
return ((PgfLexer*) lexer);

View File

@@ -1,7 +1,7 @@
#ifndef PGF_LEXER_H_
#define PGF_LEXER_H_
#include <gu/read.h>
#include <gu/in.h>
#include <pgf/expr.h>
/// A single lexical token
@@ -20,7 +20,7 @@ typedef struct {
} PgfLexer;
PgfLexer*
pgf_new_simple_lexer(GuReader *rdr, GuPool *pool);
pgf_new_simple_lexer(GuIn *in, GuPool *pool);
PgfToken
pgf_lexer_read_token(PgfLexer *lexer, GuExn* err);

View File

@@ -367,11 +367,11 @@ pgf_lzn_resolve(PgfLzn* lzn, PgfExpr expr, PgfCCat* ccat, GuPool* pool)
GuPool* tmp_pool = gu_local_pool();
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
GuWriter* wtr = gu_string_buf_writer(sbuf);
GuOut* out = gu_string_buf_out(sbuf);
gu_putc('[', wtr, err);
gu_string_write(efun->fun, wtr, err);
gu_putc(']', wtr, err);
gu_putc('[', out, err);
gu_string_write(efun->fun, out, err);
gu_putc(']', out, err);
GuString s = gu_string_buf_freeze(sbuf, pool);
if (ccat != NULL) {
@@ -594,7 +594,7 @@ typedef struct PgfSimpleLin PgfSimpleLin;
struct PgfSimpleLin {
PgfLinFuncs* funcs;
int n_tokens;
GuWriter* wtr;
GuOut* out;
GuExn* err;
};
@@ -608,10 +608,10 @@ pgf_file_lzn_symbol_tokens(PgfLinFuncs** funcs, PgfTokens toks)
size_t len = gu_seq_length(toks);
for (size_t i = 0; i < len; i++) {
if (flin->n_tokens > 0)
gu_putc(' ', flin->wtr, flin->err);
gu_putc(' ', flin->out, flin->err);
PgfToken tok = gu_seq_get(toks, PgfToken, i);
gu_string_write(tok, flin->wtr, flin->err);
gu_string_write(tok, flin->out, flin->err);
flin->n_tokens++;
}
@@ -626,23 +626,23 @@ pgf_file_lzn_expr_literal(PgfLinFuncs** funcs, PgfLiteral lit)
}
if (flin->n_tokens > 0)
gu_putc(' ', flin->wtr, flin->err);
gu_putc(' ', flin->out, flin->err);
GuVariantInfo i = gu_variant_open(lit);
switch (i.tag) {
case PGF_LITERAL_STR: {
PgfLiteralStr* lstr = i.data;
gu_string_write(lstr->val, flin->wtr, flin->err);
gu_string_write(lstr->val, flin->out, flin->err);
break;
}
case PGF_LITERAL_INT: {
PgfLiteralInt* lint = i.data;
gu_printf(flin->wtr, flin->err, "%d", lint->val);
gu_printf(flin->out, flin->err, "%d", lint->val);
break;
}
case PGF_LITERAL_FLT: {
PgfLiteralFlt* lflt = i.data;
gu_printf(flin->wtr, flin->err, "%lf", lflt->val);
gu_printf(flin->out, flin->err, "%lf", lflt->val);
break;
}
default:
@@ -661,12 +661,12 @@ static PgfLinFuncs pgf_file_lin_funcs = {
void
pgf_lzr_linearize_simple(PgfConcr* concr, PgfCncTree ctree,
size_t lin_idx, GuWriter* wtr, GuExn* err)
size_t lin_idx, GuOut* out, GuExn* err)
{
PgfSimpleLin flin = {
.funcs = &pgf_file_lin_funcs,
.n_tokens = 0,
.wtr = wtr,
.out = out,
.err = err
};
pgf_lzr_linearize(concr, ctree, lin_idx, &flin.funcs);

View File

@@ -75,4 +75,4 @@ pgf_lzr_linearize(PgfConcr* concr, PgfCncTree ctree, size_t lin_idx,
/// Linearize a concrete syntax tree as space-separated tokens.
void
pgf_lzr_linearize_simple(PgfConcr* concr, PgfCncTree ctree,
size_t lin_idx, GuWriter* wtr, GuExn* err);
size_t lin_idx, GuOut* out, GuExn* err);

View File

@@ -1,4 +1,5 @@
#include <gu/read.h>
#include <gu/in.h>
#include <gu/utf8.h>
#include <pgf/parser.h>
#include <pgf/literals.h>
#include <wctype.h>
@@ -192,25 +193,25 @@ pgf_match_name_lit(PgfConcr* concr, PgfItem* item, PgfToken tok,
if (gu_string_eq(tok, hyp)) {
iscap = true;
} else if (!gu_string_eq(tok, gu_empty_string)) {
GuReader* rdr = gu_string_reader(tok, tmp_pool);
iscap = iswupper(gu_read_ucs(rdr, err));
GuIn* in = gu_string_in(tok, tmp_pool);
iscap = iswupper(gu_in_utf8(in, err));
}
size_t n_syms = gu_seq_length(seq);
if (!iscap && n_syms > 0) {
GuStringBuf *sbuf = gu_string_buf(tmp_pool);
GuWriter* wtr = gu_string_buf_writer(sbuf);
GuOut* out = gu_string_buf_out(sbuf);
for (size_t i = 0; i < n_syms; i++) {
if (i > 0)
gu_putc(' ', wtr, err);
gu_putc(' ', out, err);
PgfSymbol sym = gu_seq_get(seq, PgfSymbol, i);
gu_assert(gu_variant_tag(sym) == PGF_SYMBOL_KS);
PgfSymbolKS* sks = gu_variant_data(sym);
PgfToken tok = gu_seq_get(sks->tokens, PgfToken, 0);
gu_string_write(tok, wtr, err);
gu_string_write(tok, out, err);
}
PgfExprProb* ep = gu_new(PgfExprProb, pool);

View File

@@ -1828,7 +1828,7 @@ pgf_get_tokens(PgfSequence seq,
GuPool* tmp_pool = gu_new_pool();
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
GuWriter* wtr = gu_string_buf_writer(sbuf);
GuOut* out = gu_string_buf_out(sbuf);
// collect the tokens in the production
size_t len = gu_seq_length(seq);
@@ -1842,11 +1842,11 @@ pgf_get_tokens(PgfSequence seq,
size_t len = gu_seq_length(symks->tokens);
for (size_t i = tok_idx; i < len; i++) {
if (i > 0) {
gu_putc(' ', wtr, err);
gu_putc(' ', out, err);
}
PgfToken tok = gu_seq_get(symks->tokens, PgfToken, i);
gu_string_write(tok, wtr, err);
gu_string_write(tok, out, err);
}
tok_idx = 0;

View File

@@ -223,7 +223,7 @@ pgf_print_name(PgfConcr* concr, PgfCId id)
}
void
pgf_linearize(PgfConcr* concr, PgfExpr expr, GuWriter* wtr, GuExn* err)
pgf_linearize(PgfConcr* concr, PgfExpr expr, GuOut* out, GuExn* err)
{
GuPool* tmp_pool = gu_local_pool();
@@ -231,7 +231,7 @@ pgf_linearize(PgfConcr* concr, PgfExpr expr, GuWriter* wtr, GuExn* err)
pgf_lzr_concretize(concr, expr, tmp_pool);
PgfCncTree ctree = gu_next(cts, PgfCncTree, tmp_pool);
if (!gu_variant_is_null(ctree)) {
pgf_lzr_linearize_simple(concr, ctree, 0, wtr, err);
pgf_lzr_linearize_simple(concr, ctree, 0, out, err);
}
gu_pool_free(tmp_pool);

View File

@@ -118,7 +118,7 @@ GuString
pgf_print_name(PgfConcr*, PgfCId id);
void
pgf_linearize(PgfConcr* concr, PgfExpr expr, GuWriter* wtr, GuExn* err);
pgf_linearize(PgfConcr* concr, PgfExpr expr, GuOut* out, GuExn* err);
PgfExprEnum*
pgf_parse(PgfConcr* concr, PgfCId cat, PgfLexer *lexer,
@@ -166,6 +166,6 @@ pgf_generate_all(PgfPGF* pgf, PgfCId cat, GuPool* pool);
/// @}
void
pgf_print(PgfPGF* pgf, GuWriter* wtr, GuExn* err);
pgf_print(PgfPGF* pgf, GuOut* out, GuExn* err);
#endif // PGF_H_

View File

@@ -3,7 +3,7 @@
typedef struct {
GuMapItor fn;
GuWriter* wtr;
GuOut* out;
} PgfPrintFn;
void
@@ -13,13 +13,13 @@ pgf_print_flag(GuMapItor* fn, const void* key, void* value,
PgfPrintFn* clo = (PgfPrintFn*) fn;
PgfCId flag = *((PgfCId *) key);
PgfLiteral lit = *((PgfLiteral *) value);
GuWriter *wtr = clo->wtr;
GuOut *out = clo->out;
gu_puts(" flag ", wtr, err);
gu_string_write(flag, wtr, err);
gu_puts(" = ", wtr, err);
pgf_print_literal(lit, wtr, err);
gu_puts(";\n", wtr, err);
gu_puts(" flag ", out, err);
gu_string_write(flag, out, err);
gu_puts(" = ", out, err);
pgf_print_literal(lit, out, err);
gu_puts(";\n", out, err);
}
void
@@ -29,17 +29,17 @@ pgf_print_cat(GuMapItor* fn, const void* key, void* value,
PgfPrintFn* clo = (PgfPrintFn*) fn;
PgfCId name = *((PgfCId *) key);
PgfAbsCat *cat = *((PgfAbsCat **) value);
GuWriter *wtr = clo->wtr;
GuOut *out = clo->out;
gu_puts(" cat ", wtr, err);
gu_string_write(name, wtr, err);
gu_puts(" cat ", out, err);
gu_string_write(name, out, err);
PgfPrintContext* ctxt = NULL;
size_t n_hypos = gu_seq_length(cat->context);
for (size_t i = 0; i < n_hypos; i++) {
PgfHypo* hypo = gu_seq_index(cat->context, PgfHypo, i);
gu_putc(' ', wtr, err);
ctxt = pgf_print_hypo(hypo, ctxt, 4, wtr, err);
gu_putc(' ', out, err);
ctxt = pgf_print_hypo(hypo, ctxt, 4, out, err);
}
while (ctxt != NULL) {
@@ -48,7 +48,7 @@ pgf_print_cat(GuMapItor* fn, const void* key, void* value,
ctxt = next;
}
gu_printf(wtr, err, " ; -- %f\n",cat->meta_prob);
gu_printf(out, err, " ; -- %f\n",cat->meta_prob);
}
void
@@ -58,31 +58,31 @@ pgf_print_absfun(GuMapItor* fn, const void* key, void* value,
PgfPrintFn* clo = (PgfPrintFn*) fn;
PgfCId name = *((PgfCId *) key);
PgfAbsFun *fun = *((PgfAbsFun **) value);
GuWriter *wtr = clo->wtr;
GuOut *out = clo->out;
gu_puts(gu_seq_is_null(fun->defns) ? " data " : " fun ", wtr, err);
gu_string_write(name, wtr, err);
gu_puts(" : ", wtr, err);
pgf_print_type(fun->type, NULL, 0, wtr, err);
gu_printf(wtr, err, " ; -- %f\n", fun->ep.prob);
gu_puts(gu_seq_is_null(fun->defns) ? " data " : " fun ", out, err);
gu_string_write(name, out, err);
gu_puts(" : ", out, err);
pgf_print_type(fun->type, NULL, 0, out, err);
gu_printf(out, err, " ; -- %f\n", fun->ep.prob);
}
static void
pgf_print_abstract(PgfAbstr* abstr, GuWriter* wtr, GuExn* err)
pgf_print_abstract(PgfAbstr* abstr, GuOut* out, GuExn* err)
{
gu_puts("abstract ", wtr, err);
gu_string_write(abstr->name, wtr, err);
gu_puts(" {\n", wtr, err);
gu_puts("abstract ", out, err);
gu_string_write(abstr->name, out, err);
gu_puts(" {\n", out, err);
PgfPrintFn clo1 = { { pgf_print_flag }, wtr };
PgfPrintFn clo1 = { { pgf_print_flag }, out };
gu_map_iter(abstr->aflags, &clo1.fn, err);
PgfPrintFn clo2 = { { pgf_print_cat }, wtr };
PgfPrintFn clo2 = { { pgf_print_cat }, out };
gu_map_iter(abstr->cats, &clo2.fn, err);
PgfPrintFn clo3 = { { pgf_print_absfun }, wtr };
PgfPrintFn clo3 = { { pgf_print_absfun }, out };
gu_map_iter(abstr->funs, &clo3.fn, err);
gu_puts("}\n", wtr, err);
gu_puts("}\n", out, err);
}
static void
@@ -92,24 +92,24 @@ pgf_print_productions(GuMapItor* fn, const void* key, void* value,
PgfPrintFn* clo = (PgfPrintFn*) fn;
int fid = *((int *) key);
PgfCCat* ccat = *((PgfCCat**) value);
GuWriter *wtr = clo->wtr;
GuOut *out = clo->out;
if (!gu_seq_is_null(ccat->prods)) {
size_t n_prods = gu_seq_length(ccat->prods);
for (size_t i = 0; i < n_prods; i++) {
PgfProduction prod = gu_seq_get(ccat->prods, PgfProduction, i);
gu_printf(wtr,err," C%d -> ",fid);
gu_printf(out,err," C%d -> ",fid);
GuVariantInfo i = gu_variant_open(prod);
switch (i.tag) {
case PGF_PRODUCTION_APPLY: {
PgfProductionApply* papp = i.data;
gu_printf(wtr,err,"F%d[",papp->fun->funid);
gu_printf(out,err,"F%d[",papp->fun->funid);
size_t n_args = gu_seq_length(papp->args);
for (size_t j = 0; j < n_args; j++) {
if (j > 0)
gu_putc(',',wtr,err);
gu_putc(',',out,err);
PgfPArg arg = gu_seq_get(papp->args, PgfPArg, j);
@@ -117,20 +117,20 @@ pgf_print_productions(GuMapItor* fn, const void* key, void* value,
size_t n_hypos = gu_list_length(arg.hypos);
for (size_t k = 0; k < n_hypos; k++) {
if (k > 0)
gu_putc(' ',wtr,err);
gu_putc(' ',out,err);
PgfCCat *hypo = gu_list_index(arg.hypos, k);
gu_printf(wtr,err,"C%d",hypo->fid);
gu_printf(out,err,"C%d",hypo->fid);
}
}
gu_printf(wtr,err,"C%d",arg.ccat->fid);
gu_printf(out,err,"C%d",arg.ccat->fid);
}
gu_printf(wtr,err,"]\n");
gu_printf(out,err,"]\n");
break;
}
case PGF_PRODUCTION_COERCE: {
PgfProductionCoerce* pcoerce = i.data;
gu_printf(wtr,err,"_[C%d]\n",pcoerce->coerce->fid);
gu_printf(out,err,"_[C%d]\n",pcoerce->coerce->fid);
break;
}
default:
@@ -147,119 +147,119 @@ pgf_print_lindefs(GuMapItor* fn, const void* key, void* value,
PgfPrintFn* clo = (PgfPrintFn*) fn;
int fid = *((int *) key);
PgfCCat* ccat = *((PgfCCat**) value);
GuWriter *wtr = clo->wtr;
GuOut *out = clo->out;
if (ccat->lindefs != NULL) {
gu_printf(wtr,err," C%d -> ",fid);
gu_printf(out,err," C%d -> ",fid);
size_t n_lindefs = gu_list_length(ccat->lindefs);
for (size_t i = 0; i < n_lindefs; i++) {
if (i > 0) gu_putc(' ', wtr, err);
if (i > 0) gu_putc(' ', out, err);
PgfCncFun* fun = gu_list_index(ccat->lindefs, i);
gu_printf(wtr,err,"F%d",fun->funid);
gu_printf(out,err,"F%d",fun->funid);
}
gu_putc('\n', wtr,err);
gu_putc('\n', out,err);
}
}
static void
pgf_print_cncfun(PgfCncFun *cncfun, PgfSequences *sequences,
GuWriter *wtr, GuExn *err)
GuOut *out, GuExn *err)
{
gu_printf(wtr,err," F%d := (", cncfun->funid);
gu_printf(out,err," F%d := (", cncfun->funid);
size_t n_seqs = gu_list_length(sequences);
for (size_t i = 0; i < cncfun->n_lins; i++) {
if (i > 0) gu_putc(',', wtr, err);
if (i > 0) gu_putc(',', out, err);
PgfSequence seq = cncfun->lins[i];
for (size_t seqid = 0; seqid < n_seqs; seqid++) {
if (gu_seq_data(gu_list_index(sequences, seqid)) == gu_seq_data(seq)) {
gu_printf(wtr,err,"S%d", seqid);
gu_printf(out,err,"S%d", seqid);
break;
}
}
}
gu_puts(")", wtr, err);
gu_puts(")", out, err);
if (cncfun->absfun != NULL) {
gu_puts(" [", wtr, err);
gu_string_write(cncfun->absfun->name, wtr, err);
gu_puts("]", wtr, err);
gu_puts(" [", out, err);
gu_string_write(cncfun->absfun->name, out, err);
gu_puts("]", out, err);
}
gu_puts("\n", wtr, err);
gu_puts("\n", out, err);
}
static void
pgf_print_tokens(PgfTokens tokens, GuWriter *wtr, GuExn *err)
pgf_print_tokens(PgfTokens tokens, GuOut *out, GuExn *err)
{
gu_putc('"', wtr, err);
gu_putc('"', out, err);
size_t n_toks = gu_seq_length(tokens);
for (size_t i = 0; i < n_toks; i++) {
if (i > 0) gu_putc(' ', wtr, err);
if (i > 0) gu_putc(' ', out, err);
PgfToken tok = gu_seq_get(tokens, PgfToken, i);
gu_string_write(tok, wtr, err);
gu_string_write(tok, out, err);
}
gu_putc('"', wtr, err);
gu_putc('"', out, err);
}
void
pgf_print_symbol(PgfSymbol sym, GuWriter *wtr, GuExn *err)
pgf_print_symbol(PgfSymbol sym, GuOut *out, GuExn *err)
{
switch (gu_variant_tag(sym)) {
case PGF_SYMBOL_CAT: {
PgfSymbolCat* scat = gu_variant_data(sym);
gu_printf(wtr, err, "<%d,%d>", scat->d, scat->r);
gu_printf(out, err, "<%d,%d>", scat->d, scat->r);
break;
}
case PGF_SYMBOL_KS: {
PgfSymbolKS* sks = gu_variant_data(sym);
pgf_print_tokens(sks->tokens, wtr, err);
pgf_print_tokens(sks->tokens, out, err);
break;
}
case PGF_SYMBOL_KP: {
PgfSymbolKP* skp = gu_variant_data(sym);
gu_puts("pre {", wtr, err);
pgf_print_tokens(skp->default_form, wtr, err);
gu_puts("pre {", out, err);
pgf_print_tokens(skp->default_form, out, err);
for (size_t i = 0; i < skp->n_forms; i++) {
gu_puts("; ", wtr, err);
pgf_print_tokens(skp->forms[i].form, wtr, err);
gu_puts(" / ", wtr, err);
gu_puts("; ", out, err);
pgf_print_tokens(skp->forms[i].form, out, err);
gu_puts(" / ", out, err);
size_t n_prefixes = gu_list_length(skp->forms[i].prefixes);
for (size_t j = 0; j < n_prefixes; j++) {
if (j > 0) gu_putc(' ', wtr, err);
if (j > 0) gu_putc(' ', out, err);
GuString prefix = gu_list_index(skp->forms[i].prefixes, j);
gu_putc('"', wtr, err);
gu_string_write(prefix, wtr, err);
gu_putc('"', wtr, err);
gu_putc('"', out, err);
gu_string_write(prefix, out, err);
gu_putc('"', out, err);
}
}
gu_puts("}", wtr, err);
gu_puts("}", out, err);
break;
}
case PGF_SYMBOL_LIT: {
PgfSymbolLit *slit = gu_variant_data(sym);
gu_printf(wtr, err, "{%d,%d}", slit->d, slit->r);
gu_printf(out, err, "{%d,%d}", slit->d, slit->r);
break;
}
case PGF_SYMBOL_VAR: {
PgfSymbolVar *svar = gu_variant_data(sym);
gu_printf(wtr, err, "<%d,$%d>", svar->d, svar->r);
gu_printf(out, err, "<%d,$%d>", svar->d, svar->r);
break;
}
case PGF_SYMBOL_NE: {
gu_puts("nonExist", wtr, err);
gu_puts("nonExist", out, err);
break;
}
default:
@@ -268,19 +268,19 @@ pgf_print_symbol(PgfSymbol sym, GuWriter *wtr, GuExn *err)
}
static void
pgf_print_sequence(size_t seqid, PgfSequence seq, GuWriter *wtr, GuExn *err)
pgf_print_sequence(size_t seqid, PgfSequence seq, GuOut *out, GuExn *err)
{
gu_printf(wtr,err," S%d := ", seqid);
gu_printf(out,err," S%d := ", seqid);
int n_syms = gu_seq_length(seq);
for (int i = 0; i < n_syms; i++) {
if (i > 0) gu_putc(' ', wtr, err);
if (i > 0) gu_putc(' ', out, err);
PgfSymbol sym = gu_seq_get(seq, PgfSymbol, i);
pgf_print_symbol(sym, wtr, err);
pgf_print_symbol(sym, out, err);
}
gu_putc('\n', wtr, err);
gu_putc('\n', out, err);
}
static void
@@ -290,66 +290,66 @@ pgf_print_cnccat(GuMapItor* fn, const void* key, void* value,
PgfPrintFn* clo = (PgfPrintFn*) fn;
PgfCId name = *((PgfCId *) key);
PgfCncCat* cnccat = *((PgfCncCat**) value);
GuWriter *wtr = clo->wtr;
GuOut *out = clo->out;
gu_puts(" ", wtr, err);
gu_string_write(name, wtr, err);
gu_puts(" :=\n", wtr, err);
gu_puts(" ", out, err);
gu_string_write(name, out, err);
gu_puts(" :=\n", out, err);
PgfCCat *start = gu_list_index(cnccat->cats, 0);
PgfCCat *end = gu_list_index(cnccat->cats, gu_list_length(cnccat->cats)-1);
gu_printf(wtr, err, " range [C%d..C%d]\n", start->fid, end->fid);
gu_printf(out, err, " range [C%d..C%d]\n", start->fid, end->fid);
gu_puts(" labels [", wtr, err);
gu_puts(" labels [", out, err);
for (size_t i = 0; i < cnccat->n_lins; i++) {
if (i > 0) {
gu_puts("\n ", wtr, err);
gu_puts("\n ", out, err);
}
gu_string_write(cnccat->labels[i], wtr, err);
gu_string_write(cnccat->labels[i], out, err);
}
gu_puts("]\n", wtr, err);
gu_puts("]\n", out, err);
}
static void
pgf_print_concrete(PgfCId cncname, PgfConcr* concr,
GuWriter* wtr, GuExn* err)
GuOut* out, GuExn* err)
{
gu_puts("concrete ", wtr, err);
gu_string_write(cncname, wtr, err);
gu_puts(" {\n", wtr, err);
gu_puts("concrete ", out, err);
gu_string_write(cncname, out, err);
gu_puts(" {\n", out, err);
PgfPrintFn clo1 = { { pgf_print_flag }, wtr };
PgfPrintFn clo1 = { { pgf_print_flag }, out };
gu_map_iter(concr->cflags, &clo1.fn, err);
gu_puts(" productions\n", wtr, err);
PgfPrintFn clo2 = { { pgf_print_productions }, wtr };
gu_puts(" productions\n", out, err);
PgfPrintFn clo2 = { { pgf_print_productions }, out };
gu_map_iter(concr->ccats, &clo2.fn, err);
gu_puts(" lindefs\n", wtr, err);
PgfPrintFn clo3 = { { pgf_print_lindefs }, wtr };
gu_puts(" lindefs\n", out, err);
PgfPrintFn clo3 = { { pgf_print_lindefs }, out };
gu_map_iter(concr->ccats, &clo3.fn, err);
gu_puts(" lin\n", wtr, err);
gu_puts(" lin\n", out, err);
size_t n_funs = gu_list_length(concr->cncfuns);
for (size_t i = 0; i < n_funs; i++) {
PgfCncFun* cncfun = gu_list_index(concr->cncfuns, i);
pgf_print_cncfun(cncfun, concr->sequences, wtr, err);
pgf_print_cncfun(cncfun, concr->sequences, out, err);
}
gu_puts(" sequences\n", wtr, err);
gu_puts(" sequences\n", out, err);
size_t n_seqs = gu_list_length(concr->sequences);
for (size_t i = 0; i < n_seqs; i++) {
PgfSequence seq = gu_list_index(concr->sequences, i);
pgf_print_sequence(i, seq, wtr, err);
pgf_print_sequence(i, seq, out, err);
}
gu_puts(" categories\n", wtr, err);
PgfPrintFn clo4 = { { pgf_print_cnccat }, wtr };
gu_puts(" categories\n", out, err);
PgfPrintFn clo4 = { { pgf_print_cnccat }, out };
gu_map_iter(concr->cnccats, &clo4.fn, err);
gu_puts("}\n", wtr, err);
gu_puts("}\n", out, err);
}
static void
@@ -360,14 +360,14 @@ pgf_print_concr_cb(GuMapItor* fn, const void* key, void* value,
PgfCId cncname = *((PgfCId *) key);
PgfConcr *concr = *((PgfConcr **) value);
pgf_print_concrete(cncname, concr, clo->wtr, err);
pgf_print_concrete(cncname, concr, clo->out, err);
}
void
pgf_print(PgfPGF* pgf, GuWriter* wtr, GuExn* err)
pgf_print(PgfPGF* pgf, GuOut* out, GuExn* err)
{
pgf_print_abstract(&pgf->abstract, wtr, err);
pgf_print_abstract(&pgf->abstract, out, err);
PgfPrintFn clo = { { pgf_print_concr_cb }, wtr };
PgfPrintFn clo = { { pgf_print_concr_cb }, out };
gu_map_iter(pgf->concretes, &clo.fn, err);
}

View File

@@ -100,13 +100,13 @@ pgf_read_cid(PgfReader* rdr)
{
GuPool* tmp_pool = gu_new_pool();
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
GuWriter* wtr = gu_string_buf_writer(sbuf);
GuOut* out = gu_string_buf_out(sbuf);
size_t len = pgf_read_len(rdr);
for (size_t i = 0; i < len; i++) {
// CIds are in latin-1
GuUCS ucs = gu_in_u8(rdr->in, rdr->err);
gu_ucs_write(ucs, wtr, rdr->err);
gu_out_utf8(ucs, out, rdr->err);
}
GuString str = gu_string_buf_freeze(sbuf, tmp_pool);
@@ -120,13 +120,13 @@ pgf_read_string(PgfReader* rdr)
{
GuPool* tmp_pool = gu_new_pool();
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
GuWriter* wtr = gu_string_buf_writer(sbuf);
GuOut* out = gu_string_buf_out(sbuf);
GuLength len = pgf_read_len(rdr);
for (size_t i = 0; i < len; i++) {
GuUCS ucs = gu_in_utf8(rdr->in, rdr->err);
gu_ucs_write(ucs, wtr, rdr->err);
gu_out_utf8(ucs, out, rdr->err);
}
GuString str = gu_string_buf_freeze(sbuf, tmp_pool);
GuSymbol sym = gu_symtable_intern(rdr->symtab, str);

View File

@@ -1,6 +1,5 @@
#include <gu/variant.h>
#include <gu/map.h>
#include <gu/dump.h>
#include <gu/log.h>
#include <gu/enum.h>
#include <gu/file.h>
@@ -75,11 +74,6 @@ int main(int argc, char* argv[]) {
// Create an output stream for stdout
GuOut* out = gu_file_out(stdout, pool);
// Locale-encoding writers are currently unsupported
// GuWriter* wtr = gu_locale_writer(out, pool);
// Use a writer with hard-coded utf-8 encoding for now.
GuWriter* wtr = gu_new_utf8_writer(out, pool);
// We will keep the latest results in the 'ppool' and
// we will iterate over them by using 'result'.
GuPool* ppool = NULL;
@@ -118,8 +112,8 @@ int main(int argc, char* argv[]) {
clock_t start = clock();
GuReader *rdr = gu_string_reader(gu_str_string(line, ppool), ppool);
PgfLexer *lexer = pgf_new_simple_lexer(rdr, ppool);
GuIn *in = gu_string_in(gu_str_string(line, ppool), ppool);
PgfLexer *lexer = pgf_new_simple_lexer(in, ppool);
GuEnum* result = pgf_parse_with_heuristics(concr, cat, lexer, heuristics, ppool, ppool);
PgfExprProb* ep = NULL;
@@ -129,15 +123,15 @@ int main(int argc, char* argv[]) {
clock_t end = clock();
double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
gu_printf(wtr, err, "%d (%.0f ms): ", ctr, 1000.0 * cpu_time_used);
gu_printf(out, err, "%d (%.0f ms): ", ctr, 1000.0 * cpu_time_used);
if (ep != NULL) {
gu_printf(wtr, err, "[%.4f] (", ep->prob);
pgf_print_expr(ep->expr, NULL, 0, wtr, err);
gu_printf(wtr, err, ")\n");
gu_printf(out, err, "[%.4f] (", ep->prob);
pgf_print_expr(ep->expr, NULL, 0, out, err);
gu_printf(out, err, ")\n");
} else {
gu_printf(wtr, err, "---\n");
gu_printf(out, err, "---\n");
}
gu_writer_flush(wtr, err);
gu_out_flush(out, err);
}
fail:

View File

@@ -1,7 +1,6 @@
#include <pgf/pgf.h>
#include <pgf/data.h>
#include <gu/dump.h>
#include <gu/file.h>
#include <gu/utf8.h>
@@ -30,9 +29,8 @@ int main(int argc, char* argv[]) {
goto fail_read;
}
GuOut* out = gu_file_out(stdout, pool);
GuWriter* wtr = gu_new_utf8_writer(out, pool);
pgf_print(pgf, wtr, err);
gu_writer_flush(wtr, err);
pgf_print(pgf, out, err);
gu_out_flush(out, err);
fail_read:
gu_pool_free(pool);
return status;

View File

@@ -91,10 +91,9 @@ render(PgfPGF* pgf, PgfExpr expr, GuPool* pool)
/* Parent. */
FILE* fstream = fdopen(pc[1], "w");
GuOut* out = gu_file_out(fstream, pool);
GuWriter* wtr = gu_new_utf8_writer(out, pool);
GuExn* err = gu_new_exn(NULL, gu_kind(type), pool);
pgf_graphviz_abstract_tree(pgf, expr, wtr, err);
pgf_graphviz_abstract_tree(pgf, expr, out, err);
fclose(fstream);
close(cp[1]);
@@ -138,11 +137,11 @@ static void
linearize(PgfConcr* concr, PgfExpr expr, GuPool* pool)
{
GuStringBuf* sbuf = gu_string_buf(pool);
GuWriter* wtr = gu_string_buf_writer(sbuf);
GuOut* out = gu_string_buf_out(sbuf);
GuExn* err = gu_new_exn(NULL, gu_kind(type), pool);
pgf_linearize(concr, expr, wtr, err);
pgf_linearize(concr, expr, out, err);
GuString s = gu_string_buf_freeze(sbuf, pool);
put_gu_string(s);

View File

@@ -1,6 +1,5 @@
#include <gu/variant.h>
#include <gu/map.h>
#include <gu/dump.h>
#include <gu/log.h>
#include <gu/enum.h>
#include <gu/file.h>
@@ -17,12 +16,12 @@
static void
print_result(PgfExprProb* ep, PgfConcr* to_concr,
GuWriter* wtr, GuExn* err, GuPool* ppool)
GuOut* out, GuExn* err, GuPool* ppool)
{
// Write out the abstract syntax tree
gu_printf(wtr, err, " [%f] ", ep->prob);
pgf_print_expr(ep->expr, NULL, 0, wtr, err);
gu_putc('\n', wtr, err);
gu_printf(out, err, " [%f] ", ep->prob);
pgf_print_expr(ep->expr, NULL, 0, out, err);
gu_putc('\n', out, err);
// Enumerate the concrete syntax trees corresponding
// to the abstract tree.
@@ -33,12 +32,12 @@ print_result(PgfExprProb* ep, PgfConcr* to_concr,
if (gu_variant_is_null(ctree)) {
break;
}
gu_putc(' ', wtr, err);
gu_putc(' ', out, err);
// Linearize the concrete tree as a simple
// sequence of strings.
pgf_lzr_linearize_simple(to_concr , ctree, 0, wtr, err);
gu_putc('\n', wtr, err);
gu_writer_flush(wtr, err);
pgf_lzr_linearize_simple(to_concr , ctree, 0, out, err);
gu_putc('\n', out, err);
gu_out_flush(out, err);
}
}
@@ -100,11 +99,6 @@ int main(int argc, char* argv[]) {
// Create an output stream for stdout
GuOut* out = gu_file_out(stdout, pool);
// Locale-encoding writers are currently unsupported
// GuWriter* wtr = gu_locale_writer(out, pool);
// Use a writer with hard-coded utf-8 encoding for now.
GuWriter* wtr = gu_new_utf8_writer(out, pool);
// We will keep the latest results in the 'ppool' and
// we will iterate over them by using 'result'.
GuPool* ppool = NULL;
@@ -145,7 +139,7 @@ int main(int argc, char* argv[]) {
goto fail_parse;
}
print_result(ep, to_concr, wtr, err, ppool);
print_result(ep, to_concr, out, err, ppool);
}
continue;
}
@@ -161,10 +155,10 @@ int main(int argc, char* argv[]) {
// sentence, so our memory usage doesn't increase over time.
ppool = gu_new_pool();
GuReader *rdr =
gu_string_reader(gu_str_string(line, ppool), ppool);
GuIn *in =
gu_string_in(gu_str_string(line, ppool), ppool);
PgfLexer *lexer =
pgf_new_simple_lexer(rdr, ppool);
pgf_new_simple_lexer(in, ppool);
clock_t start = clock();
@@ -175,11 +169,11 @@ int main(int argc, char* argv[]) {
pgf_lexer_current_token(lexer);
if (gu_string_eq(tok, gu_empty_string))
gu_puts("Couldn't begin parsing", wtr, err);
gu_puts("Couldn't begin parsing", out, err);
else {
gu_puts("Unexpected token: \"", wtr, err);
gu_string_write(tok, wtr, err);
gu_puts("\"\n", wtr, err);
gu_puts("Unexpected token: \"", out, err);
gu_string_write(tok, out, err);
gu_puts("\"\n", out, err);
}
goto fail_parse;
@@ -196,7 +190,7 @@ int main(int argc, char* argv[]) {
goto fail_parse;
}
print_result(ep, to_concr, wtr, err, ppool);
print_result(ep, to_concr, out, err, ppool);
continue;
fail_parse:

View File

@@ -1,6 +1,7 @@
#include <pgf/pgf.h>
#include <gu/mem.h>
#include <gu/exn.h>
#include <gu/utf8.h>
#include <alloca.h>
#include <jni.h>
@@ -206,8 +207,8 @@ Java_org_grammaticalframework_pgf_Parser_parse
GuString startCat = j2gu_string(env, jstartCat, pool);
GuString s = j2gu_string(env, js, pool);
GuReader* rdr = gu_string_reader(s, pool);
PgfLexer *lexer = pgf_new_simple_lexer(rdr, pool);
GuIn* in = gu_string_in(s, pool);
PgfLexer *lexer = pgf_new_simple_lexer(in, pool);
GuEnum* res =
pgf_parse(get_ref(env, concr), startCat, lexer, pool, out_pool);
@@ -261,9 +262,9 @@ Java_org_grammaticalframework_pgf_Concr_linearize(JNIEnv* env, jobject self, job
GuPool* tmp_pool = gu_local_pool();
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
GuWriter* wtr = gu_string_buf_writer(sbuf);
GuOut* out = gu_string_buf_out(sbuf);
pgf_linearize(get_ref(env, self), gu_variant_from_ptr((void*) get_ref(env, jexpr)), wtr, err);
pgf_linearize(get_ref(env, self), gu_variant_from_ptr((void*) get_ref(env, jexpr)), out, err);
if (!gu_ok(err)) {
//
return NULL;
@@ -290,9 +291,9 @@ Java_org_grammaticalframework_pgf_Expr_showExpr(JNIEnv* env, jclass clazz, jlong
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
GuWriter* wtr = gu_string_buf_writer(sbuf);
GuOut* out = gu_string_buf_out(sbuf);
pgf_print_expr(gu_variant_from_ptr(l2p(ref)), NULL, 0, wtr, err);
pgf_print_expr(gu_variant_from_ptr(l2p(ref)), NULL, 0, out, err);
GuString str = gu_string_buf_freeze(sbuf, tmp_pool);
jstring jstr = gu2j_string(env, str);

View File

@@ -124,9 +124,9 @@ Expr_repr(ExprObject *self)
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
GuWriter* wtr = gu_string_buf_writer(sbuf);
GuOut* out = gu_string_buf_out(sbuf);
pgf_print_expr(self->expr, NULL, 0, wtr, err);
pgf_print_expr(self->expr, NULL, 0, out, err);
GuString str = gu_string_buf_freeze(sbuf, tmp_pool);
PyObject* pystr = gu2py_string(str);
@@ -670,9 +670,9 @@ Type_repr(TypeObject *self)
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
GuWriter* wtr = gu_string_buf_writer(sbuf);
GuOut* out = gu_string_buf_out(sbuf);
pgf_print_type(self->type, NULL, 0, wtr, err);
pgf_print_type(self->type, NULL, 0, out, err);
GuString str = gu_string_buf_freeze(sbuf, tmp_pool);
PyObject* pystr = gu2py_string(str);
@@ -1194,8 +1194,7 @@ Concr_parse(ConcrObject* self, PyObject *args, PyObject *keywds)
PgfLexer *lexer = NULL;
if (buf != NULL) {
GuIn* in = gu_data_in(buf, len, pyres->pool);
GuReader* rdr = gu_new_utf8_reader(in, pyres->pool);
lexer = pgf_new_simple_lexer(rdr, pyres->pool);
lexer = pgf_new_simple_lexer(in, pyres->pool);
}
if (py_lexer != NULL) {
lexer = pypgf_new_python_lexer(py_lexer, pyres->pool);
@@ -1288,8 +1287,7 @@ Concr_complete(ConcrObject* self, PyObject *args, PyObject *keywds)
PgfLexer *lexer = NULL;
if (buf != NULL) {
GuIn* in = gu_data_in(buf, len, tmp_pool);
GuReader* rdr = gu_new_utf8_reader(in, tmp_pool);
lexer = pgf_new_simple_lexer(rdr, tmp_pool);
lexer = pgf_new_simple_lexer(in, tmp_pool);
}
if (py_lexer != NULL) {
lexer = pypgf_new_python_lexer(py_lexer, tmp_pool);
@@ -1376,9 +1374,9 @@ Concr_linearize(ConcrObject* self, PyObject *args)
GuPool* tmp_pool = gu_local_pool();
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
GuWriter* wtr = gu_string_buf_writer(sbuf);
GuOut* out = gu_string_buf_out(sbuf);
pgf_linearize(self->concr, pyexpr->expr, wtr, err);
pgf_linearize(self->concr, pyexpr->expr, out, err);
if (!gu_ok(err)) {
PyErr_SetString(PGFError, "The abstract tree cannot be linearized");
return NULL;
@@ -1670,9 +1668,9 @@ Concr_graphvizParseTree(ConcrObject* self, PyObject *args) {
GuPool* tmp_pool = gu_local_pool();
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
GuWriter* wtr = gu_string_buf_writer(sbuf);
GuOut* out = gu_string_buf_out(sbuf);
pgf_graphviz_parse_tree(self->concr, pyexpr->expr, wtr, err);
pgf_graphviz_parse_tree(self->concr, pyexpr->expr, out, err);
if (!gu_ok(err)) {
PyErr_SetString(PGFError, "The parse tree cannot be visualized");
return NULL;
@@ -1733,8 +1731,7 @@ Concr_lookupMorpho(ConcrObject* self, PyObject *args, PyObject *keywds) {
PgfLexer *lexer = NULL;
if (buf != NULL) {
GuIn* in = gu_data_in(buf, len, tmp_pool);
GuReader* rdr = gu_new_utf8_reader(in, tmp_pool);
lexer = pgf_new_simple_lexer(rdr, tmp_pool);
lexer = pgf_new_simple_lexer(in, tmp_pool);
}
if (py_lexer != NULL) {
// get an iterator out of the iterable object
@@ -2237,9 +2234,9 @@ PGF_graphvizAbstractTree(PGFObject* self, PyObject *args) {
GuPool* tmp_pool = gu_local_pool();
GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool);
GuStringBuf* sbuf = gu_string_buf(tmp_pool);
GuWriter* wtr = gu_string_buf_writer(sbuf);
GuOut* out = gu_string_buf_out(sbuf);
pgf_graphviz_abstract_tree(self->pgf, pyexpr->expr, wtr, err);
pgf_graphviz_abstract_tree(self->pgf, pyexpr->expr, out, err);
if (!gu_ok(err)) {
PyErr_SetString(PGFError, "The abstract tree cannot be visualized");
return NULL;