8bed158335acac35bbec87a509a4d273920e8beb
[sbp.git] / src / edu / berkeley / sbp / Ambiguous.java
1 // Copyright 2006-2007 all rights reserved; see LICENSE file for BSD-style license
2
3 package edu.berkeley.sbp;
4 import edu.berkeley.sbp.util.*;
5 import java.util.*;
6
7 /** thrown to signal that a parse was ambiguous */
8 public class Ambiguous extends Exception {
9
10     private final Forest<?> ambiguity;
11     private final HashSet<Tree<?>> possibilities;
12
13     /**
14      *  @param possibilities is a specially-constructed set of trees with shared nodes replaced by '*'
15      */
16     Ambiguous(Forest<?> ambiguity, HashSet<Tree<?>> possibilities) {
17         this.ambiguity = ambiguity;
18         this.possibilities = possibilities;
19     }
20
21     public Forest<?> getForest() { return ambiguity; }
22
23     public String toString() {
24         StringBuffer sb = new StringBuffer();
25         sb.append("unresolved ambiguity at "+ambiguity.getRegion()+"; shared subtrees are shown as \"*\" ");
26         for(Tree<?> result : possibilities) {
27             sb.append("\n    possibility: ");
28             StringBuffer sb2 = new StringBuffer();
29             result.toPrettyString(sb2);
30             sb.append(StringUtil.indent(sb2.toString(), 15));
31         }
32         return sb.toString();
33     }
34 }