a nicer pretty printing for the PGF format

This commit is contained in:
Krasimir Angelov
2017-08-22 21:13:21 +02:00
parent 82153bf271
commit 00388039f1
3 changed files with 74 additions and 41 deletions

View File

@@ -238,6 +238,12 @@ pgf_extern_syms_get(PgfItem* item, GuPool* pool)
return syms;
}
PGF_INTERNAL void
pgf_print_fid(int fid, GuOut* out, GuExn* err);
PGF_INTERNAL_DECL void
pgf_print_symbol(PgfSymbol sym, GuOut *out, GuExn *err);
#ifdef PGF_PARSER_DEBUG
static void
pgf_item_symbols(PgfItem* item,
@@ -291,12 +297,13 @@ pgf_print_production_args(PgfPArgs* args,
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);
gu_printf(out,err,"C%d ",hypo->fid);
pgf_print_fid(hypo->fid, out, err);
gu_putc(' ',out,err);
}
gu_printf(out,err,"-> ");
gu_puts("-> ",out,err);
}
gu_printf(out,err,"C%d",arg.ccat->fid);
pgf_print_fid(arg.ccat->fid, out, err);
}
}
@@ -304,7 +311,8 @@ static void
pgf_print_production(int fid, PgfProduction prod,
GuOut *out, GuExn* err, GuPool* pool)
{
gu_printf(out,err,"C%d -> ",fid);
pgf_print_fid(fid, out, err);
gu_puts(" -> ", out, err);
GuVariantInfo i = gu_variant_open(prod);
switch (i.tag) {
@@ -324,7 +332,9 @@ pgf_print_production(int fid, PgfProduction prod,
}
case PGF_PRODUCTION_COERCE: {
PgfProductionCoerce* pcoerce = i.data;
gu_printf(out,err,"_[C%d]\n",pcoerce->coerce->fid);
gu_puts("_[",out,err);
pgf_print_fid(pcoerce->coerce->fid, out, err);
gu_printf("]\n",out,err);
break;
}
case PGF_PRODUCTION_EXTERN: {
@@ -339,9 +349,6 @@ pgf_print_production(int fid, PgfProduction prod,
}
}
PGF_INTERNAL_DECL void
pgf_print_symbol(PgfSymbol sym, GuOut *out, GuExn *err);
static void
pgf_print_item_seq(PgfItem *item,
GuOut *out, GuExn* err, GuPool* pool)
@@ -376,9 +383,11 @@ pgf_print_range(PgfParseState* start, PgfParseState* end, GuOut* out, GuExn* err
static void
pgf_print_item(PgfItem* item, PgfParseState* state, GuOut* out, GuExn* err, GuPool* pool)
{
gu_printf(out, err, "[");
gu_putc('[', out, err);
pgf_print_range(item->conts->state, state, out, err);
gu_printf(out, err, "; C%d -> ", item->conts->ccat->fid);
gu_puts("; ", out, err);
pgf_print_fid(item->conts->ccat->fid, out, err);
gu_puts(" -> ", out, err);
GuVariantInfo i = gu_variant_open(item->prod);
switch (i.tag) {
@@ -398,8 +407,9 @@ pgf_print_item(PgfItem* item, PgfParseState* state, GuOut* out, GuExn* err, GuPo
break;
}
case PGF_PRODUCTION_COERCE: {
gu_printf(out, err, "_[C%d]; ",
gu_seq_index(item->args, PgfPArg, 0)->ccat->fid);
gu_puts("_[", out, err);
pgf_print_fid(gu_seq_index(item->args, PgfPArg, 0)->ccat->fid, out, err);
gu_puts("]; ", out, err);
break;
}
case PGF_PRODUCTION_EXTERN: {
@@ -976,10 +986,12 @@ pgf_parsing_complete(PgfParsing* ps, PgfItem* item, PgfExprProb *ep)
if (tmp_ccat == NULL) {
gu_printf(out, err, "[");
pgf_print_range(item->conts->state, ps->before, out, err);
gu_printf(out, err, "; C%d; %d; C%d]\n",
item->conts->ccat->fid,
item->conts->lin_idx,
ccat->fid);
gu_puts("; ", out, err);
pgf_print_fid(item->conts->ccat->fid, out, err);
gu_printf(out, err, "; %d; ",
item->conts->lin_idx);
pgf_print_fid(ccat->fid, out, err);
gu_puts("]\n", out, err);
}
pgf_print_production(ccat->fid, prod, out, err, tmp_pool);
gu_pool_free(tmp_pool);

View File

@@ -77,6 +77,23 @@ pgf_print_abstract(PgfAbstr* abstr, GuOut* out, GuExn* err)
gu_puts("}\n", out, err);
}
PGF_INTERNAL void
pgf_print_fid(int fid, GuOut* out, GuExn* err)
{
if (fid == -1)
gu_puts("CString", out, err);
else if (fid == -2)
gu_puts("CInt", out, err);
else if (fid == -3)
gu_puts("CFloat", out, err);
else if (fid == -4)
gu_puts("CVar", out, err);
else if (fid == -5)
gu_puts("CStart", out, err);
else
gu_printf(out, err, "C%d", fid);
}
static void
pgf_print_productions(GuMapItor* fn, const void* key, void* value,
GuExn* err)
@@ -91,7 +108,9 @@ pgf_print_productions(GuMapItor* fn, const void* key, void* value,
for (size_t i = 0; i < n_prods; i++) {
PgfProduction prod = gu_seq_get(ccat->prods, PgfProduction, i);
gu_printf(out,err," C%d -> ",fid);
gu_puts(" ", out, err);
pgf_print_fid(fid, out, err);
gu_puts(" -> ", out, err);
GuVariantInfo i = gu_variant_open(prod);
switch (i.tag) {
@@ -111,18 +130,20 @@ pgf_print_productions(GuMapItor* fn, const void* key, void* value,
if (k > 0)
gu_putc(' ',out,err);
PgfCCat *hypo = gu_seq_get(arg.hypos, PgfCCat*, k);
gu_printf(out,err,"C%d",hypo->fid);
pgf_print_fid(hypo->fid, out, err);
}
}
gu_printf(out,err,"C%d",arg.ccat->fid);
pgf_print_fid(arg.ccat->fid, out, err);
}
gu_printf(out,err,"]\n");
break;
}
case PGF_PRODUCTION_COERCE: {
PgfProductionCoerce* pcoerce = i.data;
gu_printf(out,err,"_[C%d]\n",pcoerce->coerce->fid);
gu_puts("_[", out, err);
pgf_print_fid(pcoerce->coerce->fid, out, err);
gu_puts("]\n", out, err);
break;
}
default:
@@ -142,17 +163,13 @@ pgf_print_lindefs(GuMapItor* fn, const void* key, void* value,
GuOut *out = clo->out;
if (ccat->lindefs != NULL) {
gu_printf(out,err," C%d -> ",fid);
size_t n_lindefs = gu_seq_length(ccat->lindefs);
for (size_t i = 0; i < n_lindefs; i++) {
if (i > 0) gu_putc(' ', out, err);
PgfCncFun* fun = gu_seq_get(ccat->lindefs, PgfCncFun*, i);
gu_printf(out,err,"F%d",fun->funid);
gu_puts(" ",out,err);
pgf_print_fid(fid, out, err);
gu_printf(out,err," -> F%d[CVar]\n",fun->funid);
}
gu_putc('\n', out,err);
}
}
@@ -166,17 +183,13 @@ pgf_print_linrefs(GuMapItor* fn, const void* key, void* value,
GuOut *out = clo->out;
if (ccat->linrefs != NULL) {
gu_puts(" ",out,err);
size_t n_linrefs = gu_seq_length(ccat->linrefs);
for (size_t i = 0; i < n_linrefs; i++) {
if (i > 0) gu_putc(' ', out, err);
PgfCncFun* fun = gu_seq_get(ccat->linrefs, PgfCncFun*, i);
gu_printf(out,err,"F%d",fun->funid);
gu_printf(out,err," CVar -> F%d[",fun->funid);
pgf_print_fid(fid, out, err);
gu_puts("]\n", out, err);
}
gu_printf(out,err," -> C%d\n",fid);
}
}
@@ -321,7 +334,11 @@ pgf_print_cnccat(GuMapItor* fn, const void* key, void* value,
PgfCCat *start = gu_seq_get(cnccat->cats, PgfCCat*, 0);
PgfCCat *end = gu_seq_get(cnccat->cats, PgfCCat*, gu_seq_length(cnccat->cats)-1);
gu_printf(out, err, " range [C%d..C%d]\n", start->fid, end->fid);
gu_puts(" range [", out, err);
pgf_print_fid(start->fid, out, err);
gu_puts("..", out, err);
pgf_print_fid(end->fid, out, err);
gu_puts("]\n", out, err);
gu_puts(" labels [", out, err);
for (size_t i = 0; i < cnccat->n_lins; i++) {

View File

@@ -47,9 +47,9 @@ ppCnc name cnc =
text "productions" $$
nest 2 (vcat [ppProduction (fcat,prod) | (fcat,set) <- IntMap.toList (productions cnc), prod <- Set.toList set]) $$
text "lindefs" $$
nest 2 (vcat (map ppFunList (IntMap.toList (lindefs cnc)))) $$
nest 2 (vcat (concatMap ppLinDefs (IntMap.toList (lindefs cnc)))) $$
text "linrefs" $$
nest 2 (vcat (map ppFunList (IntMap.toList (linrefs cnc)))) $$
nest 2 (vcat (concatMap ppLinRefs (IntMap.toList (linrefs cnc)))) $$
text "lin" $$
nest 2 (vcat (map ppCncFun (assocs (cncfuns cnc)))) $$
text "sequences" $$
@@ -75,8 +75,11 @@ ppProduction (fid,PConst _ _ ss) =
ppCncFun (funid,CncFun fun arr) =
ppFunId funid <+> text ":=" <+> parens (hcat (punctuate comma (map ppSeqId (elems arr)))) <+> brackets (ppCId fun)
ppFunList (fid,funids) =
ppFId fid <+> text "->" <+> hcat (punctuate comma (map ppFunId funids))
ppLinDefs (fid,funids) =
[ppFId fid <+> text "->" <+> ppFunId funid <> brackets (ppFId fidVar) | funid <- funids]
ppLinRefs (fid,funids) =
[ppFId fidVar <+> text "->" <+> ppFunId funid <> brackets (ppFId fid) | funid <- funids]
ppSeq (seqid,seq) =
ppSeqId seqid <+> text ":=" <+> hsep (map ppSymbol (elems seq))
@@ -109,6 +112,7 @@ ppFId fid
| fid == fidInt = text "CInt"
| fid == fidFloat = text "CFloat"
| fid == fidVar = text "CVar"
| fid == fidStart = text "CStart"
| otherwise = char 'C' <> int fid
ppFunId funid = char 'F' <> int funid