/**
 * MySQL类型数据源的自动提示和错误提示生成实现类
 */
import {CharStream, CharStreams, CommonTokenStream, Token, TokenStreamRewriter} from "antlr4ts";
import {MysqlListener} from "../../../listener/mysql/mysqlListener";
import {EditorContext} from "../../..";
import {SymbolTable} from "../../../symbols/symbolTable";
import {MySqlLexer} from "../../../grammar/mysql/MySqlLexer";
import {Recovermark} from "../../../strategy/recovermark";
import {MySqlParser} from "../../../grammar/mysql/MySqlParser";
import {MySqlErrorStrategy} from "../../../strategy/mysql/mySqlErrorStrategy";
import {ParseTreeWalker} from "antlr4ts/tree";
import {AbstractParseService} from "../abstract/abstractParseService";
import {MysqlKeyWord} from "../../../keyword/mysql/mysqlKeyWord";

export class MysqlParseService extends AbstractParseService{

    editorContext: EditorContext;
    symbolTable: SymbolTable;


    parserSQL(): CommonTokenStream {
        let charStream:CharStream = CharStreams.fromString(this.editorContext.command);
        let lexer:MySqlLexer = new MySqlLexer(charStream);
        let tokenStream:CommonTokenStream = new CommonTokenStream(lexer);

        Recovermark.rewriteStream = new TokenStreamRewriter(tokenStream);
        Recovermark.editorContext = this.editorContext;

        let parser:MySqlParser = new MySqlParser(tokenStream);
        parser.errorHandler = new MySqlErrorStrategy();

        let rootContext = parser.root();
        let walker = new ParseTreeWalker();
        let listener = new MysqlListener(this.symbolTable);
        walker.walk(listener, rootContext);

        return tokenStream;
    }


    getKeyWorld(): string[] {
        return MysqlKeyWord.keyWordList;
    }


}