diff options
author | Akim Demaille <akim.demaille@gmail.com> | 2020-01-22 22:29:09 +0100 |
---|---|---|
committer | Akim Demaille <akim.demaille@gmail.com> | 2020-01-23 08:26:33 +0100 |
commit | 46ab1d0cbe69b429e2e5f1934faa53d1de46620d (patch) | |
tree | 3ddc3a67328470b94e7b7c521f5236042d0500c0 /src/parse-gram.y | |
parent | f54a5b303bd3508aecbc79968bd61a9fc6167e00 (diff) | |
download | bison-46ab1d0cbe69b429e2e5f1934faa53d1de46620d.tar.gz |
diagnostics: report syntax errors in color
* src/parse-gram.y (parse.error): Set to 'custom'.
(yyreport_syntax_error): New.
* data/bison-default.css (.expected, .unexpected): New.
* tests/diagnostics.at: Adjust.
Diffstat (limited to 'src/parse-gram.y')
-rw-r--r-- | src/parse-gram.y | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/src/parse-gram.y b/src/parse-gram.y index 30b2bef1..3ed07a31 100644 --- a/src/parse-gram.y +++ b/src/parse-gram.y @@ -125,7 +125,7 @@ %define api.token.raw %define api.value.type union %define locations -%define parse.error detailed +%define parse.error custom %define parse.lac full %define parse.trace %defines @@ -801,6 +801,65 @@ 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); + return 0; +} + + /* Return the location of the left-hand side of a rule whose right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in the right-hand side, and return an empty location equal to the end |