Skip to content

Commit

Permalink
[incubator-kie-drools-5818] [new-parser] Parsing fails if a Java keyw…
Browse files Browse the repository at this point in the history
…ord appears in a qualified name
  • Loading branch information
tkobayas committed May 17, 2024
1 parent dce66de commit 4aed056
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -440,29 +440,6 @@ public void testExtraneousInput() {
}
}

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

// Backward Compatibility Notes:
// Old expr parser (DRL6Expressions) allows this expression because it's too tolerant (fail at runtime anyway).
// Backward compatibility doesn't seem to be required in this case. (But we may align with the old tolerant behavior.)
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 '.int'");
} else {
assertThat(parser.hasErrors()).isFalse();
}
}

@Test
void orWithMethodCall() {
String source = "value == 10 || someMethod() == 4";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5103,4 +5103,23 @@ void errorMessage_shouldNotContainEmptyString() {
assertThat(parser.hasErrors()).isTrue();
assertThat(parser.getErrors()).extracting(DroolsError::getMessage).doesNotContain("");
}

@ParameterizedTest
@MethodSource("org.drools.drl.parser.antlr4.ParserTestUtils#javaKeywords")
void javaKeywordsInPackage(String keyword) {
String pkgName = "org.drools." + keyword;
String text = "package " + pkgName + "\n" +
"rule R\n" +
"when\n" +
" $p : Person()\n" +
"then\n" +
"end\n";

PackageDescr pkg = parseAndGetPackageDescr(text);

assertThat(pkg.getName()).isEqualTo(pkgName);
assertThat(pkg.getRules()).hasSize(1);

assertThat(pkg.getRules().get(0).getName()).isEqualTo("R");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,23 @@
*/
package org.drools.drl.parser.antlr4;

import java.util.Arrays;
import java.util.List;

import org.drools.drl.parser.DrlParser;

public class ParserTestUtils {

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",
"super", "switch", "synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile",
"while"
);

private ParserTestUtils() {
// It is a utility class, so it should not be instantiated.
}
Expand All @@ -39,4 +52,8 @@ public static DrlParser getParser() {
public static void enableOldParser() {
DrlParser.ANTLR4_PARSER_ENABLED = false;
}

public static List<String> javaKeywords() {
return javaKeywords;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,58 @@ typeArgument
drlIdentifier returns [Token token]
: drlKeywords
| IDENTIFIER
// java keywords
| ABSTRACT
| ASSERT
| BOOLEAN
| BREAK
| BYTE
| CASE
| CATCH
| CHAR
| CLASS
| CONST
| CONTINUE
| DEFAULT
| DO
| DOUBLE
| ELSE
| ENUM
| EXTENDS
| FINAL
| FINALLY
| FLOAT
| FOR
| IF
| GOTO
| IMPLEMENTS
| IMPORT
| INSTANCEOF
| INT
| INTERFACE
| LONG
| NATIVE
| NEW
| PACKAGE
| PRIVATE
| PROTECTED
| PUBLIC
| RETURN
| SHORT
| STATIC
| STRICTFP
| SUPER
| SWITCH
| SYNCHRONIZED
| THIS
| THROW
| THROWS
| TRANSIENT
| TRY
| VOID
| VOLATILE
| WHILE
// Module related keywords
| MODULE
| OPEN
| REQUIRES
Expand All @@ -172,12 +224,13 @@ drlIdentifier returns [Token token]
| PROVIDES
| WITH
| TRANSITIVE
// other java keywords
| VAR
| YIELD
| RECORD
| SEALED
| PERMITS
| RECORD
| VAR
| THIS
| NON_SEALED
;

// matches any drl keywords
Expand Down

0 comments on commit 4aed056

Please sign in to comment.