Skip to content

Commit

Permalink
Merge pull request #2084 from usethesource/recovery/empty-error-disam…
Browse files Browse the repository at this point in the history
…biguation-fix

Fixed the case where one of the alternative of an ambiguity is an emp…
  • Loading branch information
PieterOlivier authored Dec 17, 2024
2 parents c460e2c + 8c405fc commit 1aee3c4
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, boole
} else if (type == RascalValueFactory.Tree_Amb) {
result = disambiguateAmb((ITree) tree, allowAmbiguity, buildTree, 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, boolean
}

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, buildTree, processedTrees);
totalScore += disambiguatedArg.score;
hasErrors |= disambiguatedArg.hasErrors;
if (buildTree && 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, boolean
resultTree = appl;
}

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

processedTrees.put(appl, result);
Expand All @@ -128,17 +132,17 @@ private ScoredTree disambiguateAmb(ITree amb, boolean allowAmbiguity, boolean bu
ScoredTree errorAltWithBestScore = null;
for (IValue alt : originalAlts) {
ScoredTree disambiguatedAlt = disambiguate((IConstructor) alt, allowAmbiguity, buildTree, 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 @@ -171,7 +175,7 @@ private ScoredTree disambiguateAmb(ITree amb, boolean allowAmbiguity, boolean bu
}
}

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

return result;
Expand Down

0 comments on commit 1aee3c4

Please sign in to comment.