Skip to content

Commit

Permalink
[incubator-kie-drools-5909] [new-parser] Accumulate parsed incorrectl… (
Browse files Browse the repository at this point in the history
#5965)

* [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 <jiri.locker@gmail.com>

---------

Co-authored-by: Jiří Locker <jiri.locker@gmail.com>
  • Loading branch information
tkobayas and yurloc committed May 22, 2024
1 parent c40b1c4 commit 7f338a3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down

0 comments on commit 7f338a3

Please sign in to comment.