summaryrefslogtreecommitdiff
path: root/src/runtime/c/pgf
diff options
context:
space:
mode:
authorkrangelov <kr.angelov@gmail.com>2019-06-24 12:13:14 +0200
committerkrangelov <kr.angelov@gmail.com>2019-06-24 12:13:14 +0200
commit2d9478b97332db11980ed12b1ab0b892ab95cfff (patch)
treebb6bd3979a5896f87487929382ed62cfaac71242 /src/runtime/c/pgf
parent8c18d7162fc197b45a0292e93f29116c918b9d2f (diff)
share some code for printing
Diffstat (limited to 'src/runtime/c/pgf')
-rw-r--r--src/runtime/c/pgf/parser.c72
-rw-r--r--src/runtime/c/pgf/printer.c111
2 files changed, 75 insertions, 108 deletions
diff --git a/src/runtime/c/pgf/parser.c b/src/runtime/c/pgf/parser.c
index ec623253d..3f67a4988 100644
--- a/src/runtime/c/pgf/parser.c
+++ b/src/runtime/c/pgf/parser.c
@@ -281,73 +281,13 @@ pgf_item_symbols(PgfItem* item,
}
}
-static void
+PGF_INTERNAL void
pgf_print_production_args(PgfPArgs* args,
- GuOut* out, GuExn* err)
-{
- size_t n_args = gu_seq_length(args);
- for (size_t j = 0; j < n_args; j++) {
- if (j > 0)
- gu_putc(',',out,err);
-
- PgfPArg arg = gu_seq_get(args, PgfPArg, j);
-
- if (arg.hypos != NULL &&
- gu_seq_length(arg.hypos) > 0) {
- size_t n_hypos = gu_seq_length(arg.hypos);
- for (size_t k = 0; k < n_hypos; k++) {
- PgfCCat *hypo = gu_seq_get(arg.hypos, PgfCCat*, k);
- pgf_print_fid(hypo->fid, out, err);
- gu_putc(' ',out,err);
- }
- gu_puts("-> ",out,err);
- }
-
- pgf_print_fid(arg.ccat->fid, out, err);
- }
-}
+ GuOut* out, GuExn* err);
-static void
-pgf_print_production(int fid, PgfProduction prod,
- GuOut *out, GuExn* err, GuPool* pool)
-{
- pgf_print_fid(fid, out, err);
- gu_puts(" -> ", out, err);
-
- GuVariantInfo i = gu_variant_open(prod);
- switch (i.tag) {
- case PGF_PRODUCTION_APPLY: {
- PgfProductionApply* papp = i.data;
- gu_printf(out,err,"F%d(",papp->fun->funid);
- if (papp->fun->ep != NULL) {
- pgf_print_expr(papp->fun->ep->expr, NULL, 0, out, err);
- } else {
- PgfPArg* parg = gu_seq_index(papp->args, PgfPArg, 0);
- gu_printf(out,err,"linref %s", parg->ccat->cnccat->abscat->name);
- }
- gu_printf(out,err,")[");
- pgf_print_production_args(papp->args,out,err);
- gu_printf(out,err,"]\n");
- break;
- }
- case PGF_PRODUCTION_COERCE: {
- PgfProductionCoerce* pcoerce = i.data;
- gu_puts("_[",out,err);
- pgf_print_fid(pcoerce->coerce->fid, out, err);
- gu_printf("]\n",out,err);
- break;
- }
- case PGF_PRODUCTION_EXTERN: {
- PgfProductionExtern* pext = i.data;
- gu_printf(out,err,"<extern>(");
- pgf_print_expr(pext->ep->expr, NULL, 0, out, err);
- gu_printf(out,err,")[]\n");
- break;
- }
- default:
- gu_impossible();
- }
-}
+PGF_INTERNAL void
+pgf_print_production(int fid, PgfProduction prod,
+ GuOut *out, GuExn* err);
static void
pgf_print_item_seq(PgfItem *item,
@@ -966,7 +906,7 @@ pgf_parsing_complete(PgfParsing* ps, PgfItem* item, PgfExprProb *ep)
pgf_print_fid(ccat->fid, out, err);
gu_puts("]\n", out, err);
}
- pgf_print_production(ccat->fid, prod, out, err, tmp_pool);
+ pgf_print_production(ccat->fid, prod, out, err);
gu_pool_free(tmp_pool);
#endif
diff --git a/src/runtime/c/pgf/printer.c b/src/runtime/c/pgf/printer.c
index 153130b54..dd21072ce 100644
--- a/src/runtime/c/pgf/printer.c
+++ b/src/runtime/c/pgf/printer.c
@@ -94,6 +94,74 @@ pgf_print_fid(int fid, GuOut* out, GuExn* err)
gu_printf(out, err, "C%d", fid);
}
+PGF_INTERNAL void
+pgf_print_production_args(PgfPArgs* args,
+ GuOut* out, GuExn* err)
+{
+ size_t n_args = gu_seq_length(args);
+ for (size_t j = 0; j < n_args; j++) {
+ if (j > 0)
+ gu_putc(',',out,err);
+
+ PgfPArg arg = gu_seq_get(args, PgfPArg, j);
+
+ if (arg.hypos != NULL &&
+ gu_seq_length(arg.hypos) > 0) {
+ size_t n_hypos = gu_seq_length(arg.hypos);
+ for (size_t k = 0; k < n_hypos; k++) {
+ PgfCCat *hypo = gu_seq_get(arg.hypos, PgfCCat*, k);
+ pgf_print_fid(hypo->fid, out, err);
+ gu_putc(' ',out,err);
+ }
+ gu_puts("-> ",out,err);
+ }
+
+ pgf_print_fid(arg.ccat->fid, out, err);
+ }
+}
+
+PGF_INTERNAL void
+pgf_print_production(int fid, PgfProduction prod,
+ GuOut *out, GuExn* err)
+{
+ pgf_print_fid(fid, out, err);
+ gu_puts(" -> ", out, err);
+
+ GuVariantInfo i = gu_variant_open(prod);
+ switch (i.tag) {
+ case PGF_PRODUCTION_APPLY: {
+ PgfProductionApply* papp = i.data;
+ gu_printf(out,err,"F%d(",papp->fun->funid);
+ if (papp->fun->ep != NULL) {
+ pgf_print_expr(papp->fun->ep->expr, NULL, 0, out, err);
+ } else {
+ PgfPArg* parg = gu_seq_index(papp->args, PgfPArg, 0);
+ gu_printf(out,err,"linref %s", parg->ccat->cnccat->abscat->name);
+ }
+ gu_printf(out,err,")[");
+ pgf_print_production_args(papp->args,out,err);
+ gu_printf(out,err,"]\n");
+ break;
+ }
+ case PGF_PRODUCTION_COERCE: {
+ PgfProductionCoerce* pcoerce = i.data;
+ gu_puts("_[",out,err);
+ pgf_print_fid(pcoerce->coerce->fid, out, err);
+ gu_puts("]\n",out,err);
+ break;
+ }
+ case PGF_PRODUCTION_EXTERN: {
+ PgfProductionExtern* pext = i.data;
+ gu_printf(out,err,"<extern>(");
+ pgf_print_expr(pext->ep->expr, NULL, 0, out, err);
+ gu_printf(out,err,")[]\n");
+ break;
+ }
+ default:
+ gu_impossible();
+ }
+}
+
static void
pgf_print_productions(GuMapItor* fn, const void* key, void* value,
GuExn* err)
@@ -107,48 +175,7 @@ pgf_print_productions(GuMapItor* fn, const void* key, void* value,
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_puts(" ", out, err);
- pgf_print_fid(fid, out, err);
- gu_puts(" -> ", out, err);
-
- GuVariantInfo i = gu_variant_open(prod);
- switch (i.tag) {
- case PGF_PRODUCTION_APPLY: {
- PgfProductionApply* papp = i.data;
- 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(',',out,err);
-
- PgfPArg arg = gu_seq_get(papp->args, PgfPArg, j);
-
- if (arg.hypos != NULL) {
- size_t n_hypos = gu_seq_length(arg.hypos);
- for (size_t k = 0; k < n_hypos; k++) {
- if (k > 0)
- gu_putc(' ',out,err);
- PgfCCat *hypo = gu_seq_get(arg.hypos, PgfCCat*, k);
- pgf_print_fid(hypo->fid, out, err);
- }
- }
-
- pgf_print_fid(arg.ccat->fid, out, err);
- }
- gu_printf(out,err,"]\n");
- break;
- }
- case PGF_PRODUCTION_COERCE: {
- PgfProductionCoerce* pcoerce = i.data;
- gu_puts("_[", out, err);
- pgf_print_fid(pcoerce->coerce->fid, out, err);
- gu_puts("]\n", out, err);
- break;
- }
- default:
- gu_impossible();
- }
+ pgf_print_production(fid, prod, out, err);
}
}
}