Skip to content

Commit

Permalink
Fix super_key failed predicate
Browse files Browse the repository at this point in the history
This is how one of the `selector` subrules looks like in the old parser:

    (DOT super_key)=>DOT { helper.emit($DOT, DroolsEditorType.SYMBOL); } super_key superSuffix

The whole rule is guarded by a syntactic predicate that is used to make
the decision whether this alternative is viable.

Syntactic predicates were removed in ANTLR 4 so this subrule became:

    DOT { helper.emit($DOT, DroolsEditorType.SYMBOL); } super_key superSuffix

The failing predicate is "hidden" inside the `super_key` rule but it actually
cannot be used to make the decision because it is now "invisible". See
https://github.com/antlr/antlr4/blob/master/doc/predicates.md#finding-visible-predicates
that explains how predicates can become invisible. The thing that makes
the predicate invisible here is the action after `DOT`. And since the
alternative is now unguarded it seems viable and so it is taken and
the `super_key` rule is evaluated.

At this point, inside `super_key` there is only a single alternative. It
is guarded by a predicate that evaluates to `false`, which is an error
condition.

The fix is to restore the guard in front of this `selector` alternative
that prevents taking should the super key predicate later turn out to be
false.

Fixes #5703.
  • Loading branch information
yurloc committed Feb 27, 2024
1 parent 7f66e83 commit b26c82a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,23 @@ public void testNestedExpression() throws Exception {
assertThat(left.getExpression()).isEqualTo("a");
assertThat(right.getExpression()).isEqualTo("b");
}

@Test
public void testChainOfMethodCallInConstraint() {
String source = "getAddress().getCity().length() == 5";
ConstraintConnectiveDescr result = parser.parse( source );
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND);
assertThat(result.getDescrs().size()).isEqualTo(1);

RelationalExprDescr expr = (RelationalExprDescr) result.getDescrs().get( 0 );
assertThat(expr.getOperator()).isEqualTo("==");

AtomicExprDescr left = (AtomicExprDescr) expr.getLeft();
AtomicExprDescr right = (AtomicExprDescr) expr.getRight();

assertThat(left.getExpression()).isEqualTo("getAddress().getCity().length()");
assertThat(right.getExpression()).isEqualTo("5");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ explicitGenericInvocationSuffix
;

selector
: DOT { helper.emit($DOT, DroolsEditorType.SYMBOL); } super_key superSuffix
: { _input.LA(1) == DRLLexer.DOT && helper.validateIdentifierKey(DroolsSoftKeywords.SUPER) }? DOT { helper.emit($DOT, DroolsEditorType.SYMBOL); } super_key superSuffix
| DOT { helper.emit($DOT, DroolsEditorType.SYMBOL); } new_key (nonWildcardTypeArguments)? innerCreator
| DOT { helper.emit($DOT, DroolsEditorType.SYMBOL); }
IDENTIFIER { helper.emit($IDENTIFIER, DroolsEditorType.IDENTIFIER); }
Expand Down

0 comments on commit b26c82a

Please sign in to comment.