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

Fixed the case where one of the alternative of an ambiguity is an emp… #2084

Merged
Merged
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
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
Loading