Skip to content

Commit

Permalink
Fixed the case where one of the alternative of an ambiguity is an emp…
Browse files Browse the repository at this point in the history
…ty error
  • Loading branch information
PieterOlivier committed Nov 19, 2024
1 parent ea5658f commit 2ce0092
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/org/rascalmpl/library/util/ErrorRecovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ public ErrorRecovery(IRascalValueFactory rascalValues) {
private static class ScoredTree {
public final IConstructor tree;
public final int score;
public final boolean hasErrors;

public ScoredTree(IConstructor tree, int score) {
public ScoredTree(IConstructor tree, int score, boolean hasErrors) {
this.tree = tree;
this.score = score;
this.hasErrors = hasErrors;
}
}

Expand All @@ -60,8 +62,8 @@ private ScoredTree disambiguate(IConstructor tree, boolean allowAmbiguity, Map<I
} else if (type == RascalValueFactory.Tree_Amb) {
result = disambiguateAmb((ITree) tree, allowAmbiguity, processedTrees);
} else {
// Other trees (cycle, char) do not have subtrees so they have a score of 0
result = new ScoredTree(tree, 0);
// Other trees (cycle, char) do not have subtrees so they have a score of 0 and no errors
result = new ScoredTree(tree, 0, false);
}

return result;
Expand All @@ -74,17 +76,19 @@ private ScoredTree disambiguateAppl(ITree appl, boolean allowAmbiguity, Map<ICon
}

if (ProductionAdapter.isSkipped(appl.getProduction())) {
result = new ScoredTree(appl, ((IList) appl.get(1)).length());
result = new ScoredTree(appl, ((IList) appl.get(1)).length(), true);
} else {
IList args = TreeAdapter.getArgs(appl);
int totalScore = 0;
boolean hasErrors = false;
IListWriter disambiguatedArgs = null;

// Disambiguate and score all children
for (int i=0; i<args.size(); i++) {
IValue arg = args.get(i);
ScoredTree disambiguatedArg = disambiguate((IConstructor) arg, allowAmbiguity, processedTrees);
totalScore += disambiguatedArg.score;
hasErrors |= disambiguatedArg.hasErrors;
if (disambiguatedArg.tree != arg && disambiguatedArgs == null) {
disambiguatedArgs = rascalValues.listWriter();
for (int j=0; j<i; j++) {
Expand All @@ -107,7 +111,7 @@ private ScoredTree disambiguateAppl(ITree appl, boolean allowAmbiguity, Map<ICon
resultTree = appl;
}

result = new ScoredTree(resultTree, totalScore);
result = new ScoredTree(resultTree, totalScore, hasErrors);
}

processedTrees.put(appl, result);
Expand All @@ -127,17 +131,17 @@ private ScoredTree disambiguateAmb(ITree amb, boolean allowAmbiguity, Map<IConst
ScoredTree errorAltWithBestScore = null;
for (IValue alt : originalAlts) {
ScoredTree disambiguatedAlt = disambiguate((IConstructor) alt, allowAmbiguity, processedTrees);
if (disambiguatedAlt.score == 0) {
if (disambiguatedAlt.hasErrors) {
// Only keep the best of the error trees
if (errorAltWithBestScore == null || errorAltWithBestScore.score > disambiguatedAlt.score) {
errorAltWithBestScore = disambiguatedAlt;
}
} else {
// Non-error tree
if (alternativesWithoutErrors == null) {
alternativesWithoutErrors = rascalValues.setWriter();
}
alternativesWithoutErrors.insert(disambiguatedAlt.tree);
} else {
// Only keep the best of the error trees
if (errorAltWithBestScore == null || errorAltWithBestScore.score > disambiguatedAlt.score) {
errorAltWithBestScore = disambiguatedAlt;
}
}
}

Expand Down Expand Up @@ -166,7 +170,7 @@ private ScoredTree disambiguateAmb(ITree amb, boolean allowAmbiguity, Map<IConst
}
}

result = new ScoredTree(resultTree, 0);
result = new ScoredTree(resultTree, 0, false);
processedTrees.put(amb, result);

return result;
Expand Down

0 comments on commit 2ce0092

Please sign in to comment.