This is an automated email from the ASF dual-hosted git repository.
mariofusco pushed a commit to branch dev-new-parser
in repository https://gitbox.apache.org/repos/asf/incubator-kie-drools.git
The following commit(s) were added to refs/heads/dev-new-parser by this push:
new 739740f36d [incubator-kie-drools-5918] [new-parser] Annotations can
appear in lh… (#5921)
739740f36d is described below
commit 739740f36de7139f70875163b68171d1e946a1e0
Author: Toshiya Kobayashi <[email protected]>
AuthorDate: Fri May 10 22:53:38 2024 +0900
[incubator-kie-drools-5918] [new-parser] Annotations can appear in lh…
(#5921)
* [incubator-kie-drools-5918] [new-parser] Annotations can appear in lhsOr
and lhsAnd
* - infix or, and
---
.../drl/parser/antlr4/MiscDRLParserTest.java | 83 ++++++++++++++++++++++
.../org/drools/drl/parser/antlr4/DRLParser.g4 | 8 +--
.../drools/drl/parser/antlr4/DRLVisitorImpl.java | 26 +++++--
3 files changed, 107 insertions(+), 10 deletions(-)
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 e2a039f709..f4cd6e4c32 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
@@ -3990,6 +3990,89 @@ class MiscDRLParserTest {
assertThat(annotationDescr.getSingleValueAsString()).isEqualTo("!*,
age");
}
+ @Test
+ void prefixAndDescrAnnotation() {
+ final String text =
+ "rule R\n" +
+ " when\n" +
+ " ( and @Annot \n" +
+ " String() \n" +
+ " Integer() ) \n" +
+ " then\n" +
+ "end\n";
+ PackageDescr packageDescr = parseAndGetPackageDescr(text);
+
+ RuleDescr ruleDescr = packageDescr.getRules().get(0);
+ AndDescr andDescr = ruleDescr.getLhs();
+ AnnotationDescr annotationDescr =
andDescr.getAnnotations().iterator().next();
+ assertThat(annotationDescr.getName()).isEqualTo("Annot");
+ assertThat(annotationDescr.hasValue()).isFalse();
+
+ assertThat(andDescr.getDescrs()).hasSize(2);
+ }
+
+ @Test
+ void prefixOrDescrAnnotation() {
+ final String text =
+ "rule R\n" +
+ " when\n" +
+ " ( or @Annot \n" +
+ " String() \n" +
+ " Integer() ) \n" +
+ " then\n" +
+ "end\n";
+ PackageDescr packageDescr = parseAndGetPackageDescr(text);
+
+ RuleDescr ruleDescr = packageDescr.getRules().get(0);
+ OrDescr orDescr = (OrDescr) ruleDescr.getLhs().getDescrs().get(0);
+ AnnotationDescr annotationDescr =
orDescr.getAnnotations().iterator().next();
+
+ assertThat(annotationDescr.getName()).isEqualTo("Annot");
+ assertThat(annotationDescr.hasValue()).isFalse();
+
+ assertThat(orDescr.getDescrs()).hasSize(2);
+ }
+
+ @Test
+ void infixAndDescrAnnotation() {
+ final String text =
+ "rule R\n" +
+ " when\n" +
+ " ( Double() \n" +
+ " and @Annot1 String() \n" +
+ " and @Annot2 Integer() ) " +
+ " then\n" +
+ "end\n";
+ PackageDescr packageDescr = parseAndGetPackageDescr(text);
+
+ RuleDescr ruleDescr = packageDescr.getRules().get(0);
+ AndDescr andDescr = ruleDescr.getLhs();
+ Collection<AnnotationDescr> annotationDescrs =
andDescr.getAnnotations();
+
assertThat(annotationDescrs).extracting(AnnotationDescr::getName).containsExactlyInAnyOrder("Annot1",
"Annot2");
+
+ assertThat(andDescr.getDescrs()).hasSize(3);
+ }
+
+ @Test
+ void infixOrDescrAnnotation() {
+ final String text =
+ "rule R\n" +
+ " when\n" +
+ " ( Double() \n" +
+ " or @Annot1 String() \n" +
+ " or @Annot2 Integer() ) " +
+ " then\n" +
+ "end\n";
+ PackageDescr packageDescr = parseAndGetPackageDescr(text);
+
+ RuleDescr ruleDescr = packageDescr.getRules().get(0);
+ OrDescr orDescr = (OrDescr) ruleDescr.getLhs().getDescrs().get(0);
+ Collection<AnnotationDescr> annotationDescrs =
orDescr.getAnnotations();
+
assertThat(annotationDescrs).extracting(AnnotationDescr::getName).containsExactlyInAnyOrder("Annot1",
"Annot2");
+
+ assertThat(orDescr.getDescrs()).hasSize(3);
+ }
+
@Test
void annotationWithEmptyParentheses() {
final String text = "package org.drools;\n" +
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 bf0d2f41ad..018375dd5d 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
@@ -105,10 +105,10 @@ queryLhs : lhsExpression* ;
lhsExpression : LPAREN lhsExpression RPAREN
#lhsExpressionEnclosed
| lhsUnary
#lhsUnarySingle
- | DRL_AND lhsExpression+ #lhsAnd
- | lhsExpression (DRL_AND lhsExpression)+ #lhsAnd
- | DRL_OR lhsExpression+ #lhsOr
- | lhsExpression (DRL_OR lhsExpression)+ #lhsOr
+ | DRL_AND drlAnnotation* lhsExpression+
#lhsAnd
+ | lhsExpression (DRL_AND drlAnnotation* lhsExpression)+
#lhsAnd
+ | DRL_OR drlAnnotation* lhsExpression+
#lhsOr
+ | lhsExpression (DRL_OR drlAnnotation* lhsExpression)+
#lhsOr
;
// lhsAnd is used as a label in lhsExpression rule. But some other rules
explicitly use the def, so lhsAndDef is declared.
diff --git
a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLVisitorImpl.java
b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLVisitorImpl.java
index 8212a7f64c..80ce8d6986 100644
---
a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLVisitorImpl.java
+++
b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLVisitorImpl.java
@@ -988,7 +988,13 @@ public class DRLVisitorImpl extends
DRLParserBaseVisitor<Object> {
// B() C()
// So, we need to flatten it so that OrDescr has A(), B() and C()
as children.
List<BaseDescr> flattenedDescrs = flattenOrDescr(descrList);
- flattenedDescrs.forEach(orDescr::addDescr);
+ flattenedDescrs.forEach(descr -> {
+ if (descr instanceof AnnotationDescr annotationDescr) {
+ orDescr.addAnnotation(annotationDescr);
+ } else {
+ orDescr.addDescr(descr);
+ }
+ });
return orDescr;
}
}
@@ -998,9 +1004,10 @@ public class DRLVisitorImpl extends
DRLParserBaseVisitor<Object> {
for (DescrNodePair descrNodePair : descrList) {
BaseDescr descr = descrNodePair.getDescr();
ParseTree node = descrNodePair.getNode(); // parser node
corresponding to the descr
- if (descr instanceof OrDescr && !(node instanceof
DRLParser.LhsExpressionEnclosedContext)) {
+ if (descr instanceof OrDescr orDescr && !(node instanceof
DRLParser.LhsExpressionEnclosedContext)) {
// sibling OrDescr should be flattened unless it's explicitly
enclosed by parenthesis
- flattenedDescrs.addAll(((OrDescr) descr).getDescrs());
+ flattenedDescrs.addAll(orDescr.getDescrs());
+ flattenedDescrs.addAll(orDescr.getAnnotations());
} else {
flattenedDescrs.add(descr);
}
@@ -1032,7 +1039,13 @@ public class DRLVisitorImpl extends
DRLParserBaseVisitor<Object> {
// B() C()
// So, we need to flatten it so that AndDescr has A(), B() and C()
as children.
List<BaseDescr> flattenedDescrs = flattenAndDescr(descrList);
- flattenedDescrs.forEach(andDescr::addDescr);
+ flattenedDescrs.forEach(descr -> {
+ if (descr instanceof AnnotationDescr annotationDescr) {
+ andDescr.addAnnotation(annotationDescr);
+ } else {
+ andDescr.addDescr(descr);
+ }
+ });
return andDescr;
}
}
@@ -1042,9 +1055,10 @@ public class DRLVisitorImpl extends
DRLParserBaseVisitor<Object> {
for (DescrNodePair descrNodePair : descrList) {
BaseDescr descr = descrNodePair.getDescr();
ParseTree node = descrNodePair.getNode(); // parser node
corresponding to the descr
- if (descr instanceof AndDescr && !(node instanceof
DRLParser.LhsExpressionEnclosedContext)) {
+ if (descr instanceof AndDescr andDescr && !(node instanceof
DRLParser.LhsExpressionEnclosedContext)) {
// sibling AndDescr should be flattened unless it's explicitly
enclosed by parenthesis
- flattenedDescrs.addAll(((AndDescr) descr).getDescrs());
+ flattenedDescrs.addAll(andDescr.getDescrs());
+ flattenedDescrs.addAll(andDescr.getAnnotations());
} else {
flattenedDescrs.add(descr);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]