From e13629a95a548144b335fdd175c7d5c31f1bc2d5 Mon Sep 17 00:00:00 2001 From: Toshiya Kobayashi Date: Tue, 4 Jun 2024 17:29:23 +0900 Subject: [PATCH] [incubator-kie-drools-5911] [new-parser] Support optional type keyword for backward compatibility (#5977) --- .../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 d3a919d0c48..7057579d8d8 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 @@ -5327,4 +5327,17 @@ void functionWithAnonymousClass() { " }\n" + " };"); } + + @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 026beef4cea..9819713fbb5 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