From 2f756f54f3c8687295d696c1b2c4f4ba8ee6fb18 Mon Sep 17 00:00:00 2001 From: Toshiya Kobayashi Date: Wed, 22 May 2024 16:24:28 +0900 Subject: [PATCH] =?UTF-8?q?[incubator-kie-drools-5909]=20[new-parser]=20Ac?= =?UTF-8?q?cumulate=20parsed=20incorrectl=E2=80=A6=20(#5965)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [incubator-kie-drools-5909] [new-parser] Accumulate parsed incorrectly if init and action statements are empty * Update drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/MiscDRLParserTest.java Co-authored-by: Jiří Locker --------- Co-authored-by: Jiří Locker --- .../drl/parser/antlr4/MiscDRLParserTest.java | 20 +++++++++++++++++++ .../org/drools/drl/parser/antlr4/DRLParser.g4 | 2 +- .../antlr4/Antlr4ParserStringUtils.java | 3 +++ .../drl/parser/antlr4/DRLVisitorImpl.java | 2 +- 4 files changed, 25 insertions(+), 2 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 85c8423ba89..ca565a83209 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 @@ -5122,4 +5122,24 @@ void javaKeywordsInPackage(String keyword) { assertThat(pkg.getRules().get(0).getName()).isEqualTo("R"); } + + @Test + void accumulateEmptyChunks() { + String text = "rule R\n" + + "when\n" + + " $totalAmount : Number() from accumulate( Cheese( $price : price ),\n" + + " init( ),\n" + + " action( ),\n" + + " result( null ) );\n" + + "then\n" + + "end"; + RuleDescr rule = parseAndGetFirstRuleDescr(text); + + final PatternDescr outPattern = (PatternDescr) rule.getLhs().getDescrs().get( 0 ); + final AccumulateDescr accumulateDescr = (AccumulateDescr) outPattern.getSource(); + assertThat(accumulateDescr.getInitCode()).isEmpty(); + assertThat(accumulateDescr.getActionCode()).isEmpty(); + assertThat(accumulateDescr.getReverseCode()).isNull(); + assertThat(accumulateDescr.getResultCode()).isEqualTo( "null"); + } } 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 da397785c1d..e96aa729aec 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 @@ -340,7 +340,7 @@ fromAccumulate := ACCUMULATE LEFT_PAREN lhsAnd (COMMA|SEMICOLON) ) RIGHT_PAREN */ fromAccumulate : (DRL_ACCUMULATE|DRL_ACC) LPAREN lhsAndDef (COMMA|SEMI) - ( DRL_INIT LPAREN initBlockStatements=chunk RPAREN COMMA? DRL_ACTION LPAREN actionBlockStatements=chunk RPAREN COMMA? ( DRL_REVERSE LPAREN reverseBlockStatements=chunk RPAREN COMMA?)? DRL_RESULT LPAREN expression RPAREN + ( DRL_INIT LPAREN initBlockStatements=chunk? RPAREN COMMA? DRL_ACTION LPAREN actionBlockStatements=chunk? RPAREN COMMA? ( DRL_REVERSE LPAREN reverseBlockStatements=chunk? RPAREN COMMA?)? DRL_RESULT LPAREN resultBlockStatements=chunk RPAREN | accumulateFunction ) RPAREN (SEMI)? diff --git a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/Antlr4ParserStringUtils.java b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/Antlr4ParserStringUtils.java index a33eb143dd8..a0f09fb4717 100644 --- a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/Antlr4ParserStringUtils.java +++ b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/Antlr4ParserStringUtils.java @@ -38,6 +38,9 @@ private Antlr4ParserStringUtils() { * Get text from ParserRuleContext's CharStream without trimming whitespace */ public static String getTextPreservingWhitespace(ParserRuleContext ctx) { + if (ctx == null) { + return ""; + } // Using raw CharStream int startIndex = ctx.start.getStartIndex(); int stopIndex = ctx.stop.getStopIndex(); 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 485928d6e7a..ddb3d370fad 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 @@ -798,7 +798,7 @@ public AccumulateDescr visitFromAccumulate(DRLParser.FromAccumulateContext ctx) if (ctx.DRL_REVERSE() != null) { accumulateDescr.setReverseCode(getTextPreservingWhitespace(ctx.reverseBlockStatements)); } - accumulateDescr.setResultCode(getTextPreservingWhitespace(ctx.expression())); + accumulateDescr.setResultCode(getTextPreservingWhitespace(ctx.resultBlockStatements)); } else { // accumulate function accumulateDescr.addFunction(visitAccumulateFunction(ctx.accumulateFunction()));