checkpoint
[sbp.git] / src / edu / berkeley / sbp / misc / Demo.java
1 package edu.berkeley.sbp.misc;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.chr.*;
5 import java.util.*;
6 import java.lang.annotation.*;
7 import java.lang.reflect.*;
8 import java.io.*;
9
10 public class Demo {
11     
12     public static boolean harsh = false;
13
14     public static void main(String[] s) throws Exception {
15
16         ReflectiveMeta m = new ReflectiveMeta();
17         Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
18         MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res);
19         MetaGrammar.BuildContext bc = new MetaGrammar.BuildContext(mgf);
20
21         Union meta = mgf.get("s").build(bc);
22         Tree t = new CharParser(meta).parse(new FileInputStream(s[1])).expand1();
23
24         Union u = Demo.make(t, "s");
25
26         System.err.println();
27         System.err.println("== parsing with parsed grammar =================================================================================");
28         t = new CharParser((Union)u).parse(new FileInputStream(s[1])).expand1();
29         System.out.println(t.toPrettyString());
30
31         System.err.println("== parsing with parsed-parsed grammar ==========================================================================");
32         t = new CharParser(new Context(t, m).build()).parse(new FileInputStream(s[1])).expand1();
33         System.out.println(t.toPrettyString());
34     }
35
36     public static class ReflectiveMetaPlain extends ReflectiveMeta {
37         public Object repeatTag() { return null; }
38         public Sequence tryResolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
39             return null; }
40         public Sequence resolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
41             return Sequence.rewritingSequence(tag, els, labels, drops);
42         }
43     }
44
45     public static class ReflectiveMeta extends MetaGrammar.Meta {
46         private final Class _cl;
47         private final Class[] _inner;
48         public ReflectiveMeta() {
49             this(MG.class);
50         }
51         public ReflectiveMeta(Class c) {
52             this._cl = c;
53             this._inner = c.getDeclaredClasses();
54         }
55         public ReflectiveMeta(Class c, Class[] inner) {
56             this._cl = c;
57             this._inner = inner;
58         }
59         private boolean match(Method m, String s) { return match(m.getAnnotation(bind.as.class), null, s); }
60         private boolean match(bind.as t, Class c, String s) {
61             if (t==null) return false;
62             if (t.value().equals(s)) return true;
63             if (c != null && t.equals("") && c.getSimpleName().equals(s)) return true;
64             return false;
65         }
66         /*
67         private boolean match(nonterminal t, Class c, String s) {
68             if (t==null) return false;
69             if (t.value().equals(s)) return true;
70             if (c != null && t.equals("") && c.getSimpleName().equals(s)) return true;
71             return false;
72         }
73         */
74         private boolean match(Class c, String s, String nonTerminalName) {
75             if (match((bind.as)c.getAnnotation(bind.as.class), c, s)) return true;
76             //if (match((nonterminal)c.getAnnotation(bind.as.class), c, nonTerminalName)) return true;
77             return false;
78         }
79         public boolean match(Constructor con, String s, String nonTerminalName) {
80             Class c = con.getDeclaringClass();
81             if (match((bind.as)con.getAnnotation(bind.as.class), null, s)) return true;
82             //if (match((nonterminal)con.getAnnotation(bind.as.class), c, s)) return true;
83             return false;
84         }
85         public Object repeatTag() {
86             return new Tree.ArrayBuildingTreeFunctor<Object>();
87         }
88         public Sequence tryResolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
89             Production p = new Production(tag, nonTerminalName, els, labels, drops);
90             for(Method m : _cl.getMethods())
91                 if (new Target(m).isCompatible(p))
92                     return new Target(m).makeSequence(p);
93             for(Class c : _inner)
94                 for(Constructor con : c.getConstructors())
95                     if (new Target(con).isCompatible(p))
96                         return new Target(con).makeSequence(p);
97             for(Class c : _inner)
98                 if (new Target(c).isCompatible(p))
99                     return new Target(c).makeSequence(p);
100             return null;
101         }
102         public Sequence resolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
103             Sequence ret = tryResolveTag(tag, nonTerminalName, els, labels, drops);
104             if (ret != null) return ret;
105             String message = "could not find a Java method/class/ctor matching tag \""+tag+
106                 "\", nonterminal \""+nonTerminalName+"\" with " + els.length + " arguments";
107             if (harsh) {
108                 throw new RuntimeException(message);
109             } else {
110                 System.err.println(message);
111                 return Sequence.rewritingSequence(tag, els, labels, drops);
112             }
113         }
114     }
115
116     
117     public static class bind {    
118         /**
119          *  Constructors, classes, and methods with this attribute will
120          *  match every production of the nonterminal called "value()"
121          *  that is arg-compatible.  If value() is undefined, then the
122          *  class/constructor/method name is used.
123          */ 
124         /*
125           @Retention(RetentionPolicy.RUNTIME) public static @interface nonterminal { String value() default ""; }
126         */
127
128         @Retention(RetentionPolicy.RUNTIME) public static @interface raw { }
129
130         /**
131          *  Constructors, classes, and methods with this attribute will
132          *  match every tree tagged with "value()" that is arg-compatible.
133          *  If value() is undefined, then the class/constructor/method
134          *  name is used.
135          */ 
136
137         @Retention(RetentionPolicy.RUNTIME) public static @interface as         { String value() default ""; }
138
139
140         /**
141          *  If any parameter to a method or field in a class has a named
142          *  arg-tag, that parameter/field matches the child of the tree
143          *  which either has that label or else is a reference to a
144          *  nonterminal with the corresponding name.
145          *  
146          *  The remaining non-named arg-tags match the remaining children
147          *  of the tree in sequential order.
148          *
149          *  If any arg-tagged parameters/fields remain, the match fails.
150          *  If there were no arg-tagged parameters-fields, it is as if all
151          *  of them were non-named and arg-tagged.
152          *
153          *  A method/constructor is arg-compatible if all of its arguments
154          *  are arg-compatible.
155          *
156          *  A class is arg-compatible if all of its fields are
157          *  arg-compatible, or if one of its constructors is arg-compatible.
158          *
159          */
160         @Retention(RetentionPolicy.RUNTIME) public static @interface arg         { String value() default ""; }
161     }
162
163     public static class Production {
164         public String tag;
165         public String nonTerminal;
166         public Object[] labels;
167         public boolean[] drops;
168         public Element[] elements;
169         public int count = 0;
170         public Production(String tag, String nonTerminal, Element[] elements, Object[] labels, boolean[] drops) {
171             this.tag = tag;
172             this.elements = elements;
173             this.nonTerminal = nonTerminal;
174             this.labels = labels;
175             this.drops = drops;
176             for(int i=0; i<drops.length; i++)
177                 if (!drops[i])
178                     count++;
179         }
180     }
181
182     public static class Target {
183             public int[] buildSequence(Production p) {
184                 Annotation[][] annotations = _bindable.getArgAnnotations();
185                 String[]       names       = _bindable.getArgNames();
186                 String name = _bindable.getSimpleName();
187                 int len = annotations.length;
188                 int ofs = 0;
189                 bind.arg[] argtags  = new bind.arg[len];
190                 for(int i=0; i<names.length; i++)
191                     for(Annotation a : annotations[i+ofs])
192                         if (a instanceof bind.arg)
193                             argtags[i+ofs] = (bind.arg)a;
194                 return Target.this.buildSequence(p, names, argtags);
195             }
196         private Reflection.Bindable _bindable;
197
198         public Target(Object o) { this(Reflection.Bindable.create(o)); }
199         public Target(Reflection.Bindable b) { this._bindable = b; }
200
201         public String getName() { return _bindable.getSimpleName(); }
202         public bind.as getBindAs() { return (bind.as)_bindable.getAnnotation(bind.as.class); }
203         //public nonterminal getNonTerminal() { return (nonterminal)_bindable.getAnnotation(bind.as.class); }
204         public String toString() { return _bindable.getSimpleName(); }
205         public Object plant(Object[] fields) { return _bindable.impose(fields); }
206         public boolean isRaw() { return _bindable.isAnnotationPresent(bind.raw.class); }
207         public Object invokeRaw(Iterable<Tree<Object>> t) { return _bindable.impose(new Object[] { t }); }
208
209         public boolean isCompatible(Production p) {
210             bind.as t = getBindAs();
211             if (t != null &&
212                 (t.value().equals(p.tag) ||
213                  (t.value().equals("") && getName().equals(p.tag))))
214                 return buildSequence(p)!=null;
215
216             bind.as n = getBindAs();
217             if (n != null &&
218                 (n.value().equals(p.nonTerminal) ||
219                  (n.value().equals("") && getName().equals(p.nonTerminal))))
220                 return buildSequence(p)!=null;
221
222             return false;
223         }
224
225         public int[] buildSequence(Production p, String[] names, bind.arg[] argtags) {
226             int argTagged = 0;
227             for(int i=0; i<argtags.length; i++)
228                 if (argtags[i] != null)
229                     argTagged++;
230
231             // FIXME: can be smarter here
232             if (names.length==p.count) {
233                 int[] ret = new int[p.count];
234                 for(int i=0; i<p.count; i++) ret[i] = i;
235                 return ret;
236             } else if (argTagged==p.count) {
237                 int[] ret = new int[argtags.length];
238                 int j = 0;
239                 for(int i=0; i<argtags.length; i++)
240                     ret[i] = argtags[i]==null ? -1 : (j++);
241                 return ret;
242             } else {
243                 return null;
244             }
245         }
246         public Sequence makeSequence(Production p) {
247             return Sequence.rewritingSequence(new TargetReducer(p, buildSequence(p), "reducer-"+this),
248                                               p.elements, p.labels, p.drops);
249         }
250
251         public class TargetReducer implements Tree.TreeFunctor<Object,Object> {
252             private Production p;
253             private int[] map;
254             private String name;
255             public TargetReducer(Production p, int[] map, String name) {
256                 this.p = p;
257                 this.map = map;
258                 this.name = name;
259             }
260             public String toString() { return name; }
261             public Object invoke(Iterable<Tree<Object>> t) {
262                 if (isRaw()) return invokeRaw(t);
263                 ArrayList ret = new ArrayList();
264                 for(Tree tc : t) {
265                     if (tc.head() != null && tc.head() instanceof Functor)
266                         ret.add(((Tree.TreeFunctor<Object,Object>)tc.head()).invoke(tc.children()));
267                     else if (tc.numChildren() == 0)
268                         ret.add(tc.head());
269                     else {
270                         System.err.println("FIXME: don't know what to do about " + tc);
271                         ret.add(null);
272                     }
273                 }
274                 System.err.println("input tree: " + t);
275                 Object[] o = (Object[])ret.toArray(new Object[0]);
276                 int max = 0;
277                 for(int i=0; i<map.length; i++) max = Math.max(map[i], max);
278                 Object[] o2 = new Object[max+1];
279                 for(int i=0; i<o.length; i++) o2[map[i]] = o[i];
280                 return plant(o2);
281             }
282         }
283     }
284
285     public static Union cached = null;
286     public static Union make() {
287         if (cached != null) return cached;
288         try {
289             ReflectiveMeta m = new ReflectiveMeta();
290             Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream("tests/meta.g")).expand1();
291             MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res);
292             MetaGrammar.BuildContext bc = new MetaGrammar.BuildContext(mgf);
293             Union meta = mgf.get("s").build(bc);
294             Tree t = new CharParser(meta).parse(new FileInputStream("tests/meta.g")).expand1();
295             return cached = make(t, "s");
296         } catch (Exception e) {
297             throw new RuntimeException(e);
298         }
299     }
300     public static Union make(Tree t, String s) { return make(t, s, new ReflectiveMeta()); }
301     public static Union make(Tree t, String s, ReflectiveMeta rm) {
302         Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
303         MG.Grammar g = (MG.Grammar)red.invoke(t.children());
304         Context cx = new Context(g,rm);
305         Union u = null;
306         for(MG.NonTerminal nt : g.nonterminals) {
307             System.out.println(nt.name);
308             Union el = (Union)cx.get(nt.name);
309             StringBuffer st = new StringBuffer();
310             el.toString(st);
311             System.err.println(st);
312             if (nt.name.equals(s)) u = el;
313         }
314         return u;
315     }
316
317     public static class MG {
318         public static @bind.as("grammar") class Grammar {
319             public NonTerminal get(String s) {
320                 for(NonTerminal nt : nonterminals)
321                     if (nt.name.equals(s))
322                         return nt;
323                 return null;
324             }
325             public @bind.arg("NonTerminal") NonTerminal[] nonterminals;
326             public String toString() {
327                 String ret = "[ ";
328                 for(NonTerminal nt : nonterminals) ret += nt + ", ";
329                 return ret + " ]";
330             }
331         }
332         public abstract static class Un extends El {
333             public Seq[][] sequences;
334             public void build(Context cx, Union u) {
335                 HashSet<Sequence> bad2 = new HashSet<Sequence>();
336                 for(int i=0; i<sequences.length; i++) {
337                     Seq[] group = sequences[i];
338                     Union u2 = new Union();
339                     if (sequences.length==1) u2 = u;
340                     for(int j=0; j<group.length; j++) {
341                         group[j].build(cx, u2, false);
342                     }
343                     if (sequences.length==1) break;
344                     Sequence seq = Sequence.singleton(u2);
345                     for(Sequence s : bad2) {
346                         s.lame = true;
347                         seq = seq.not(s);
348                     }
349                     u.add(seq);
350                     bad2.add(Sequence.singleton(u2));
351                 }
352             }
353         }
354         public static class NonTerminal extends Un {
355             public String  name = null;
356             public @bind.as("=") NonTerminal(@bind.arg("Word") String name,
357                                          @bind.arg("RHS") Seq[][] sequences) {
358                 this.name = name;
359                 this.sequences = sequences;
360             }
361             public Element build(Context cx) { return cx.get(name); }
362         }
363
364         public static class AnonUn extends Un {
365             public @bind.as("(") AnonUn(Seq[][] sequences) {
366                 this.sequences = sequences;
367             }
368             public Element build(Context cx) {
369                 Union ret = new Union();
370                 build(cx, ret);
371                 return ret;
372             }
373         }
374
375         //public static @bind.as void range(char c) { }
376         public static class Range {
377             public @bind.as("range") Range(char only) { first = only; last = only; }
378             public @bind.as("-")     Range(char first, char last) { this.first = first; this.last = last; }
379             public char first;
380             public char last;
381         }
382         public static abstract class El {
383             public String getLabel() { return null; }
384             public String getOwnerTag() { return null; }
385             public boolean drop() { return false; }
386             public abstract Element build(Context cx);
387         }
388         public static class Drop extends El {
389             public El e;
390             public Drop(El e) { this.e = e; }
391             public String getLabel() { return null; }
392             public boolean drop() { return true; }
393             public String getOwnerTag() { return e.getOwnerTag(); }
394             public Element build(Context cx) { return e.build(cx); }
395         }
396         public static class Label extends El {
397             public String label;
398             public El e;
399             public Label(String label, El e) { this.e = e; this.label = label; }
400             public String getLabel() { return label; }
401             public String getOwnerTag() { return e.getOwnerTag(); }
402             public Element build(Context cx) { return e.build(cx); }
403         }
404         public static /*abstract*/ class Seq {
405             HashSet<Seq> and = new HashSet<Seq>();
406             HashSet<Seq> not = new HashSet<Seq>();
407             El[] elements;
408             El follow;
409             String tag = null;
410             boolean lame;
411             public Seq(El e) { this(new El[] { e }); }
412             public Seq(El[] elements) { this.elements = elements; }
413             public Seq tag(String tag) { this.tag = tag; return this; }
414             public Seq follow(El follow) { this.follow = follow; return this; }
415             public Seq dup() {
416                 Seq ret = new Seq(elements);
417                 ret.and.addAll(and);
418                 ret.not.addAll(not);
419                 ret.follow = follow;
420                 ret.tag = tag;
421                 return ret;
422             }
423             public Seq and(Seq s) { and.add(s); s.lame = true; return this; }
424             public Seq andnot(Seq s) { not.add(s); s.lame = true; return this; }
425             public Seq separate(El sep) {
426                 El[] elements = new El[this.elements.length * 2 - 1];
427                 for(int i=0; i<this.elements.length; i++) {
428                     elements[i*2]   = this.elements[i];
429                     if (i<this.elements.length-1)
430                         elements[i*2+1] = new Drop(sep);
431                 }
432                 this.elements = elements;
433                 return this;
434             }
435             public Sequence build(Context cx, Union u, boolean lame) {
436                 Sequence ret = build0(cx, lame || this.lame);
437                 for(Seq s : and) { Sequence dork = s.build(cx, u, true); ret = ret.and(dork); }
438                 for(Seq s : not) { Sequence dork = s.build(cx, u, true); ret = ret.not(dork); }
439                 u.add(ret);
440                 ret.lame = lame;
441                 return ret;
442             }
443             public Sequence build0(Context cx, boolean lame) {
444                 boolean unwrap = false;
445                 boolean dropAll = lame;
446                 if (tag!=null && tag.equals("[]")) unwrap  = true;
447                 if (tag!=null && "()".equals(tag)) dropAll = true;
448                 Object[] labels = new Object[elements.length];
449                 boolean[] drops = new boolean[elements.length];
450                 Element[] els = new Element[elements.length];
451                 for(int i=0; i<elements.length; i++) {
452                     labels[i] = elements[i].getLabel();
453                     drops[i]  = elements[i].drop();
454                     els[i] = elements[i].build(cx);
455                     if (elements[i].getOwnerTag() != null)
456                         tag = elements[i].getOwnerTag();
457                 }
458                 Sequence ret = null;
459                 if (dropAll)     ret = Sequence.drop(els, false);
460                 else if (unwrap) ret = Sequence.unwrap(els, cx.rm.repeatTag(), drops);
461                 else if (tag!=null) {
462                     ret = cx.rm.resolveTag(tag, cx.cnt, els, labels, drops);
463                 } else {
464                     int idx = -1;
465                     for(int i=0; i<els.length; i++)
466                         if (!drops[i])
467                             if (idx==-1) idx = i;
468                             else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els,false));
469                     if (idx != -1) ret = Sequence.singleton(els, idx);
470                     else           ret = Sequence.drop(els, false);
471                 }
472                 if (this.follow != null)
473                     ret.follow = MetaGrammar.infer(this.follow.build(cx));
474                 ret.lame = this.lame;
475                 return ret;
476             }
477         }
478         public static @bind.as("&")   Seq  and(Seq s,         El[] elements) { return s.and(seq(elements)); }
479         public static @bind.as("&~")  Seq  andnot(Seq s,      El[] elements) { return s.andnot(seq(elements)); }
480         public static @bind.as("->")  Seq  arrow(Seq s, El e)                { return s.follow(e); }
481         public static @bind.as("::")  Seq  tag(String tagname, Seq s)        { return s.tag(tagname); }
482         public static @bind.as("/")   Seq  slash(Seq s, El e)                { return s.separate(e); }
483
484         public static @bind.as("ps")  Seq  seq(El[] elements)                { return new Seq(elements); }
485         public static @bind.as        Seq  psx(Seq s)                        { return s; }
486         public static @bind.as(":")   El   colon(String s, El e)             { return new Label(s, e); }
487         public static @bind.as(")")   void close(String foo)                 { throw new Error("not supported"); }
488         public static @bind.as("()")  El   epsilon()                         { return new Constant(Union.epsilon); }
489
490         public static @bind.as("nonTerminal") class NonTerminalReference extends El {
491             public @bind.arg String nonTerminal;
492             public Element build(Context cx) {
493                 return cx.get(nonTerminal);
494             }
495         }
496
497         public static class StringLiteral        extends Constant {
498             public @bind.as("literal") StringLiteral(String string) { super(CharRange.string(string)); }
499             public boolean drop() { return true; }
500         }
501
502         public static                     class CharClass            extends El {
503             Range[] ranges;
504             public @bind.as("[") CharClass(Range[] ranges) { this.ranges = ranges; }
505             public Element build(Context cx) {
506                 edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
507                 for(Range r : ranges)
508                         set.add(r.first, r.last);
509                 return CharRange.set(set);
510             }
511         }
512
513         public static @bind.as("{")           class XTree                 extends El {
514             public @bind.arg Seq body;
515             public Element build(Context cx) {
516                 throw new Error();
517             }
518         }
519
520         public static class Rep extends El {
521             public El e, sep;
522             public boolean zero, many, max;
523             public Rep(El e, El sep, boolean zero, boolean many, boolean max) {
524                 this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
525             public Element build(Context cx) {
526                 return (!max)
527                     ? Sequence.repeat(e.build(cx),        zero, many, sep==null ? null : sep.build(cx), cx.rm.repeatTag())
528                     : sep==null
529                     ? Sequence.repeatMaximal(MetaGrammar.infer(e.build(cx)), zero, many,                                   cx.rm.repeatTag())
530                     : Sequence.repeatMaximal(e.build(cx),                    zero, many, MetaGrammar.infer(sep.build(cx)), cx.rm.repeatTag());
531             }
532         }
533         public static class Constant extends El {
534             Element constant;
535             public Constant(Element constant) { this.constant = constant; }
536             public Element build(Context cx) { return constant; }
537         }
538         public abstract static class PostProcess extends El {
539             El e;
540             public PostProcess(El e) { this.e = e; }
541             public Element build(Context cx) { return postProcess(e.build(cx)); }
542             public abstract Element postProcess(Element e);
543         }
544
545         // FIXME: it would be nice if we could hoist this into "Rep"
546         public static @bind.as("++")  El plusmax(final El e)                     { return new Rep(e, null, false, true, true); }
547         public static @bind.as("+")   El plus(final El e)                        { return new Rep(e, null, false, true, false); }
548         public static @bind.as("++/") El plusmaxfollow(final El e, final El sep) { return new Rep(e, sep,  false, true, true); }
549         public static @bind.as("+/")  El plusfollow(final El e, final El sep)    { return new Rep(e, sep,  false, true, false); }
550         public static @bind.as("**")  El starmax(final El e)                     { return new Rep(e, null, true,  true, true); }
551         public static @bind.as("*")   El star(final El e)                        { return new Rep(e, null, true,  true, false); }
552         public static @bind.as("**/") El starmaxfollow(final El e, final El sep) { return new Rep(e, sep,  true,  true, true); }
553         public static @bind.as("*/")  El starfollow(final El e, final El sep)    { return new Rep(e, sep,  true,  true, false); }
554         public static @bind.as("?")   El question(final El e)                    { return new Rep(e, null, true,  true, false); }
555
556         public static @bind.as("!")   El bang(final El e)                        { return new Drop(e); }
557
558         public static @bind.as("^")   El caret(final String s) {
559             return new Drop(new Constant(CharRange.string(s)) {
560                     public String getOwnerTag() { return s; }
561                 });
562         }
563
564         public static @bind.as("~")   El tilde(final El e) {
565             return new PostProcess(e) {
566                     public Element postProcess(Element e) {
567                         return MetaGrammar.infer((Topology<Character>)Atom.toAtom(e).complement()); 
568                     } }; }
569
570         public static @bind.as("^^")  void doublecaret(final El e)                 { throw new Error("not implemented"); }
571
572         //public static @bind.as("(")   El subexpression(Seq[][] rhs)                { return new NonTerminal(rhs); }
573
574         public static @bind.as("Word")    String word(String s) { return s; }
575         public static @bind.as("Quoted")  String quoted(String s) { return s; }
576         public static @bind.as("escaped") String c(char c) { return c+""; }
577         public static @bind.as("\"\"")    String emptystring() { return ""; }
578         public static @bind.as("\n")      String retur() { return "\n"; }
579         public static @bind.as("\r")      String lf() { return "\r"; }
580
581     }
582     public static class Context {
583         HashMap<String,Union> map = new HashMap<String,Union>();
584         private MG.Grammar grammar;
585         public String cnt = null;
586         private ReflectiveMeta rm;
587         public Context(MG.Grammar g, ReflectiveMeta rm) {
588             this.grammar = g;
589             this.rm = rm;
590         }
591         public Union build() {
592             Union ret = null;
593             for(MG.NonTerminal nt : grammar.nonterminals) {
594                 Union u = get(nt.name);
595                 if ("s".equals(nt.name))
596                     ret = u;
597             }
598             return ret;
599         }
600         public Context(Tree t, ReflectiveMeta rm) {
601             this.rm = rm;
602             Tree.TreeFunctor<Object,Object> red = (Tree.TreeFunctor<Object,Object>)t.head();
603             this.grammar = (MG.Grammar)red.invoke(t.children());
604         }
605         public Union peek(String name) { return map.get(name); }
606         public void  put(String name, Union u) { map.put(name, u); }
607         public Union get(String name) {
608             Union ret = map.get(name);
609             if (ret != null) return ret;
610             ret = new Union(name);
611             map.put(name, ret);
612             MG.NonTerminal nt = grammar.get(name);
613             if (nt==null) {
614                 System.err.println("*** warning could not find " + name);
615             } else {
616                 String old = cnt;
617                 cnt = name;
618                 nt.build(this, ret);
619                 cnt = old;
620             }
621             return ret;
622         }
623
624     }
625 }