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 1 commit
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, 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
Loading