Search in sources :

Example 1 with NewickParser

use of beast.util.treeparser.NewickParser 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);
}
Also used : NewickLexer(beast.util.treeparser.NewickLexer) ParseTree(org.antlr.v4.runtime.tree.ParseTree) NewickParser(beast.util.treeparser.NewickParser)

Aggregations

NewickLexer (beast.util.treeparser.NewickLexer)1 NewickParser (beast.util.treeparser.NewickParser)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1