1 package edu.berkeley.sbp.misc;
2 import edu.berkeley.sbp.util.*;
3 import edu.berkeley.sbp.*;
4 import edu.berkeley.sbp.chr.*;
6 import java.lang.annotation.*;
7 import java.lang.reflect.*;
12 public static boolean harsh = false;
14 public static void main(String[] s) throws Exception {
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);
21 Union meta = mgf.get("s").build(bc);
22 Tree t = new CharParser(meta).parse(new FileInputStream(s[1])).expand1();
24 Union u = Demo.make(t, "s");
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());
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());
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) {
40 public Sequence resolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
41 return Sequence.rewritingSequence(tag, els, labels, drops);
45 public static class ReflectiveMeta extends MetaGrammar.Meta {
46 private final Class _cl;
47 private final Class[] _inner;
48 public ReflectiveMeta() {
57 MG.NonTerminalReference.class,
58 MG.StringLiteral.class,
63 public ReflectiveMeta(Class c, Class[] inner) {
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;
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;
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;
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;
91 public Object repeatTag() {
92 return new Tree.ArrayBuildingTreeFunctor<Object>();
94 public Sequence tryResolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
95 Production p = new Production(tag, nonTerminalName, els, labels, drops);
96 for(Method m : _cl.getMethods())
97 if (new TargetMethod(m).isCompatible(p))
98 return new TargetMethod(m).makeSequence(p);
100 for(Constructor con : c.getConstructors())
101 if (new TargetConstructor(con).isCompatible(p))
102 return new TargetConstructor(con).makeSequence(p);
103 for(Class c : _inner)
104 if (new TargetClass(c).isCompatible(p))
105 return new TargetClass(c).makeSequence(p);
108 public Sequence resolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
109 Sequence ret = tryResolveTag(tag, nonTerminalName, els, labels, drops);
110 if (ret != null) return ret;
111 String message = "could not find a Java method/class/ctor matching tag \""+tag+
112 "\", nonterminal \""+nonTerminalName+"\" with " + els.length + " arguments";
114 throw new RuntimeException(message);
116 System.err.println(message);
117 return Sequence.rewritingSequence(tag, els, labels, drops);
124 * Constructors, classes, and methods with this attribute will
125 * match every production of the nonterminal called "value()"
126 * that is arg-compatible. If value() is undefined, then the
127 * class/constructor/method name is used.
129 @Retention(RetentionPolicy.RUNTIME) public static @interface nonterminal { String value() default ""; }
131 @Retention(RetentionPolicy.RUNTIME) public static @interface raw { }
134 * Constructors, classes, and methods with this attribute will
135 * match every tree tagged with "value()" that is arg-compatible.
136 * If value() is undefined, then the class/constructor/method
139 @Retention(RetentionPolicy.RUNTIME) public static @interface tag { String value() default ""; }
142 * If any parameter to a method or field in a class has a named
143 * arg-tag, that parameter/field matches the child of the tree
144 * which either has that label or else is a reference to a
145 * nonterminal with the corresponding name.
147 * The remaining non-named arg-tags match the remaining children
148 * of the tree in sequential order.
150 * If any arg-tagged parameters/fields remain, the match fails.
151 * If there were no arg-tagged parameters-fields, it is as if all
152 * of them were non-named and arg-tagged.
154 * A method/constructor is arg-compatible if all of its arguments
155 * are arg-compatible.
157 * A class is arg-compatible if all of its fields are
158 * arg-compatible, or if one of its constructors is arg-compatible.
161 @Retention(RetentionPolicy.RUNTIME) public static @interface arg { String value() default ""; }
163 public static class Production {
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) {
172 this.elements = elements;
173 this.nonTerminal = nonTerminal;
174 this.labels = labels;
176 for(int i=0; i<drops.length; i++)
182 public static abstract class Target {
183 public abstract String getName();
184 public abstract tag getTag();
185 public abstract nonterminal getNonTerminal();
186 public abstract int[] buildSequence(Production p);
187 public boolean isCompatible(Production p) {
190 (t.value().equals(p.tag) ||
191 (t.value().equals("") && getName().equals(p.tag))))
192 return buildSequence(p)!=null;
194 nonterminal n = getNonTerminal();
196 (n.value().equals(p.nonTerminal) ||
197 (n.value().equals("") && getName().equals(p.nonTerminal))))
198 return buildSequence(p)!=null;
202 public int[] buildSequence(Production p, String[] names, arg[] argtags) {
204 for(int i=0; i<argtags.length; i++)
205 if (argtags[i] != null)
208 // FIXME: can be smarter here
209 if (names.length==p.count) {
210 int[] ret = new int[p.count];
211 for(int i=0; i<p.count; i++) ret[i] = i;
213 } else if (argTagged==p.count) {
214 int[] ret = new int[argtags.length];
216 for(int i=0; i<argtags.length; i++)
217 ret[i] = argtags[i]==null ? -1 : (j++);
223 public Sequence makeSequence(Production p) {
224 return Sequence.rewritingSequence(new TargetReducer(p, buildSequence(p), "reducer-"+this), p.elements, p.labels, p.drops);
226 public abstract Object plant(Object[] fields, int[] map);
227 public boolean isRaw() { return false; }
228 public Object invokeRaw(Iterable<Tree> t) { return null; }
229 public class TargetReducer implements Functor<Iterable<Tree>,Object> {
230 private Production p;
233 public TargetReducer(Production p, int[] map, String name) {
238 public String toString() { return name; }
239 public Object invoke(Iterable<Tree> t) {
240 if (isRaw()) return invokeRaw(t);
241 ArrayList ret = new ArrayList();
243 if (tc.head() != null && tc.head() instanceof Functor)
244 ret.add(((Functor<Iterable<Tree>,Object>)tc.head()).invoke(tc.children()));
245 else if (tc.numChildren() == 0)
248 System.err.println("FIXME: don't know what to do about " + tc);
252 System.err.println("input tree: " + t);
253 return plant(ret.toArray(new Object[0]), map);
258 public static class TargetClass extends Target {
259 public final Class _class;
260 public TargetClass(Class _class) { this._class = _class; }
261 public String getName() { return _class.getSimpleName(); }
262 public tag getTag() { return (tag)_class.getAnnotation(tag.class); }
263 public nonterminal getNonTerminal() { return (nonterminal)_class.getAnnotation(nonterminal.class); }
264 public String toString() { return _class.getSimpleName(); }
265 public int[] buildSequence(Production p) {
266 Field[] f = _class.getDeclaredFields();
267 String[] names = new String[f.length];
268 arg[] argtags = new arg[f.length];
269 for(int i=0; i<f.length; i++) {
270 names[i] = f[i].getName();
271 argtags[i] = f[i].getAnnotation(arg.class);
273 int[] ret = buildSequence(p, names, argtags);
274 if (ret!=null) return ret;
275 for(Constructor c : _class.getConstructors())
276 if (new TargetConstructor(c).buildSequence(p)!=null)
277 return new TargetConstructor(c).buildSequence(p);
280 public Object plant(Object[] fields, int[] map) {
282 Object ret = _class.newInstance();
283 Field[] f = _class.getFields();
285 for(int i=0; i<f.length; i++)
287 Object tgt = Reflection.lub(fields[map[i]]);
288 if (f[i].getType() == String.class) tgt = stringify(tgt);
290 tgt = coerce(tgt, f[i].getType());
291 System.err.println("setting a " + f[i].getType().getName() + " to " + Reflection.show(tgt));
295 } catch (Exception e) {
297 throw new RuntimeException(e);
302 public static String stringify(Object o) {
303 if (o==null) return "";
304 if (!(o instanceof Object[])) return o.toString();
305 Object[] arr = (Object[])o;
306 StringBuffer ret = new StringBuffer();
307 for(int i=0; i<arr.length; i++)
309 return ret.toString();
312 public static class TargetConstructor extends Target {
313 public final Constructor _ctor;
314 public TargetConstructor(Constructor _ctor) { this._ctor = _ctor; }
315 public String getName() { return _ctor.getName(); }
316 public tag getTag() { return (tag)_ctor.getAnnotation(tag.class); }
317 public nonterminal getNonTerminal() { return (nonterminal)_ctor.getAnnotation(nonterminal.class); }
318 public String toString() { return _ctor.getName(); }
319 public int[] buildSequence(Production p) {
320 Annotation[][] annotations = _ctor.getParameterAnnotations();
321 int len = annotations.length;
323 String name = _ctor.getDeclaringClass().getName();
325 if (name.indexOf('$') > name.lastIndexOf('.')) {
330 String[] names = new String[len];
331 arg[] argtags = new arg[len];
332 for(int i=0; i<names.length; i++)
333 for(Annotation a : annotations[i+ofs])
334 if (a instanceof arg)
335 argtags[i+ofs] = (arg)a;
336 return buildSequence(p, names, argtags);
338 public Object plant(Object[] fields, int[] map) {
340 Class[] argTypes = _ctor.getParameterTypes();
341 Object[] args = new Object[argTypes.length];
343 for(int i=0; i<args.length; i++)
345 Object tgt = Reflection.lub(fields[map[i]]);
346 if (argTypes[i] == String.class) tgt = stringify(tgt);
348 tgt = coerce(tgt, argTypes[i]);
349 System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
352 return _ctor.newInstance(args);
353 } catch (Exception e) {
354 throw new RuntimeException(e);
358 public static class TargetMethod extends Target {
359 public final Method _method;
360 public TargetMethod(Method _method) { this._method = _method; }
361 public String getName() { return _method.getName(); }
362 public String toString() { return _method.getName(); }
363 public tag getTag() { return (tag)_method.getAnnotation(tag.class); }
364 public nonterminal getNonTerminal() { return (nonterminal)_method.getAnnotation(nonterminal.class); }
365 public int[] buildSequence(Production p) {
366 Annotation[][] annotations = _method.getParameterAnnotations();
367 String[] names = new String[annotations.length];
368 arg[] argtags = new arg[annotations.length];
369 for(int i=0; i<names.length; i++)
370 for(Annotation a : annotations[i])
371 if (a instanceof arg)
373 int[] ret = buildSequence(p, names, argtags);
376 public boolean isRaw() { return _method.isAnnotationPresent(raw.class); }
377 public Object invokeRaw(Iterable<Tree> t) {
379 return _method.invoke(null, new Object[] { t });
380 } catch (Exception e) {
381 throw new RuntimeException(e);
384 public Object plant(Object[] fields, int[] map) {
386 Class[] argTypes = _method.getParameterTypes();
387 Object[] args = new Object[argTypes.length];
389 for(int i=0; i<args.length; i++)
391 Object tgt = Reflection.lub(fields[map[i]]);
392 if (argTypes[i] == String.class) tgt = stringify(tgt);
394 tgt = coerce(tgt, argTypes[i]);
395 System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
398 System.err.println("invoking " + _method + " with " + Reflection.show(args));
399 return _method.invoke(null, args);
400 } catch (Exception e) {
401 throw new RuntimeException(e);
406 public static Object coerce(Object o, Class c) {
407 if (o==null) return null;
408 if (c.isInstance(o)) return o;
409 if (c == char.class) {
410 return o.toString().charAt(0);
413 if (o.getClass().isArray() &&
414 o.getClass().getComponentType().isArray() &&
415 o.getClass().getComponentType().getComponentType() == String.class &&
417 c.getComponentType() == String.class) {
418 String[] ret = new String[((Object[])o).length];
419 for(int i=0; i<ret.length; i++) {
420 StringBuffer sb = new StringBuffer();
421 for(Object ob : (Object[])(((Object[])o)[i]))
423 ret[i] = sb.toString();
428 if (c.isArray() && (c.getComponentType().isInstance(o))) {
429 Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
434 if (o.getClass().isArray() && c.isArray()) {
436 for(int i=0; i<((Object[])o).length; i++) {
437 Object ob = (((Object[])o)[i]);
439 System.err.println("no hit with " + c.getComponentType().getName() + " on " + Reflection.show(((Object[])o)[i]));
444 System.err.println("hit with " + c.getComponentType().getName());
445 return Array.newInstance(c.getComponentType(), ((Object[])o).length);
451 public static Union cached = null;
452 public static Union make() {
453 if (cached != null) return cached;
455 ReflectiveMeta m = new ReflectiveMeta();
456 Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream("tests/meta.g")).expand1();
457 MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res);
458 MetaGrammar.BuildContext bc = new MetaGrammar.BuildContext(mgf);
459 Union meta = mgf.get("s").build(bc);
460 Tree t = new CharParser(meta).parse(new FileInputStream("tests/meta.g")).expand1();
461 return cached = make(t, "s");
462 } catch (Exception e) {
463 throw new RuntimeException(e);
466 public static Union make(Tree t, String s) { return make(t, s, new ReflectiveMeta()); }
467 public static Union make(Tree t, String s, ReflectiveMeta rm) {
468 Functor<Iterable<Tree>,Object> red = (Functor<Iterable<Tree>,Object>)t.head();
469 MG.Grammar g = (MG.Grammar)red.invoke(t.children());
470 Context cx = new Context(g,rm);
472 for(MG.NonTerminal nt : g.nonterminals) {
473 System.out.println(nt.name);
474 Union el = (Union)cx.get(nt.name);
475 StringBuffer st = new StringBuffer();
477 System.err.println(st);
478 if (nt.name.equals(s)) u = el;
483 public static class MG {
484 public static @tag("grammar") class Grammar {
485 public NonTerminal get(String s) {
486 for(NonTerminal nt : nonterminals)
487 if (nt.name.equals(s))
491 public @arg("NonTerminal") NonTerminal[] nonterminals;
492 public String toString() {
494 for(NonTerminal nt : nonterminals) ret += nt + ", ";
498 public abstract static class Un extends El {
499 public Seq[][] sequences;
500 public void build(Context cx, Union u) {
501 HashSet<Sequence> bad2 = new HashSet<Sequence>();
502 for(int i=0; i<sequences.length; i++) {
503 Seq[] group = sequences[i];
504 Union u2 = new Union();
505 if (sequences.length==1) u2 = u;
506 for(int j=0; j<group.length; j++) {
507 group[j].build(cx, u2, false);
509 if (sequences.length==1) break;
510 Sequence seq = Sequence.singleton(u2);
511 for(Sequence s : bad2) {
516 bad2.add(Sequence.singleton(u2));
520 public static class NonTerminal extends Un {
521 public String name = null;
522 public @nonterminal("NonTerminal") NonTerminal(@arg("Word") String name,
523 @arg("RHS") Seq[][] sequences) {
525 this.sequences = sequences;
527 public Element build(Context cx) { return cx.get(name); }
530 public static class AnonUn extends Un {
531 public @tag("(") AnonUn(Seq[][] sequences) {
532 this.sequences = sequences;
534 public Element build(Context cx) {
535 Union ret = new Union();
541 //public static @tag void range(char c) { }
542 public static class Range {
543 public @tag("range") Range(char only) { first = only; last = only; }
544 public @tag("-") Range(char first, char last) { this.first = first; this.last = last; }
548 public static abstract class El {
549 public String getLabel() { return null; }
550 public String getOwnerTag() { return null; }
551 public boolean drop() { return false; }
552 public abstract Element build(Context cx);
554 public static class Drop extends El {
556 public Drop(El e) { this.e = e; }
557 public String getLabel() { return null; }
558 public boolean drop() { return true; }
559 public String getOwnerTag() { return e.getOwnerTag(); }
560 public Element build(Context cx) { return e.build(cx); }
562 public static class Label extends El {
565 public Label(String label, El e) { this.e = e; this.label = label; }
566 public String getLabel() { return label; }
567 public String getOwnerTag() { return e.getOwnerTag(); }
568 public Element build(Context cx) { return e.build(cx); }
570 public static /*abstract*/ class Seq {
571 HashSet<Seq> and = new HashSet<Seq>();
572 HashSet<Seq> not = new HashSet<Seq>();
577 public Seq(El e) { this(new El[] { e }); }
578 public Seq(El[] elements) { this.elements = elements; }
579 public Seq tag(String tag) { this.tag = tag; return this; }
580 public Seq follow(El follow) { this.follow = follow; return this; }
582 Seq ret = new Seq(elements);
589 public Seq and(Seq s) { and.add(s); s.lame = true; return this; }
590 public Seq andnot(Seq s) { not.add(s); s.lame = true; return this; }
591 public Seq separate(El sep) {
592 El[] elements = new El[this.elements.length * 2 - 1];
593 for(int i=0; i<this.elements.length; i++) {
594 elements[i*2] = this.elements[i];
595 if (i<this.elements.length-1)
596 elements[i*2+1] = new Drop(sep);
598 this.elements = elements;
601 public Sequence build(Context cx, Union u, boolean lame) {
602 Sequence ret = build0(cx, lame || this.lame);
603 for(Seq s : and) { Sequence dork = s.build(cx, u, true); ret = ret.and(dork); }
604 for(Seq s : not) { Sequence dork = s.build(cx, u, true); ret = ret.not(dork); }
609 public Sequence build0(Context cx, boolean lame) {
610 boolean unwrap = false;
611 boolean dropAll = lame;
612 if (tag!=null && tag.equals("[]")) unwrap = true;
613 if (tag!=null && "()".equals(tag)) dropAll = true;
614 Object[] labels = new Object[elements.length];
615 boolean[] drops = new boolean[elements.length];
616 Element[] els = new Element[elements.length];
617 for(int i=0; i<elements.length; i++) {
618 labels[i] = elements[i].getLabel();
619 drops[i] = elements[i].drop();
620 els[i] = elements[i].build(cx);
621 if (elements[i].getOwnerTag() != null)
622 tag = elements[i].getOwnerTag();
625 if (dropAll) ret = Sequence.drop(els, false);
626 else if (unwrap) ret = Sequence.unwrap(els, cx.rm.repeatTag(), drops);
627 else if (tag!=null) {
628 ret = cx.rm.resolveTag(tag, cx.cnt, els, labels, drops);
631 for(int i=0; i<els.length; i++)
633 if (idx==-1) idx = i;
634 else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els,false));
635 if (idx != -1) ret = Sequence.singleton(els, idx);
636 else ret = Sequence.drop(els, false);
638 if (this.follow != null)
639 ret.follow = MetaGrammar.infer(this.follow.build(cx));
640 ret.lame = this.lame;
644 public static @tag("&") Seq and(Seq s, El[] elements) { return s.and(seq(elements)); }
645 public static @tag("&~") Seq andnot(Seq s, El[] elements) { return s.andnot(seq(elements)); }
646 public static @tag("->") Seq arrow(Seq s, El e) { return s.follow(e); }
647 public static @tag("::") Seq tag(String tagname, Seq s) { return s.tag(tagname); }
648 public static @tag("/") Seq slash(Seq s, El e) { return s.separate(e); }
650 public static @tag("ps") Seq seq(El[] elements) { return new Seq(elements); }
651 public static @tag Seq psx(Seq s) { return s; }
652 public static @tag(":") El colon(String s, El e) { return new Label(s, e); }
653 public static @tag(")") void close(String foo) { throw new Error("not supported"); }
654 public static @tag("()") El epsilon() { return new Constant(Union.epsilon); }
656 public static @tag("nonTerminal") class NonTerminalReference extends El {
657 public @arg String nonTerminal;
658 public Element build(Context cx) {
659 return cx.get(nonTerminal);
663 public static class StringLiteral extends Constant {
664 public @tag("literal") StringLiteral(String string) { super(CharRange.string(string)); }
665 public boolean drop() { return true; }
668 public static class CharClass extends El {
670 public @tag("[") CharClass(Range[] ranges) { this.ranges = ranges; }
671 public Element build(Context cx) {
672 edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
673 for(Range r : ranges)
674 set.add(r.first, r.last);
675 return CharRange.set(set);
679 public static @tag("{") class XTree extends El {
680 public @arg Seq body;
681 public Element build(Context cx) {
686 public static class Rep extends El {
688 public boolean zero, many, max;
689 public Rep(El e, El sep, boolean zero, boolean many, boolean max) {
690 this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
691 public Element build(Context cx) {
693 ? Sequence.repeat(e.build(cx), zero, many, sep==null ? null : sep.build(cx), cx.rm.repeatTag())
695 ? Sequence.repeatMaximal(MetaGrammar.infer(e.build(cx)), zero, many, cx.rm.repeatTag())
696 : Sequence.repeatMaximal(e.build(cx), zero, many, MetaGrammar.infer(sep.build(cx)), cx.rm.repeatTag());
699 public static class Constant extends El {
701 public Constant(Element constant) { this.constant = constant; }
702 public Element build(Context cx) { return constant; }
704 public abstract static class PostProcess extends El {
706 public PostProcess(El e) { this.e = e; }
707 public Element build(Context cx) { return postProcess(e.build(cx)); }
708 public abstract Element postProcess(Element e);
711 // FIXME: it would be nice if we could hoist this into "Rep"
712 public static @tag("++") El plusmax(final El e) { return new Rep(e, null, false, true, true); }
713 public static @tag("+") El plus(final El e) { return new Rep(e, null, false, true, false); }
714 public static @tag("++/") El plusmaxfollow(final El e, final El sep) { return new Rep(e, sep, false, true, true); }
715 public static @tag("+/") El plusfollow(final El e, final El sep) { return new Rep(e, sep, false, true, false); }
716 public static @tag("**") El starmax(final El e) { return new Rep(e, null, true, true, true); }
717 public static @tag("*") El star(final El e) { return new Rep(e, null, true, true, false); }
718 public static @tag("**/") El starmaxfollow(final El e, final El sep) { return new Rep(e, sep, true, true, true); }
719 public static @tag("*/") El starfollow(final El e, final El sep) { return new Rep(e, sep, true, true, false); }
720 public static @tag("?") El question(final El e) { return new Rep(e, null, true, true, false); }
722 public static @tag("!") El bang(final El e) { return new Drop(e); }
724 public static @tag("^") El caret(final String s) {
725 return new Drop(new Constant(CharRange.string(s)) {
726 public String getOwnerTag() { return s; }
730 public static @tag("~") El tilde(final El e) {
731 return new PostProcess(e) {
732 public Element postProcess(Element e) {
733 return MetaGrammar.infer((Topology<Character>)Atom.toAtom(e).complement());
736 public static @tag("^^") void doublecaret(final El e) { throw new Error("not implemented"); }
738 //public static @tag("(") El subexpression(Seq[][] rhs) { return new NonTerminal(rhs); }
740 public static @nonterminal("Word") String word(String s) { return s; }
741 public static @nonterminal("Quoted") String quoted(String s) { return s; }
742 public static @nonterminal("escaped") String c(char c) { return c+""; }
743 public static @tag("\"\"") String emptystring() { return ""; }
744 public static @tag("\n") String retur() { return "\n"; }
745 public static @tag("\r") String lf() { return "\r"; }
748 public static class Context {
749 HashMap<String,Union> map = new HashMap<String,Union>();
750 private MG.Grammar grammar;
751 public String cnt = null;
752 private ReflectiveMeta rm;
753 public Context(MG.Grammar g, ReflectiveMeta rm) {
757 public Union build() {
759 for(MG.NonTerminal nt : grammar.nonterminals) {
760 Union u = get(nt.name);
761 if ("s".equals(nt.name))
766 public Context(Tree t, ReflectiveMeta rm) {
768 Functor<Iterable<Tree>,Object> red = (Functor<Iterable<Tree>,Object>)t.head();
769 this.grammar = (MG.Grammar)red.invoke(t.children());
771 public Union peek(String name) { return map.get(name); }
772 public void put(String name, Union u) { map.put(name, u); }
773 public Union get(String name) {
774 Union ret = map.get(name);
775 if (ret != null) return ret;
776 ret = new Union(name);
778 MG.NonTerminal nt = grammar.get(name);
780 System.err.println("*** warning could not find " + name);