"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const antlr4_1 = require("antlr4");
const AnalyticsSQLLexer_1 = require("../lib/analytics/AnalyticsSQLLexer");
const AnalyticsSQLParser_1 = require("../lib/analytics/AnalyticsSQLParser");
const basicParser_1 = require("./common/basicParser");
const parserErrorListener_1 = require("./common/parserErrorListener");
var SQLType;
(function (SQLType) {
    SQLType["SQL"] = "SQL";
    SQLType["WSQL"] = "WAND_SQL";
})(SQLType || (SQLType = {}));
class AnalyticsSQL extends basicParser_1.default {
    constructor(sqlType) {
        super();
        this._sqlType = sqlType;
    }
    createLexer(input) {
        const chars = new antlr4_1.InputStream(input.toUpperCase()); // Some Lexer only support uppercase token, So you need transform
        const lexer = new AnalyticsSQLLexer_1.AnalyticsSQLLexer(chars);
        return lexer;
    }
    createParserFromLexer(lexer) {
        const tokenStream = new antlr4_1.CommonTokenStream(lexer);
        return new AnalyticsSQLParser_1.AnalyticsSQLParser(tokenStream);
    }
    visitTree(parser) {
        var parserTree;
        switch (this._sqlType) {
            case SQLType.WSQL:
                parserTree = parser.singleStatement();
                break;
            case SQLType.SQL:
                parserTree = parser.singleSQLStatement();
                break;
            default:
                throw new Error('Not support SQL type: ' + this._sqlType);
                break;
        }
        return parserTree;
    }
    parse(input, errorListener) {
        const parser = this.createParser(input);
        this._parser = parser;
        parser.removeErrorListeners();
        parser.addErrorListener(new parserErrorListener_1.default(errorListener));
        return this.visitTree(parser);
    }
    validate(input) {
        const lexerError = [];
        const syntaxErrors = [];
        const parser = this.createParser(input);
        this._parser = parser;
        parser.removeErrorListeners();
        parser.addErrorListener(new parserErrorListener_1.ParserErrorCollector(syntaxErrors));
        this.visitTree(parser);
        return lexerError.concat(syntaxErrors);
    }
    /**
     * It convert tree to string, it's convenient to use in unit test.
     * @param string input
     */
    parserTreeToString(input) {
        const parser = this.createParser(input);
        this._parser = parser;
        const tree = this.visitTree(parser);
        return tree.toStringTree(parser.ruleNames);
    }
}
exports.default = AnalyticsSQL;
//# sourceMappingURL=analytics.js.map