make sure we track Input.Region for epsilon reductions (important for ambiguity-hunting)
[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         //sb.append("\noffending text: ");
24         for(Tree<?> result : ht) {
25             sb.append("\n  possibility: ");
26             StringBuffer sb2 = new StringBuffer();
27             result.toPrettyString(sb2);
28             sb.append(StringUtil.indent(sb2.toString(), 15));
29         }
30         return sb.toString();
31     }
32 }