From: adam Date: Sun, 12 Feb 2006 00:22:31 +0000 (-0500) Subject: checkpoint X-Git-Tag: tag_for_25-Mar~290 X-Git-Url: http://git.megacz.com/?p=sbp.git;a=commitdiff_plain;h=b318adb49d46a596314d7b7c0dd9f52681abb449 checkpoint darcs-hash:20060212002231-5007d-afdf87886cd38407db887064b86058b72f8b517a.gz --- diff --git a/src/edu/berkeley/sbp/Forest.java b/src/edu/berkeley/sbp/Forest.java index 90c4e43..d47e721 100644 --- a/src/edu/berkeley/sbp/Forest.java +++ b/src/edu/berkeley/sbp/Forest.java @@ -43,14 +43,14 @@ public abstract class Forest /*extends PrintableTree>*/ impl } static Forest singleton(Input.Location loc, Position p) { - return create(loc, null, new Forest[] { }, false, true, p); } + return create(loc, null, new Forest[] { }, new Object[0], false, true, p); } static Forest singleton(Input.Location loc, Forest body, Position p) { //return create(loc, null, new Forest[] { body }, false, true, p); return body; } - static Forest leaf(Input.Location loc, T tag, Position p) { return create(loc, tag, null, false, false, p); } - public static Forest create(Input.Location loc, T tag, Forest[] tokens, boolean unwrap, boolean singleton, Position p) { - return new MyBody(loc, tag, tokens, unwrap, singleton, p); + static Forest leaf(Input.Location loc, T tag, Position p) { return create(loc, tag, null, null, false, false, p); } + public static Forest create(Input.Location loc, T tag, Forest[] tokens, Object[] labels, boolean unwrap, boolean singleton, Position p) { + return new MyBody(loc, tag, tokens, labels, unwrap, singleton, p); } // Body ////////////////////////////////////////////////////////////////////////////// @@ -78,7 +78,7 @@ public abstract class Forest /*extends PrintableTree>*/ impl if (i==tokens.length-1 && unwrap) { tokens[i].edges(n); } else { - n.edge(tokens[i]); + n.edge(tokens[i], labels==null?null:labels[i]); } } } @@ -90,11 +90,12 @@ public abstract class Forest /*extends PrintableTree>*/ impl private final Input.Location location; private final T tag; private final Forest[] tokens; + private final Object[] labels; private final boolean unwrap; private final boolean singleton; private final Sequence.Position reduction; - private MyBody(Input.Location loc, T tag, Forest[] tokens, boolean unwrap, boolean singleton, Position reduction) { + private MyBody(Input.Location loc, T tag, Forest[] tokens, Object[] labels, boolean unwrap, boolean singleton, Position reduction) { this.location = loc; this.tag = tag; this.tokens = tokens==null ? emptyForestArray : new Forest[tokens.length]; @@ -103,6 +104,7 @@ public abstract class Forest /*extends PrintableTree>*/ impl this.unwrap = unwrap; this.singleton = singleton; this.reduction = reduction; + this.labels = labels; } public void expand(final int i, final TreeMaker h) { @@ -122,12 +124,13 @@ public abstract class Forest /*extends PrintableTree>*/ impl } else { tokens[i].visit(new TreeMaker(h.toss) { public void start(T head, Input.Location loc) { } - public void addTree(Tree t) { toks.add(t); } + public void addTree(Tree t, Object label) { toks.add(t); labs.add(label); } public void finish(T head, Input.Location loc) { int old = h.toks.size(); - h.addTree(new Tree(loc, head, toks.toArray(tree_hint))); + h.addTree(new Tree(loc, head, toks.toArray(tree_hint), labs.toArray(string_hint)), labels==null?null:labels[i]); expand(i+1, h); while(h.toks.size() > old) h.toks.remove(h.toks.size()-1); + while(h.labs.size() > old) h.labs.remove(h.labs.size()-1); } }, null, null); } @@ -167,7 +170,7 @@ public abstract class Forest /*extends PrintableTree>*/ impl GraphViz.Node n = gv.createNode(this); n.label = "?"; n.color = "red"; - for(Forest f : hp) n.edge(f); + for(Forest f : hp) n.edge(f, null); return n; } @@ -183,24 +186,28 @@ public abstract class Forest /*extends PrintableTree>*/ impl private static class TreeMaker2 extends TreeMaker { private TreeConsumer tc; public TreeMaker2(boolean toss, TreeConsumer tc) { super(toss); this.tc = tc; } - public void finish(T head, Input.Location loc) { tc.addTree(new Tree(loc, head, toks.toArray(tree_hint)));; } + public void finish(T head, Input.Location loc) { tc.addTree(new Tree(loc, head, toks.toArray(tree_hint), labs.toArray(string_hint)));; } public void start(T head, Input.Location loc) { } - public void addTree(Tree t) { toks.add(t); } + public void addTree(Tree t, Object label) { toks.add(t); labs.add(label); } } - private static abstract class TreeMaker implements Invokable,Boolean,Integer>, TreeConsumer { + private static abstract class TreeMaker implements Invokable,Boolean,Integer>/*, TreeConsumer*/ { public ArrayList> toks = new ArrayList>(); + public ArrayList labs = new ArrayList(); private boolean toss; protected T head; public TreeMaker(boolean toss) { this.toss = toss; } public abstract void start(T head, Input.Location loc); public abstract void finish(T head, Input.Location loc); - public abstract void addTree(Tree t); + public abstract void addTree(Tree t, Object label); public void invoke(Forest.Body bod, Boolean o, Integer i) { if (i==null) { ArrayList> toks = this.toks; this.toks = new ArrayList>(); + ArrayList labs = this.labs; + this.labs = new ArrayList(); bod.expand(0, this); this.toks = toks; + this.labs = labs; } else { bod.expand(i, this); } @@ -210,6 +217,7 @@ public abstract class Forest /*extends PrintableTree>*/ impl // Statics ////////////////////////////////////////////////////////////////////////////// private static Tree[] tree_hint = new Tree[0]; + private static String[] string_hint = new String[0]; private static final Forest[] emptyForestArray = new Forest[0]; protected String headToString() { return null; } diff --git a/src/edu/berkeley/sbp/Repeat.java b/src/edu/berkeley/sbp/Repeat.java index bd0dbe0..4267b36 100644 --- a/src/edu/berkeley/sbp/Repeat.java +++ b/src/edu/berkeley/sbp/Repeat.java @@ -39,7 +39,7 @@ public class Repeat extends Union { if (manyOkay) add(new Sequence.Singleton(many1(e, separator))); else add(new Sequence.Singleton(e)); } else { - add(new Sequence.RewritingSequence(null, new Element[] { e })); + add(new Sequence.RewritingSequence(null, new Element[] { e }, null)); if (separator==null) add(new Sequence.Unwrap(new Element[] { e, Repeat.this })); else diff --git a/src/edu/berkeley/sbp/Sequence.java b/src/edu/berkeley/sbp/Sequence.java index 071c219..fb1262f 100644 --- a/src/edu/berkeley/sbp/Sequence.java +++ b/src/edu/berkeley/sbp/Sequence.java @@ -39,7 +39,8 @@ public abstract class Sequence extends Element implements Iterable { * @param e the elements to match * @param drops only elements of e whose corresponding boolean in drops is false will be included in the output tree **/ - public static Sequence rewritingSequence(Object tag, Element[] e, boolean[] drops) { return new RewritingSequence(tag, e, drops); } + public static Sequence rewritingSequence(Object tag, Element[] e, Object[] labs, boolean[] drops) { + return new RewritingSequence(tag, e, labs, drops); } //////////////////////////////////////////////////////////////////////////////// @@ -201,34 +202,36 @@ public abstract class Sequence extends Element implements Iterable { Sequence _clone() { return new Unwrap(elements, drops); } public Forest postReduce(Input.Location loc, Forest[] args, Position p) { for(int i=0; i[] args2 = new Forest[count]; int j = 0; for(int i=0; i Forest postReduce(Input.Location loc, Forest[] args, Position p) { Forest[] args2 = new Forest[count]; int j = 0; for(int i=0; i extends PrintableTree> implements Iterable> final T head; Tree[] children; + Object[] labels; final Input.Location location; public T head() { return head; } @@ -18,16 +19,23 @@ public class Tree extends PrintableTree> implements Iterable> public Iterable> children() { return new ArrayIterator(children); } public Iterator> iterator() { return new ArrayIterator(children); } public Tree child(int i) { return children[i]; } + public Object label(int i) { return labels[i]; } - public Input.Location getLocation() { return location; } + public Input.Location getLocation() { return location; } public Tree(Input.Location loc, T head) { this(loc, head, null); } - public Tree(Input.Location loc, T head, Tree[] children) { + public Tree(Input.Location loc, T head, Tree[] children) { this(loc, head, children, null); } + public Tree(Input.Location loc, T head, Tree[] children, Object[] labels) { this.location = loc; this.head = head; + Tree[] children2 = children==null ? new Tree[0] : new Tree[children.length]; if (children != null) System.arraycopy(children, 0, children2, 0, children.length); this.children = children2; + + Object[] labels2 = labels==null ? new Object[0] : new Object[labels.length]; + if (labels != null) System.arraycopy(labels, 0, labels2, 0, labels.length); + this.labels = labels2; } protected String headToString() { return head==null?null:head.toString(); } diff --git a/src/edu/berkeley/sbp/chr/CharParser.java b/src/edu/berkeley/sbp/chr/CharParser.java index b3ba6cb..657afaf 100644 --- a/src/edu/berkeley/sbp/chr/CharParser.java +++ b/src/edu/berkeley/sbp/chr/CharParser.java @@ -16,7 +16,7 @@ public class CharParser extends Parser { public CharParser(Union u) { super(u, new CharTopology()); } public Forest shiftToken(Character ct, Location loc) { - return Forest.create(loc, ct.toString(), null, false, false, null); + return Forest.create(loc, ct.toString(), null, null, false, false, null); } } diff --git a/src/edu/berkeley/sbp/misc/MetaGrammar.java b/src/edu/berkeley/sbp/misc/MetaGrammar.java index 92c05de..aa433d3 100644 --- a/src/edu/berkeley/sbp/misc/MetaGrammar.java +++ b/src/edu/berkeley/sbp/misc/MetaGrammar.java @@ -292,7 +292,7 @@ public class MetaGrammar extends StringWalker { Sequence ret = null; if (dropAll || lame) ret = Sequence.drop(expansion, lame); else if (unwrap) ret = new Sequence.Unwrap(expansion, drops); - else if (tag!=null) ret = Sequence.rewritingSequence(tag, expansion, drops); + else if (tag!=null) ret = Sequence.rewritingSequence(tag, expansion, null, drops); else { int idx = -1; for(int i=0; i edges = new ArrayList(); + public ArrayList labels = new ArrayList(); public ArrayList inbound = new ArrayList(); - public void edge(ToGraphViz o) { + public void edge(ToGraphViz o, Object label) { Node n = o.toGraphViz(GraphViz.this); if (n==null) return; edges.add(n); + labels.add(label); n.inbound.add(this); } public String name() { @@ -37,8 +39,11 @@ public class GraphViz { } public void edges(PrintWriter pw) { if (simple()) return; - for(Node n : edges) - pw.println(" "+name()+" -> " + n.name() + " [color="+color+"];\n"); + for(int i=0; i " + n.name() + " [color="+color+" " +(label==null?"":("label=\""+label+"\""))+ "];\n"); + } } public int numEdges() { return edges.size(); } public boolean simple() { diff --git a/tests/meta.g b/tests/meta.g index 062bf25..3ee8fdf 100644 --- a/tests/meta.g +++ b/tests/meta.g @@ -4,7 +4,7 @@ // #include (with renaming?) // ANTLR uses ! and ^ suffixes -s = ws grammar:Grammar ws +s = !ws (grammar::Grammar) !ws Grammar = NonTerminal +/ ws NonTerminal = Word ^"=" RHS /ws