TwoPhaseParser.java 文件源码

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

项目:hypertalk-java 作者:
/**
 * "Second phase" parsing attempt. Will accept any valid HyperTalk script entry, but is less performant for inputs
 * 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}).
 * @throws HtSyntaxException Thrown if an error occurs while parsing the script.
 */
static Object parseLL(CompilationUnit compilationUnit, String scriptText) throws HtSyntaxException {
    HyperTalkErrorListener errors = new HyperTalkErrorListener();
    HyperTalkLexer lexer = new HyperTalkLexer(new CaseInsensitiveInputStream(scriptText));
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    HyperTalkParser parser = new HyperTalkParser(tokens);

    parser.setErrorHandler(new DefaultErrorStrategy());
    parser.getInterpreter().setPredictionMode(PredictionMode.LL);
    parser.removeErrorListeners();        // don't log to console
    parser.addErrorListener(errors);

    try {
        ParseTree tree = compilationUnit.getParseTree(parser);

        if (!errors.errors.isEmpty()) {
            throw errors.errors.get(0);
        }

        return new HyperTalkTreeVisitor().visit(tree);
    } catch (RecognitionException e) {
        throw new HtSyntaxException(e);
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号