Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[incubator-kie-drools-5818] [new-parser] Parsing fails if a Java keyw… #5958

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of this PR fix, this expression no longer causes an error. I couldn't figure out another expression to produce "no viable alternative", so removed this test for now. I think we can add the test later, but I think it's relatively low priority.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a~a

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Added the test.


// 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
Loading