"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const parseResult_1 = require("./vo/parseResult");
const parseServiceFactory_1 = require("../service/parseServiceFactory");
const recovermark_1 = require("../strategy/recovermark");
class EditorParser {
    static parse(editorContext) {
        let parseService = parseServiceFactory_1.ParseServiceFactory.createParseService(editorContext);
        return EditorParser.start(parseService);
    }
    //处理逻辑
    static start(parseService) {
        let result = new parseResult_1.ParseResult();
        //1.先对语句进行解析
        let tokenStream = parseService.parserSQL();
        if (tokenStream == undefined) {
            console.log("SQL语句解析错误，返回的tokenStream为undefined");
            return EditorParser.generateErrorParseResult(parseService);
        }
        //2.看看是否进行了修复，如果修复了就再解析一次
        if (recovermark_1.Recovermark.isRecover) {
            parseService.editorContext = recovermark_1.Recovermark.editorContext;
            tokenStream = parseService.parserSQL();
        }
        //3.如果解析还是出错了，就返回结果，进行错误提示，并进行关键字提示。
        if (recovermark_1.Recovermark.errorMarkList != undefined) {
            return EditorParser.generateErrorParseResult(parseService);
        }
        //4.找到当前需要补全的Token
        let token;
        try {
            token = parseService.findTokenByRangeOffSetInAST(tokenStream);
        }
        catch (err) {
            console.log("当前光标在AST中定位错误");
            console.log(err);
        }
        if (token == undefined) {
            console.log("当前光标在AST中定位错误");
            return EditorParser.generateErrorParseResult(parseService);
        }
        //5.在符号表中找到对应的符号元素
        let symbolElement = parseService.findSymbolElementByTokenInSymbolTable(token);
        if (symbolElement == undefined) {
            console.log("无法在符号表中找到当前token");
            return EditorParser.generateErrorParseResult(parseService);
        }
        //6.生成需要向后端发送补全请求的参数对象
        let completionRequest = parseService.generateCodeCompletionRequest(symbolElement);
        //7.构造统一的返回值，统一返回。
        return this.generateCorrectParseResult(completionRequest);
    }
    static generateErrorParseResult(parseService) {
        let result = new parseResult_1.ParseResult();
        result.isSucceed = false;
        result.errorMarkList = recovermark_1.Recovermark.errorMarkList;
        result.expectList = parseService.getKeyWorld();
        recovermark_1.Recovermark.errorMarkList = [];
        return result;
    }
    static generateCorrectParseResult(completionRequest) {
        let result = new parseResult_1.ParseResult();
        result.isSucceed = true;
        result.completionRequest = completionRequest;
        return result;
    }
}
exports.EditorParser = EditorParser;
