Skip to content

Commit

Permalink
Merge pull request #2036 from usethesource/recovery/optimize-fix-errors
Browse files Browse the repository at this point in the history
Optimized fixErrorAppl
  • Loading branch information
PieterOlivier authored Oct 3, 2024
2 parents ad5f036 + 409aa57 commit 5cf6b57
Showing 1 changed file with 40 additions and 39 deletions.
79 changes: 40 additions & 39 deletions src/org/rascalmpl/parser/gtd/SGTDBF.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.concurrent.atomic.AtomicBoolean;

import org.rascalmpl.parser.gtd.debug.IDebugListener;
import org.rascalmpl.parser.gtd.exception.ParseError;
Expand Down Expand Up @@ -48,6 +47,8 @@
import org.rascalmpl.parser.util.ParseStateVisualizer;
import org.rascalmpl.util.visualize.dot.NodeId;
import org.rascalmpl.values.RascalValueFactory;
import org.rascalmpl.values.parsetrees.ITree;
import org.rascalmpl.values.parsetrees.TreeAdapter;

import io.usethesource.vallang.IConstructor;
import io.usethesource.vallang.IList;
Expand Down Expand Up @@ -134,7 +135,7 @@ public abstract class SGTDBF<P, T, S> implements IGTD<P, T, S> {
private final DoubleStack<AbstractStackNode<P>, AbstractNode> filteredNodes;

// Error reporting guards
private boolean parseErrorOccured;
private boolean parseErrorEncountered;

// Error recovery
private IRecoverer<P> recoverer;
Expand Down Expand Up @@ -958,7 +959,7 @@ private boolean findFirstStacksToReduce() {
return findStacksToReduce();
}

parseErrorOccured = true;
parseErrorEncountered = true;
}

return false;
Expand Down Expand Up @@ -1016,14 +1017,14 @@ private boolean findStacksToReduce() {
return findStacksToReduce();
}

parseErrorOccured = true;
parseErrorEncountered = true;
}

return false;
}

public boolean parseErrorHasOccurred() {
return parseErrorOccured;
return parseErrorEncountered;
}

/**
Expand Down Expand Up @@ -1449,7 +1450,7 @@ protected AbstractNode parse(AbstractStackNode<P> startNode, URI inputURI, int[]

try {
// A parse error occured, and recovery failed as well
parseErrorOccured = true;
parseErrorEncountered = true;

int errorLocation = (location == Integer.MAX_VALUE ? 0 : location);
int line = positionStore.findLine(errorLocation);
Expand Down Expand Up @@ -1594,7 +1595,7 @@ protected T buildResult(AbstractNode result, INodeFlattener<T, S> converter,
actionExecutor.completed(rootEnvironment, (parseResult == null));
}
if (parseResult != null) {
if (recoverer != null) {
if (recoverer != null && parseErrorEncountered) {
parseResult = introduceErrorNodes(parseResult, nodeConstructorFactory);
}
return parseResult; // Success.
Expand Down Expand Up @@ -1639,13 +1640,13 @@ private IConstructor introduceErrorNodes(IConstructor tree,
IConstructor result;
Type type = tree.getConstructorType();
if (type == RascalValueFactory.Tree_Appl) {
result = fixErrorAppl(tree, nodeConstructorFactory);
result = fixErrorAppl((ITree) tree, nodeConstructorFactory);
}
else if (type == RascalValueFactory.Tree_Char) {
result = tree;
}
else if (type == RascalValueFactory.Tree_Amb) {
result = fixErrorAmb(tree, nodeConstructorFactory);
result = fixErrorAmb((ITree) tree, nodeConstructorFactory);
}
else if (type == RascalValueFactory.Tree_Cycle) {
result = tree;
Expand All @@ -1662,50 +1663,52 @@ else if (type == RascalValueFactory.Tree_Cycle) {
return result;
}

private IConstructor fixErrorAppl(IConstructor tree,
private IConstructor fixErrorAppl(ITree tree,
INodeConstructorFactory<IConstructor, S> nodeConstructorFactory) {
IValue prod = tree.get(0);
IList childList = (IList) tree.get(1);
int childCount = childList.length();
IValue prod = TreeAdapter.getProduction(tree);
IList childList = TreeAdapter.getArgs(tree);

ArrayList<IConstructor> children = new ArrayList<>(childCount);
boolean anyChanges = false;
ArrayList<IConstructor> newChildren = null;
boolean errorTree = false;
int childCount = childList.length();
for (int i = 0; i < childCount; i++) {
if (i == childCount - 1) {
// Last child could be a skipped child
IConstructor last = (IConstructor) childList.get(childCount - 1);
if (last.getConstructorType() == RascalValueFactory.Tree_Appl) {
IConstructor lastProd = (IConstructor) last.get(0);
if (lastProd.getConstructorType() == RascalValueFactory.Production_Skipped) {
errorTree = true;
children.add(last);
break;
}
}
IConstructor child = (IConstructor) childList.get(i);
IConstructor newChild = null;

// Last child could be a skipped child
if (i == childCount - 1
&& child.getConstructorType() == RascalValueFactory.Tree_Appl
&& TreeAdapter.getProduction((ITree)child).getConstructorType() == RascalValueFactory.Production_Skipped) {
errorTree = true;
newChild = child;
} else {
newChild = introduceErrorNodes(child, nodeConstructorFactory);
}

IConstructor child = (IConstructor) childList.get(i);
IConstructor resultChild = introduceErrorNodes(child, nodeConstructorFactory);
children.add(resultChild);
if (resultChild != child) {
anyChanges = true;
if (newChild != child || errorTree) {
if (newChildren == null) {
newChildren = new ArrayList<>(childCount);
for (int j=0; j<i; j++) {
newChildren.add((IConstructor) childList.get(j));
}
}
newChildren.add(newChild);
}
}

if (errorTree) {
return nodeConstructorFactory.createErrorNode(children, prod);
return nodeConstructorFactory.createErrorNode(newChildren, prod);
}
else if (anyChanges) {
return nodeConstructorFactory.createSortNode(children, prod);
else if (newChildren != null) {
return nodeConstructorFactory.createSortNode(newChildren, prod);
}

return tree;
}

private IConstructor fixErrorAmb(IConstructor tree,
private IConstructor fixErrorAmb(ITree tree,
INodeConstructorFactory<IConstructor, S> nodeConstructorFactory) {
ISet alternativeSet = (ISet) tree.get(0);
ISet alternativeSet = TreeAdapter.getAlternatives(tree);
ArrayList<IConstructor> alternatives = new ArrayList<>(alternativeSet.size());
boolean anyChanges = false;
for (IValue alt : alternativeSet) {
Expand All @@ -1714,7 +1717,7 @@ private IConstructor fixErrorAmb(IConstructor tree,
anyChanges = true;
}
alternatives.add(newAlt);
};
}

if (anyChanges) {
return nodeConstructorFactory.createAmbiguityNode(alternatives);
Expand All @@ -1723,8 +1726,6 @@ private IConstructor fixErrorAmb(IConstructor tree,
return tree;
}



/**
* Datastructure visualization for debugging purposes
*/
Expand Down

0 comments on commit 5cf6b57

Please sign in to comment.