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> implements NonTerminalSource {
17 public NonTerminalNode[] getNonTerminals() {
18 return (NonTerminalNode[])values().toArray(new NonTerminalNode[0]);
20 public GrammarNode(NonTerminalNode[] nonterminals) {
21 for(NonTerminalNode nt : nonterminals) {
22 if (nt==null) continue;
23 if (this.get(nt.name)!=null)
24 throw new RuntimeException("duplicate definition of nonterminal \""+nt.name+"\"");
25 this.put(nt.name, nt);
28 public @bind.as("Grammar") GrammarNode(Object[] nt) {
31 private void add(Object[] obs) {
33 if (o==null) continue;
34 else if (o instanceof Object[]) add((Object[])o);
35 else if (o instanceof NonTerminalNode) {
36 NonTerminalNode nt = (NonTerminalNode)o;
37 if (this.get(nt.name)!=null)
38 throw new RuntimeException("duplicate definition of nonterminal \""+nt.name+"\"");
39 this.put(nt.name, nt);
41 else if (o instanceof NonTerminalSource) add(((NonTerminalSource)o).getNonTerminals());
44 public String toString() {
46 for(NonTerminalNode nt : values()) ret += nt + ", ";
49 public Union build(String s, GrammarBindingResolver rm) {
50 Context cx = new Context(this,rm);
52 for(MetaGrammarBindings.NonTerminalNode nt : values()) {
53 Union el = (Union)cx.get(nt.name);
54 StringBuffer st = new StringBuffer();
56 if (nt.name.equals(s)) u = el;
62 public abstract static class UnionNode extends ElementNode {
63 public Seq[][] sequences;
64 public void build(Context cx, Union u, NonTerminalNode cnt) {
65 HashSet<Sequence> bad2 = new HashSet<Sequence>();
66 for(int i=0; i<sequences.length; i++) {
67 Seq[] group = sequences[i];
68 Union u2 = new Union();
69 if (sequences.length==1) u2 = u;
70 for(int j=0; j<group.length; j++) {
71 group[j].build(cx, u2, false, cnt);
73 if (sequences.length==1) break;
74 Sequence seq = Sequence.singleton(u2);
75 for(Sequence s : bad2) {
80 bad2.add(Sequence.singleton(u2));
85 public static @bind.as("#import") GrammarNode poundimport(String fileName) {
86 System.err.println("#import " + fileName);
88 Tree t = new CharParser(MetaGrammar.make()).parse(new FileInputStream(fileName)).expand1();
89 Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
90 return (MetaGrammarBindings.GrammarNode)red.invoke(t);
91 } catch (Exception e) {
93 throw new RuntimeException(e);
97 public static interface NonTerminalSource {
98 public NonTerminalNode[] getNonTerminals();
101 public static class NonTerminalNode extends UnionNode implements NonTerminalSource {
103 public String name = null;
104 public String sep = null;
105 public NonTerminalNode[] getNonTerminals() { return new NonTerminalNode[] { this }; }
106 public @bind.as("NonTerminal") NonTerminalNode(@bind.arg String name, @bind.arg Seq[][] sequences) { this(name, sequences, false); }
107 public NonTerminalNode(String name, Seq[][] sequences, boolean rep) { this(name, sequences, rep, null); }
108 public NonTerminalNode(String name, Seq[][] sequences, boolean rep, String sep) {
110 this.sequences = sequences;
114 public Element build(Context cx, NonTerminalNode cnt) { return cx.get(name); }
115 public void build(Context cx, Union u, NonTerminalNode cnt) {
116 if (!rep) { super.build(cx, u, this); return; }
117 HashSet<Sequence> bad2 = new HashSet<Sequence>();
119 Union urep = new Union();
120 urep.add(Sequence.empty);
121 urep.add(Sequence.singleton(new Element[] { cx.get(sep), u }, 1));
123 for(int i=0; i<sequences.length; i++) {
124 Seq[] group = sequences[i];
125 Union u2 = new Union();
126 if (sequences.length==1) u2 = u;
127 for(int j=0; j<group.length; j++) {
128 Union u3 = new Union();
129 group[j].build(cx, u3, false, this);
130 Sequence s = Sequence.unwrap(new Element[] { u3, urep },
132 new boolean[] { false, false });
135 if (sequences.length==1) break;
136 Sequence seq = Sequence.singleton(u2);
137 for(Sequence s : bad2) {
142 bad2.add(Sequence.singleton(u2));
146 public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg Seq[][] sequences) { return new NonTerminalNode(name, sequences, true); }
147 public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg String sep, @bind.arg Seq[][] sequences) {
148 return new NonTerminalNode(name, sequences, true, sep);
151 public static class AnonUnionNode extends UnionNode {
152 public @bind.as("(") AnonUnionNode(Seq[][] sequences) {
153 this.sequences = sequences;
155 public Element build(Context cx, NonTerminalNode cnt) {
156 Union ret = new Union();
162 public static class Range {
163 public @bind Range(char only) { first = only; last = only; }
164 public @bind Range(char first, char last) { this.first = first; this.last = last; }
169 public static abstract class ElementNode {
170 public String getLabel() { return null; }
171 public String getOwnerTag() { return null; }
172 public boolean drop() { return false; }
173 public abstract Element build(Context cx, NonTerminalNode cnt);
176 public static class Drop extends ElementNode {
177 public ElementNode e;
178 public Drop(ElementNode e) { this.e = e; }
179 public String getLabel() { return null; }
180 public boolean drop() { return true; }
181 public String getOwnerTag() { return e.getOwnerTag(); }
182 public Element build(Context cx, NonTerminalNode cnt) { return e.build(cx, cnt); }
185 public static class Label extends ElementNode {
187 public ElementNode e;
188 public Label(String label, ElementNode e) { this.e = e; this.label = label; }
189 public String getLabel() { return label; }
190 public String getOwnerTag() { return e.getOwnerTag(); }
191 public Element build(Context cx, NonTerminalNode cnt) { return e.build(cx, cnt); }
194 public static /*abstract*/ class Seq {
195 HashSet<Seq> and = new HashSet<Seq>();
196 HashSet<Seq> not = new HashSet<Seq>();
197 ElementNode[] elements;
201 public void append(ElementNode e) {
202 ElementNode[] elements = new ElementNode[this.elements.length+1];
203 System.arraycopy(this.elements, 0, elements, 0, this.elements.length);
204 this.elements = elements;
205 elements[elements.length-1] = e;
207 public Seq(ElementNode e) { this(new ElementNode[] { e }); }
208 public Seq(ElementNode[] elements) { this.elements = elements; }
209 public Seq tag(String tag) { this.tag = tag; return this; }
210 public Seq follow(ElementNode follow) { this.follow = follow; return this; }
212 Seq ret = new Seq(elements);
219 public Seq and(Seq s) { and.add(s); s.lame = true; return this; }
220 public Seq andnot(Seq s) { not.add(s); s.lame = true; return this; }
221 public Seq separate(ElementNode sep) {
222 ElementNode[] elements = new ElementNode[this.elements.length * 2 - 1];
223 for(int i=0; i<this.elements.length; i++) {
224 elements[i*2] = this.elements[i];
225 if (i<this.elements.length-1)
226 elements[i*2+1] = new Drop(sep);
228 this.elements = elements;
231 public Sequence build(Context cx, Union u, boolean lame, NonTerminalNode cnt) {
232 Sequence ret = build0(cx, lame || this.lame, cnt);
233 for(Seq s : and) { Sequence dork = s.build(cx, u, true, cnt); ret = ret.and(dork); }
234 for(Seq s : not) { Sequence dork = s.build(cx, u, true, cnt); ret = ret.not(dork); }
239 public Sequence build0(Context cx, boolean lame, NonTerminalNode cnt) {
240 boolean dropAll = lame;
241 if (tag!=null && "()".equals(tag)) dropAll = true;
242 boolean[] drops = new boolean[elements.length];
243 Element[] els = new Element[elements.length];
244 for(int i=0; i<elements.length; i++) {
245 drops[i] = elements[i].drop();
246 els[i] = elements[i].build(cx, cnt);
247 if (elements[i].getOwnerTag() != null)
248 tag = elements[i].getOwnerTag();
251 if (dropAll) ret = Sequence.drop(els, false);
253 ret = cx.rm.tryResolveTag(tag, cnt==null?null:cnt.name, els, drops);
256 for(int i=0; i<els.length; i++)
258 if (idx==-1) idx = i;
259 else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els,false));
260 if (idx != -1) ret = Sequence.singleton(els, idx);
261 else ret = Sequence.drop(els, false);
264 if (this.follow != null)
265 ret.follow = infer(this.follow.build(cx, null));
266 ret.lame = this.lame;
270 public static @bind.as("&") Seq and2(Seq s, Seq a) { return s.and(a); }
271 public static @bind.as("&~") Seq andnot2(Seq s, Seq a) { return s.andnot(a); }
272 public static @bind.as("->") Seq arrow(Seq s, ElementNode e) { return s.follow(e); }
273 public static @bind.as("::") Seq tag(String tagname, Seq s) { return s.tag(tagname); }
274 public static @bind.as("/") Seq slash(Seq s, ElementNode e) { return s.separate(e); }
276 public static Seq seq(ElementNode[] elements) { return new Seq(elements); }
277 public static @bind.as("Elements") Seq seq2(ElementNode[] elements) { return new Seq(elements); }
278 public static @bind.as Seq psx(Seq s) { return s; }
279 public static @bind.as(":") ElementNode colon(String s, ElementNode e) { return new Label(s, e); }
280 public static @bind.as(")") void close(String foo) { throw new Error("not supported"); }
281 public static @bind.as("()") ElementNode epsilon() { return new Constant(Union.epsilon); }
283 public static @bind.as("NonTerminalReference") class NonTerminalReferenceNode extends ElementNode {
284 public @bind.arg String nonTerminal;
285 public Element build(Context cx, NonTerminalNode cnt) {
286 Element ret = cx.get(nonTerminal);
287 if (ret == null) throw new RuntimeException("unkown nonterminal \""+nonTerminal+"\"");
292 public static class Literal extends Constant {
293 public @bind Literal(@bind.arg String string) { super(CharRange.string(string)); }
294 public boolean drop() { return true; }
297 public static class CharClass extends ElementNode {
299 public @bind.as("[") CharClass(Range[] ranges) { this.ranges = ranges; }
300 public Element build(Context cx, NonTerminalNode cnt) {
301 edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
302 for(Range r : ranges)
303 set.add(r.first, r.last);
304 return CharRange.set(set);
308 public static @bind.as("{") class XTree extends ElementNode {
309 public @bind.arg Seq body;
310 public Element build(Context cx, NonTerminalNode cnt) {
311 Union u = new Union();
312 Sequence s = body.build(cx, u, false, null);
313 Union u2 = new Union();
314 u2.add(Sequence.singleton(new Element[] {
325 public static class Rep extends ElementNode {
326 public ElementNode e, sep;
327 public boolean zero, many, max;
328 public Rep(ElementNode e, ElementNode sep, boolean zero, boolean many, boolean max) {
329 this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
330 public Element build(Context cx, NonTerminalNode cnt) {
332 ? Sequence.repeat(e.build(cx, null), zero, many, sep==null ? null : sep.build(cx, null), cx.rm.repeatTag())
334 ? Sequence.repeatMaximal(infer(e.build(cx, null)), zero, many, cx.rm.repeatTag())
335 : Sequence.repeatMaximal(e.build(cx, null), zero, many, infer(sep.build(cx, null)), cx.rm.repeatTag());
338 public static class Constant extends ElementNode {
340 public Constant(Element constant) { this.constant = constant; }
341 public Element build(Context cx, NonTerminalNode cnt) { return constant; }
343 public abstract static class PostProcess extends ElementNode {
345 public PostProcess(ElementNode e) { this.e = e; }
346 public Element build(Context cx, NonTerminalNode cnt) { return postProcess(e.build(cx, cnt)); }
347 public abstract Element postProcess(Element e);
350 // FIXME: it would be nice if we could hoist this into "Rep"
351 public static @bind.as("++") ElementNode plusmax(final ElementNode e) { return new Rep(e, null, false, true, true); }
352 public static @bind.as("+") ElementNode plus(final ElementNode e) { return new Rep(e, null, false, true, false); }
353 public static @bind.as("++/") ElementNode plusmaxfollow(final ElementNode e, final ElementNode sep) { return new Rep(e, sep, false, true, true); }
354 public static @bind.as("+/") ElementNode plusfollow(final ElementNode e, final ElementNode sep) { return new Rep(e, sep, false, true, false); }
355 public static @bind.as("**") ElementNode starmax(final ElementNode e) { return new Rep(e, null, true, true, true); }
356 public static @bind.as("*") ElementNode star(final ElementNode e) { return new Rep(e, null, true, true, false); }
357 public static @bind.as("**/") ElementNode starmaxfollow(final ElementNode e, final ElementNode sep) { return new Rep(e, sep, true, true, true); }
358 public static @bind.as("*/") ElementNode starfollow(final ElementNode e, final ElementNode sep) { return new Rep(e, sep, true, true, false); }
359 public static @bind.as("?") ElementNode question(final ElementNode e) { return new Rep(e, null, true, true, false); }
361 public static @bind.as("!") ElementNode bang(final ElementNode e) { return new Drop(e); }
363 public static @bind.as("^") ElementNode caret(final String s) {
364 return new Drop(new Constant(CharRange.string(s)) {
365 public String getOwnerTag() { return s; }
369 public static @bind.as("~") ElementNode tilde(final ElementNode e) {
370 return new PostProcess(e) {
371 public Element postProcess(Element e) {
372 return infer((Topology<Character>)Atom.toAtom(e).complement().minus(CharRange.braces));
375 public static @bind.as("Word") String word(String s) { return s; }
376 public static @bind.as("Quoted") String quoted(String s) { return s; }
377 public static @bind.as("escaped") String c(char c) { return c+""; }
378 public static @bind.as("EmptyString") String emptystring() { return ""; }
379 public static @bind.as("\n") String retur() { return "\n"; }
380 public static @bind.as("\r") String lf() { return "\r"; }
382 static Atom infer(Element e) { return infer((Topology<Character>)Atom.toAtom(e)); }
383 static Atom infer(Topology<Character> t) { return new CharRange(new CharTopology(t)); }
385 public static class Context {
386 public HashMap<String,Union> map = new HashMap<String,Union>();
387 public GrammarNode grammar;
388 public String cnt = null;
389 public GrammarBindingResolver rm;
390 public Context(GrammarNode g, GrammarBindingResolver rm) {
394 public Union build() {
396 for(NonTerminalNode nt : grammar.values()) {
397 Union u = get(nt.name);
398 if ("s".equals(nt.name))
403 public Context(Tree t, GrammarBindingResolver rm) {
405 Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
406 this.grammar = (GrammarNode)red.invoke(t);
408 public Union peek(String name) { return map.get(name); }
409 public void put(String name, Union u) { map.put(name, u); }
410 public Union get(String name) {
411 Union ret = map.get(name);
412 if (ret != null) return ret;
413 ret = new Union(name);
415 NonTerminalNode nt = grammar.get(name);
417 System.err.println("*** warning could not find " + name);
421 nt.build(this, ret, nt);