From 0715c1f7200ca6db6e21b78a29c5247868fbbc1a Mon Sep 17 00:00:00 2001 From: Pompolic <pompolic@special-circumstanc.es> Date: Thu, 6 Feb 2020 18:34:39 +0100 Subject: [PATCH] Improve pretty printer for TT_Dict --- pdf.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pdf.c b/pdf.c index ffac6b4..d5749a1 100644 --- a/pdf.c +++ b/pdf.c @@ -68,9 +68,10 @@ bytes_eq(HBytes b, const char *s) return strncmp(s, b.token, b.len) == 0 && b.len == strlen(s); } +/* This probably belongs next to Ref and XREntry, + * but dictentry() needs to know about this type */ typedef HCountedArray Dict; - /* a helper to look up a value in a dictionary */ const HParsedToken * dictentry(const Dict *dict, const char *key) @@ -165,14 +166,16 @@ pp_ref(FILE *stream, const HParsedToken *tok, int indent, int delta) fprintf(stream, "[%zu,%zu]", r->nr, r->gen); } -/* TODO XXX call pretty printer on each seq element */ -/* TODO: does the default behavior need to be overriden to only print the V: field? */ -/* TODO: booleans get printed as null */ +/* Can the default behavior be overriden to only print the V: field? */ +/* Warning: boolean keywords get printed as (null), + * since the LIT() macro parses them with h_ignore() */ void pp_dict(FILE *stream, const HParsedToken *tok, int indent, int delta) { if (tok->seq->used == 0) fprintf(stream, "{ }"); + /* If there is more than one key-value pair, print all but + * the last, separated by commas. */ else if(tok->seq->used > 1) { fprintf(stream, "{%*s", delta - 1, ""); @@ -183,12 +186,12 @@ pp_dict(FILE *stream, const HParsedToken *tok, int indent, int delta) h_pprint(stream, k_v->elements[0], indent + delta, delta); fprintf(stream, ":"); h_pprint(stream, k_v->elements[1], indent + delta, delta); - fprintf(stream, ","); + fprintf(stream, "\n%*s, ", indent, ""); } } - /* Special case: no comma at the end of last entry */ + /* Special case: the last entry doesn't have a comma after it */ const HCountedArray *k_v; k_v = tok->seq->elements[(tok->seq->used)-1]->seq; h_pprint(stream, k_v->elements[0], indent + delta, delta); @@ -412,8 +415,6 @@ act_dict(const HParseResult *p, void *env) { Dict *dict = H_ALLOC(Dict); - //dict = H_FIELD(Dict,0); - return H_MAKE(Dict, (void*)p->ast->seq); } -- GitLab