package parser;

/**
 * Factory for F2 grammar non-terminal objects.
 */
public class F2Fact extends ATVFactory {
    /**
     * Initializer lambda for this factory.
     */
    private ILambda _initializer = new ILambda() {
        public Object apply(Object param) {
            // change state to no-op
            _initializer = NoOpLambda.Singleton;

            // nothing to initialize
            return null;
        }
    };

    /**
     * Constructor for the F2 factory.
     *
     * @param tkz tokenizer to use
     */
    public F2Fact(ITokenizer tkz) {
        super(tkz);
    }

    /**
     * Make a token visitor to parse an E1a non-terminal.
     *
     * @return token visitor
     */
    public ITokVisitor makeVisitor() {
        initialize();
        return new IdToken.IIdVisitor() {
            public Object idCase(IdToken host, Object inp) {
                return new F2(host);
            }

            public Object defaultCase(AToken host, Object param) {
                throw new IllegalArgumentException("Wrong token: '" + host + "'");
            }
        };
    }

    /**
     * Make a token visitor that delegates to the given visitor in a chain of responsibility
     *
     * @param successor visitor to serve as successor in the chain
     */
    public ITokVisitor makeChainedVisitor(final ITokVisitor successor) {
        initialize();
        return new IdToken.AChainVis(successor) {
            public Object idCase(IdToken host, Object inp) {
                return new F2(host);
            }
        };
    }

    /**
     * Make the visitor.
     */
    private void initialize() {
        _initializer.apply(null);

    }
}

