Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reimplemented findAllErrors function in Java #2043

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/org/rascalmpl/library/util/ErrorRecovery.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.rascalmpl.library.util;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.Map;

import org.rascalmpl.interpreter.asserts.Ambiguous;
Expand Down Expand Up @@ -169,4 +171,46 @@ private ScoredTree disambiguateAmb(ITree amb, boolean allowAmbiguity, Map<IConst

return result;
}

public IList findAllErrors(IConstructor tree) {
IListWriter errors = rascalValues.listWriter();
collectErrors((ITree) tree, errors, new HashSet<>());
return errors.done();
}

private void collectErrors(ITree tree, IListWriter errors, Set<IConstructor> processedTrees) {
Type type = tree.getConstructorType();

if (type == RascalValueFactory.Tree_Appl) {
collectApplErrors(tree, errors, processedTrees);
} else if (type == RascalValueFactory.Tree_Amb) {
collectAmbErrors(tree, errors, processedTrees);
}
}

private void collectApplErrors(ITree appl, IListWriter errors, Set<IConstructor> processedTrees) {
if (!processedTrees.add(appl)) {
return;
}

if (ProductionAdapter.isError(appl.getProduction())) {
errors.append(appl);
}

IList args = TreeAdapter.getArgs(appl);
for (int i=0; i<args.size(); i++) {
IValue arg = args.get(i);
PieterOlivier marked this conversation as resolved.
Show resolved Hide resolved
collectErrors((ITree) arg, errors, processedTrees);
}
}

private void collectAmbErrors(ITree amb, IListWriter errors, Set<IConstructor> processedTrees) {
if (!processedTrees.add(amb)) {
return;
}

for (IValue alt : TreeAdapter.getAlternatives(amb)) {
collectErrors((ITree) alt, errors, processedTrees);
}
}
}
3 changes: 2 additions & 1 deletion src/org/rascalmpl/library/util/ErrorRecovery.rsc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import util::Maybe;
@synopsis{Check if a parse tree contains any error nodes, the result of error recovery.}
bool hasErrors(Tree tree) = /appl(error(_, _, _), _) := tree;

@javaClass{org.rascalmpl.library.util.ErrorRecovery}
@synopsis{Find all error productions in a parse tree.}
list[Tree] findAllErrors(Tree tree) = [err | /err:appl(error(_, _, _), _) := tree];
java list[Tree] findAllErrors(Tree tree);

@synopsis{Find the first production containing an error.}
Tree findFirstError(/err:appl(error(_, _, _), _)) = err;
Expand Down
4 changes: 4 additions & 0 deletions src/org/rascalmpl/values/parsetrees/ProductionAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ public static boolean isSkipped(IConstructor tree) {
return tree.getConstructorType() == RascalValueFactory.Production_Skipped;
}

public static boolean isError(IConstructor tree) {
return tree.getConstructorType() == RascalValueFactory.Production_Error;
}

public static boolean isSeparatedList(IConstructor tree) {
IConstructor rhs = getType(tree);
return SymbolAdapter.isIterPlusSeps(rhs) || SymbolAdapter.isIterStarSeps(rhs);
Expand Down
Loading