1 package edu.berkeley.sbp.meta;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.chr.*;
5 import edu.berkeley.sbp.misc.*;
6 import edu.berkeley.sbp.bind.*;
8 import java.lang.annotation.*;
9 import java.lang.reflect.*;
12 /** The java classes typically used to represent a parsed grammar AST; each inner class is a type of AST node. */
13 public class MetaGrammarBindings {
15 /** A grammar (a set of nonterminals) */
16 public static class GrammarNode extends HashMap<String,NonTerminalNode> {
17 public @bind.as("Grammar") GrammarNode(NonTerminalNode[] nonterminals) {
18 for(NonTerminalNode nt : nonterminals) this.put(nt.name, nt); }
19 public String toString() {
21 for(NonTerminalNode nt : values()) ret += nt + ", ";
24 public Union build(String s, GrammarBindingResolver rm) {
25 Context cx = new Context(this,rm);
27 for(MetaGrammarBindings.NonTerminalNode nt : values()) {
28 Union el = (Union)cx.get(nt.name);
29 StringBuffer st = new StringBuffer();
31 if (nt.name.equals(s)) u = el;
37 public abstract static class UnionNode extends ElementNode {
38 public Seq[][] sequences;
39 public void build(Context cx, Union u) {
40 HashSet<Sequence> bad2 = new HashSet<Sequence>();
41 for(int i=0; i<sequences.length; i++) {
42 Seq[] group = sequences[i];
43 Union u2 = new Union();
44 if (sequences.length==1) u2 = u;
45 for(int j=0; j<group.length; j++) {
46 group[j].build(cx, u2, false);
48 if (sequences.length==1) break;
49 Sequence seq = Sequence.singleton(u2);
50 for(Sequence s : bad2) {
55 bad2.add(Sequence.singleton(u2));
60 public static class NonTerminalNode extends UnionNode {
62 public String name = null;
63 public String sep = null;
64 public @bind.as("NonTerminal") NonTerminalNode(@bind.arg String name, @bind.arg Seq[][] sequences) { this(name, sequences, false); }
65 public NonTerminalNode(String name, Seq[][] sequences, boolean rep) { this(name, sequences, rep, null); }
66 public NonTerminalNode(String name, Seq[][] sequences, boolean rep, String sep) {
68 this.sequences = sequences;
72 public Element build(Context cx) { return cx.get(name); }
73 public void build(Context cx, Union u) {
74 if (!rep) { super.build(cx, u); return; }
75 HashSet<Sequence> bad2 = new HashSet<Sequence>();
77 Union urep = new Union();
78 urep.add(Sequence.empty);
79 urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1));
81 for(int i=0; i<sequences.length; i++) {
82 Seq[] group = sequences[i];
83 Union u2 = new Union();
84 if (sequences.length==1) u2 = u;
85 for(int j=0; j<group.length; j++) {
86 Union u3 = new Union();
87 group[j].build(cx, u3, false);
88 Sequence s = Sequence.unwrap(new Element[] { u3, urep },
90 new boolean[] { false, false });
93 if (sequences.length==1) break;
94 Sequence seq = Sequence.singleton(u2);
95 for(Sequence s : bad2) {
100 bad2.add(Sequence.singleton(u2));
104 public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg Seq[][] sequences) { return new NonTerminalNode(name, sequences, true); }
105 public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg String sep, @bind.arg Seq[][] sequences) {
106 return new NonTerminalNode(name, sequences, true, sep);
109 public static class AnonUnionNode extends UnionNode {
110 public @bind.as("(") AnonUnionNode(Seq[][] sequences) {
111 this.sequences = sequences;
113 public Element build(Context cx) {
114 Union ret = new Union();
120 public static class Range {
121 public @bind Range(char only) { first = only; last = only; }
122 public @bind Range(char first, char last) { this.first = first; this.last = last; }
127 public static abstract class ElementNode {
128 public String getLabel() { return null; }
129 public String getOwnerTag() { return null; }
130 public boolean drop() { return false; }
131 public abstract Element build(Context cx);
134 public static class Drop extends ElementNode {
135 public ElementNode e;
136 public Drop(ElementNode e) { this.e = e; }
137 public String getLabel() { return null; }
138 public boolean drop() { return true; }
139 public String getOwnerTag() { return e.getOwnerTag(); }
140 public Element build(Context cx) { return e.build(cx); }
143 public static class Label extends ElementNode {
145 public ElementNode e;
146 public Label(String label, ElementNode e) { this.e = e; this.label = label; }
147 public String getLabel() { return label; }
148 public String getOwnerTag() { return e.getOwnerTag(); }
149 public Element build(Context cx) { return e.build(cx); }
152 public static /*abstract*/ class Seq {
153 HashSet<Seq> and = new HashSet<Seq>();
154 HashSet<Seq> not = new HashSet<Seq>();
155 ElementNode[] elements;
159 public void append(ElementNode e) {
160 ElementNode[] elements = new ElementNode[this.elements.length+1];
161 System.arraycopy(this.elements, 0, elements, 0, this.elements.length);
162 this.elements = elements;
163 elements[elements.length-1] = e;
165 public Seq(ElementNode e) { this(new ElementNode[] { e }); }
166 public Seq(ElementNode[] elements) { this.elements = elements; }
167 public Seq tag(String tag) { this.tag = tag; return this; }
168 public Seq follow(ElementNode follow) { this.follow = follow; return this; }
170 Seq ret = new Seq(elements);
177 public Seq and(Seq s) { and.add(s); s.lame = true; return this; }
178 public Seq andnot(Seq s) { not.add(s); s.lame = true; return this; }
179 public Seq separate(ElementNode sep) {
180 ElementNode[] elements = new ElementNode[this.elements.length * 2 - 1];
181 for(int i=0; i<this.elements.length; i++) {
182 elements[i*2] = this.elements[i];
183 if (i<this.elements.length-1)
184 elements[i*2+1] = new Drop(sep);
186 this.elements = elements;
189 public Sequence build(Context cx, Union u, boolean lame) {
190 Sequence ret = build0(cx, lame || this.lame);
191 for(Seq s : and) { Sequence dork = s.build(cx, u, true); ret = ret.and(dork); }
192 for(Seq s : not) { Sequence dork = s.build(cx, u, true); ret = ret.not(dork); }
197 public Sequence build0(Context cx, boolean lame) {
198 boolean dropAll = lame;
199 if (tag!=null && "()".equals(tag)) dropAll = true;
200 Object[] labels = new Object[elements.length];
201 boolean[] drops = new boolean[elements.length];
202 Element[] els = new Element[elements.length];
203 for(int i=0; i<elements.length; i++) {
204 labels[i] = elements[i].getLabel();
205 drops[i] = elements[i].drop();
206 els[i] = elements[i].build(cx);
207 if (elements[i].getOwnerTag() != null)
208 tag = elements[i].getOwnerTag();
211 if (dropAll) ret = Sequence.drop(els, false);
213 ret = cx.rm.tryResolveTag(tag, cx.cnt, els, labels, drops);
216 for(int i=0; i<els.length; i++)
218 if (idx==-1) idx = i;
219 else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els,false));
220 if (idx != -1) ret = Sequence.singleton(els, idx);
221 else ret = Sequence.drop(els, false);
224 if (this.follow != null)
225 ret.follow = infer(this.follow.build(cx));
226 ret.lame = this.lame;
230 public static @bind.as("&") Seq and2(Seq s, Seq a) { return s.and(a); }
231 public static @bind.as("&~") Seq andnot2(Seq s, Seq a) { return s.andnot(a); }
232 public static @bind.as("->") Seq arrow(Seq s, ElementNode e) { return s.follow(e); }
233 public static @bind.as("::") Seq tag(String tagname, Seq s) { return s.tag(tagname); }
234 public static @bind.as("/") Seq slash(Seq s, ElementNode e) { return s.separate(e); }
236 public static Seq seq(ElementNode[] elements) { return new Seq(elements); }
237 public static @bind.as("Elements") Seq seq2(ElementNode[] elements) { return new Seq(elements); }
238 public static @bind.as Seq psx(Seq s) { return s; }
239 public static @bind.as(":") ElementNode colon(String s, ElementNode e) { return new Label(s, e); }
240 public static @bind.as(")") void close(String foo) { throw new Error("not supported"); }
241 public static @bind.as("()") ElementNode epsilon() { return new Constant(Union.epsilon); }
243 public static @bind.as("NonTerminalReference") class NonTerminalReferenceNode extends ElementNode {
244 public @bind.arg String nonTerminal;
245 public Element build(Context cx) { return cx.get(nonTerminal); }
248 public static class Literal extends Constant {
249 public @bind Literal(@bind.arg String string) { super(CharRange.string(string)); }
250 public boolean drop() { return true; }
253 public static class CharClass extends ElementNode {
255 public @bind.as("[") CharClass(Range[] ranges) { this.ranges = ranges; }
256 public Element build(Context cx) {
257 edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
258 for(Range r : ranges)
259 set.add(r.first, r.last);
260 return CharRange.set(set);
264 public static @bind.as("{") class XTree extends ElementNode {
265 public @bind.arg Seq body;
266 public Element build(Context cx) {
267 Union u = new Union();
268 Sequence s = body.build(cx, u, false);
269 Union u2 = new Union();
270 u2.add(Sequence.singleton(new Element[] {
281 public static class Rep extends ElementNode {
282 public ElementNode e, sep;
283 public boolean zero, many, max;
284 public Rep(ElementNode e, ElementNode sep, boolean zero, boolean many, boolean max) {
285 this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
286 public Element build(Context cx) {
288 ? Sequence.repeat(e.build(cx), zero, many, sep==null ? null : sep.build(cx), cx.rm.repeatTag())
290 ? Sequence.repeatMaximal(infer(e.build(cx)), zero, many, cx.rm.repeatTag())
291 : Sequence.repeatMaximal(e.build(cx), zero, many, infer(sep.build(cx)), cx.rm.repeatTag());
294 public static class Constant extends ElementNode {
296 public Constant(Element constant) { this.constant = constant; }
297 public Element build(Context cx) { return constant; }
299 public abstract static class PostProcess extends ElementNode {
301 public PostProcess(ElementNode e) { this.e = e; }
302 public Element build(Context cx) { return postProcess(e.build(cx)); }
303 public abstract Element postProcess(Element e);
306 // FIXME: it would be nice if we could hoist this into "Rep"
307 public static @bind.as("++") ElementNode plusmax(final ElementNode e) { return new Rep(e, null, false, true, true); }
308 public static @bind.as("+") ElementNode plus(final ElementNode e) { return new Rep(e, null, false, true, false); }
309 public static @bind.as("++/") ElementNode plusmaxfollow(final ElementNode e, final ElementNode sep) { return new Rep(e, sep, false, true, true); }
310 public static @bind.as("+/") ElementNode plusfollow(final ElementNode e, final ElementNode sep) { return new Rep(e, sep, false, true, false); }
311 public static @bind.as("**") ElementNode starmax(final ElementNode e) { return new Rep(e, null, true, true, true); }
312 public static @bind.as("*") ElementNode star(final ElementNode e) { return new Rep(e, null, true, true, false); }
313 public static @bind.as("**/") ElementNode starmaxfollow(final ElementNode e, final ElementNode sep) { return new Rep(e, sep, true, true, true); }
314 public static @bind.as("*/") ElementNode starfollow(final ElementNode e, final ElementNode sep) { return new Rep(e, sep, true, true, false); }
315 public static @bind.as("?") ElementNode question(final ElementNode e) { return new Rep(e, null, true, true, false); }
317 public static @bind.as("!") ElementNode bang(final ElementNode e) { return new Drop(e); }
319 public static @bind.as("^") ElementNode caret(final String s) {
320 return new Drop(new Constant(CharRange.string(s)) {
321 public String getOwnerTag() { return s; }
325 public static @bind.as("~") ElementNode tilde(final ElementNode e) {
326 return new PostProcess(e) {
327 public Element postProcess(Element e) {
328 return infer((Topology<Character>)Atom.toAtom(e).complement());
331 public static @bind.as("Word") String word(String s) { return s; }
332 public static @bind.as("Quoted") String quoted(String s) { return s; }
333 public static @bind.as("escaped") String c(char c) { return c+""; }
334 public static @bind.as("EmptyString") String emptystring() { return ""; }
335 public static @bind.as("\n") String retur() { return "\n"; }
336 public static @bind.as("\r") String lf() { return "\r"; }
338 static Atom infer(Element e) { return infer((Topology<Character>)Atom.toAtom(e)); }
339 static Atom infer(Topology<Character> t) { return new CharRange(new CharTopology(t)); }
341 public static class Context {
342 public HashMap<String,Union> map = new HashMap<String,Union>();
343 public GrammarNode grammar;
344 public String cnt = null;
345 public GrammarBindingResolver rm;
346 public Context(GrammarNode g, GrammarBindingResolver rm) {
350 public Union build() {
352 for(NonTerminalNode nt : grammar.values()) {
353 Union u = get(nt.name);
354 if ("s".equals(nt.name))
359 public Context(Tree t, GrammarBindingResolver rm) {
361 Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
362 this.grammar = (GrammarNode)red.invoke(t.children());
364 public Union peek(String name) { return map.get(name); }
365 public void put(String name, Union u) { map.put(name, u); }
366 public Union get(String name) {
367 Union ret = map.get(name);
368 if (ret != null) return ret;
369 ret = new Union(name);
371 NonTerminalNode nt = grammar.get(name);
373 System.err.println("*** warning could not find " + name);