package accrue;

import polyglot.frontend.EmptyPass;
import polyglot.frontend.ExtensionInfo;
import polyglot.frontend.JLExtensionInfo;
import polyglot.frontend.JLScheduler;
import polyglot.frontend.Job;
import polyglot.frontend.Pass;
import polyglot.frontend.goals.Goal;
import polyglot.frontend.goals.SourceFileGoal;

public class AccrueJLScheduler extends JLScheduler implements AccrueScheduler {

    AccrueSchedulerHelper helper;

    public AccrueJLScheduler(JLExtensionInfo extInfo) {
        super(extInfo);
        this.helper = createHelper();
    }

    protected AccrueSchedulerHelper createHelper() {
        return new AccrueSchedulerHelper(extInfo, this);
    }

    @Override
    public Goal Parsed(Job job) {
        AccrueExtensionInfo extInfo = (AccrueExtensionInfo) this.extInfo;
        if (extInfo.isOutputExtensionInfo() && job.ast() != null) {
            // this ExtensionInfo is being used as an output extension info, i.e.,
            // it is taking the output of some other extension info.
            // So if the ast for the job is non-null, then return an empty pass, so that we don't look
            // for the source files.
            // If it is null, then return the previous behavior.
            return internGoal(new SourceFileGoal(job) {
                @Override
                public Pass createPass(ExtensionInfo extInfo) {
                    return new EmptyPass(this);
                }
            });

        }
        return super.Parsed(job);
    }

    @Override
    public AccrueSchedulerHelper helper() {
        return this.helper;
    }

}
