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 extends AnnotationGrammarBindings {
15 public MetaGrammarBindings() { super(MetaGrammarBindings.class); }
17 // FIXME ugly ugly ugly scary dangerous
18 public static String prefix = "";
20 /** A grammar (a set of nonterminals) */
21 public static class GrammarNode extends HashMap<String,NonTerminalNode> {
22 public NonTerminalNode[] getNonTerminals() {
23 return (NonTerminalNode[])values().toArray(new NonTerminalNode[0]);
25 public GrammarNode(NonTerminalNode[] nonterminals) {
26 for(NonTerminalNode nt : nonterminals) {
27 if (nt==null) continue;
28 if (this.get(nt.name)!=null)
29 throw new RuntimeException("duplicate definition of nonterminal \""+nt.name+"\"");
30 this.put(nt.name, nt);
33 public @bind.as("Grammar") GrammarNode(Object[] nt) { add(nt); }
34 private void add(Object[] obs) {
36 if (o==null) continue;
37 else if (o instanceof Object[]) add((Object[])o);
38 else if (o instanceof NonTerminalNode) {
39 NonTerminalNode nt = (NonTerminalNode)o;
40 if (this.get(nt.name)!=null)
41 throw new RuntimeException("duplicate definition of nonterminal \""+nt.name+"\"");
42 this.put(nt.name, nt);
44 else if (o instanceof GrammarNode) add(((GrammarNode)o).getNonTerminals());
47 public String toString() {
49 for(NonTerminalNode nt : values()) ret += nt + ", ";
52 public Union build(String s, Grammar.Bindings rm) {
53 Context cx = new Context(this,rm);
55 for(MetaGrammarBindings.NonTerminalNode nt : values()) {
56 Union el = (Union)cx.get(nt.name);
57 StringBuffer st = new StringBuffer();
59 if (nt.name.equals(s)) u = el;
65 public abstract static class UnionNode extends ElementNode {
66 public Seq[][] sequences;
67 public Atom toAtom(Context cx) {
69 for(Seq[] ss : sequences)
71 ret = ret==null ? s.toAtom(cx) : (Atom)ret.union(s.toAtom(cx));
74 public void build(Context cx, Union u, NonTerminalNode cnt) {
75 HashSet<Sequence> bad2 = new HashSet<Sequence>();
76 for(int i=0; i<sequences.length; i++) {
77 Seq[] group = sequences[i];
78 Union u2 = new Union(null, false);
79 if (sequences.length==1) u2 = u;
80 for(int j=0; j<group.length; j++) {
81 group[j].build(cx, u2, cnt);
83 if (sequences.length==1) break;
84 Sequence seq = Sequence.newSingletonSequence(u2);
85 for(Sequence s : bad2) seq = seq.not(s);
87 bad2.add(Sequence.newSingletonSequence(u2));
92 public static @bind.as("#import") GrammarNode poundimport(String fileName, String as) {
93 if (as==null) as = "";
94 else if ("".equals(as)) { }
98 Tree t = new CharParser(MetaGrammar.newInstance()).parse(new FileInputStream("tests/"+fileName)).expand1();
99 TreeFunctor<Object,Object> red = (TreeFunctor<Object,Object>)t.head();
100 String oldprefix = prefix;
102 GrammarNode gn = (GrammarNode)red.invoke(t);
105 } catch (Exception e) {
107 throw new RuntimeException(e);
111 public static class NonTerminalNode extends UnionNode {
113 public String name = null;
114 public String sep = null;
115 public NonTerminalNode[] getNonTerminals() { return new NonTerminalNode[] { this }; }
116 public @bind.as("NonTerminal") NonTerminalNode(@bind.arg String name, @bind.arg Seq[][] sequences) {
117 this(name, sequences, false); }
118 public NonTerminalNode(String name, Seq[][] sequences, boolean rep) { this(name, sequences, rep, null); }
119 public NonTerminalNode(String name, Seq[][] sequences, boolean rep, String sep) {
120 this.name = prefix + name;
121 this.sequences = sequences;
123 this.sep = sep==null?null:(prefix + sep);
125 public Element build(Context cx, NonTerminalNode cnt) { return cx.get(name); }
126 public void build(Context cx, Union u, NonTerminalNode cnt) {
127 if (!rep) { super.build(cx, u, this); return; }
128 HashSet<Sequence> bad2 = new HashSet<Sequence>();
130 Union urep = new Union(null, false);
131 urep.add(Sequence.newEmptySequence());
133 urep.add(Sequence.newSingletonSequence(new Element[] { cx.get(sep), u }, 1));
135 urep.add(Sequence.newSingletonSequence(new Element[] { u }, 0));
137 for(int i=0; i<sequences.length; i++) {
138 Seq[] group = sequences[i];
139 Union u2 = new Union(null, false);
140 if (sequences.length==1) u2 = u;
141 for(int j=0; j<group.length; j++) {
142 Union u3 = new Union(null, false);
143 group[j].build(cx, u3, this);
144 Sequence s = Sequence.newUnwrapSequence(new Element[] { u3, urep },
146 new boolean[] { false, false });
149 if (sequences.length==1) break;
150 Sequence seq = Sequence.newSingletonSequence(u2);
151 for(Sequence s : bad2) seq = seq.not(s);
153 bad2.add(Sequence.newSingletonSequence(u2));
158 public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg Seq[][] sequences) {
159 return new NonTerminalNode(name, sequences, true); }
160 public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg String sep, @bind.arg Seq[][] sequences) {
161 return new NonTerminalNode(name, sequences, true, sep); }
163 public static class AnonUnionNode extends UnionNode {
164 public @bind.as("(") AnonUnionNode(Seq[][] sequences) {
165 this.sequences = sequences;
167 public Element build(Context cx, NonTerminalNode cnt) {
168 Union ret = new Union(null, false);
174 public static class Range {
175 public @bind Range(char only) { first = only; last = only; }
176 public @bind Range(char first, char last) { this.first = first; this.last = last; }
181 public static /*abstract*/ class Seq {
182 HashSet<Seq> and = new HashSet<Seq>();
183 HashSet<Seq> not = new HashSet<Seq>();
184 ElementNode[] elements;
187 public void append(ElementNode e) {
188 ElementNode[] elements = new ElementNode[this.elements.length+1];
189 System.arraycopy(this.elements, 0, elements, 0, this.elements.length);
190 this.elements = elements;
191 elements[elements.length-1] = e;
193 public Seq(ElementNode e) { this(new ElementNode[] { e }); }
194 public Seq(ElementNode[] elements) { this.elements = elements; }
195 public Atom toAtom(Context cx) {
196 if (elements.length != 1) throw new Error("FIXME");
197 return elements[0].toAtom(cx);
199 public Seq tag(String tag) { this.tag = prefix+tag; return this; }
200 public Seq follow(ElementNode follow) {
201 this.follow = follow;
205 Seq ret = new Seq(elements);
209 ret.tag = prefix+tag;
212 public Seq and(Seq s) { and.add(s); return this; }
213 public Seq andnot(Seq s) { not.add(s); return this; }
214 public Seq separate(ElementNode sep) {
215 ElementNode[] elements = new ElementNode[this.elements.length * 2 - 1];
216 for(int i=0; i<this.elements.length; i++) {
217 elements[i*2] = this.elements[i];
218 if (i<this.elements.length-1)
219 elements[i*2+1] = new Drop(sep);
221 this.elements = elements;
224 public Sequence build(Context cx, Union u, NonTerminalNode cnt) {
225 Sequence ret = build0(cx, cnt);
226 for(Seq s : and) { Sequence dork = s.build(cx, u, cnt); ret = ret.and(dork); }
227 for(Seq s : not) { Sequence dork = s.build(cx, u, cnt); ret = ret.not(dork); }
231 public Sequence build0(Context cx, NonTerminalNode cnt) {
232 boolean[] drops = new boolean[elements.length];
233 Element[] els = new Element[elements.length];
234 for(int i=0; i<elements.length; i++) {
235 drops[i] = elements[i].drop();
236 els[i] = elements[i].build(cx, cnt);
237 if (elements[i].getOwnerTag() != null)
238 tag = elements[i].getOwnerTag();
241 Production prod = new Production(tag, (cnt==null?null:cnt.name), els, drops);
242 ret = cx.rm.createSequence(prod);
245 for(int i=0; i<els.length; i++)
247 if (idx==-1) idx = i;
248 else throw new Error("multiple non-dropped elements in sequence: " + Sequence.newDropSequence(els));
249 if (idx != -1) ret = Sequence.newSingletonSequence(els, idx);
250 else ret = Sequence.newDropSequence(els);
252 if (this.follow != null)
253 ret = ret.followedBy(this.follow.toAtom(cx));
257 public static @bind.as("&") Seq and2(Seq s, Seq a) { return s.and(a); }
258 public static @bind.as("&~") Seq andnot2(Seq s, Seq a) { return s.andnot(a); }
259 public static @bind.as("->") Seq arrow(Seq s, ElementNode e) { return s.follow(e); }
260 public static @bind.as("::") Seq tag(String tagname, Seq s) { return s.tag(tagname); }
261 public static @bind.as("/") Seq slash(Seq s, ElementNode e) { return s.separate(e); }
263 public static Seq seq(ElementNode[] elements) { return new Seq(elements); }
264 public static @bind.as("Elements") Seq seq2(ElementNode[] elements) { return new Seq(elements); }
265 public static @bind.as Seq psx(Seq s) { return s; }
266 public static @bind.as(":") ElementNode colon(String s, ElementNode e) { return new Label(s, e); }
267 public static @bind.as(")") void close(String foo) { throw new Error("not supported"); }
268 public static @bind.as("()") ElementNode epsilon() { return new Constant(epsilon); }
270 private static Union epsilon = new Union("()");
271 static { epsilon.add(Sequence.newEmptySequence()); }
273 public static class NonTerminalReferenceNode extends ElementNode {
274 public String nonTerminal;
275 public NonTerminalReferenceNode() { }
276 public @bind.as("NonTerminalReference") NonTerminalReferenceNode(String nonTerminal) {
277 this.nonTerminal = prefix + nonTerminal;
279 public Atom toAtom(Context cx) {
280 return cx.grammar.get(nonTerminal).toAtom(cx);
282 public Element build(Context cx, NonTerminalNode cnt) {
283 if (!this.nonTerminal.startsWith(prefix)) nonTerminal = prefix + nonTerminal;
284 Element ret = cx.get(nonTerminal);
285 if (ret == null) throw new RuntimeException("unknown nonterminal \""+nonTerminal+"\"");
290 public static class Literal extends Constant {
291 private String string;
292 public @bind Literal(@bind.arg String string) {
293 super(CharAtom.string(string));
294 this.string = string;
296 public boolean drop() { return true; }
297 public Atom toAtom(Context cx) {
298 if (string.length()!=1) return super.toAtom(cx);
299 edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
300 set.add(string.charAt(0), string.charAt(0));
301 return CharAtom.set(set);
305 public static class CharClass extends ElementNode {
307 public @bind.as("[") CharClass(Range[] ranges) { this.ranges = ranges; }
308 public Atom toAtom(Context cx) {
309 edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
310 for(Range r : ranges)
311 set.add(r.first, r.last);
312 return CharAtom.set(set);
314 public Element build(Context cx, NonTerminalNode cnt) {
315 edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
316 for(Range r : ranges)
317 set.add(r.first, r.last);
318 return CharAtom.set(set);
322 public static @bind.as("{") class XTree extends ElementNode {
323 public @bind.arg Seq body;
324 public Element build(Context cx, NonTerminalNode cnt) {
325 Union u = new Union(null, false);
326 Sequence s = body.build(cx, u, null);
327 Union u2 = new Union(null, false);
328 u2.add(Sequence.newSingletonSequence(new Element[] {
339 public static class Rep extends ElementNode {
340 public ElementNode e, sep;
341 public boolean zero, many, max;
342 public Rep(ElementNode e, ElementNode sep, boolean zero, boolean many, boolean max) {
343 this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
344 public Atom toAtom(Context cx) {
345 if (sep != null) return super.toAtom(cx);
348 public Element build(Context cx, NonTerminalNode cnt) {
350 ? Repeat.repeat(e.build(cx, null), zero, many, sep==null ? null : sep.build(cx, null), cx.rm.repeatTag())
352 ? Repeat.repeatMaximal(e.toAtom(cx), zero, many, cx.rm.repeatTag())
353 : Repeat.repeatMaximal(e.build(cx, null), zero, many, sep.toAtom(cx), cx.rm.repeatTag());
357 // FIXME: it would be nice if we could hoist this into "Rep"
358 public static @bind.as("++") ElementNode plusmax(final ElementNode e)
359 { return new Rep(e, null, false, true, true); }
360 public static @bind.as("+") ElementNode plus(final ElementNode e)
361 { return new Rep(e, null, false, true, false); }
362 public static @bind.as("++/") ElementNode plusmaxfollow(final ElementNode e, final ElementNode sep)
363 { return new Rep(e, sep, false, true, true); }
364 public static @bind.as("+/") ElementNode plusfollow(final ElementNode e, final ElementNode sep)
365 { return new Rep(e, sep, false, true, false); }
366 public static @bind.as("**") ElementNode starmax(final ElementNode e)
367 { return new Rep(e, null, true, true, true); }
368 public static @bind.as("*") ElementNode star(final ElementNode e)
369 { return new Rep(e, null, true, true, false); }
370 public static @bind.as("**/") ElementNode starmaxfollow(final ElementNode e, final ElementNode sep)
371 { return new Rep(e, sep, true, true, true); }
372 public static @bind.as("*/") ElementNode starfollow(final ElementNode e, final ElementNode sep)
373 { return new Rep(e, sep, true, true, false); }
374 public static @bind.as("?") ElementNode question(final ElementNode e)
375 { return new Rep(e, null, true, true, false); }
376 public static @bind.as("!") ElementNode bang(final ElementNode e)
377 { return new Drop(e); }
379 public static @bind.as("^") ElementNode caret(final String s) {
380 final String thePrefix = prefix;
381 return new Constant(CharAtom.string(s)) {
382 public String getOwnerTag() { return thePrefix+s; }
383 public boolean drop() { return true; }
387 public static @bind.as("~") ElementNode tilde(final ElementNode e) {
388 return new ElementNodeWrapper(e) {
389 public Atom toAtom(Context cx) {
390 return infer((Topology<Character>)e.toAtom(cx).complement().minus(CharAtom.braces));
392 public Element build(Context cx, NonTerminalNode cnt) {
393 return infer((Topology<Character>)e.toAtom(cx).complement().minus(CharAtom.braces));
396 public static @bind.as("Word") String word(String s) { return s; }
397 public static @bind.as("Quoted") String quoted(String s) { return s; }
398 public static @bind.as("escaped") String c(char c) { return c+""; }
399 public static @bind.as("EmptyString") String emptystring() { return ""; }
400 public static @bind.as("\n") String retur() { return "\n"; }
401 public static @bind.as("\r") String lf() { return "\r"; }
403 //static Atom infer(Element e) { return infer((Topology<Character>)Atom.toAtom(e)); }
404 static Atom infer(Object t) { return (Atom)t; }
406 public static class Context {
407 public HashMap<String,Union> map = new HashMap<String,Union>();
408 public GrammarNode grammar;
409 public String cnt = null;
410 public Grammar.Bindings rm;
411 public Context(GrammarNode g, Grammar.Bindings rm) {
415 public Union build() {
417 for(NonTerminalNode nt : grammar.values()) {
418 Union u = get(nt.name);
419 if ("s".equals(nt.name))
424 public Context(Tree t, Grammar.Bindings rm) {
426 TreeFunctor<Object,Object> red = (TreeFunctor<Object,Object>)t.head();
427 this.grammar = (GrammarNode)red.invoke(t);
429 public Union peek(String name) { return map.get(name); }
430 public void put(String name, Union u) { map.put(name, u); }
431 public Union get(String name) {
432 Union ret = map.get(name);
433 if (ret != null) return ret;
434 ret = new Union(name);
436 NonTerminalNode nt = grammar.get(name);
438 throw new Error("warning could not find " + name);
442 nt.build(this, ret, nt);
450 public static abstract class ElementNode {
451 public String getLabel() { return null; }
452 public String getOwnerTag() { return null; }
453 public boolean drop() { return false; }
454 public Atom toAtom(Context cx) { throw new Error("can't convert a " + this.getClass().getName() + " to an atom"); }
455 public abstract Element build(Context cx, NonTerminalNode cnt);
458 public static abstract class ElementNodeWrapper extends ElementNode {
459 protected ElementNode _e;
460 public ElementNodeWrapper(ElementNode e) { this._e = e; }
461 public String getLabel() { return _e.getLabel(); }
462 public String getOwnerTag() { return _e.getOwnerTag(); }
463 public boolean drop() { return _e.drop(); }
464 public Atom toAtom(Context cx) { return _e.toAtom(cx); }
465 public Element build(Context cx, NonTerminalNode cnt) { return _e.build(cx, cnt); }
468 public static class Constant extends ElementNode {
470 public Constant(Element constant) { this.constant = constant; }
471 public Element build(Context cx, NonTerminalNode cnt) { return constant; }
472 public Atom toAtom(Context cx) {
473 if (constant instanceof Atom) return ((Atom)constant);
474 return super.toAtom(cx);
478 public abstract static class PostProcess extends ElementNodeWrapper {
479 public PostProcess(ElementNode e) { super(e); }
480 public Element build(Context cx, NonTerminalNode cnt) { return postProcess(_e.build(cx, cnt)); }
481 public abstract Element postProcess(Element e);
484 public static class Drop extends ElementNodeWrapper {
485 public Drop(ElementNode e) { super(e); }
486 public boolean drop() { return true; }
489 public static class Label extends ElementNodeWrapper {
491 public Label(String label, ElementNode e) { super(e); this.label = label; }
492 public String getLabel() { return label; }
496 static class Invert extends Atom {
497 private final Atom a;
498 public Invert(Atom a) { this.a = a; }
499 public Topology top() { return a.complement(); }
500 public String toString() { return "~"+a; }