From b8a597c8d1a29afc24f9b89f726d5b1a9b9aeec1 Mon Sep 17 00:00:00 2001 From: adam Date: Thu, 20 Jul 2006 23:19:04 -0400 Subject: [PATCH] checkpoint darcs-hash:20060721031904-5007d-f4b754f8ca999c9f468c403d4a66ade90e8f121e.gz --- TODO | 2 +- src/edu/berkeley/sbp/Ambiguous.java | 7 +++-- src/edu/berkeley/sbp/ParseFailed.java | 10 +++---- src/edu/berkeley/sbp/Parser.java | 2 +- src/edu/berkeley/sbp/Sequence.java | 6 ++--- src/edu/berkeley/sbp/Union.java | 28 +++++++++++++++++--- src/edu/berkeley/sbp/Walk.java | 2 +- src/edu/berkeley/sbp/meta/MetaGrammarBindings.java | 2 +- src/edu/berkeley/sbp/tib/TibDoc.java | 2 +- src/edu/berkeley/sbp/util/PrintableTree.java | 2 ++ 10 files changed, 45 insertions(+), 18 deletions(-) diff --git a/TODO b/TODO index 0c2d747..b476721 100644 --- 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 really be type-parameterized? - More topology untangling diff --git a/src/edu/berkeley/sbp/Ambiguous.java b/src/edu/berkeley/sbp/Ambiguous.java index 6288f1f..1acd8cd 100644 --- a/src/edu/berkeley/sbp/Ambiguous.java +++ b/src/edu/berkeley/sbp/Ambiguous.java @@ -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> ht; - public Ambiguous(Forest ambiguity, HashSet> ht) { + Ambiguous(Forest ambiguity, HashSet> 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(); diff --git a/src/edu/berkeley/sbp/ParseFailed.java b/src/edu/berkeley/sbp/ParseFailed.java index d1e60ae..316caaf 100644 --- a/src/edu/berkeley/sbp/ParseFailed.java +++ b/src/edu/berkeley/sbp/ParseFailed.java @@ -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 touched = new HashSet(); - public static void complain(GSS.Phase.Node n, HashMap> errors, boolean force) { + static void complain(GSS.Phase.Node n, HashMap> 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 nodes) { + static String error(String message, Object token, Iterable nodes) { String lookAhead = token==null ? "" : token.toString(); StringBuffer ret = new StringBuffer(); ret.append("\n "); diff --git a/src/edu/berkeley/sbp/Parser.java b/src/edu/berkeley/sbp/Parser.java index 9825a9f..4aa6515 100644 --- a/src/edu/berkeley/sbp/Parser.java +++ b/src/edu/berkeley/sbp/Parser.java @@ -21,7 +21,7 @@ public abstract class Parser { public String toString() { return pt.toString(); } - /** parse input, using the table pt to drive the parser */ + /** parse input, and return the shared packed parse forest (or throw an exception) */ public Forest parse(Input input) throws IOException, ParseFailed { GSS gss = new GSS(); Input.Location loc = input.getLocation(); diff --git a/src/edu/berkeley/sbp/Sequence.java b/src/edu/berkeley/sbp/Sequence.java index 1d56343..975b2b1 100644 --- a/src/edu/berkeley/sbp/Sequence.java +++ b/src/edu/berkeley/sbp/Sequence.java @@ -19,8 +19,7 @@ public abstract class Sequence extends Element implements Iterable { 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 { 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 needs() { return needs; } public Iterable hates() { return hates; } @@ -90,7 +90,7 @@ public abstract class Sequence extends Element implements Iterable { // 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() { diff --git a/src/edu/berkeley/sbp/Union.java b/src/edu/berkeley/sbp/Union.java index 3348742..204404d 100644 --- a/src/edu/berkeley/sbp/Union.java +++ b/src/edu/berkeley/sbp/Union.java @@ -7,11 +7,19 @@ import java.util.*; import java.lang.reflect.*; import java.lang.ref.*; -/** an element which can produce one of several alternatives */ +/** + * an element which can produce one of several alternatives. + *

+ * + * Unlike the other Elements, Union is not immutable once + * constructed. To simulate this desirable feature, it is immutable + * once examined by taking its iterator or calling contains(). + */ public class Union extends Element implements Iterable { private final String name; private final boolean synthetic; + private boolean viewed = false; private final List alternatives = new ArrayList(); @@ -31,11 +39,20 @@ public class Union extends Element implements Iterable { this.synthetic = synthetic; } - public boolean contains(Sequence s) { return alternatives.contains(s); } - public Iterator iterator() { return alternatives.iterator(); } + public boolean contains(Sequence s) { + viewed = true; + return alternatives.contains(s); + } + + public Iterator 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 { 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 { 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 { } 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? diff --git a/src/edu/berkeley/sbp/Walk.java b/src/edu/berkeley/sbp/Walk.java index c6bb514..a82cf52 100644 --- a/src/edu/berkeley/sbp/Walk.java +++ b/src/edu/berkeley/sbp/Walk.java @@ -155,7 +155,7 @@ abstract class Walk { 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) { diff --git a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java index 74e8a31..41e343b 100644 --- a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java +++ b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java @@ -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; } } diff --git a/src/edu/berkeley/sbp/tib/TibDoc.java b/src/edu/berkeley/sbp/tib/TibDoc.java index ea996ac..c67e541 100644 --- a/src/edu/berkeley/sbp/tib/TibDoc.java +++ b/src/edu/berkeley/sbp/tib/TibDoc.java @@ -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(); diff --git a/src/edu/berkeley/sbp/util/PrintableTree.java b/src/edu/berkeley/sbp/util/PrintableTree.java index 1b5ffc0..7577137 100644 --- a/src/edu/berkeley/sbp/util/PrintableTree.java +++ b/src/edu/berkeley/sbp/util/PrintableTree.java @@ -83,6 +83,8 @@ public abstract class PrintableTree implements Iterable sb.append("})"); } + // this is here to keep it out of the javadoc for Tree + public GraphViz.Node toGraphViz(GraphViz gv) { if (gv.hasNode(this)) return gv.createNode(this); GraphViz.Node n = gv.createNode(this); -- 1.7.10.4