X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmeta%2FMetaGrammarBindings.java;h=927cd6248b6aba71085ae5a27ccb9a0a57ed9ebf;hp=8f322a545937a665a127245da76b3b6f9de171f7;hb=9d727bd14c659cdc6c34153b988e8d3fdb8067f5;hpb=9b7ce1d3e4ac84ecd2d0f5f461b42c40f38e4783 diff --git a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java index 8f322a5..927cd62 100644 --- a/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java +++ b/src/edu/berkeley/sbp/meta/MetaGrammarBindings.java @@ -1,3 +1,5 @@ +// Copyright 2006 all rights reserved; see LICENSE file for BSD-style license + package edu.berkeley.sbp.meta; import edu.berkeley.sbp.util.*; import edu.berkeley.sbp.*; @@ -64,23 +66,26 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings { public abstract static class UnionNode extends ElementNode { public Seq[][] sequences; + public Atom toAtom(Context cx) { + Atom ret = null; + for(Seq[] ss : sequences) + for(Seq s : ss) + ret = ret==null ? s.toAtom(cx) : (Atom)ret.union(s.toAtom(cx)); + return ret; + } public void build(Context cx, Union u, NonTerminalNode cnt) { HashSet bad2 = new HashSet(); for(int i=0; i bad2 = new HashSet(); - Union urep = new Union(); - urep.add(Sequence.empty); + Union urep = new Union(null, false); + urep.add(Sequence.create()); if (sep != null) - urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1)); + urep.add(Sequence.create(new Element[] { cx.get(sep), u }, 1)); else - urep.add(Sequence.singleton(new Element[] { u }, 0)); + urep.add(Sequence.create(new Element[] { u }, 0)); for(int i=0; i, **, ++, or a similar character-class operator on a [potentially] multicharacter production"); + return elements[0].toAtom(cx); + } public Seq tag(String tag) { this.tag = prefix+tag; return this; } - public Seq follow(ElementNode follow) { this.follow = follow; return this; } + public Seq follow(ElementNode follow) { + this.follow = follow; + return this; + } public Seq dup() { Seq ret = new Seq(elements); ret.and.addAll(and); @@ -202,8 +211,8 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings { ret.tag = prefix+tag; return ret; } - public Seq and(Seq s) { and.add(s); s.lame = true; return this; } - public Seq andnot(Seq s) { not.add(s); s.lame = true; return this; } + public Seq and(Seq s) { and.add(s); return this; } + public Seq andnot(Seq s) { not.add(s); return this; } public Seq separate(ElementNode sep) { ElementNode[] elements = new ElementNode[this.elements.length * 2 - 1]; for(int i=0; i") Seq arrow(Seq s, ElementNode e) { return s.follow(e); } - public static @bind.as("::") Seq tag(String tagname, Seq s) { return s.tag(tagname); } - public static @bind.as("/") Seq slash(Seq s, ElementNode e) { return s.separate(e); } + public static @bind.as("&") Seq and2(Seq s, Seq a) { return s.and(a); } + public static @bind.as("&~") Seq andnot2(Seq s, Seq a) { return s.andnot(a); } + public static @bind.as("->") Seq arrow(Seq s, ElementNode e) { return s.follow(e); } + public static @bind.as("::") Seq tag(String tagname, Seq s) { return s.tag(tagname); } + public static @bind.as("/") Seq slash(Seq s, ElementNode e) { return s.separate(e); } public static Seq seq(ElementNode[] elements) { return new Seq(elements); } public static @bind.as("Elements") Seq seq2(ElementNode[] elements) { return new Seq(elements); } public static @bind.as Seq psx(Seq s) { return s; } public static @bind.as(":") ElementNode colon(String s, ElementNode e) { return new Label(s, e); } public static @bind.as(")") void close(String foo) { throw new Error("not supported"); } - public static @bind.as("()") ElementNode epsilon() { return new Constant(Union.epsilon); } + public static @bind.as("()") ElementNode epsilon() { return new Constant(epsilon); } + + private static Union epsilon = new Union("()"); + static { epsilon.add(Sequence.create()); } public static class NonTerminalReferenceNode extends ElementNode { public String nonTerminal; @@ -273,6 +278,9 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings { public @bind.as("NonTerminalReference") NonTerminalReferenceNode(String nonTerminal) { this.nonTerminal = prefix + nonTerminal; } + public Atom toAtom(Context cx) { + return cx.grammar.get(nonTerminal).toAtom(cx); + } public Element build(Context cx, NonTerminalNode cnt) { if (!this.nonTerminal.startsWith(prefix)) nonTerminal = prefix + nonTerminal; Element ret = cx.get(nonTerminal); @@ -282,33 +290,54 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings { } public static class Literal extends Constant { - public @bind Literal(@bind.arg String string) { super(CharRange.string(string)); } + private String string; + public @bind Literal(@bind.arg String string) { + super(CharAtom.string(string)); + this.string = string; + } public boolean drop() { return true; } + public Atom toAtom(Context cx) { + if (string.length()!=1) return super.toAtom(cx); + edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set(); + set.add(string.charAt(0), string.charAt(0)); + return CharAtom.set(set); + } } public static class CharClass extends ElementNode { Range[] ranges; public @bind.as("[") CharClass(Range[] ranges) { this.ranges = ranges; } + public Atom toAtom(Context cx) { + edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set(); + for(Range r : ranges) + set.add(r.first, r.last); + return CharAtom.set(set); + } public Element build(Context cx, NonTerminalNode cnt) { edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set(); for(Range r : ranges) set.add(r.first, r.last); - return CharRange.set(set); + return CharAtom.set(set); } } + public static @bind.as("\\{") ElementNode leftBrace() { + return new Drop(new CharClass(new Range[] { new Range(CharAtom.left, CharAtom.left) })); } + public static @bind.as("\\}") ElementNode rightBrace() { + return new Drop(new CharClass(new Range[] { new Range(CharAtom.right, CharAtom.right) })); } + public static @bind.as("{") class XTree extends ElementNode { public @bind.arg Seq body; public Element build(Context cx, NonTerminalNode cnt) { - Union u = new Union(); - Sequence s = body.build(cx, u, false, null); - Union u2 = new Union(); - u2.add(Sequence.singleton(new Element[] { - CharRange.leftBrace, + Union u = new Union(null, false); + Sequence s = body.build(cx, u, null); + Union u2 = new Union(null, false); + u2.add(Sequence.create(new Element[] { + CharAtom.leftBrace, cx.get("ws"), u, cx.get("ws"), - CharRange.rightBrace + CharAtom.rightBrace }, 2)); return u2; } @@ -319,12 +348,16 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings { public boolean zero, many, max; public Rep(ElementNode e, ElementNode sep, boolean zero, boolean many, boolean max) { this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;} + public Atom toAtom(Context cx) { + if (sep != null) return super.toAtom(cx); + return e.toAtom(cx); + } public Element build(Context cx, NonTerminalNode cnt) { return (!max) - ? Sequence.repeat(e.build(cx, null), zero, many, sep==null ? null : sep.build(cx, null), cx.rm.repeatTag()) + ? Repeat.repeat(e.build(cx, null), zero, many, sep==null ? null : sep.build(cx, null), cx.rm.repeatTag()) : sep==null - ? Sequence.repeatMaximal(infer(e.build(cx, null)), zero, many, cx.rm.repeatTag()) - : Sequence.repeatMaximal(e.build(cx, null), zero, many, infer(sep.build(cx, null)), cx.rm.repeatTag()); + ? Repeat.repeatMaximal(e.toAtom(cx), zero, many, cx.rm.repeatTag()) + : Repeat.repeatMaximal(e.build(cx, null), zero, many, sep.toAtom(cx), cx.rm.repeatTag()); } } @@ -352,27 +385,34 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings { public static @bind.as("^") ElementNode caret(final String s) { final String thePrefix = prefix; - return new Constant(CharRange.string(s)) { + return new Constant(CharAtom.string(s)) { public String getOwnerTag() { return thePrefix+s; } public boolean drop() { return true; } }; } public static @bind.as("~") ElementNode tilde(final ElementNode e) { - return new PostProcess(e) { - public Element postProcess(Element e) { - return infer((Topology)Atom.toAtom(e).complement().minus(CharRange.braces)); + return new ElementNodeWrapper(e) { + public Atom toAtom(Context cx) { + return infer((Topology)e.toAtom(cx).complement()/*.minus(CharAtom.braces)*/); + } + public Element build(Context cx, NonTerminalNode cnt) { + return infer((Topology)e.toAtom(cx).complement()/*.minus(CharAtom.braces)*/); } }; } public static @bind.as("Word") String word(String s) { return s; } public static @bind.as("Quoted") String quoted(String s) { return s; } - public static @bind.as("escaped") String c(char c) { return c+""; } + public static @bind.as("escaped") String c(char c) { + if (c=='{') return CharAtom.left+""; + if (c=='}') return CharAtom.right+""; + return c+""; + } public static @bind.as("EmptyString") String emptystring() { return ""; } public static @bind.as("\n") String retur() { return "\n"; } public static @bind.as("\r") String lf() { return "\r"; } - static Atom infer(Element e) { return infer((Topology)Atom.toAtom(e)); } - static Atom infer(Topology t) { return new CharRange(new CharTopology(t)); } + //static Atom infer(Element e) { return infer((Topology)Atom.toAtom(e)); } + static Atom infer(Object t) { return (Atom)t; } public static class Context { public HashMap map = new HashMap(); @@ -422,6 +462,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings { public String getLabel() { return null; } public String getOwnerTag() { return null; } public boolean drop() { return false; } + public Atom toAtom(Context cx) { throw new Error("can't convert a " + this.getClass().getName() + " to an atom"); } public abstract Element build(Context cx, NonTerminalNode cnt); } @@ -431,6 +472,7 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings { public String getLabel() { return _e.getLabel(); } public String getOwnerTag() { return _e.getOwnerTag(); } public boolean drop() { return _e.drop(); } + public Atom toAtom(Context cx) { return _e.toAtom(cx); } public Element build(Context cx, NonTerminalNode cnt) { return _e.build(cx, cnt); } } @@ -438,10 +480,13 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings { Element constant; public Constant(Element constant) { this.constant = constant; } public Element build(Context cx, NonTerminalNode cnt) { return constant; } + public Atom toAtom(Context cx) { + if (constant instanceof Atom) return ((Atom)constant); + return super.toAtom(cx); + } } public abstract static class PostProcess extends ElementNodeWrapper { - ElementNode e; public PostProcess(ElementNode e) { super(e); } public Element build(Context cx, NonTerminalNode cnt) { return postProcess(_e.build(cx, cnt)); } public abstract Element postProcess(Element e); @@ -457,4 +502,13 @@ public class MetaGrammarBindings extends AnnotationGrammarBindings { public Label(String label, ElementNode e) { super(e); this.label = label; } public String getLabel() { return label; } } + + /* + 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; } + } + */ }