Skip to content

Commit

Permalink
- Added DRLParserWrapper to store errors using DRLErrorListener (#5)
Browse files Browse the repository at this point in the history
- Added MiscDRLParserTest which is being ported from RuleParserTest to enhance coverage.
  • Loading branch information
tkobayas committed Feb 15, 2024
1 parent 5436cb7 commit a0c015c
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 0 deletions.
6 changes: 6 additions & 0 deletions drools-drl/drools-drl10-parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${version.ch.qos.logback}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.drools.parser;

import java.util.ArrayList;
import java.util.List;

import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;

public class DRLErrorListener extends BaseErrorListener {

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

public List<DRLParserError> getErrors() {
return errors;
}

@Override
public void syntaxError(Recognizer<?, ?> recognizer,
Object offendingSymbol,
int line,
int charPositionInLine,
String msg,
RecognitionException e) {

errors.add(new DRLParserError(line, charPositionInLine, msg));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.drools.parser;

public class DRLParserError {

private int lineNumber;
private int column;
private String message;

private Exception exception;

public DRLParserError(int lineNumber, int column, String message) {
this.lineNumber = lineNumber;
this.column = column;
this.message = message;
}

public DRLParserError(Exception exception) {
this.exception = exception;
}

public int getLineNumber() {
return lineNumber;
}

public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}

public int getColumn() {
return column;
}

public void setColumn(int column) {
this.column = column;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

@Override
public String toString() {
return "DRLParserError{" +
"lineNumber=" + lineNumber +
", column=" + column +
", message='" + message + '\'' +
", exception=" + exception +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.drools.parser;

import java.util.ArrayList;
import java.util.List;

import org.antlr.v4.runtime.tree.ParseTree;
import org.drools.drl.ast.descr.PackageDescr;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DRLParserWrapper {

private static final Logger LOGGER = LoggerFactory.getLogger(DRLParserWrapper.class);

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

public DRLParserWrapper() {
}

public PackageDescr parse(String drl) {
DRLParser drlParser = DRLParserHelper.createDrlParser(drl);
DRLErrorListener errorListener = new DRLErrorListener();
drlParser.addErrorListener(errorListener);

ParseTree parseTree = drlParser.compilationUnit();

errors.addAll(errorListener.getErrors());

try {
return DRLParserHelper.parseTree2PackageDescr(parseTree);
} catch (Exception e) {
LOGGER.error("Exception while creating PackageDescr", e);
errors.add(new DRLParserError(e));
return null;
}
}

public List<DRLParserError> getErrors() {
return errors;
}

public boolean hasErrors() {
return !errors.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.drools.parser;

import junit.framework.TestCase;
import org.drools.drl.ast.descr.PackageDescr;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/*
* This test class is being ported from org.drools.mvel.compiler.lang.RuleParserTest
*/
public class MiscDRLParserTest extends TestCase {

private DRLParserWrapper parser;

@Before
protected void setUp() throws Exception {
super.setUp();
parser = new DRLParserWrapper();
}

@After
protected void tearDown() throws Exception {
super.tearDown();
}

@Test
public void testPackage() throws Exception {
final String source = "package foo.bar.baz";
final PackageDescr pkg = parser.parse(source);
assertEquals("foo.bar.baz", pkg.getName());
}

@Test
public void testPackageWithErrorNode() throws Exception {
final String source = "package 12 foo.bar.baz";
final PackageDescr pkg = parser.parse(source);
assertTrue(parser.hasErrors());
// getText() combines an ErrorNode "12" so the result is different from DRL6Parser.
assertEquals("12foo.bar.baz", pkg.getName());
}

@Test
public void testPackageWithAllErrorNode() throws Exception {
final String source = "package 12 12312 231";
final PackageDescr pkg = parser.parse(source);
assertTrue(parser.hasErrors());
// NPE inside DRLVisitorImpl.visitIdentifier(). So pkg is null. Different from DRL6Parser.
assertNull(pkg);
}
}
16 changes: 16 additions & 0 deletions drools-drl/drools-drl10-parser/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %class{36}.%method:%line - %msg%n</pattern>
</encoder>
</appender>

<logger name="org.kie" level="warn"/>
<logger name="org.drools" level="warn"/>

<root level="warn">
<appender-ref ref="consoleAppender" />
</root>
</configuration>

0 comments on commit a0c015c

Please sign in to comment.