use of org.antlr.v4.runtime.tree.Tree in project crate by crate.
the class SqlParser method invokeParser.
private Node invokeParser(String name, String sql, Function<SqlBaseParser, ParserRuleContext> parseFunction) {
try {
SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(new ANTLRInputStream(sql)));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
SqlBaseParser parser = new SqlBaseParser(tokenStream);
parser.addParseListener(new PostProcessor());
lexer.removeErrorListeners();
lexer.addErrorListener(ERROR_LISTENER);
parser.removeErrorListeners();
parser.addErrorListener(ERROR_LISTENER);
ParserRuleContext tree;
try {
// first, try parsing with potentially faster SLL mode
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
tree = parseFunction.apply(parser);
} catch (ParseCancellationException ex) {
// if we fail, parse with LL mode
// rewind input stream
tokenStream.reset();
parser.reset();
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
tree = parseFunction.apply(parser);
}
return new AstBuilder().visit(tree);
} catch (StackOverflowError e) {
throw new ParsingException(name + " is too large (stack overflow while parsing)");
}
}
use of org.antlr.v4.runtime.tree.Tree in project checkstyle by checkstyle.
the class JavadocDetailNodeParser method convertParseTreeToDetailNode.
/**
* Converts ParseTree (that is generated by ANTLRv4) to DetailNode tree.
*
* @param parseTreeNode root node of ParseTree
* @return root of DetailNode tree
*/
private DetailNode convertParseTreeToDetailNode(ParseTree parseTreeNode) {
final JavadocNodeImpl rootJavadocNode = createRootJavadocNode(parseTreeNode);
JavadocNodeImpl currentJavadocParent = rootJavadocNode;
ParseTree parseTreeParent = parseTreeNode;
while (currentJavadocParent != null) {
// remove unnecessary children tokens
if (currentJavadocParent.getType() == JavadocTokenTypes.TEXT) {
currentJavadocParent.setChildren((DetailNode[]) JavadocNodeImpl.EMPTY_DETAIL_NODE_ARRAY);
}
final JavadocNodeImpl[] children = (JavadocNodeImpl[]) currentJavadocParent.getChildren();
insertChildrenNodes(children, parseTreeParent);
if (children.length > 0) {
currentJavadocParent = children[0];
parseTreeParent = parseTreeParent.getChild(0);
} else {
JavadocNodeImpl nextJavadocSibling = (JavadocNodeImpl) JavadocUtils.getNextSibling(currentJavadocParent);
ParseTree nextParseTreeSibling = getNextSibling(parseTreeParent);
if (nextJavadocSibling == null) {
JavadocNodeImpl tempJavadocParent = (JavadocNodeImpl) currentJavadocParent.getParent();
ParseTree tempParseTreeParent = parseTreeParent.getParent();
while (nextJavadocSibling == null && tempJavadocParent != null) {
nextJavadocSibling = (JavadocNodeImpl) JavadocUtils.getNextSibling(tempJavadocParent);
nextParseTreeSibling = getNextSibling(tempParseTreeParent);
tempJavadocParent = (JavadocNodeImpl) tempJavadocParent.getParent();
tempParseTreeParent = tempParseTreeParent.getParent();
}
}
currentJavadocParent = nextJavadocSibling;
parseTreeParent = nextParseTreeSibling;
}
}
return rootJavadocNode;
}
use of org.antlr.v4.runtime.tree.Tree in project presto by prestodb.
the class SqlParser method invokeParser.
private Node invokeParser(String name, String sql, Function<SqlBaseParser, ParserRuleContext> parseFunction) {
try {
SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(new ANTLRInputStream(sql)));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
SqlBaseParser parser = new SqlBaseParser(tokenStream);
parser.addParseListener(new PostProcessor());
lexer.removeErrorListeners();
lexer.addErrorListener(ERROR_LISTENER);
parser.removeErrorListeners();
parser.addErrorListener(ERROR_LISTENER);
ParserRuleContext tree;
try {
// first, try parsing with potentially faster SLL mode
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
tree = parseFunction.apply(parser);
} catch (ParseCancellationException ex) {
// if we fail, parse with LL mode
// rewind input stream
tokenStream.reset();
parser.reset();
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
tree = parseFunction.apply(parser);
}
return new AstBuilder().visit(tree);
} catch (StackOverflowError e) {
throw new ParsingException(name + " is too large (stack overflow while parsing)");
}
}
use of org.antlr.v4.runtime.tree.Tree in project presto by prestodb.
the class TypeCalculation method calculateLiteralValue.
public static Long calculateLiteralValue(String calculation, Map<String, Long> inputs) {
try {
ParserRuleContext tree = parseTypeCalculation(calculation);
CalculateTypeVisitor visitor = new CalculateTypeVisitor(inputs);
BigInteger result = visitor.visit(tree);
return result.longValueExact();
} catch (StackOverflowError e) {
throw new ParsingException("Type calculation is too large (stack overflow while parsing)");
}
}
use of org.antlr.v4.runtime.tree.Tree in project antlr4 by antlr.
the class ParseTreePattern method findAll.
/**
* Find all nodes using XPath and then try to match those subtrees against
* this tree pattern.
*
* @param tree The {@link ParseTree} to match against this pattern.
* @param xpath An expression matching the nodes
*
* @return A collection of {@link ParseTreeMatch} objects describing the
* successful matches. Unsuccessful matches are omitted from the result,
* regardless of the reason for the failure.
*/
public List<ParseTreeMatch> findAll(ParseTree tree, String xpath) {
Collection<ParseTree> subtrees = XPath.findAll(tree, xpath, matcher.getParser());
List<ParseTreeMatch> matches = new ArrayList<ParseTreeMatch>();
for (ParseTree t : subtrees) {
ParseTreeMatch match = match(t);
if (match.succeeded()) {
matches.add(match);
}
}
return matches;
}
Aggregations