X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Fedu%2Fberkeley%2Fsbp%2FSequence.java;h=975b2b11802575d307372cc90b8445835e2e46da;hb=b8a597c8d1a29afc24f9b89f726d5b1a9b9aeec1;hp=0ac3500bfd476d358c509062fdc2785dce43b54b;hpb=c8a17fdd2e149fe5feecd96c71b7f2cab286ab96;p=sbp.git
diff --git a/src/edu/berkeley/sbp/Sequence.java b/src/edu/berkeley/sbp/Sequence.java
index 0ac3500..975b2b1 100644
--- a/src/edu/berkeley/sbp/Sequence.java
+++ b/src/edu/berkeley/sbp/Sequence.java
@@ -7,18 +7,28 @@ import java.util.*;
import java.lang.reflect.*;
import java.lang.ref.*;
-/** juxtaposition; zero or more adjacent Elements; can specify a rewriting */
+/** juxtaposition; zero or more adjacent Elements; can specify a rewriting */
public abstract class Sequence extends Element implements Iterable {
+ protected final Element[] elements;
+
+ final HashSet hated = new HashSet();
+
+ final HashSet needs = new HashSet();
+ final HashSet hates = new HashSet();
+
+ final Position firstp;
+
+ Atom follow = null;
+
// Static Constructors //////////////////////////////////////////////////////////////////////////////
abstract Sequence _clone();
Sequence dup() {
Sequence ret = _clone();
- for(Sequence s : needs) { ret.needs.add(s); s.needed.add(ret); }
+ for(Sequence s : needs) { ret.needs.add(s); }
for(Sequence s : hates) { ret.hates.add(s); s.hated.add(ret); }
ret.follow = follow;
- ret.lame = lame;
return ret;
}
@@ -26,7 +36,7 @@ public abstract class Sequence extends Element implements Iterable {
public static final Sequence empty = new Sequence.Constant.Empty();
/** after matching the sequence, do not add anything to the output tree */
- public static Sequence drop(Element[] e, boolean lame) { return new Constant.Drop(e, lame); }
+ public static Sequence drop(Element[] e) { return new Constant.Drop(e); }
/** after matching the sequence, insert a constant into the output tree */
public static Sequence constant(Element[] e, Object o) { return new Constant(e, o); }
@@ -45,29 +55,18 @@ public abstract class Sequence extends Element implements Iterable {
public static Sequence rewritingSequence(Object tag, Element[] e, boolean[] drops) {
return new RewritingSequence(tag, e, drops); }
- ////////////////////////////////////////////////////////////////////////////////
-
- public Element follow = null;
- public final Topology follow() { return follow==null ? null : Atom.toAtom(follow); }
+ public static Sequence regionRewritingSequence(Functor tagfunctor, Element[] e, boolean[] drops) {
+ return new RegionRewritingSequence(tagfunctor, e, drops); }
- Topology toAtom() {
- if (elements.length!=1)
- throw new RuntimeException("cannot invoke toAtom() on a Sequence with " + elements.length + " elements: " + this);
- return Atom.toAtom(elements[0]);
- }
+ ////////////////////////////////////////////////////////////////////////////////
- public Sequence and(Sequence s) { Sequence ret = dup(); ret.needs.add(s); s.needed.add(ret); return ret; }
+ 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; }
- protected final Element[] elements;
+ public Iterable needs() { return needs; }
+ public Iterable hates() { return hates; }
- final HashSet needed = new HashSet();
- final HashSet hated = new HashSet();
- final HashSet needs = new HashSet();
- final HashSet hates = new HashSet();
- public boolean lame = false;
-
- final Position firstp;
Position firstp() { return firstp; }
public Iterator iterator() { return new ArrayIterator(elements); }
@@ -77,10 +76,10 @@ public abstract class Sequence extends Element implements Iterable {
}
// DO NOT MESS WITH THE FOLLOWING LINE!!!
- private Forest.Ref epsilonForm = null;
+ private Forest.Many epsilonForm = null;
Forest epsilonForm() {
if (epsilonForm!=null) return epsilonForm;
- epsilonForm = new Forest.Ref();
+ epsilonForm = new Forest.Many();
epsilonForm.merge(firstp().rewrite(null, false));
return epsilonForm;
}
@@ -91,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() {
@@ -181,25 +180,22 @@ public abstract class Sequence extends Element implements Iterable {
public Constant(Element[] e, Object result) { super(e); this.result = result; }
Sequence _clone() { return new Constant(elements, result); }
public Forest postReduce(Input.Region loc, Forest[] args, Position p) {
- return (Forest)Forest.leaf(loc, result, p);
+ return (Forest)Forest.create(loc, result, null, false);
}
static class Drop extends Constant {
- Sequence _clone() { return new Drop(elements, lame); }
- public Drop(Element[] e, boolean lame) {
- super(e, null);
- this.lame = lame;
- }
+ Sequence _clone() { return new Drop(elements); }
+ public Drop(Element[] e) { super(e, null); }
}
static class Empty extends Sequence.Constant.Drop {
Sequence _clone() { return new Empty(); }
- public Empty() { super(new Element[] { }, false); } }
+ public Empty() { super(new Element[] { }); } }
}
static class Singleton extends Sequence {
private final int idx;
public Singleton(Element e) { this(new Element[] { e }, 0); }
public Singleton(Element[] e, int idx) { super(e); this.idx = idx; }
- public Forest postReduce(Input.Region loc, Forest[] args, Position p) { return (Forest)Forest.singleton(loc, args[idx], p); }
+ public Forest postReduce(Input.Region loc, Forest[] args, Position p) { return args[idx]; }
Sequence _clone() { return new Singleton(elements,idx); }
}
@@ -212,18 +208,34 @@ public abstract class Sequence extends Element implements Iterable {
Sequence _clone() { return new Unwrap(elements, drops); }
public Forest postReduce(Input.Region loc, Forest[] args, Position p) {
for(int i=0; i[] args2 = new Forest[count];
int j = 0;
for(int i=0; i tagf;
+ public RegionRewritingSequence(Functor tagfunctor, Element[] e, boolean[] drops) {
+ super(null, e, drops);
+ this.tagf = tagfunctor;
+ }
+ public Forest postReduce(Input.Region loc, Forest[] args, Position p) {
+ this.tag = tagf.invoke(loc);
+ Forest ret = super.postReduce(loc, args, p);
+ this.tag = null;
+ return ret;
}
}
static class RewritingSequence extends Sequence {
- /*private*/public final Object tag;
+ /*private*/public /*final*/ Object tag;
private final boolean[] drops;
private int count = 0;
Sequence _clone() { return new RewritingSequence(tag, elements, drops); }
@@ -239,7 +251,7 @@ public abstract class Sequence extends Element implements Iterable {
int j = 0;
for(int i=0; i