use of org.antlr.v4.runtime.tree.Tree in project antlr4 by antlr.
the class BasePythonTest method execStartRule.
public ParseTree execStartRule(String startRuleName, Parser parser) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Method startRule = null;
Object[] args = null;
try {
startRule = parser.getClass().getMethod(startRuleName);
} catch (NoSuchMethodException nsme) {
// try with int _p arg for recursive func
startRule = parser.getClass().getMethod(startRuleName, int.class);
args = new Integer[] { 0 };
}
ParseTree result = (ParseTree) startRule.invoke(parser, args);
// System.out.println("parse tree = "+result.toStringTree(parser));
return result;
}
use of org.antlr.v4.runtime.tree.Tree in project antlr4 by antlr.
the class TestXPath method testError.
protected void testError(String input, String path, String expected, String startRuleName, String parserName, String lexerName) throws Exception {
Pair<Parser, Lexer> pl = getParserAndLexer(input, parserName, lexerName);
Parser parser = pl.a;
ParseTree tree = execStartRule(startRuleName, parser);
IllegalArgumentException e = null;
try {
XPath.findAll(tree, path, parser);
} catch (IllegalArgumentException iae) {
e = iae;
}
assertNotNull(e);
assertEquals(expected, e.getMessage());
}
use of org.antlr.v4.runtime.tree.Tree in project antlr4 by antlr.
the class LeftRecursiveRuleTransformer method parseArtificialRule.
public RuleAST parseArtificialRule(final Grammar g, String ruleText) {
ANTLRLexer lexer = new ANTLRLexer(new ANTLRStringStream(ruleText));
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(lexer.getCharStream());
CommonTokenStream tokens = new CommonTokenStream(lexer);
lexer.tokens = tokens;
ToolANTLRParser p = new ToolANTLRParser(tokens, tool);
p.setTreeAdaptor(adaptor);
Token ruleStart = null;
try {
ParserRuleReturnScope r = p.rule();
RuleAST tree = (RuleAST) r.getTree();
ruleStart = (Token) r.getStart();
GrammarTransformPipeline.setGrammarPtr(g, tree);
GrammarTransformPipeline.augmentTokensWithOriginalPosition(g, tree);
return tree;
} catch (Exception e) {
tool.errMgr.toolError(ErrorType.INTERNAL_ERROR, e, ruleStart, "error parsing rule created during left-recursion detection: " + ruleText);
}
return null;
}
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;
}
Aggregations