use of org.antlr.v4.parse.ToolANTLRParser in project antlr4 by antlr.
the class Tool method parse.
public GrammarRootAST parse(String fileName, CharStream in) {
try {
GrammarASTAdaptor adaptor = new GrammarASTAdaptor(in);
ToolANTLRLexer lexer = new ToolANTLRLexer(in, this);
CommonTokenStream tokens = new CommonTokenStream(lexer);
lexer.tokens = tokens;
ToolANTLRParser p = new ToolANTLRParser(tokens, this);
p.setTreeAdaptor(adaptor);
try {
ParserRuleReturnScope r = p.grammarSpec();
GrammarAST root = (GrammarAST) r.getTree();
if (root instanceof GrammarRootAST) {
((GrammarRootAST) root).hasErrors = lexer.getNumberOfSyntaxErrors() > 0 || p.getNumberOfSyntaxErrors() > 0;
assert ((GrammarRootAST) root).tokenStream == tokens;
if (grammarOptions != null) {
((GrammarRootAST) root).cmdLineOptions = grammarOptions;
}
return ((GrammarRootAST) root);
}
} catch (v3TreeGrammarException e) {
errMgr.grammarError(ErrorType.V3_TREE_GRAMMAR, fileName, e.location);
}
return null;
} catch (RecognitionException re) {
// TODO: do we gen errors now?
ErrorManager.internalError("can't generate this message at moment; antlr recovers");
}
return null;
}
use of org.antlr.v4.parse.ToolANTLRParser 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;
}
Aggregations