From b8c5e5609ffdb1f27aacab2209b6517817dce79d Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Mon, 20 Jul 2020 07:14:50 +0200 Subject: cex: label all the derivations by their initial action From input.y: warning: reduce/reduce conflict on token $end [-Wcounterexamples] Example: A b . First derivation a `-> A b . Second derivation a `-> A b `-> b . to input.y: warning: reduce/reduce conflict on token $end [-Wcounterexamples] Example: A b . First reduce derivation a `-> A b . Second reduce derivation a `-> A b `-> b . * src/counterexample.c (print_counterexample): here. Compute the width of the labels to properly align the values. * tests/conflicts.at, tests/counterexample.at, tests/diagnostics.at, * tests/report.at: Adjust. --- src/counterexample.c | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/counterexample.c b/src/counterexample.c index 7df75d64..9e89a5c0 100644 --- a/src/counterexample.c +++ b/src/counterexample.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -112,15 +113,37 @@ free_counterexample (counterexample *cex) free (cex); } +static int max (int a, int b) +{ + return a < b ? b : a; +} + static void print_counterexample (const counterexample *cex, FILE *out, const char *prefix) { const bool flat = getenv ("YYFLAT"); - fprintf (out, flat ? " %s%-20s " : " %s%s: ", - prefix, cex->unifying ? _("Example") : _("First example")); + const char *example1_label + = cex->unifying ? _("Example") : _("First example"); + const char *example2_label + = cex->unifying ? _("Example") : _("Second example"); + const char *deriv1_label + = cex->shift_reduce ? _("Shift derivation") : _("First reduce derivation"); + const char *deriv2_label + = cex->shift_reduce ? _("Reduce derivation") : _("Second reduce derivation"); + const int width = + max (max (mbswidth (example1_label, 0), mbswidth (example2_label, 0)), + max (mbswidth (deriv1_label, 0), mbswidth (deriv2_label, 0))); + if (flat) + fprintf (out, " %s%s%*s ", prefix, + example1_label, width - mbswidth (example1_label, 0), ""); + else + fprintf (out, " %s%s: ", prefix, example1_label); derivation_print_leaves (cex->d1, out); - fprintf (out, flat ? " %s%-20s " : " %s%s", - prefix, cex->shift_reduce ? _("Shift derivation") : _("First derivation")); + if (flat) + fprintf (out, " %s%s%*s ", prefix, + deriv1_label, width - mbswidth (deriv1_label, 0), ""); + else + fprintf (out, " %s%s", prefix, deriv1_label); derivation_print (cex->d1, out, prefix); // If we output to the terminal (via stderr) and we have color @@ -128,12 +151,18 @@ print_counterexample (const counterexample *cex, FILE *out, const char *prefix) // to see the differences. if (!cex->unifying || is_styled (stderr)) { - fprintf (out, flat ? " %s%-20s " : " %s%s: ", - prefix, cex->unifying ? _("Example") : _("Second example")); + if (flat) + fprintf (out, " %s%s%*s ", prefix, + example2_label, width - mbswidth (example2_label, 0), ""); + else + fprintf (out, " %s%s: ", prefix, example2_label); derivation_print_leaves (cex->d2, out); } - fprintf (out, flat ? " %s%-20s " : " %s%s", - prefix, cex->shift_reduce ? _("Reduce derivation") : _("Second derivation")); + if (flat) + fprintf (out, " %s%s%*s ", prefix, + deriv2_label, width - mbswidth (deriv2_label, 0), ""); + else + fprintf (out, " %s%s", prefix, deriv2_label); derivation_print (cex->d2, out, prefix); if (out != stderr) -- cgit v1.2.1