Interpreter.java 文件源码

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

项目:Health 作者:
/**
 * Interprets the given script.
 *
 * @param script
 *            the script to interpret.
 * @param context
 *            the script context.
 * @param interpreter
 *            a statement listener for interpreting the statements in the
 *            script.
 * @throws IOException
 *             if any I/O-exceptions occur.
 */
public static void interpret(final String script, final Context context, final StatementListener interpreter)
        throws IOException {
    Objects.requireNonNull(script);
    Objects.requireNonNull(context);

    // Converts the script to an input stream for the lexer
    InputStream is = new ByteArrayInputStream(script.getBytes("UTF-8"));
    ANTLRInputStream input = new ANTLRInputStream(is);

    // Creates a lexer and parser to parse the script
    MyScriptLexer lexer = new MyScriptLexer(input);
    MyScriptParser parser = new MyScriptParser(new CommonTokenStream(lexer));
    parser.setBuildParseTree(true);

    ParseTree tree = parser.parse();

    // Create a walker to walk the parse tree for interpreting
    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(interpreter, tree);
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号