Skip to content

Commit

Permalink
[DROOLS-7253] Review and improve drools-lsp parser Visitor design (ap…
Browse files Browse the repository at this point in the history
…ache#16)

* [DROOLS-7253] Review and improve drools-lsp parser Visitor design
- Remove currentConstructStack. Utilize return object instead.

* - Remove instance fields. Utilize return objects instead.
  • Loading branch information
tkobayas committed Feb 15, 2024
1 parent 9ef91f4 commit fed7278
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 161 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.drools.parser;

public class DRLParserException extends RuntimeException {

public DRLParserException() {
super();
}

public DRLParserException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.ParseTree;
Expand All @@ -12,40 +11,46 @@

public class DRLParserHelper {

private DRLParserHelper() {
}

public static PackageDescr parse(String drl) {
return parseTree2PackageDescr(createParseTree(drl));
return compilationUnitContext2PackageDescr(createParseTree(drl));
}

public static ParseTree createParseTree(String drl) {
public static DRLParser.CompilationUnitContext createParseTree(String drl) {
return createDrlParser(drl).compilationUnit();
}

public static DRLParser createDrlParser(String drl) {
CharStream inputStream = CharStreams.fromString(drl);
DRLLexer drlLexer = new DRLLexer(inputStream);
CommonTokenStream commonTokenStream = new CommonTokenStream(drlLexer);
DRLParser drlParser = new DRLParser(commonTokenStream);
return drlParser;
return new DRLParser(commonTokenStream);
}

public static PackageDescr parseTree2PackageDescr(ParseTree parseTree) {
public static PackageDescr compilationUnitContext2PackageDescr(DRLParser.CompilationUnitContext ctx) {
DRLVisitorImpl visitor = new DRLVisitorImpl();
visitor.visit(parseTree);
return visitor.getPackageDescr();
Object descr = visitor.visit(ctx);
if (descr instanceof PackageDescr) {
return (PackageDescr) descr;
} else {
throw new DRLParserException("CompilationUnitContext should produce PackageDescr. descr = " + descr.getClass());
}
}


public static Integer computeTokenIndex(DRLParser parser, int row, int col) {
for (int i = 0; i < parser.getInputStream().size(); i++) {
Token token = parser.getInputStream().get(i);
int start = token.getCharPositionInLine();
int stop = token.getCharPositionInLine() + token.getText().length();
if (token.getLine() > row)
if (token.getLine() > row) {
return token.getTokenIndex() - 1;
else if (token.getLine() == row && start >= col)
} else if (token.getLine() == row && start >= col) {
return token.getTokenIndex() == 0 ? 0 : token.getTokenIndex() - 1;
else if (token.getLine() == row && start < col && stop >= col)
} else if (token.getLine() == row && start < col && stop >= col) {
return token.getTokenIndex();
}
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,29 @@
import java.util.List;
import java.util.stream.Collectors;

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

import static org.drools.parser.DRLParserHelper.compilationUnitContext2PackageDescr;

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();
DRLParser.CompilationUnitContext cxt = drlParser.compilationUnit();

errors.addAll(errorListener.getErrors());

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

0 comments on commit fed7278

Please sign in to comment.