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