presto(二)——词法解析

基于上一篇博文:executeCommand(queryRunner, query, clientOptions.outputFormat);开始

//outputFormat:默认是csv格式
    private static void executeCommand(QueryRunner queryRunner, String query, OutputFormat outputFormat)
    {
        //默认使用";"将语句隔开,通过ANTLR4的词法解析获取语句列表。
        //比如非法的语句在这里识别不出来“sele * from events limit select” 也不会报错
        StatementSplitter splitter = new StatementSplitter(query);
        //遍历每一条语句
        for (Statement split : splitter.getCompleteStatements()) {
            if (!isEmptyStatement(split.statement())) {
                process(queryRunner, split.statement(), outputFormat, false);
            }
        }
        if (!isEmptyStatement(splitter.getPartialStatement())) {
            System.err.println("Non-terminated statement: " + splitter.getPartialStatement());
        }
    }
public StatementSplitter(String sql, Set delimiters)
    {
        //生成词流
        TokenSource tokens = getLexer(sql, delimiters);
        ImmutableList.Builder list = ImmutableList.builder();
        StringBuilder sb = new StringBuilder();
        while (true) {
            //获取一个词
            Token token = tokens.nextToken();
            //如果类型是结束则退出
            if (token.getType() == Token.EOF) {
                break;
            }
            //如果类型是一个句子的结束符号,默认是分号
            if (token.getType() == SqlBaseParser.DELIMITER) {
                String statement = sb.toString().trim();
                if (!statement.isEmpty()) {
                    //语句加入到列表中
                    list.add(new Statement(statement, token.getText()));
                }
                sb = new StringBuilder();
            }
            else {
                //不断组装词成一个语句
                sb.append(token.getText());
            }
        }
        //完整的语句列表
        this.completeStatements = list.build();
        //不完整的语句
        this.partialStatement = sb.toString().trim();
    }

你可能感兴趣的:(presto(二)——词法解析)