1 // Copyright 2006 all rights reserved; see LICENSE file for BSD-style license
3 package edu.berkeley.sbp.meta;
4 import edu.berkeley.sbp.util.*;
5 import edu.berkeley.sbp.*;
6 import edu.berkeley.sbp.chr.*;
7 import edu.berkeley.sbp.misc.*;
8 import edu.berkeley.sbp.bind.*;
10 import java.lang.annotation.*;
11 import java.lang.reflect.*;
14 /** The java classes typically used to represent a parsed grammar AST; each inner class is a type of AST node. */
15 public class MetaGrammarBindings extends AnnotationGrammarBindings {
17 public MetaGrammarBindings() { super(MetaGrammarBindings.class); }
19 // FIXME ugly ugly ugly scary dangerous
20 public static String prefix = "";
22 /** A grammar (a set of nonterminals) */
23 public static class GrammarNode extends HashMap<String,NonTerminalNode> {
24 public NonTerminalNode[] getNonTerminals() {
25 return (NonTerminalNode[])values().toArray(new NonTerminalNode[0]);
27 public GrammarNode(NonTerminalNode[] nonterminals) {
28 for(NonTerminalNode nt : nonterminals) {
29 if (nt==null) continue;
30 if (this.get(nt.name)!=null)
31 throw new RuntimeException("duplicate definition of nonterminal \""+nt.name+"\"");
32 this.put(nt.name, nt);
35 public @bind.as("Grammar") GrammarNode(Object[] nt) { add(nt); }
36 private void add(Object[] obs) {
38 if (o==null) continue;
39 else if (o instanceof Object[]) add((Object[])o);
40 else if (o instanceof NonTerminalNode) {
41 NonTerminalNode nt = (NonTerminalNode)o;
42 if (this.get(nt.name)!=null)
43 throw new RuntimeException("duplicate definition of nonterminal \""+nt.name+"\"");
44 this.put(nt.name, nt);
46 else if (o instanceof GrammarNode) add(((GrammarNode)o).getNonTerminals());
49 public String toString() {
51 for(NonTerminalNode nt : values()) ret += nt + ", ";
54 public Union build(String s, Grammar.Bindings rm) {
55 Context cx = new Context(this,rm);
57 for(MetaGrammarBindings.NonTerminalNode nt : values()) {
58 Union el = (Union)cx.get(nt.name);
59 StringBuffer st = new StringBuffer();
61 if (nt.name.equals(s)) u = el;
67 public abstract static class UnionNode extends ElementNode {
68 public Seq[][] sequences;
69 public Atom toAtom(Context cx) {
71 for(Seq[] ss : sequences)
73 ret = ret==null ? s.toAtom(cx) : (Atom)ret.union(s.toAtom(cx));
76 public void build(Context cx, Union u, NonTerminalNode cnt) {
77 HashSet<Sequence> bad2 = new HashSet<Sequence>();
78 for(int i=0; i<sequences.length; i++) {
79 Seq[] group = sequences[i];
80 Union u2 = new Union(null, false);
81 if (sequences.length==1) u2 = u;
82 for(int j=0; j<group.length; j++)
83 group[j].build(cx, u2, cnt);
84 if (sequences.length==1) break;
85 Sequence seq = Sequence.create(u2);
86 for(Sequence s : bad2) seq = seq.andnot(s);
88 bad2.add(Sequence.create(u2));
93 public static @bind.as("#import") GrammarNode poundimport(String fileName, String as) {
94 if (as==null) as = "";
95 else if ("".equals(as)) { }
99 Tree t = new CharParser(MetaGrammar.newInstance()).parse(new FileInputStream("tests/"+fileName)).expand1();
100 TreeFunctor<Object,Object> red = (TreeFunctor<Object,Object>)t.head();
101 String oldprefix = prefix;
103 GrammarNode gn = (GrammarNode)red.invoke(t);
106 } catch (Exception e) {
108 throw new RuntimeException(e);
112 public static class NonTerminalNode extends UnionNode {
114 public String name = null;
115 public String sep = null;
116 public NonTerminalNode[] getNonTerminals() { return new NonTerminalNode[] { this }; }
117 public @bind.as("NonTerminal") NonTerminalNode(@bind.arg String name, @bind.arg Seq[][] sequences) {
118 this(name, sequences, false); }
119 public NonTerminalNode(String name, Seq[][] sequences, boolean rep) { this(name, sequences, rep, null); }
120 public NonTerminalNode(String name, Seq[][] sequences, boolean rep, String sep) {
121 this.name = prefix + name;
122 this.sequences = sequences;
124 this.sep = sep==null?null:(prefix + sep);
126 public Element build(Context cx, NonTerminalNode cnt) { return cx.get(name); }
127 public void build(Context cx, Union u, NonTerminalNode cnt) {
128 if (!rep) { super.build(cx, u, this); return; }
129 HashSet<Sequence> bad2 = new HashSet<Sequence>();
131 Union urep = new Union(null, false);
132 urep.add(Sequence.create());
134 urep.add(Sequence.create(new Element[] { cx.get(sep), u }, 1));
136 urep.add(Sequence.create(new Element[] { u }, 0));
138 for(int i=0; i<sequences.length; i++) {
139 Seq[] group = sequences[i];
140 Union u2 = new Union(null, false);
141 if (sequences.length==1) u2 = u;
142 for(int j=0; j<group.length; j++) {
143 Union u3 = new Union(null, false);
144 group[j].build(cx, u3, this);
145 Sequence s = Sequence.create(cx.rm.repeatTag(),
146 new Element[] { u3, urep },
147 new boolean[] { false, false },
151 if (sequences.length==1) break;
152 Sequence seq = Sequence.create(u2);
153 for(Sequence s : bad2) seq = seq.andnot(s);
155 bad2.add(Sequence.create(u2));
160 public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg Seq[][] sequences) {
161 return new NonTerminalNode(name, sequences, true); }
162 public static @bind.as("=") NonTerminalNode go(@bind.arg String name, @bind.arg String sep, @bind.arg Seq[][] sequences) {
163 return new NonTerminalNode(name, sequences, true, sep); }
165 public static class AnonUnionNode extends UnionNode {
166 public @bind.as("(") AnonUnionNode(Seq[][] sequences) {
167 this.sequences = sequences;
169 public Element build(Context cx, NonTerminalNode cnt) {
170 Union ret = new Union(null, false);
176 public static class Range {
177 public @bind Range(char only) { first = only; last = only; }
178 public @bind Range(char first, char last) { this.first = first; this.last = last; }
183 public static /*abstract*/ class Seq {
184 HashSet<Seq> and = new HashSet<Seq>();
185 HashSet<Seq> not = new HashSet<Seq>();
186 ElementNode[] elements;
189 public void append(ElementNode e) {
190 ElementNode[] elements = new ElementNode[this.elements.length+1];
191 System.arraycopy(this.elements, 0, elements, 0, this.elements.length);
192 this.elements = elements;
193 elements[elements.length-1] = e;
195 public Seq(ElementNode e) { this(new ElementNode[] { e }); }
196 public Seq(ElementNode[] elements) { this.elements = elements; }
197 public Atom toAtom(Context cx) {
198 if (elements.length != 1) throw new Error("you attempted to use ->, **, ++, or a similar character-class operator on a [potentially] multicharacter production");
199 return elements[0].toAtom(cx);
201 public Seq tag(String tag) { this.tag = prefix+tag; return this; }
202 public Seq follow(ElementNode follow) {
203 this.follow = follow;
207 Seq ret = new Seq(elements);
211 ret.tag = prefix+tag;
214 public Seq and(Seq s) { and.add(s); return this; }
215 public Seq andnot(Seq s) { not.add(s); return this; }
216 public Seq separate(ElementNode sep) {
217 ElementNode[] elements = new ElementNode[this.elements.length * 2 - 1];
218 for(int i=0; i<this.elements.length; i++) {
219 elements[i*2] = this.elements[i];
220 if (i<this.elements.length-1)
221 elements[i*2+1] = new Drop(sep);
223 this.elements = elements;
226 public Sequence build(Context cx, Union u, NonTerminalNode cnt) {
227 Sequence ret = build0(cx, cnt);
228 for(Seq s : and) { Sequence dork = s.build(cx, null, cnt); ret = ret.and(dork); }
229 for(Seq s : not) { Sequence dork = s.build(cx, null, cnt); ret = ret.andnot(dork); }
230 if (u!=null) u.add(ret);
233 public Sequence build0(Context cx, NonTerminalNode cnt) {
234 boolean[] drops = new boolean[elements.length];
235 Element[] els = new Element[elements.length];
236 for(int i=0; i<elements.length; i++) {
237 drops[i] = elements[i].drop();
238 els[i] = elements[i].build(cx, cnt);
239 if (elements[i].getOwnerTag() != null)
240 tag = elements[i].getOwnerTag();
243 Production prod = new Production(tag, (cnt==null?null:cnt.name), els, drops);
244 ret = cx.rm.createSequence(prod);
247 for(int i=0; i<els.length; i++)
249 if (idx==-1) idx = i;
250 else throw new Error("multiple non-dropped elements in sequence: " + Sequence.create(els, null));
251 if (idx != -1) ret = Sequence.create(els, idx);
252 else ret = Sequence.create(els, null);
254 if (this.follow != null)
255 ret = ret.followedBy(this.follow.toAtom(cx));
259 public static @bind.as("&") Seq and2(Seq s, Seq a) { return s.and(a); }
260 public static @bind.as("&~") Seq andnot2(Seq s, Seq a) { return s.andnot(a); }
261 public static @bind.as("->") Seq arrow(Seq s, ElementNode e) { return s.follow(e); }
262 public static @bind.as("::") Seq tag(String tagname, Seq s) { return s.tag(tagname); }
263 public static @bind.as("/") Seq slash(Seq s, ElementNode e) { return s.separate(e); }
265 public static Seq seq(ElementNode[] elements) { return new Seq(elements); }
266 public static @bind.as("Elements") Seq seq2(ElementNode[] elements) { return new Seq(elements); }
267 public static @bind.as Seq psx(Seq s) { return s; }
268 public static @bind.as(":") ElementNode colon(String s, ElementNode e) { return new Label(s, e); }
269 public static @bind.as(")") void close(String foo) { throw new Error("not supported"); }
270 public static @bind.as("()") ElementNode epsilon() { return new Constant(epsilon); }
272 private static Union epsilon = new Union("()");
273 static { epsilon.add(Sequence.create()); }
275 public static class NonTerminalReferenceNode extends ElementNode {
276 public String nonTerminal;
277 public NonTerminalReferenceNode() { }
278 public @bind.as("NonTerminalReference") NonTerminalReferenceNode(String nonTerminal) {
279 this.nonTerminal = prefix + nonTerminal;
281 public Atom toAtom(Context cx) {
282 return cx.grammar.get(nonTerminal).toAtom(cx);
284 public Element build(Context cx, NonTerminalNode cnt) {
285 if (!this.nonTerminal.startsWith(prefix)) nonTerminal = prefix + nonTerminal;
286 Element ret = cx.get(nonTerminal);
287 if (ret == null) throw new RuntimeException("unknown nonterminal \""+nonTerminal+"\"");
292 public static class Literal extends Constant {
293 private String string;
294 public @bind Literal(@bind.arg String string) {
295 super(CharAtom.string(string));
296 this.string = string;
298 public boolean drop() { return true; }
299 public Atom toAtom(Context cx) {
300 if (string.length()!=1) return super.toAtom(cx);
301 edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
302 set.add(string.charAt(0), string.charAt(0));
303 return CharAtom.set(set);
307 public static class CharClass extends ElementNode {
309 public @bind.as("[") CharClass(Range[] ranges) { this.ranges = ranges; }
310 public Atom toAtom(Context cx) {
311 edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
312 for(Range r : ranges)
313 set.add(r.first, r.last);
314 return CharAtom.set(set);
316 public Element build(Context cx, NonTerminalNode cnt) {
317 edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
318 for(Range r : ranges)
319 set.add(r.first, r.last);
320 return CharAtom.set(set);
324 public static @bind.as("\\{") ElementNode leftBrace() {
325 return new Drop(new CharClass(new Range[] { new Range(CharAtom.left, CharAtom.left) })); }
326 public static @bind.as("\\}") ElementNode rightBrace() {
327 return new Drop(new CharClass(new Range[] { new Range(CharAtom.right, CharAtom.right) })); }
329 public static @bind.as("{") class XTree extends ElementNode {
330 public @bind.arg Seq body;
331 public Element build(Context cx, NonTerminalNode cnt) {
332 Union u = new Union(null, false);
333 Sequence s = body.build(cx, u, null);
334 Union u2 = new Union(null, false);
335 u2.add(Sequence.create(new Element[] {
346 public static class Rep extends ElementNode {
347 public ElementNode e, sep;
348 public boolean zero, many, max;
349 public Rep(ElementNode e, ElementNode sep, boolean zero, boolean many, boolean max) {
350 this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
351 public Atom toAtom(Context cx) {
352 if (sep != null) return super.toAtom(cx);
355 public Element build(Context cx, NonTerminalNode cnt) {
357 ? Repeat.repeat(e.build(cx, null), zero, many, sep==null ? null : sep.build(cx, null), cx.rm.repeatTag())
359 ? Repeat.repeatMaximal(e.toAtom(cx), zero, many, cx.rm.repeatTag())
360 : Repeat.repeatMaximal(e.build(cx, null), zero, many, sep.toAtom(cx), cx.rm.repeatTag());
364 // FIXME: it would be nice if we could hoist this into "Rep"
365 public static @bind.as("++") ElementNode plusmax(final ElementNode e)
366 { return new Rep(e, null, false, true, true); }
367 public static @bind.as("+") ElementNode plus(final ElementNode e)
368 { return new Rep(e, null, false, true, false); }
369 public static @bind.as("++/") ElementNode plusmaxfollow(final ElementNode e, final ElementNode sep)
370 { return new Rep(e, sep, false, true, true); }
371 public static @bind.as("+/") ElementNode plusfollow(final ElementNode e, final ElementNode sep)
372 { return new Rep(e, sep, false, true, false); }
373 public static @bind.as("**") ElementNode starmax(final ElementNode e)
374 { return new Rep(e, null, true, true, true); }
375 public static @bind.as("*") ElementNode star(final ElementNode e)
376 { return new Rep(e, null, true, true, false); }
377 public static @bind.as("**/") ElementNode starmaxfollow(final ElementNode e, final ElementNode sep)
378 { return new Rep(e, sep, true, true, true); }
379 public static @bind.as("*/") ElementNode starfollow(final ElementNode e, final ElementNode sep)
380 { return new Rep(e, sep, true, true, false); }
381 public static @bind.as("?") ElementNode question(final ElementNode e)
382 { return new Rep(e, null, true, true, false); }
383 public static @bind.as("!") ElementNode bang(final ElementNode e)
384 { return new Drop(e); }
386 public static @bind.as("^") ElementNode caret(final String s) {
387 final String thePrefix = prefix;
388 return new Constant(CharAtom.string(s)) {
389 public String getOwnerTag() { return thePrefix+s; }
390 public boolean drop() { return true; }
394 public static @bind.as("~") ElementNode tilde(final ElementNode e) {
395 return new ElementNodeWrapper(e) {
396 public Atom toAtom(Context cx) {
397 return infer((Topology<Character>)e.toAtom(cx).complement()/*.minus(CharAtom.braces)*/);
399 public Element build(Context cx, NonTerminalNode cnt) {
400 return infer((Topology<Character>)e.toAtom(cx).complement()/*.minus(CharAtom.braces)*/);
403 public static @bind.as("Word") String word(String s) { return s; }
404 public static @bind.as("Quoted") String quoted(String s) { return s; }
405 public static @bind.as("escaped") String c(char c) {
406 if (c=='{') return CharAtom.left+"";
407 if (c=='}') return CharAtom.right+"";
410 public static @bind.as("EmptyString") String emptystring() { return ""; }
411 public static @bind.as("\n") String retur() { return "\n"; }
412 public static @bind.as("\r") String lf() { return "\r"; }
414 //static Atom infer(Element e) { return infer((Topology<Character>)Atom.toAtom(e)); }
415 static Atom infer(Object t) { return (Atom)t; }
417 public static class Context {
418 public HashMap<String,Union> map = new HashMap<String,Union>();
419 public GrammarNode grammar;
420 public String cnt = null;
421 public Grammar.Bindings rm;
422 public Context(GrammarNode g, Grammar.Bindings rm) {
426 public Union build() {
428 for(NonTerminalNode nt : grammar.values()) {
429 Union u = get(nt.name);
430 if ("s".equals(nt.name))
435 public Context(Tree t, Grammar.Bindings rm) {
437 TreeFunctor<Object,Object> red = (TreeFunctor<Object,Object>)t.head();
438 this.grammar = (GrammarNode)red.invoke(t);
440 public Union peek(String name) { return map.get(name); }
441 public void put(String name, Union u) { map.put(name, u); }
442 public Union get(String name) {
443 Union ret = map.get(name);
444 if (ret != null) return ret;
445 ret = new Union(name);
447 NonTerminalNode nt = grammar.get(name);
449 throw new Error("warning could not find " + name);
453 nt.build(this, ret, nt);
461 public static abstract class ElementNode {
462 public String getLabel() { return null; }
463 public String getOwnerTag() { return null; }
464 public boolean drop() { return false; }
465 public Atom toAtom(Context cx) { throw new Error("can't convert a " + this.getClass().getName() + " to an atom"); }
466 public abstract Element build(Context cx, NonTerminalNode cnt);
469 public static abstract class ElementNodeWrapper extends ElementNode {
470 protected ElementNode _e;
471 public ElementNodeWrapper(ElementNode e) { this._e = e; }
472 public String getLabel() { return _e.getLabel(); }
473 public String getOwnerTag() { return _e.getOwnerTag(); }
474 public boolean drop() { return _e.drop(); }
475 public Atom toAtom(Context cx) { return _e.toAtom(cx); }
476 public Element build(Context cx, NonTerminalNode cnt) { return _e.build(cx, cnt); }
479 public static class Constant extends ElementNode {
481 public Constant(Element constant) { this.constant = constant; }
482 public Element build(Context cx, NonTerminalNode cnt) { return constant; }
483 public Atom toAtom(Context cx) {
484 if (constant instanceof Atom) return ((Atom)constant);
485 return super.toAtom(cx);
489 public abstract static class PostProcess extends ElementNodeWrapper {
490 public PostProcess(ElementNode e) { super(e); }
491 public Element build(Context cx, NonTerminalNode cnt) { return postProcess(_e.build(cx, cnt)); }
492 public abstract Element postProcess(Element e);
495 public static class Drop extends ElementNodeWrapper {
496 public Drop(ElementNode e) { super(e); }
497 public boolean drop() { return true; }
500 public static class Label extends ElementNodeWrapper {
502 public Label(String label, ElementNode e) { super(e); this.label = label; }
503 public String getLabel() { return label; }
507 static class Invert extends Atom {
508 private final Atom a;
509 public Invert(Atom a) { this.a = a; }
510 public Topology top() { return a.complement(); }
511 public String toString() { return "~"+a; }