From a7a565b01b9bc9e23b8673bdb80663f556ccd14d Mon Sep 17 00:00:00 2001 From: Toshiya Kobayashi Date: Mon, 3 Jun 2024 18:07:40 +0900 Subject: [PATCH] [incubator-kie-drools-5911] [new-parser] Support optional type keyword for backward compatibility --- .../drools/drl/parser/antlr4/MiscDRLParserTest.java | 13 +++++++++++++ .../org/drools/drl/parser/antlr4/DRL6Expressions.g4 | 1 + .../antlr4/org/drools/drl/parser/antlr4/DRLLexer.g4 | 1 + .../org/drools/drl/parser/antlr4/DRLParser.g4 | 2 +- 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/MiscDRLParserTest.java b/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/MiscDRLParserTest.java index 875315157d1..0d8c983598e 100644 --- a/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/MiscDRLParserTest.java +++ b/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/MiscDRLParserTest.java @@ -5304,4 +5304,17 @@ void accumulateWithEmptyActionAndReverse() { }); }); } + + @Test + void typeDeclarationWithTypeToken() { + final String drl = "declare type Foo\n" + // "type" is just optional + " id : int\n" + + "end"; + final PackageDescr pkg = parseAndGetPackageDescr(drl); + + TypeDeclarationDescr typeDeclarationDescr = pkg.getTypeDeclarations().get(0); + assertThat(typeDeclarationDescr.getTypeName()).isEqualTo("Foo"); + TypeFieldDescr typeFieldDescr = typeDeclarationDescr.getFields().get("id"); + assertThat(typeFieldDescr.getPattern().getObjectType()).isEqualTo("int"); + } } diff --git a/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRL6Expressions.g4 b/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRL6Expressions.g4 index f2a5a79f195..181a9470817 100644 --- a/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRL6Expressions.g4 +++ b/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRL6Expressions.g4 @@ -241,6 +241,7 @@ drlKeywords returns [Token token] | DRL_GLOBAL | DRL_DECLARE | DRL_TRAIT + | DRL_TYPE | DRL_RULE | DRL_QUERY | DRL_WHEN diff --git a/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRLLexer.g4 b/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRLLexer.g4 index 5809147d651..198dfccfc2a 100644 --- a/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRLLexer.g4 +++ b/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRLLexer.g4 @@ -36,6 +36,7 @@ DRL_FUNCTION : 'function'; DRL_GLOBAL : 'global'; DRL_DECLARE : 'declare'; DRL_TRAIT : 'trait'; +DRL_TYPE : 'type'; DRL_RULE : 'rule'; DRL_QUERY : 'query'; DRL_WHEN : 'when'; diff --git a/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRLParser.g4 b/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRLParser.g4 index cabdaaa1f34..40b3bc66596 100644 --- a/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRLParser.g4 +++ b/drools-drl/drools-drl-parser/src/main/antlr4/org/drools/drl/parser/antlr4/DRLParser.g4 @@ -61,7 +61,7 @@ declaredef : DRL_DECLARE ( * END */ -typeDeclaration : DRL_TRAIT? name=drlQualifiedName (EXTENDS superTypes+=drlQualifiedName (COMMA superTypes+=drlQualifiedName)* )? drlAnnotation* field* DRL_END ; +typeDeclaration : DRL_TRAIT? DRL_TYPE? name=drlQualifiedName (EXTENDS superTypes+=drlQualifiedName (COMMA superTypes+=drlQualifiedName)* )? drlAnnotation* field* DRL_END ; // entryPointDeclaration := ENTRY-POINT stringId annotation* END