checkpoint
authoradam <adam@megacz.com>
Fri, 21 Jul 2006 03:19:04 +0000 (23:19 -0400)
committeradam <adam@megacz.com>
Fri, 21 Jul 2006 03:19:04 +0000 (23:19 -0400)
darcs-hash:20060721031904-5007d-f4b754f8ca999c9f468c403d4a66ade90e8f121e.gz

TODO
src/edu/berkeley/sbp/Ambiguous.java
src/edu/berkeley/sbp/ParseFailed.java
src/edu/berkeley/sbp/Parser.java
src/edu/berkeley/sbp/Sequence.java
src/edu/berkeley/sbp/Union.java
src/edu/berkeley/sbp/Walk.java
src/edu/berkeley/sbp/meta/MetaGrammarBindings.java
src/edu/berkeley/sbp/tib/TibDoc.java
src/edu/berkeley/sbp/util/PrintableTree.java

diff --git a/TODO b/TODO
index 0c2d747..b476721 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,7 +1,7 @@
 _____________________________________________________________________________
 Immediately
 
-  - Sequence shouldn't be an Element
+  - Sequence shouldn't be an Element -> make Union.add(Element)
   - Should Tree<T> really be type-parameterized?
 
   - More topology untangling
index 6288f1f..1acd8cd 100644 (file)
@@ -7,12 +7,15 @@ import java.util.*;
 
 /** if ambiguity checking is enabled, this exception is thrown to signal that the parse was ambiguous */
 public class Ambiguous extends Exception {
-    public final Forest<?> ambiguity;
+    final Forest<?> ambiguity;
     private final HashSet<Tree<?>> ht;
-    public Ambiguous(Forest<?> ambiguity, HashSet<Tree<?>> ht) {
+    Ambiguous(Forest<?> ambiguity, HashSet<Tree<?>> ht) {
         this.ambiguity = ambiguity;
         this.ht = ht;
     }
+
+    public Forest<?> getAmbiguity() { return ambiguity; }
+
     public String toString() {
         // FIXME: print the input region that was ambiguously matched
         StringBuffer sb = new StringBuffer();
index d1e60ae..316caaf 100644 (file)
@@ -12,14 +12,14 @@ public class ParseFailed extends Exception {
 
     private final Input.Location location;
     private final String message;
-    public ParseFailed() { this("", null); }
-    public ParseFailed(String message, Input.Location loc) { this.location = loc; this.message = message; }
+    ParseFailed() { this("", null); }
+    ParseFailed(String message, Input.Location loc) { this.location = loc; this.message = message; }
     public Input.Location getLocation() { return location; }
     public String toString() { return message/* + (location==null ? "" : (" at " + location))*/; }
 
     // FIXME
     private static HashSet<GSS.Phase.Node> touched = new HashSet<GSS.Phase.Node>();
-    public static <Tok> void complain(GSS.Phase<Tok>.Node n, HashMap<String,HashSet<String>> errors, boolean force) {
+    static <Tok> void complain(GSS.Phase<Tok>.Node n, HashMap<String,HashSet<String>> errors, boolean force) {
         if (touched.contains(n)) return;
         touched.add(n);
         for(Position p : n.state) {
@@ -35,7 +35,7 @@ public class ParseFailed extends Exception {
         }
     }
 
-    public static String el(Object e) {
+    static String el(Object e) {
         String s = e.toString();
         if (s.length()==0 || s.charAt(0)!='\"' || s.charAt(s.length()-1)!='\"') return /*ANSI.yellow(s)*/s;
         s = s.substring(1);
@@ -47,7 +47,7 @@ public class ParseFailed extends Exception {
         }
         return /*ANSI.purple(ret.toString())*/ret.toString();
     }
-    public static String error(String message, Object token, Iterable<Node> nodes) {
+    static String error(String message, Object token, Iterable<Node> nodes) {
         String lookAhead = token==null ? "<EOF>" : token.toString();
         StringBuffer ret = new StringBuffer();
         ret.append("\n  ");
index 9825a9f..4aa6515 100644 (file)
@@ -21,7 +21,7 @@ public abstract class Parser<Tok, Result> {
 
     public String toString() { return pt.toString(); }
 
-    /** parse <tt>input</tt>, using the table <tt>pt</tt> to drive the parser */
+    /** parse <tt>input</tt>, and return the shared packed parse forest (or throw an exception) */
     public Forest<Result> parse(Input<Tok> input) throws IOException, ParseFailed {
         GSS gss = new GSS();
         Input.Location loc = input.getLocation();
index 1d56343..975b2b1 100644 (file)
@@ -19,8 +19,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
 
     final Position          firstp;
 
-    public Atom follow = null;
-    public final Atom follow() { return follow; }
+    Atom follow = null;
 
     // Static Constructors //////////////////////////////////////////////////////////////////////////////
 
@@ -63,6 +62,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
 
     public Sequence and(Sequence s) { Sequence ret = dup(); ret.needs.add(s); return ret; }
     public Sequence not(Sequence s) { Sequence ret = dup(); ret.hates.add(s); s.hated.add(ret); return ret; }
+    public Sequence followedBy(Atom a) { Sequence ret = dup(); ret.follow = a; return ret; }
 
     public Iterable<Sequence> needs() { return needs; }
     public Iterable<Sequence> hates() { return hates; }
@@ -90,7 +90,7 @@ public abstract class Sequence extends Element implements Iterable<Element> {
     // Position //////////////////////////////////////////////////////////////////////////////
 
     /** the imaginary position before or after an element of a sequence; corresponds to an "LR item" */
-    public class Position implements IntegerMappable {
+    class Position implements IntegerMappable {
 
         private Forest zero = null;
         public Forest zero() {
index 3348742..204404d 100644 (file)
@@ -7,11 +7,19 @@ import java.util.*;
 import java.lang.reflect.*;
 import java.lang.ref.*;
 
-/** <font color=green>an element which can produce one of several alternatives</font> */
+/**
+ *  <font color=green>an element which can produce one of several alternatives</font>.
+ *  <p>
+ *
+ *  Unlike the other Elements, Union is not immutable once
+ *  constructed.  To simulate this desirable feature, it is immutable
+ *  <i>once examined</i> by taking its iterator or calling contains().
+ */
 public class Union extends Element implements Iterable<Sequence> {
 
     private final String name;
     private final boolean synthetic;
+    private boolean viewed = false;
 
     private final List<Sequence> alternatives = new ArrayList<Sequence>();
 
@@ -31,11 +39,20 @@ public class Union extends Element implements Iterable<Sequence> {
         this.synthetic = synthetic;
     }
 
-    public boolean contains(Sequence s) { return alternatives.contains(s); }
-    public Iterator<Sequence> iterator() { return alternatives.iterator(); }
+    public boolean contains(Sequence s) {
+        viewed = true;
+        return alternatives.contains(s);
+    }
+
+    public Iterator<Sequence> iterator() {
+        viewed = true;
+        return alternatives.iterator();
+    }
 
     /** adds an alternative */
     public void add(Sequence s) {
+        if (viewed)
+            throw new RuntimeException("attempt to add a Sequence to a Union that has already been examined");
         if (alternatives.contains(s)) return;
         alternatives.add(s);
     }
@@ -64,6 +81,7 @@ public class Union extends Element implements Iterable<Sequence> {
         return "(anon_union)";
     }
     public String toString() {
+        viewed = true;
         if (name != null) return name;
         StringBuffer sb = new StringBuffer();
         sb.append("(");
@@ -71,7 +89,10 @@ public class Union extends Element implements Iterable<Sequence> {
         sb.append(")");
         return sb.toString();
     }
+
+    /** display this union in long/expanded form */
     public StringBuffer toString(StringBuffer sb) {
+        viewed = true;
         if (synthetic) return sb;
         boolean first = true;
         String before = StringUtil.pad(15, getName()) + " = ";
@@ -87,6 +108,7 @@ public class Union extends Element implements Iterable<Sequence> {
     }
     
     private void bodyToString(StringBuffer sb, String before, String between) {
+        viewed = true;
         boolean first = true;
         for(Sequence s : this) {
             // FIXME: what to do here about printing out negated sequences?
index c6bb514..a82cf52 100644 (file)
@@ -155,7 +155,7 @@ abstract class Walk<T> {
 
             if (e instanceof Sequence) {
                 Sequence s = (Sequence)e;
-                if (s.follow() != null) cs = cs.intersect(s.follow().getTokenTopology());
+                if (s.follow != null) cs = cs.intersect(s.follow.getTokenTopology());
             }
 
             if (c != null && e==me) {
index 74e8a31..41e343b 100644 (file)
@@ -255,7 +255,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings {
                 }
             }
             if (this.follow != null)
-                ret.follow = this.follow.toAtom(cx);
+                ret = ret.followedBy(this.follow.toAtom(cx));
             return ret;
         }
     }
index ea996ac..c67e541 100644 (file)
@@ -731,7 +731,7 @@ toContex ll = prefix ++ (concatMap tl ll) ++ suffix
             FileOutputStream fos = new FileOutputStream("/Users/megacz/Desktop/out.dot");
             PrintWriter p = new PrintWriter(new OutputStreamWriter(fos));
             GraphViz gv = new GraphViz();
-            a.ambiguity.toGraphViz(gv);
+            a.getAmbiguity().toGraphViz(gv);
             gv.dump(p);
             p.flush();
             p.close();
index 1b5ffc0..7577137 100644 (file)
@@ -83,6 +83,8 @@ public abstract class PrintableTree<T extends PrintableTree> implements Iterable
         sb.append("})");
     }
 
+    // this is here to keep it out of the javadoc for Tree<T>
+    
     public GraphViz.Node toGraphViz(GraphViz gv) {
         if (gv.hasNode(this)) return gv.createNode(this);
         GraphViz.Node n = gv.createNode(this);