From 6ae224025882c9929e2e4e9e8461decbf3b9cae4 Mon Sep 17 00:00:00 2001 From: adam Date: Sun, 28 May 2006 04:25:13 -0400 Subject: [PATCH] checkpoint darcs-hash:20060528082513-5007d-12704eb79ae6fa742756b42a2c5af7c9d96c8c3d.gz --- src/edu/berkeley/sbp/Repeat.java | 19 ++++++--- src/edu/berkeley/sbp/Sequence.java | 43 ++++++++++++--------- src/edu/berkeley/sbp/misc/MetaGrammar.java | 49 +++++++----------------- src/edu/berkeley/sbp/misc/MetaGrammarTree.java | 8 ++++ 4 files changed, 61 insertions(+), 58 deletions(-) diff --git a/src/edu/berkeley/sbp/Repeat.java b/src/edu/berkeley/sbp/Repeat.java index 9cce700..b7e7e39 100644 --- a/src/edu/berkeley/sbp/Repeat.java +++ b/src/edu/berkeley/sbp/Repeat.java @@ -8,11 +8,12 @@ import java.lang.reflect.*; import java.lang.ref.*; /** currently this class exports only static methods to create repetitions; there are no public instance methods or constructors */ -/* FIXME make private again */ public class Repeat extends Union { +class Repeat extends Union { + public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, Object tag) { + this(e, zeroOkay, manyOkay, null, false, tag); } public Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, Object tag) { - this(e, zeroOkay, manyOkay, separator, false, tag); - } + this(e, zeroOkay, manyOkay, separator, false, tag); } protected Repeat(final Element e, boolean zeroOkay, boolean manyOkay, final Element separator, boolean maximal, Object tag) { super(e+(!manyOkay ? "?" : (zeroOkay ? (maximal ? "**" : "*") : (maximal ? "++" : "+")))+(separator==null?"":("/"+separator)), true); if (zeroOkay) { @@ -34,13 +35,21 @@ import java.lang.ref.*; if (zeroOkay && separator != null) throw new RuntimeException("cannot create a maximal repetition of zero or more items with a separator (yet): " + this); for(Sequence s : this) - s.follow = new edu.berkeley.sbp.misc.MetaGrammar.Invert(separator); + s.follow = new Invert(separator); } public Maximal(final Atom e, boolean zeroOkay, boolean manyOkay, Object tag) { super(e, zeroOkay, manyOkay, null, true, tag); for(Sequence s : this) - s.follow = new edu.berkeley.sbp.misc.MetaGrammar.Invert(e); + s.follow = new Invert(e); } } + + /** an atom which tracks the inverse of some other atom */ + private static class Invert extends Atom { + private final Atom a; + public Invert(Atom a) { this.a = a; } + public Topology top() { return a.complement(); } + public String toString() { return "~"+a; } + } } diff --git a/src/edu/berkeley/sbp/Sequence.java b/src/edu/berkeley/sbp/Sequence.java index e5e984f..a75bafa 100644 --- a/src/edu/berkeley/sbp/Sequence.java +++ b/src/edu/berkeley/sbp/Sequence.java @@ -255,30 +255,37 @@ public abstract class Sequence extends Element implements Iterable { // Repeat ////////////////////////////////////////////////////////////////////////////// /** repeat zero or one times */ - public static Repeat maybe(Element e) { return new Repeat(e, true, false, null, null); } - public static Repeat maybe(Element e, Object tag) { return new Repeat(e, true, false, null, tag); } + public static Element maybe(Element e) { return new Repeat(e, true, false, null, null); } + public static Element maybe(Element e, Object tag) { return new Repeat(e, true, false, null, tag); } /** repeat zero or more times */ - public static Repeat many0(Element e) { return new Repeat(e, true, true, null, null); } - public static Repeat many0(Element e, Object tag) { return new Repeat(e, true, true, null, tag); } + public static Element many0(Element e) { return new Repeat(e, true, true, null, null); } + public static Element many0(Element e, Object tag) { return new Repeat(e, true, true, null, tag); } /** repeat zero or more times, separated by sep */ - public static Repeat many0(Element e, Element sep) { return new Repeat(e, true, true, sep, null); } - public static Repeat many0(Element e, Element sep, Object tag) { return new Repeat(e, true, true, sep, tag); } + public static Element many0(Element e, Element sep) { return new Repeat(e, true, true, sep, null); } + public static Element many0(Element e, Element sep, Object tag) { return new Repeat(e, true, true, sep, tag); } /** repeat one or more times */ - public static Repeat many1(Element e) { return new Repeat(e, false, true, null, null); } - public static Repeat many1(Element e, Object tag) { return new Repeat(e, false, true, null, tag); } + public static Element many1(Element e) { return new Repeat(e, false, true, null, null); } + public static Element many1(Element e, Object tag) { return new Repeat(e, false, true, null, tag); } /** repeat one or more times, separated by sep */ - public static Repeat many1(Element e, Element sep) { return new Repeat(e, false, true, sep, null); } - public static Repeat many1(Element e, Element sep, Object tag) { return new Repeat(e, false, true, sep, tag); } + public static Element many1(Element e, Element sep) { return new Repeat(e, false, true, sep, null); } + public static Element many1(Element e, Element sep, Object tag) { return new Repeat(e, false, true, sep, tag); } /** repeat zero or more times, matching a maximal sequence of atoms */ - public static Repeat maximal0(Atom e) { return new Repeat.Maximal(e, true, true, null); } - public static Repeat maximal0(Atom e, Object tag) { return new Repeat.Maximal(e, true, true, tag); } + public static Element maximal0(Atom e) { return new Repeat.Maximal(e, true, true, null); } + public static Element maximal0(Atom e, Object tag) { return new Repeat.Maximal(e, true, true, tag); } /** repeat one or more times, matching a maximal sequence of atoms */ - public static Repeat maximal1(Atom e) { return new Repeat.Maximal(e, false, true, null); } - public static Repeat maximal1(Atom e, Object tag) { return new Repeat.Maximal(e, false, true, tag); } + public static Element maximal1(Atom e) { return new Repeat.Maximal(e, false, true, null); } + public static Element maximal1(Atom e, Object tag) { return new Repeat.Maximal(e, false, true, tag); } /** repeat one or more times, separated by an atom sep, matching a maximal sequence */ - public static Repeat maximal1(Element e, Atom sep) { return new Repeat.Maximal(e, false, true, sep, null); } - public static Repeat maximal1(Element e, Atom sep, Object tag) { return new Repeat.Maximal(e, false, true, sep, tag); } - - + public static Element maximal1(Element e, Atom sep) { return new Repeat.Maximal(e, false, true, sep, null); } + public static Element maximal1(Element e, Atom sep, Object tag) { return new Repeat.Maximal(e, false, true, sep, tag); } + + public static Element repeatMaximal(Atom e, boolean zero, boolean many, Object tag) { + return new Repeat.Maximal(e, zero, many, tag); } + public static Element repeatMaximal(Element e, boolean zero, boolean many, Atom sep, Object tag) { + return new Repeat.Maximal(e, zero, many, sep, tag); } + public static Element repeat(Element e, boolean zero, boolean many, Object tag) { + return new Repeat(e, zero, many, tag); } + public static Element repeat(Element e, boolean zero, boolean many, Element sep, Object tag) { + return new Repeat(e, zero, many, sep, tag); } } diff --git a/src/edu/berkeley/sbp/misc/MetaGrammar.java b/src/edu/berkeley/sbp/misc/MetaGrammar.java index 285016b..53c9bed 100644 --- a/src/edu/berkeley/sbp/misc/MetaGrammar.java +++ b/src/edu/berkeley/sbp/misc/MetaGrammar.java @@ -9,32 +9,6 @@ public class MetaGrammar extends StringWalker { public static Object repeatTag = null; - /** an atom which tracks the possible tokenset of some element, provided that element can only match single-token sequences */ - public static class Infer extends Atom { - private final Element e; - public Infer(Element e) { this.e = e; } - public Topology top() { return (Topology)toAtom(e); } - public String toString() { return e.toString(); } - } - - public static Atom infer(Element e) { return new CharRange((Topology)Atom.toAtom(e)); } - - /** an atom which tracks the inverse of some other atom */ - public static class Invert extends Atom { - private final Atom a; - public Invert(Atom a) { this.a = a; } - public Topology top() { return a.complement(); } - public String toString() { return "~"+a; } - } - - public static class Hack extends Atom { - private final Atom a; - static final Topology leftright = CharRange.rightBrace.union(CharRange.leftBrace); - public Hack(Atom a) { this.a = a; } - public Topology top() { return a.minus(leftright); } - public String toString() { return a.toString(); } - } - public static Union make() throws Exception { return make(MetaGrammarTree.meta, "s"); } public static Union make(Tree tree, String nt) throws Exception { Meta.MetaGrammarFile mgf = new MetaGrammar.Meta.MetaGrammarFile(tree); @@ -44,9 +18,10 @@ public class MetaGrammar extends StringWalker { //////////////////////////////////////////////////////////////////////////////// - private static boolean strings; - private static Element set(Range.Set r) { if (strings) throw new Error(); return CharRange.set(r); } - private static Element string(String s) { return strings ? StringInput.string(s) : CharRange.string(s); } + private static Element set(Range.Set r) { return CharRange.set(r); } + private static Element string(String s) { return CharRange.string(s); } + private static Atom infer(Element e) { return infer(Atom.toAtom(e)); } + private static Atom infer(Topology t) { return new CharRange((Topology)t); } private MetaGrammar() { } @@ -227,7 +202,8 @@ public class MetaGrammar extends StringWalker { if (idx != -1) ret = Sequence.singleton(els, idx); else ret = Sequence.drop(els, false); } - if (this.followedBy != null) ret.follow = new Hack(infer(this.followedBy.build(bc))); + if (this.followedBy != null) + ret.follow = infer(this.followedBy.build(bc)); return ret; } private MetaConjunct(Tree t) { @@ -305,10 +281,12 @@ public class MetaGrammar extends StringWalker { public boolean maximal, zero, many; public Element build(BuildContext bc) { return !maximal - ? new Repeat(element.build(bc), zero, many, separator==null?null:separator.build(bc), null) - : separator==null - ? new Repeat.Maximal(infer(element.build(bc)), zero, many, null) - : new Repeat.Maximal(element.build(bc), zero, many, infer(separator.build(bc)), null); + ? (separator==null + ? Sequence.repeat(element.build(bc), zero, many, null, null) + : Sequence.repeat(element.build(bc), zero, many, separator.build(bc), null)) + : (separator==null + ? Sequence.repeatMaximal(infer(element.build(bc)), zero, many, null) + : Sequence.repeatMaximal(element.build(bc), zero, many, infer(separator.build(bc)), null)); } public MetaRepeat(MetaClause element, boolean maximal, MetaClause separator, boolean zero, boolean many) { this.separator = separator; @@ -379,7 +357,7 @@ public class MetaGrammar extends StringWalker { public MetaClause element; public MetaInvert(Tree t, MetaConjunct c) { this.element = make(t, c); } public String toString() { return "~"+element; } - public Element build(BuildContext bc) { return new Hack(new Invert(infer(element.build(bc)))); } + public Element build(BuildContext bc) { return infer(Atom.toAtom(element.build(bc)).complement()); } } } @@ -431,4 +409,5 @@ public class MetaGrammar extends StringWalker { p.flush(); os.close(); } + } diff --git a/src/edu/berkeley/sbp/misc/MetaGrammarTree.java b/src/edu/berkeley/sbp/misc/MetaGrammarTree.java index 0f40fed..7dd8cbe 100644 --- a/src/edu/berkeley/sbp/misc/MetaGrammarTree.java +++ b/src/edu/berkeley/sbp/misc/MetaGrammarTree.java @@ -44,6 +44,10 @@ public class MetaGrammarTree { + + + + // DO NOT EDIT STUFF BELOW: IT IS AUTOMATICALLY GENERATED new edu.berkeley.sbp.Tree(null, "grammar", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "=", new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, null, new edu.berkeley.sbp.Tree[] { new edu.berkeley.sbp.Tree(null, "G", new edu.berkeley.sbp.Tree[] { }), new edu.berkeley.sbp.Tree(null, "r", new edu.berkeley.sbp.Tree[] { }), @@ -564,3 +568,7 @@ new edu.berkeley.sbp.Tree(null, "grammar", new edu.berkeley.sbp.Tree[] { new edu + + + + -- 1.7.10.4