"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Rule = void 0;
const NonTerminal_js_1 = require("../core/NonTerminal.js");
const EBNFProduction_js_1 = require("./EBNFProduction.js");
const NamedRule_js_1 = require("./NamedRule.js");
class Rule {
    constructor(type, tgt, ...children) {
        this.type = type;
        this.tgt = tgt !== undefined
            ? tgt
            : new NonTerminal_js_1.NonTerminal(type + ":" + NonTerminal_js_1.NonTerminal.makeRandomSymbol());
        this.children = children;
    }
    withName(name = undefined) {
        return new NamedRule_js_1.NamedRule(this, name);
    }
    getTarget() {
        return this.tgt;
    }
    getRepresentedSymbol() {
        return this.tgt;
    }
    getChildren() {
        return this.children;
    }
    getEvaluator() {
        return this.evaluator;
    }
    setEvaluator(evaluator) {
        if (typeof (evaluator) === 'function')
            evaluator = { evaluate: evaluator };
        this.evaluator = evaluator;
        return this;
    }
    getAutocompleter() {
        return this.autocompleter;
    }
    setAutocompleter(autocompleter) {
        if (typeof (autocompleter) === 'function')
            autocompleter = { getAutocompletion: autocompleter };
        this.autocompleter = autocompleter;
        return this;
    }
    onSuccessfulParsed(listener) {
        this._onSuccessfulParsed = listener;
        return this;
    }
    getOnSuccessfulParsed() {
        return this._onSuccessfulParsed;
    }
    static addProduction(grammar, rule, left, ...right) {
        let production = new EBNFProduction_js_1.EBNFProduction(rule, left, ...right);
        grammar.addProduction(production);
        return production;
    }
    getNameForChild(idx) {
        if (this.parsedChildNames === undefined) {
            return undefined;
        }
        if (this.parsedChildNames.length == 1)
            return this.parsedChildNames[0];
        if (idx >= this.parsedChildNames.length)
            return "no name";
        return this.parsedChildNames[idx];
    }
    setParsedChildNames(...parsedChildNames) {
        this.parsedChildNames = parsedChildNames;
    }
}
exports.Rule = Rule;
//# sourceMappingURL=Rule.js.map