use of beast.util.treeparser.NewickLexer in project beast2 by CompEvol.
the class TreeParser method parseNewick.
/**
* Parse a newick-ish string and generate the BEAST tree it describes.
*
* @param newick string to parse
* @return root node of tree
*/
public Node parseNewick(String newick) {
CharStream charStream = CharStreams.fromString(newick);
// Custom parse/lexer error listener
BaseErrorListener errorListener = new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
throw new TreeParsingException(msg, charPositionInLine, line);
}
};
// Use lexer to produce token stream
NewickLexer lexer = new NewickLexer(charStream);
lexer.removeErrorListeners();
lexer.addErrorListener(errorListener);
CommonTokenStream tokens = new CommonTokenStream(lexer);
// Parse token stream to produce parse tree
NewickParser parser = new NewickParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(errorListener);
ParseTree parseTree = parser.tree();
// Traverse parse tree, constructing BEAST tree along the way
NewickASTVisitor visitor = new NewickASTVisitor();
return visitor.visit(parseTree);
}
Aggregations