diagnostics: use libtextstyle for colored output

Bruno Haible released libtextstyle, a library for colored output based
on CSS.  Let's use it to generate colored diagnostics, provided
libtextstyle is available.

See
https://lists.gnu.org/archive/html/bug-gnulib/2019-01/msg00176.html
https://lists.gnu.org/archive/html/bison-patches/2019-02/msg00073.html
https://lists.gnu.org/archive/html/bison-patches/2019-02/msg00084.html
https://lists.gnu.org/archive/html/bison-patches/2019-03/msg00007.html

* bootstrap.conf (gnulib_modules): Use libtextstyle when possible.
* data/diagnostics.css: New.
* src/complain.c (begin_use_class, end_use_class, flush)
(severity_style, complain_init_color): New.
Use them.
* src/getargs.c (getargs_colors): New.
(getargs): Use it.
Skip --color and --style.
* src/location.h, src/location.c (location_print): Use a style.

* tests/bison.in: Force --color=yes when stderr is a tty.
* tests/local.at: Disable colors during the test suite.
* tests/input.at: Adjust expectations to the extra options passed on
the command line.
This commit is contained in:
Akim Demaille
2019-02-14 06:49:29 +01:00
parent 855fbf1c11
commit f6e38d7ac9
13 changed files with 203 additions and 25 deletions

View File

@@ -160,7 +160,7 @@ caret_free ()
}
void
location_caret (location loc, FILE *out)
location_caret (location loc, const char *style, FILE *out)
{
/* FIXME: find a way to support multifile locations, and only open once each
file. That would make the procedure future-proof. */
@@ -193,9 +193,17 @@ location_caret (location loc, FILE *out)
if (c != EOF)
{
/* Quote the file, indent by a single column. */
putc (' ', out);
fputc (' ', out);
int col = 0;
do
putc (c, out);
{
++col;
if (col == loc.start.column)
begin_use_class (style, out);
fputc (c, out);
if (col + 1 == loc.end.column)
end_use_class (style, out);
}
while ((c = getc (caret_info.source)) != EOF && c != '\n');
putc ('\n', out);
@@ -208,8 +216,10 @@ location_caret (location loc, FILE *out)
/* Print the carets (at least one), with the same indent as above.*/
fprintf (out, " %*s", loc.start.column - 1, "");
begin_use_class (style, out);
for (i = loc.start.column; i == loc.start.column || i < len; ++i)
putc (i == loc.start.column ? '^' : '~', out);
end_use_class (style, out);
}
putc ('\n', out);
}