TwoPhaseParser.java 文件源码

java
阅读 20 收藏 0 点赞 0 评论 0

项目:hypertalk-java 作者:
/**
 * "First phase" parsing attempt. Provides better performance than {@link #parseLL(CompilationUnit, String)}, but
 * will erroneously report syntax errors when parsing script text utilizing certain parts of the grammar.
 *
 * @param compilationUnit The unit of work to compile/parse. Represents the grammar's start symbol that should be
 *                        used.
 * @param scriptText A plaintext representation of the HyperTalk script to parse
 * @return The root of the abstract syntax tree associated with the given compilation unit (i.e., {@link Script}),
 * or null if parsing fails.
 */
static Object parseSLL(CompilationUnit compilationUnit, String scriptText) {
    HyperTalkLexer lexer = new HyperTalkLexer(new CaseInsensitiveInputStream(scriptText));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    HyperTalkParser parser = new HyperTalkParser(tokens);

    parser.setErrorHandler(new BailErrorStrategy());
    parser.removeErrorListeners();
    parser.getInterpreter().setPredictionMode(PredictionMode.SLL);

    try {
        ParseTree tree = compilationUnit.getParseTree(parser);
        return new HyperTalkTreeVisitor().visit(tree);
    } catch (ParseCancellationException e) {
        return null;
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号