import {AbstractParseService} from "../abstract/abstractParseService";
import {CharStream, CharStreams, CommonTokenStream, TokenStreamRewriter} from "antlr4ts";
import {OracleKeyWord} from "../../../keyword/oracle/oracleKeyWord";
import {PlSqlLexer} from "../../../grammar/oracle/PlSqlLexer";
import {Recovermark} from "../../../strategy/recovermark";
import {OracleErrorStrategy} from "../../../strategy/oracle/oracleErrorStrategy";

import {OracleListener} from "../../../listener/oracle/oracleListener";
import {PlSqlParser} from "../../../grammar/oracle/PlSqlParser";
import {ParseTreeWalker} from "antlr4ts/tree";

export class OracleParseService extends AbstractParseService{

    getKeyWorld(): string[] {
        return OracleKeyWord.keyword;
    }

    parserSQL(): CommonTokenStream {

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

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

        let parser:PlSqlParser = new PlSqlParser(tokenStream);

        parser.errorHandler = new OracleErrorStrategy();

        let rootContext = parser.sql_script();
        let walker = new ParseTreeWalker();
        let listener = new OracleListener(this.symbolTable);
        // @ts-ignore
        walker.walk(listener, rootContext);

        return tokenStream;


    }

}