Skip to content

Commit

Permalink
- Exclude 'new' from 'drlIdentifier' because of 'primary' ambiguity
Browse files Browse the repository at this point in the history
- Add tests
  • Loading branch information
tkobayas committed May 20, 2024
1 parent 4aed056 commit 5fe50b4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,30 @@ public void testExtraneousInput() {
}
}

@Test
public void testNoViableAlt() {
String source = "a~a";
parser.parse(source);

// Backward Compatibility Notes:
// Old expr parser (DRL6Expressions) allows this expression and only takes "a" ignoring the invalid part "~a" without emitting an error.
// This is rather a bug in the old parser, and the new parser (ANTLR4) correctly emits an error for this case.
// Backward compatibility doesn't seem to be required in this case.
if (DrlParser.ANTLR4_PARSER_ENABLED) {
assertThat(parser.hasErrors()).isTrue();
assertThat(parser.getErrors()).hasSize(1);
DroolsParserException exception = parser.getErrors().get(0);
assertThat(exception.getErrorCode()).isEqualTo("ERR 101");
assertThat(exception.getLineNumber()).isEqualTo(1);
assertThat(exception.getColumn()).isEqualTo(2);
assertThat(exception.getOffset()).isEqualTo(2);
assertThat(exception.getMessage())
.isEqualToIgnoringCase("[ERR 101] Line 1:2 no viable alternative at input 'a'");
} else {
assertThat(parser.hasErrors()).isFalse();
}
}

@Test
void orWithMethodCall() {
String source = "value == 10 || someMethod() == 4";
Expand Down Expand Up @@ -541,4 +565,17 @@ void andWithMethodCallWithArg() {
assertThat(left.getExpression()).isEqualTo("someMethod(value)");
assertThat(right.getExpression()).isEqualTo("4");
}

@Test
void newBigDecimal() {
String source = "$bd : new BigDecimal(30)";
ConstraintConnectiveDescr result = parser.parse(source);
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getDescrs().size()).isEqualTo(1);

BindingDescr bind = (BindingDescr) result.getDescrs().get(0);
assertThat(bind.getVariable()).isEqualTo("$bd");
assertThat(bind.getExpression()).isEqualTo("new BigDecimal(30)");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@

public class ParserTestUtils {

// 'new' is not included because it cannot be included in drlIdentifier.
// See https://github.com/apache/incubator-kie-drools/pull/5958
public static List<String> javaKeywords =
Arrays.asList(
"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const",
"continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float",
"for", "goto", "if", "implements", "import", "instanceof", "int", "interface", "long", "native",
"new", "package", "private", "protected", "public", "return", "short", "static", "strictfp",
"package", "private", "protected", "public", "return", "short", "static", "strictfp",
"super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile",
"while"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ drlIdentifier returns [Token token]
| INTERFACE
| LONG
| NATIVE
| NEW
// | NEW // avoid ambiguity with 'new_key creator' and 'drlIdentifier' in 'primary'
| PACKAGE
| PRIVATE
| PROTECTED
Expand Down

0 comments on commit 5fe50b4

Please sign in to comment.