X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FMetaGrammar.java;h=53c9bedcad2ccfe7cf60cd5a523d89ae8902a01e;hp=285016bc8c3a6d097cefcd4c5fb6ccb85ffd7ada;hb=6ae224025882c9929e2e4e9e8461decbf3b9cae4;hpb=c48a9498daf7604e41cf4aea7f6f7110388fd659 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(); } + }