From 630d372449033a2fe684f33fea7a27b4eebfebb3 Mon Sep 17 00:00:00 2001 From: Toshiya Kobayashi Date: Thu, 28 Mar 2024 18:24:46 +0900 Subject: [PATCH] [incubator-kie-drools#5742] [new-parser] Broken inline cast (#5806) * [incubator-kie-drools#5742] [new-parser] Broken inline cast * fixed duplicate test --- .../drl/parser/antlr4/MiscDRLParserTest.java | 39 +++++++++++++++++++ .../drl/parser/antlr4/DRL6Expressions.g4 | 2 +- .../org/drools/drl/parser/antlr4/DRLParser.g4 | 5 +++ 3 files changed, 45 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 a65fafec286..0029602df5a 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 @@ -3870,4 +3870,43 @@ void ooPathMixedWithStandardConstraint() { assertThat(constraintDescr.toString()) .isEqualToIgnoringWhitespace("/wife[$age : age] && age > $age"); } + + @Test + void inlineCast() { + final String text = "package org.drools\n" + + "rule R1\n" + + "when\n" + + " $a : ICA( someB#ICB.onlyConcrete() == \"Hello\" )\n" + + "then\n" + + "end\n"; + PackageDescr packageDescr = parser.parse(text); + ExprConstraintDescr constraintDescr = getFirstExprConstraintDescr(packageDescr); + assertThat(constraintDescr.toString()).isEqualToIgnoringWhitespace("someB#ICB.onlyConcrete() == \"Hello\""); + } + + @Test + void inlineCastMultiple() { + final String text = "package org.drools\n" + + "rule R1\n" + + "when\n" + + " $a : ICA( someB#ICB.someC#ICC.onlyConcrete() == \"Hello\" )\n" + + "then\n" + + "end\n"; + PackageDescr packageDescr = parser.parse(text); + ExprConstraintDescr constraintDescr = getFirstExprConstraintDescr(packageDescr); + assertThat(constraintDescr.toString()).isEqualToIgnoringWhitespace("someB#ICB.someC#ICC.onlyConcrete() == \"Hello\""); + } + + @Test + void inlineCastThis() { + final String text = "package org.drools\n" + + "rule R1\n" + + "when\n" + + " $o : Object( this#Person.name == \"Mark\" )\n" + + "then\n" + + "end\n"; + PackageDescr packageDescr = parser.parse(text); + ExprConstraintDescr constraintDescr = getFirstExprConstraintDescr(packageDescr); + assertThat(constraintDescr.toString()).isEqualToIgnoringWhitespace("this#Person.name == \"Mark\""); + } } 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 d85859d3bd5..ee5ce23259a 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 @@ -230,6 +230,7 @@ drlIdentifier returns [Token token] | PERMITS | RECORD | VAR + | THIS ; // -------------------------------------------------------- @@ -671,7 +672,6 @@ primary returns [BaseDescr result] : expr=parExpression { if( buildDescr ) { $result = $expr.result; } } | nonWildcardTypeArguments (explicitGenericInvocationSuffix | this_key arguments) | literal { if( buildDescr ) { $result = new AtomicExprDescr( $literal.text, true ); } } - | this_key (DOT drlIdentifier)* identifierSuffix? | super_key superSuffix | new_key creator | primitiveType (LBRACK RBRACK)* DOT class_key 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 5891d6eaa90..7eb20980cd3 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 @@ -197,6 +197,7 @@ drlIdentifier | PERMITS | RECORD | VAR + | THIS ; drlKeywords @@ -258,6 +259,7 @@ drlExpression | NEW nonWildcardTypeArguments? innerCreator | SUPER superSuffix | explicitGenericInvocation + | inlineCast ) | drlExpression NULL_SAFE_DOT ( drlIdentifier | drlMethodCall ) | drlExpression LBRACK drlExpression RBRACK @@ -321,8 +323,11 @@ drlPrimary | nonWildcardTypeArguments (explicitGenericInvocationSuffix | THIS arguments) | inlineListExpression | inlineMapExpression + | inlineCast ; +inlineCast : drlIdentifier HASH drlIdentifier ; + /* extending JavaParser literal */ drlLiteral : integerLiteral