Skip to content

Commit

Permalink
No longer building the result tree when just checking for regular amb…
Browse files Browse the repository at this point in the history
…iguities
  • Loading branch information
PieterOlivier committed Nov 11, 2024
1 parent ae9aa76 commit 40b1226
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions src/org/rascalmpl/library/util/ErrorRecovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ public ScoredTree(IConstructor tree, int score) {
*/

public IConstructor disambiguateErrors(IConstructor arg, IBool allowAmbiguity) {
return disambiguate(arg, allowAmbiguity.getValue(), new HashMap<>()).tree;
return disambiguate(arg, allowAmbiguity.getValue(), true, new HashMap<>()).tree;
}

private ScoredTree disambiguate(IConstructor tree, boolean allowAmbiguity, Map<IConstructor, ScoredTree> processedTrees) {
private ScoredTree disambiguate(IConstructor tree, boolean allowAmbiguity, boolean buildTree, Map<IConstructor, ScoredTree> processedTrees) {
Type type = tree.getConstructorType();
ScoredTree result;

if (type == RascalValueFactory.Tree_Appl) {
result = disambiguateAppl((ITree) tree, allowAmbiguity, processedTrees);
result = disambiguateAppl((ITree) tree, allowAmbiguity, buildTree, processedTrees);
} else if (type == RascalValueFactory.Tree_Amb) {
result = disambiguateAmb((ITree) tree, allowAmbiguity, processedTrees);
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);
Expand All @@ -67,7 +67,7 @@ private ScoredTree disambiguate(IConstructor tree, boolean allowAmbiguity, Map<I
return result;
}

private ScoredTree disambiguateAppl(ITree appl, boolean allowAmbiguity, Map<IConstructor, ScoredTree> processedTrees) {
private ScoredTree disambiguateAppl(ITree appl, boolean allowAmbiguity, boolean buildTree, Map<IConstructor, ScoredTree> processedTrees) {
ScoredTree result = processedTrees.get(appl);
if (result != null) {
return result;
Expand All @@ -83,9 +83,9 @@ private ScoredTree disambiguateAppl(ITree appl, boolean allowAmbiguity, Map<ICon
// 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);
ScoredTree disambiguatedArg = disambiguate((IConstructor) arg, allowAmbiguity, buildTree, processedTrees);
totalScore += disambiguatedArg.score;
if (disambiguatedArg.tree != arg && disambiguatedArgs == null) {
if (buildTree && disambiguatedArg.tree != arg && disambiguatedArgs == null) {
disambiguatedArgs = rascalValues.listWriter();
for (int j=0; j<i; j++) {
disambiguatedArgs.append(args.get(j));
Expand All @@ -98,11 +98,11 @@ private ScoredTree disambiguateAppl(ITree appl, boolean allowAmbiguity, Map<ICon
}

// Only build a new tree if at least one of the arguments has changed
ITree resultTree;
ITree resultTree = null;
if (disambiguatedArgs != null) {
// Some arguments have changed
resultTree = TreeAdapter.setArgs(appl, disambiguatedArgs.done());
} else {
} else if (buildTree) {
// None of the arguments have changed
resultTree = appl;
}
Expand All @@ -115,7 +115,7 @@ private ScoredTree disambiguateAppl(ITree appl, boolean allowAmbiguity, Map<ICon
return result;
}

private ScoredTree disambiguateAmb(ITree amb, boolean allowAmbiguity, Map<IConstructor, ScoredTree> processedTrees) {
private ScoredTree disambiguateAmb(ITree amb, boolean allowAmbiguity, boolean buildTree, Map<IConstructor, ScoredTree> processedTrees) {
ScoredTree result = processedTrees.get(amb);
if (result != null) {
return result;
Expand All @@ -124,9 +124,10 @@ private ScoredTree disambiguateAmb(ITree amb, boolean allowAmbiguity, Map<IConst
ISet originalAlts = (ISet) amb.get(0);

ISetWriter alternativesWithoutErrors = null;

ScoredTree errorAltWithBestScore = null;
for (IValue alt : originalAlts) {
ScoredTree disambiguatedAlt = disambiguate((IConstructor) alt, allowAmbiguity, processedTrees);
ScoredTree disambiguatedAlt = disambiguate((IConstructor) alt, allowAmbiguity, buildTree, processedTrees);
if (disambiguatedAlt.score == 0) {
// Non-error tree
if (alternativesWithoutErrors == null) {
Expand All @@ -149,20 +150,24 @@ private ScoredTree disambiguateAmb(ITree amb, boolean allowAmbiguity, Map<IConst

ISet remainingAlts = alternativesWithoutErrors.done();

ITree resultTree;
if (remainingAlts.size() == originalAlts.size()) {
// All children are without errors, return the original tree
resultTree = amb;
} else if (remainingAlts.size() == 1) {
// One child without errors remains, dissolve the amb tree
resultTree = (ITree) remainingAlts.iterator().next();
} else {
// Create a new amb tree with the remaining non-error trees
resultTree = rascalValues.amb(remainingAlts);
ITree resultTree = null;

if (remainingAlts.size() > 1 && !allowAmbiguity) {
// We have an ambiguity between non-error trees
if (!allowAmbiguity) {
throw new Ambiguous(resultTree);
resultTree = rascalValues.amb(remainingAlts);
throw new Ambiguous(resultTree);

Check warning on line 158 in src/org/rascalmpl/library/util/ErrorRecovery.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/util/ErrorRecovery.java#L157-L158

Added lines #L157 - L158 were not covered by tests
}

if (buildTree) {
if (remainingAlts.size() == originalAlts.size()) {
// All children are without errors, return the original tree
resultTree = amb;

Check warning on line 164 in src/org/rascalmpl/library/util/ErrorRecovery.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/util/ErrorRecovery.java#L164

Added line #L164 was not covered by tests
} else if (remainingAlts.size() == 1) {
// One child without errors remains, dissolve the amb tree
resultTree = (ITree) remainingAlts.iterator().next();
} else {
// Create a new amb tree with the remaining non-error trees
resultTree = rascalValues.amb(remainingAlts);

Check warning on line 170 in src/org/rascalmpl/library/util/ErrorRecovery.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/util/ErrorRecovery.java#L170

Added line #L170 was not covered by tests
}
}

Expand Down Expand Up @@ -219,6 +224,6 @@ private void collectAmbErrors(ITree amb, IListWriter errors, Set<IConstructor> p
}

public void checkForRegularAmbiguities(IConstructor parseForest) {
disambiguate(parseForest, false, new HashMap<>());
disambiguate(parseForest, false, false, new HashMap<>());
}

Check warning on line 228 in src/org/rascalmpl/library/util/ErrorRecovery.java

View check run for this annotation

Codecov / codecov/patch

src/org/rascalmpl/library/util/ErrorRecovery.java#L227-L228

Added lines #L227 - L228 were not covered by tests
}

0 comments on commit 40b1226

Please sign in to comment.