From 9b9f4501604388795e01f5b163e5b38039ee8d49 Mon Sep 17 00:00:00 2001 From: Toshiya Kobayashi Date: Wed, 19 Jul 2023 16:17:27 +0900 Subject: [PATCH] [DROOLS-7287] Failed to parse comments in RHS (#23) * [DROOLS-7287] Failed to parse comments in RHS * - add rhs string check --- .../src/main/java/org/drools/parser/DRLVisitorImpl.java | 3 ++- .../main/java/org/drools/parser/ParserStringUtils.java | 8 ++++++++ .../test/java/org/drools/parser/MiscDRLParserTest.java | 3 +-- .../parser/test_CommentLineNumbersInConsequence.drl | 4 ++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLVisitorImpl.java b/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLVisitorImpl.java index 2a2a2088eff..7f69502301d 100644 --- a/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLVisitorImpl.java +++ b/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLVisitorImpl.java @@ -32,6 +32,7 @@ import static org.drools.parser.ParserStringUtils.getTextPreservingWhitespace; import static org.drools.parser.ParserStringUtils.getTokenTextPreservingWhitespace; import static org.drools.parser.ParserStringUtils.safeStripStringDelimiters; +import static org.drools.parser.ParserStringUtils.trimThen; import static org.drools.util.StringUtils.unescapeJava; public class DRLVisitorImpl extends DRLParserBaseVisitor { @@ -152,7 +153,7 @@ public RuleDescr visitRuledef(DRLParser.RuledefContext ctx) { if (ctx.rhs() != null) { ruleDescr.setConsequenceLocation(ctx.rhs().getStart().getLine(), ctx.rhs().getStart().getCharPositionInLine()); // location of "then" - ruleDescr.setConsequence(getTextPreservingWhitespace(ctx.rhs().consequence())); + ruleDescr.setConsequence(trimThen(getTextPreservingWhitespace(ctx.rhs()))); } return ruleDescr; diff --git a/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/ParserStringUtils.java b/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/ParserStringUtils.java index 0fcef4e5a10..1f84eb6f9c8 100644 --- a/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/ParserStringUtils.java +++ b/drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/ParserStringUtils.java @@ -38,4 +38,12 @@ public static String getTokenTextPreservingWhitespace(ParserRuleContext ctx, Tok // tokenStream is required to get hidden channel token (e.g. whitespace). Unlike getTextPreservingWhitespace, this method reflects Lexer normalizeString return tokenStream.getText(ctx.start, ctx.stop); } + + public static String trimThen(String rhs) { + if (rhs.startsWith("then")) { + return rhs.substring("then".length()); + } else { + throw new DRLParserException("rhs has to start with 'then' : rhs = " + rhs); + } + } } diff --git a/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/MiscDRLParserTest.java b/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/MiscDRLParserTest.java index d053f6ae295..ed5532ff2f9 100644 --- a/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/MiscDRLParserTest.java +++ b/drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/MiscDRLParserTest.java @@ -901,7 +901,6 @@ public void parse_LineNumberInAST() throws Exception { assertThat(third.getLine()).isEqualTo(21); } - @Disabled("Priority : High | Failed to parse comments in RHS") @Test public void parse_LineNumberIncludingCommentsInRHS() throws Exception { PackageDescr pkg = parseAndGetPackageDescrFromFile( @@ -910,7 +909,7 @@ public void parse_LineNumberIncludingCommentsInRHS() throws Exception { assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse(); final String rhs = (String) ((RuleDescr) pkg.getRules().get( 0 )).getConsequence(); - String expected = "\\s*//woot$\\s*first$\\s*$\\s*//$\\s*$\\s*/\\* lala$\\s*$\\s*\\*/$\\s*second$\\s*"; + String expected = "\\s*//woot$\\s*first;$\\s*$\\s*//$\\s*$\\s*/\\* lala$\\s*$\\s*\\*/$\\s*second;$\\s*"; assertThat(Pattern.compile(expected, Pattern.DOTALL | Pattern.MULTILINE).matcher(rhs).matches()).isTrue(); } diff --git a/drools-drl/drools-drl10-parser/src/test/resources/org/drools/parser/test_CommentLineNumbersInConsequence.drl b/drools-drl/drools-drl10-parser/src/test/resources/org/drools/parser/test_CommentLineNumbersInConsequence.drl index 0a120702c34..5ed4e1ca301 100644 --- a/drools-drl/drools-drl10-parser/src/test/resources/org/drools/parser/test_CommentLineNumbersInConsequence.drl +++ b/drools-drl/drools-drl10-parser/src/test/resources/org/drools/parser/test_CommentLineNumbersInConsequence.drl @@ -21,12 +21,12 @@ rule simple_rule Baz() then //woot - first + first; // /* lala */ - second + second; end