"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tree_1 = require("antlr4/tree");
const parserErrorListener_1 = require("./parserErrorListener");
/**
 * Custom Parser class, subclass needs extends it.
 */
class BasicParser {
    parse(input, errorListener) {
        const parser = this.createParser(input);
        this._parser = parser;
        parser.removeErrorListeners();
        parser.addErrorListener(new parserErrorListener_1.default(errorListener));
        const parserTree = parser.program();
        return parserTree;
    }
    validate(input) {
        const lexerError = [];
        const syntaxErrors = [];
        const parser = this.createParser(input);
        this._parser = parser;
        parser.removeErrorListeners();
        parser.addErrorListener(new parserErrorListener_1.ParserErrorCollector(syntaxErrors));
        parser.program();
        return lexerError.concat(syntaxErrors);
    }
    /**
     * Visit parser tree
     * @param parserTree
     */
    // public abstract visit(visitor: any, parserTree: any);
    /**
     * The source string
     * @param input string
     */
    getAllTokens(input) {
        return this.createLexer(input).getAllTokens();
    }
    ;
    /**
     * Get Parser instance by input string
     * @param input
     */
    createParser(input) {
        const lexer = this.createLexer(input);
        const parser = this.createParserFromLexer(lexer);
        parser.buildParseTrees = true;
        this._parser = parser;
        return parser;
    }
    /**
     * 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 = parser.program();
        return tree.toStringTree(parser.ruleNames);
    }
    /**
     * Get List-like style tree string
     * @param parserTree
     */
    toString(parserTree) {
        return parserTree.toStringTree(this._parser.ruleNames);
    }
    /**
     * @param listener Listener instance extends ParserListener
     * @param parserTree parser Tree
     */
    listen(listener, parserTree) {
        tree_1.ParseTreeWalker.DEFAULT.walk(listener, parserTree);
    }
}
exports.default = BasicParser;
//# sourceMappingURL=basicParser.js.map