-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to print better parse errors (#3700)
This PR fixes #3672 by providing a best-effort rendering of the partial state of the parser when a parse error happens. To do this, we grab the Earley state corresponding to the span `[0, token_index_before_error]` and inspect the partial parse trees available, as well as the production being attempted at this point. Consider as a simple example the following standard K definition for arithmetic expressions: ```k module ARITHMETIC-SYNTAX imports UNSIGNED-INT-SYNTAX imports ID-SYNTAX syntax Exp ::= Int | "-" Exp [group(neg), strict] | Exp "+" Exp [left, group(add), strict] | Exp "-" Exp [left, group(sub), strict] | Exp "*" Exp [left, group(mul), strict] | Exp "/" Exp [left, group(div), strict] | "(" Exp ")" [bracket] syntax priorities neg > mul div > add sub endmodule module ARITHMETIC imports ARITHMETIC-SYNTAX endmodule ``` On trying to `kast` an invalid term (for example, `1 * 2 + a * 4` - `a` lexes because we've imported `ID-SYNTAX`, but terms of sort `Id` can't appear in a valid derivation of `Exp`), we see the following message from the new component: ```console $ kast <(echo '1 * 2 + a * 4') --no-exc-wrap [Error] Inner Parser: Parse error: unexpected token 'a' following token '+'. Additional parsing diagnostic information: Attempting to apply production: syntax Exp ::= Exp "+" Exp produced partial term: `_+__ARITHMETIC-SYNTAX_Exp_Exp_Exp`(`_*__ARITHMETIC-SYNTAX_Exp_Exp_Exp`(#token("1","Int"),#token("2","Int")),#token("<error>","<invalid>")) Source(/dev/fd/11) Location(1,9,1,10) 1 | . ^ ``` we make a best-effort attempt to construct what the top-level `_+_` term might have looked like, placing an `<error>` token where we expected to see a valid term of sort `Exp`. The changes made in this PR are as follows: * Update the implementation of `EarleyParser` to conditionally perform the rendering as above. * Add boilerplate code to thread a new option `--debug-parse` through to the parser when appropriate. * Add a test case based on the above arithmetic module. I have additionally verified with @dkcumming that the implementation as reviewed produces useful information that would have been useful when addressing the original problem; these cases come from the MIR semantics and are therefore too large to include here as tests. --------- Co-authored-by: rv-jenkins <[email protected]>
- Loading branch information
1 parent
d18ea8f
commit cc3d089
Showing
14 changed files
with
156 additions
and
32 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
k-distribution/tests/regression-new/issue-3672-debugParse/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
DEF=test | ||
EXT=test | ||
TESTDIR=. | ||
KAST_FLAGS=--debug-parse | ||
PIPEFAIL= | ||
CHECK=$(CONSIDER_ERRORS) $(REMOVE_PATHS) | diff - | ||
|
||
include ../../../include/kframework/ktest.mak |
1 change: 1 addition & 0 deletions
1
k-distribution/tests/regression-new/issue-3672-debugParse/a.test.kast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 ++ 2 |
11 changes: 11 additions & 0 deletions
11
k-distribution/tests/regression-new/issue-3672-debugParse/a.test.kast.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[Error] Inner Parser: Parse error: unexpected token '+' following token '+'. | ||
Additional parsing diagnostic information: | ||
Attempting to apply production: | ||
syntax Exp ::= Exp "+" Exp | ||
produced partial term: | ||
`_+__TEST-SYNTAX_Exp_Exp_Exp`(#token("1","Int")) | ||
|
||
Source(a.test.kast) | ||
Location(1,4,1,5) | ||
1 | 1 ++ 2 | ||
. ^ |
1 change: 1 addition & 0 deletions
1
k-distribution/tests/regression-new/issue-3672-debugParse/b.test.kast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 + 5 * a / 2 |
11 changes: 11 additions & 0 deletions
11
k-distribution/tests/regression-new/issue-3672-debugParse/b.test.kast.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[Error] Inner Parser: Parse error: unexpected token 'a' following token '*'. | ||
Additional parsing diagnostic information: | ||
Attempting to apply production: | ||
syntax Exp ::= Exp "*" Exp | ||
produced partial term: | ||
`_*__TEST-SYNTAX_Exp_Exp_Exp`(`_+__TEST-SYNTAX_Exp_Exp_Exp`(#token("1","Int"),#token("5","Int"))) | ||
|
||
Source(b.test.kast) | ||
Location(1,9,1,10) | ||
1 | 1 + 5 * a / 2 | ||
. ^ |
21 changes: 21 additions & 0 deletions
21
k-distribution/tests/regression-new/issue-3672-debugParse/test.k
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) K Team. All Rights Reserved. | ||
|
||
module TEST-SYNTAX | ||
imports UNSIGNED-INT-SYNTAX | ||
imports ID-SYNTAX | ||
|
||
syntax Exp ::= Int | ||
| "-" Exp [group(neg)] | ||
| Exp "+" Exp [left, group(add)] | ||
| Exp "-" Exp [left, group(sub)] | ||
| Exp "*" Exp [left, group(mul)] | ||
| Exp "/" Exp [left, group(div)] | ||
| "(" Exp ")" [bracket] | ||
|
||
syntax priorities neg > mul div > add sub | ||
endmodule | ||
|
||
module TEST | ||
imports TEST-SYNTAX | ||
configuration <k> $PGM:Exp </k> | ||
endmodule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters