intellij-plugin-v4 icon indicating copy to clipboard operation
intellij-plugin-v4 copied to clipboard

Incorrect alternative label displayed for grammar rule

Open markkrijgsman opened this issue 7 years ago • 3 comments

It seems that the plugin does not visualize the matching alternative label for a rule correctly. Take the following grammar:

grammar EverythingAndGrammar;

query       : expression;

expression  : expression 'AND' expression   #AndExpr
            | TERM+                         #LeafExpr;

TERM        : [a-z]+;

WS          : (' ' | '\t' | '\n' | '\r')+ -> skip;

Input: term term

Expected behaviour: expected-behaviour

Actual behaviour: actual-behaviour

However, when I implement a Listener for this grammar, the LeafExpr methods are called and not the AndExpr methods (as expected).

markkrijgsman avatar Nov 28 '18 09:11 markkrijgsman

@parrt it looks like in this case, org.antlr.v4.tool.GrammarInterpreterRuleContext#outerAltNum is not updated and keeps its default value (1), which unfortunately corresponds to the AndExpr alternative. I'm not familiar enough with ANTLR's internals to understand what's going on, though :(

bjansen avatar Dec 20 '18 21:12 bjansen

Hmm...I wonder if that's a bug or whether I never made the interpreter update those values. It seems like since it displays a different alternative I expected this to work. I guess I will have to look at it sometime. thanks for looking

parrt avatar Dec 21 '18 00:12 parrt

In some cases it works as expected. If we add a third 'OrExpr' alt to the grammar, the interpreter will correctly update the value when the input is an OR expr. It just never gets updated when the input text is just a TERM. Maybe because it doesn't have any named rule?

bjansen avatar Dec 21 '18 07:12 bjansen