mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-14 14:53:03 +00:00
diagnostics: modernize bison's syntax errors
We used to display the unexpected token first:
$ bison foo.y
foo.y:1.8-13: error: syntax error, unexpected %token, expecting character literal or identifier or <tag>
1 | %token %token
| ^~~~~~
GCC uses a different format:
$ gcc-mp-9 foo.c
foo.c:1:5: error: expected identifier or '(' before ')' token
1 | int()()()
| ^
and so does Clang:
$ clang-mp-9.0 foo.c
foo.c:1:5: error: expected identifier or '('
int()()()
^
1 error generated.
They display the unexpected token last (or not at all). Also, they
don't waste width with "syntax error". Let's try that. It gives, for
the same example as above:
$ bison foo.y
foo.y:1.8-13: error: expected character literal or identifier or <tag> before %token
1 | %token %token
| ^~~~~~
* src/complain.h, src/complain.c (syntax_error): New.
* src/parse-gram.y (yyreport_syntax_error): Use it.
This commit is contained in:
@@ -804,58 +804,17 @@ epilogue.opt:
|
||||
int
|
||||
yyreport_syntax_error (const yyparse_context_t *ctx)
|
||||
{
|
||||
if (complaint_status < status_complaint)
|
||||
complaint_status = status_complaint;
|
||||
enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
|
||||
/* Internationalized format string. */
|
||||
const char *format = YY_NULLPTR;
|
||||
/* Arguments of format: reported tokens (one for the "unexpected",
|
||||
one per "expected"). */
|
||||
int arg[YYERROR_VERBOSE_ARGS_MAXIMUM];
|
||||
int n = yysyntax_error_arguments (ctx, arg, YYERROR_VERBOSE_ARGS_MAXIMUM);
|
||||
switch (n)
|
||||
{
|
||||
case -2:
|
||||
return 2;
|
||||
# define YYCASE_(N, S) \
|
||||
case N: \
|
||||
format = S; \
|
||||
break
|
||||
default: /* Avoid compiler warnings. */
|
||||
YYCASE_(0, YY_("syntax error"));
|
||||
YYCASE_(1, YY_("syntax error, unexpected %s"));
|
||||
YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s"));
|
||||
YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s"));
|
||||
YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s"));
|
||||
YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s"));
|
||||
# undef YYCASE_
|
||||
}
|
||||
location_print (*yyparse_context_location (ctx), stderr);
|
||||
fputs (": ", stderr);
|
||||
begin_use_class ("error", stderr);
|
||||
fputs ("error:", stderr);
|
||||
end_use_class ("error", stderr);
|
||||
fputc (' ', stderr);
|
||||
{
|
||||
int i = 0;
|
||||
while (*format)
|
||||
if (format[0] == '%' && format[1] == 's' && i < n)
|
||||
{
|
||||
const char *style = i == 0 ? "unexpected" : "expected";
|
||||
begin_use_class (style, stderr);
|
||||
fputs (yysymbol_name (arg[i]), stderr);
|
||||
end_use_class (style, stderr);
|
||||
format += 2;
|
||||
++i;
|
||||
}
|
||||
else
|
||||
{
|
||||
fputc (*format, stderr);
|
||||
++format;
|
||||
}
|
||||
}
|
||||
fputc ('\n', stderr);
|
||||
location_caret (*yyparse_context_location (ctx), "error", stderr);
|
||||
if (n == -2)
|
||||
return 2;
|
||||
const char *argv[YYERROR_VERBOSE_ARGS_MAXIMUM];
|
||||
for (int i = 0; i < n; ++i)
|
||||
argv[i] = yysymbol_name (arg[i]);
|
||||
syntax_error (*yyparse_context_location (ctx), n, argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user