Skip to content

Commit

Permalink
[DROOLS-7591] Experiment branch : migrate a new drools-lsp parser int…
Browse files Browse the repository at this point in the history
…o drools (#44)

- Add some methods to call from drools code base
  • Loading branch information
tkobayas authored Dec 5, 2023
1 parent 4cf1bf5 commit 8d3e1e8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
21 changes: 19 additions & 2 deletions drools-parser/src/main/java/org/drools/parser/DRLParserHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.drools.parser;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
Expand Down Expand Up @@ -28,8 +32,21 @@ public static PackageDescr parse(String drl) {
}

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

public static DRLParser createDrlParser(InputStream is) {
try {
CharStream charStream = CharStreams.fromStream(is);
return createDrlParser(charStream);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private static DRLParser createDrlParser(CharStream charStream) {
DRLLexer drlLexer = new DRLLexer(charStream);
CommonTokenStream commonTokenStream = new CommonTokenStream(drlLexer);
return new DRLParser(commonTokenStream);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.drools.parser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -24,6 +25,18 @@ public class DRLParserWrapper {
*/
public PackageDescr parse(String drl) {
DRLParser drlParser = DRLParserHelper.createDrlParser(drl);
return parse(drlParser);
}

/**
* Main entry point for parsing DRL
*/
public PackageDescr parse(InputStream is) {
DRLParser drlParser = DRLParserHelper.createDrlParser(is);
return parse(drlParser);
}

private PackageDescr parse(DRLParser drlParser) {
DRLErrorListener errorListener = new DRLErrorListener();
drlParser.addErrorListener(errorListener);

Expand Down

0 comments on commit 8d3e1e8

Please sign in to comment.