Open bseib opened 1 year ago
@parrt I need help on this one. The code is basically this:
Token tokenUnderCursor = ParsingUtils.getTokenUnderCursor(previewState, offset);
Integer atnState = parser.inputTokenToStateMap.get(tokenUnderCursor);
Interval region = previewState.g.getStateToGrammarRegion(atnState);
CommonToken token = (CommonToken) previewState.g.tokenStream.get(region.a);
For some reason, atnState
refers to a state that is not in the grammar, but apparently in a internal (generated?) grammar that looks like this:
expr
: ( {} INT<tokenIndex=41>
| '('<tokenIndex=45> expr<tokenIndex=47> ')'<tokenIndex=49>
)
(
{precpred(_ctx, 4)}?<p=4> ('*'<tokenIndex=20>|'/'<tokenIndex=22>) expr<tokenIndex=25,p=5>
| {precpred(_ctx, 3)}?<p=3> ('+'<tokenIndex=32>|'-'<tokenIndex=34>) expr<tokenIndex=37,p=4>
)*
;
Then region
refers to a location in this grammar instead of the grammar being tested, that's what causes the exception.
I have no idea how to fix this.
wow. that's a tricky one and I don't have a huge amount of time to dig into this. I wonder if the grammar and text window are somehow out of sync or grammar is delayed getting updated. I think I saw that at some point.
Ah. yeah, most likely it has something to do with conversion of left recursive grammars. Anytime there is a left recursive grammar we convert it to a new version of the rules which is where the atnState will be referring to. going backwards to the location in the original grammar would be tricky but on guessing I have a mechanism to do that mapping if only to produce error messages from the command line. I guess we will need a check in the plugin looking for left recursive rules and, if so, look in the original grammar for the position not inthe generated grammar. I would start by poking around and seeing how I generate error messages from the command line for left recursive rules.
To reproduce this bug, quit IntelliJ and start up fresh. The bug is triggered only once, so the fresh startup is needed to see it the first time.
Using this grammar:
Open the ANTLR Preview (i.e. right click
prog
in the source, and choose "Test ruleprog
")Enter the following input:
Hold the CTRL key, and hover over the
+
character, and you should see the grammar rule tool tip appear saying#1 Type '+', Line 1:2
:While still holding the CTRL key in this spot, click the mouse button. An exception occurs with this stacktrace:
I'm using the latest IntelliJ: