"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParsingWarehouse = void 0;
/* --------------------------------------------------------------------------------------------
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for license information.
 * ------------------------------------------------------------------------------------------ */
const antlr4ts_1 = require("antlr4ts");
const atn_1 = require("antlr4ts/atn");
const FlinkSQLLexer_1 = require("./lib/FlinkSQLLexer");
const FlinkSQLParser_1 = require("./lib/FlinkSQLParser");
const parse_error_listener_1 = require("./listeners/parse-error.listener");
class ParsingWarehouse {
    constructor() {
        this._warehouse = new Map();
    }
    setParsingValue(document, useSLL = true) {
        const text = document.getText();
        const charStream = antlr4ts_1.CharStreams.fromString(text);
        const lexer = new FlinkSQLLexer_1.FlinkSQLLexer(charStream);
        const tokenStream = new antlr4ts_1.CommonTokenStream(lexer);
        const parser = new FlinkSQLParser_1.FlinkSQLParser(tokenStream);
        const errorListener = new parse_error_listener_1.ParseErrorListener(document);
        lexer.removeErrorListeners();
        parser.removeErrorListeners();
        let tree;
        if (useSLL) {
            parser.errorHandler = new antlr4ts_1.BailErrorStrategy();
            parser.interpreter.setPredictionMode(atn_1.PredictionMode.SLL);
            try {
                // try with simpler/faster SLL(*)
                tree = parser.program();
            }
            catch (e) {
                // thrown by BailErrorStrategy
                parser.reset();
                // back to standard listeners/handlers
                parser.errorHandler = new antlr4ts_1.DefaultErrorStrategy();
                parser.addErrorListener(errorListener);
                // full now with full LL(*)
                parser.interpreter.setPredictionMode(atn_1.PredictionMode.LL);
                tree = parser.program();
            }
        }
        else {
            parser.addErrorListener(errorListener);
            tree = parser.program();
        }
        this._warehouse.set(document.uri, { origin: text, tokenStream, parser, tree, errors: errorListener.getErrors() });
    }
    hasParsingValue(document) {
        return this._warehouse.has(document.uri);
    }
    getParsingValue(document) {
        if (!this._warehouse.has(document.uri)) {
            this.setParsingValue(document);
        }
        return this._warehouse.get(document.uri);
    }
    removeParsingValue(documentUri) {
        this._warehouse.delete(documentUri);
    }
}
exports.ParsingWarehouse = ParsingWarehouse;
//# sourceMappingURL=parsing-warehouse.js.map