package org.spoofax.jsglr2.incremental.parseforest;

import org.metaborg.parsetable.IProduction;
import org.metaborg.parsetable.IState;
import org.metaborg.parsetable.ProductionType;
import org.spoofax.jsglr2.parseforest.IDerivation;
import org.spoofax.jsglr2.util.TreePrettyPrinter;

public class IncrementalDerivation implements IDerivation<IncrementalParseForest> {
    private final int width;
    private final IProduction production;
    private final ProductionType productionType;
    public final IncrementalParseForest[] parseForests;
    public final IState state;
    private boolean changed;

    public IncrementalDerivation(IProduction production, ProductionType productionType,
        IncrementalParseForest[] parseForests, IState state) {

        this.production = production;
        this.productionType = productionType;
        this.parseForests = parseForests;
        this.state = state;
        int width = 0;
        for(IncrementalParseForest parseForest : parseForests) {
            if(parseForest == null)
                continue; // Skippable parse nodes are null
            width += parseForest.width();
        }
        this.width = width;
    }

    @Override public int width() {
        return width;
    }

    @Override public IProduction production() {
        return changed ? null : production;
    }

    @Override public ProductionType productionType() {
        return changed ? null : productionType;
    }

    @Override public IncrementalParseForest[] parseForests() {
        return parseForests;
    }

    public void markChanged() {
        this.changed = true;
    }

    protected void prettyPrint(TreePrettyPrinter printer) {
        if(production == null)
            printer.println("d null : {");
        else
            printer.println("d" + production.id() + " : " + production.descriptor() + "{ (s" + state.id() + ")");
        printer.indent(2);

        for(IncrementalParseForest parseForest : parseForests) {
            if(parseForest != null) {
                parseForest.prettyPrint(printer);
            } else
                printer.println("null");
        }

        printer.indent(-2);
        printer.println("}");
    }


}

@@ -572,37 +572,8 @@
 ate;
-%0A    private boolean changed;
 %0A%0A  
@@ -1273,33 +1273,16 @@
   return
- changed ? null :
  product
@@ -1282,24 +1282,24 @@
 production;%0A
+
     %7D%0A%0A    @
@@ -1366,25 +1366,8 @@
 turn
- changed ? null :
  pro
@@ -1445,24 +1445,24 @@
 Forests() %7B%0A
+
         retu
@@ -1489,76 +1489,8 @@
  %7D%0A%0A
-    public void markChanged() %7B%0A        this.changed = true;%0A    %7D%0A%0A
     

Remove `changed` field from IncrementalDerivation