Skip to content

Commit

Permalink
[DROOLS-7287] Failed to parse comments in RHS (apache#23)
Browse files Browse the repository at this point in the history
* [DROOLS-7287] Failed to parse comments in RHS

* - add rhs string check
  • Loading branch information
tkobayas committed Jun 11, 2024
1 parent b664cc3 commit 9b9f450
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ rule simple_rule
Baz()
then
//woot
first
first;

//

/* lala

*/
second
second;
end

0 comments on commit 9b9f450

Please sign in to comment.