Skip to content

Commit

Permalink
[incubator-kie-drools-5933] [new-parser] PackageDescr.resource not set (
Browse files Browse the repository at this point in the history
  • Loading branch information
tkobayas authored and rgdoliveira committed Oct 24, 2024
1 parent 3940d11 commit f5055e5
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
*/
package org.drools.drl.parser.antlr4;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import org.drools.drl.ast.descr.AccumulateDescr;
import org.drools.drl.ast.descr.AccumulateImportDescr;
import org.drools.drl.ast.descr.AndDescr;
Expand Down Expand Up @@ -49,8 +54,10 @@
import org.drools.drl.ast.descr.WindowReferenceDescr;
import org.drools.drl.parser.DrlParser;
import org.drools.drl.parser.DroolsParserException;
import org.drools.io.InputStreamResource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.kie.api.io.Resource;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -61,17 +68,20 @@ class DescrCommonPropertyTest {

private DrlParser parser;

private Resource resource;

@BeforeEach
public void setUp() {
parser = ParserTestUtils.getParser();
}

private PackageDescr parseAndGetPackageDescr(String drl) {
try {
PackageDescr pkg = parser.parse(null, drl);
try (InputStream inputStream = new ByteArrayInputStream(drl.getBytes(StandardCharsets.UTF_8))) {
resource = new InputStreamResource(inputStream);
PackageDescr pkg = parser.parse(resource);
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();
return pkg;
} catch (DroolsParserException e) {
} catch (DroolsParserException | IOException e) {
throw new RuntimeException(e);
}
}
Expand All @@ -83,6 +93,7 @@ private void assertProperties(BaseDescr descr, int startCharacter, int endCharac
assertThat(descr.getColumn()).isEqualTo(column); // first column of the start token. column is 0-based
assertThat(descr.getEndLine()).isEqualTo(endLine); // line of the end token. line is 1-based
assertThat(descr.getEndColumn()).isEqualTo(endColumn); // last column of the end token. column is 0-based
assertThat(descr.getResource()).isEqualTo(resource);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public PackageDescr parse(final boolean isEditor,
private PackageDescr compileWithAntlr4Parser(Function<DRLParserWrapper, PackageDescr> packageDescrFunction) throws DroolsParserException {
try {
// we don't use languageLevel here, assuming DRL6 compatible
DRLParserWrapper parser = new DRLParserWrapper();
DRLParserWrapper parser = new DRLParserWrapper(resource);
PackageDescr packageDescr = packageDescrFunction.apply(parser);
for (final DRLParserError drlParserError : parser.getErrors()) {
final ParserError err = new ParserError(resource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.antlr.v4.runtime.ParserRuleContext;
import org.drools.drl.ast.descr.BaseDescr;
import org.kie.api.io.Resource;

import static org.drools.drl.parser.antlr4.DescrHelper.populateCommonProperties;

Expand Down Expand Up @@ -53,6 +54,11 @@ public Builder<T> withParserRuleContext(ParserRuleContext ctx) {
return this;
}

public Builder<T> withResource(Resource resource) {
toReturn.setResource(resource);
return this;
}

public T build() {
return toReturn;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.drools.drl.ast.descr.PackageDescr;
import org.kie.api.io.Resource;
import org.kie.internal.builder.conf.LanguageLevelOption;

/**
Expand All @@ -49,7 +50,7 @@ private DRLParserHelper() {
*/
public static PackageDescr parse(String drl) {
DRLParser drlParser = createDrlParser(drl);
return compilationUnitContext2PackageDescr(drlParser.compilationUnit(), drlParser.getTokenStream());
return compilationUnitContext2PackageDescr(drlParser.compilationUnit(), drlParser.getTokenStream(), null);
}

public static DRLParser createDrlParser(String drl) {
Expand Down Expand Up @@ -89,8 +90,8 @@ private static DRLParser createDrlParser(CharStream charStream) {
/**
* DRLVisitorImpl visits a parse tree and creates a PackageDescr
*/
public static PackageDescr compilationUnitContext2PackageDescr(DRLParser.CompilationUnitContext ctx, TokenStream tokenStream) {
DRLVisitorImpl visitor = new DRLVisitorImpl(tokenStream);
public static PackageDescr compilationUnitContext2PackageDescr(DRLParser.CompilationUnitContext ctx, TokenStream tokenStream, Resource resource) {
DRLVisitorImpl visitor = new DRLVisitorImpl(tokenStream, resource);
Object descr = visitor.visit(ctx);
if (descr instanceof PackageDescr) {
return (PackageDescr) descr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.stream.Collectors;

import org.drools.drl.ast.descr.PackageDescr;
import org.kie.api.io.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,6 +40,12 @@ public class DRLParserWrapper {

private final List<DRLParserError> errors = new ArrayList<>();

private final Resource resource;

public DRLParserWrapper(Resource resource) {
this.resource = resource;
}

/**
* Main entry point for parsing DRL
*/
Expand Down Expand Up @@ -72,7 +79,7 @@ private PackageDescr parse(DRLParser drlParser) {
errors.addAll(errorListener.getErrors());

try {
return compilationUnitContext2PackageDescr(cxt, drlParser.getTokenStream());
return compilationUnitContext2PackageDescr(cxt, drlParser.getTokenStream(), resource);
} catch (Exception e) {
LOGGER.error("Exception while creating PackageDescr", e);
errors.add(new DRLParserError(e));
Expand Down
Loading

0 comments on commit f5055e5

Please sign in to comment.