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:
Akim Demaille
2020-01-22 23:24:20 +01:00
parent 6fb362c87a
commit fc2191f137
5 changed files with 88 additions and 68 deletions

View File

@@ -273,7 +273,7 @@ AT_TEST([[Carriage return]],
[[input.y:10.8-11.0: <error>error:</error> missing '"' at end of line
10 | %token <error>"</error>
| <error>^</error>
input.y:10.8-11.0: <error>error:</error> syntax error, unexpected <unexpected>string</unexpected>, expecting <expected>character literal</expected> or <expected>identifier</expected> or <expected><tag></expected>
input.y:10.8-11.0: <error>error:</error> expected <expected>character literal</expected> or <expected>identifier</expected> or <expected><tag></expected> before <unexpected>string</unexpected>
10 | %token <error>"</error>
| <error>^</error>
]])

View File

@@ -102,7 +102,7 @@ input.y:6.1-17: error: invalid directive: '%a-does-not-exist'
input.y:7.1: error: invalid character: '%'
input.y:7.2: error: invalid character: '-'
input.y:8.1-9.0: error: missing '%}' at end of file
input.y:8.1-9.0: error: syntax error, unexpected %{...%}
input.y:8.1-9.0: error: unexpected %{...%}
]])
AT_CLEANUP
@@ -124,7 +124,7 @@ AT_DATA([input.y],
]])
AT_BISON_CHECK([input.y], [1], [],
[[input.y:3.1-15: error: syntax error, unexpected %initial-action, expecting {...}
[[input.y:3.1-15: error: expected {...} before %initial-action
]])
AT_CLEANUP
@@ -281,16 +281,16 @@ input.y:3.17-24: error: nonterminals cannot be given a string alias
input.y:4.8-10: error: character literals cannot be nonterminals
4 | %nterm '+' '*';
| ^~~
input.y:5.8-15: error: syntax error, unexpected string, expecting character literal or identifier or <tag>
input.y:5.8-15: error: expected character literal or identifier or <tag> before string
5 | %nterm "number";
| ^~~~~~~~
input.y:6.8-13: error: syntax error, unexpected string, expecting character literal or identifier or <tag>
input.y:6.8-13: error: expected character literal or identifier or <tag> before string
6 | %token "tok1" 1;
| ^~~~~~
input.y:7.14: error: syntax error, unexpected integer literal
input.y:7.14: error: unexpected integer literal
7 | %left "tok2" 2;
| ^
input.y:8.14: error: syntax error, unexpected integer literal
input.y:8.14: error: unexpected integer literal
8 | %type "tok3" 3;
| ^
]])
@@ -1277,7 +1277,7 @@ AT_SETUP([Torturing the Scanner])
AT_BISON_OPTION_PUSHDEFS
AT_DATA([input.y], [])
AT_BISON_CHECK([input.y], [1], [],
[[input.y:1.1: error: syntax error, unexpected end of file
[[input.y:1.1: error: unexpected end of file
]])
@@ -1285,7 +1285,7 @@ AT_DATA([input.y],
[{}
])
AT_BISON_CHECK([-fcaret input.y], [1], [],
[[input.y:1.1-2: error: syntax error, unexpected {...}
[[input.y:1.1-2: error: unexpected {...}
1 | {}
| ^~
]])
@@ -1655,7 +1655,7 @@ input.y:16.11-17.0: error: missing '"' at end of line
input.y:19.13-20.0: error: missing '}' at end of file
19 | %destructor { free ($$)
| ^~~~~~~~~~~
input.y:20.1: error: syntax error, unexpected end of file
input.y:20.1: error: unexpected end of file
]])
AT_CLEANUP