forked from apache/incubator-kie-drools
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added DRLParserWrapper to store errors using DRLErrorListener (#5)
- Added MiscDRLParserTest which is being ported from RuleParserTest to enhance coverage.
- Loading branch information
Showing
6 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLErrorListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLParserError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + | ||
'}'; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
drools-drl/drools-drl10-parser/src/main/java/org/drools/parser/DRLParserWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
drools-drl/drools-drl10-parser/src/test/java/org/drools/parser/MiscDRLParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
drools-drl/drools-drl10-parser/src/test/resources/logback-test.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |