corrected the reporting+alignment of error locations
[sbp.git] / src / edu / berkeley / sbp / Ambiguous.java
1 package edu.berkeley.sbp;
2 import edu.berkeley.sbp.*;
3 import edu.berkeley.sbp.util.*;
4 import edu.berkeley.sbp.Sequence.Position;
5 import java.io.*;
6 import java.util.*;
7
8 /** if ambiguity checking is enabled, this exception is thrown to signal that the parse was ambiguous */
9 public class Ambiguous extends Exception {
10     final Forest<?> ambiguity;
11     private final HashSet<Tree<?>> ht;
12     Ambiguous(Forest<?> ambiguity, HashSet<Tree<?>> ht) {
13         this.ambiguity = ambiguity;
14         this.ht = ht;
15     }
16
17     public Forest<?> getAmbiguity() { return ambiguity; }
18
19     public String toString() {
20         // FIXME: print the input region that was ambiguously matched
21         StringBuffer sb = new StringBuffer();
22         sb.append("unresolved ambiguity at "+ambiguity.getRegion()+"; shared subtrees are shown as \"*\" ");
23         for(Tree<?> result : ht) {
24             sb.append("\n  possibility: ");
25             StringBuffer sb2 = new StringBuffer();
26             result.toPrettyString(sb2);
27             sb.append(StringUtil.indent(sb2.toString(), 15));
28         }
29         return sb.toString();
30     }
31 }