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 Functor<Iterable<Tree>,Object>() {
93 public String toString() { return ""; }
94 public Object invoke(Iterable<Tree> t) {
95 ArrayList ret = new ArrayList();
97 if (tc.head() != null && tc.head() instanceof Functor)
98 ret.add(((Functor<Iterable<Tree>,Object>)tc.head()).invoke(tc.children()));
99 else if (tc.numChildren() == 0)
102 System.err.println("FIXME: don't know what to do about " + tc);
106 return ret.toArray(new Object[0]);
110 public Sequence tryResolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
111 Production p = new Production(tag, nonTerminalName, els, labels, drops);
112 for(Method m : _cl.getMethods())
113 if (new TargetMethod(m).isCompatible(p))
114 return new TargetMethod(m).makeSequence(p);
115 for(Class c : _inner)
116 for(Constructor con : c.getConstructors())
117 if (new TargetConstructor(con).isCompatible(p))
118 return new TargetConstructor(con).makeSequence(p);
119 for(Class c : _inner)
120 if (new TargetClass(c).isCompatible(p))
121 return new TargetClass(c).makeSequence(p);
124 public Sequence resolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
125 Sequence ret = tryResolveTag(tag, nonTerminalName, els, labels, drops);
126 if (ret != null) return ret;
127 String message = "could not find a Java method/class/ctor matching tag \""+tag+
128 "\", nonterminal \""+nonTerminalName+"\" with " + els.length + " arguments";
130 throw new RuntimeException(message);
132 System.err.println(message);
133 return Sequence.rewritingSequence(tag, els, labels, drops);
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.
145 @Retention(RetentionPolicy.RUNTIME) public static @interface nonterminal { String value() default ""; }
147 @Retention(RetentionPolicy.RUNTIME) public static @interface raw { }
150 * Constructors, classes, and methods with this attribute will
151 * match every tree tagged with "value()" that is arg-compatible.
152 * If value() is undefined, then the class/constructor/method
155 @Retention(RetentionPolicy.RUNTIME) public static @interface tag { String value() default ""; }
158 * If any parameter to a method or field in a class has a named
159 * arg-tag, that parameter/field matches the child of the tree
160 * which either has that label or else is a reference to a
161 * nonterminal with the corresponding name.
163 * The remaining non-named arg-tags match the remaining children
164 * of the tree in sequential order.
166 * If any arg-tagged parameters/fields remain, the match fails.
167 * If there were no arg-tagged parameters-fields, it is as if all
168 * of them were non-named and arg-tagged.
170 * A method/constructor is arg-compatible if all of its arguments
171 * are arg-compatible.
173 * A class is arg-compatible if all of its fields are
174 * arg-compatible, or if one of its constructors is arg-compatible.
177 @Retention(RetentionPolicy.RUNTIME) public static @interface arg { String value() default ""; }
179 public static class Production {
181 public String nonTerminal;
182 public Object[] labels;
183 public boolean[] drops;
184 public Element[] elements;
185 public int count = 0;
186 public Production(String tag, String nonTerminal, Element[] elements, Object[] labels, boolean[] drops) {
188 this.elements = elements;
189 this.nonTerminal = nonTerminal;
190 this.labels = labels;
192 for(int i=0; i<drops.length; i++)
198 public static abstract class Target {
199 public abstract String getName();
200 public abstract tag getTag();
201 public abstract nonterminal getNonTerminal();
202 public abstract int[] buildSequence(Production p);
203 public boolean isCompatible(Production p) {
206 (t.value().equals(p.tag) ||
207 (t.value().equals("") && getName().equals(p.tag))))
208 return buildSequence(p)!=null;
210 nonterminal n = getNonTerminal();
212 (n.value().equals(p.nonTerminal) ||
213 (n.value().equals("") && getName().equals(p.nonTerminal))))
214 return buildSequence(p)!=null;
218 public int[] buildSequence(Production p, String[] names, arg[] argtags) {
220 for(int i=0; i<argtags.length; i++)
221 if (argtags[i] != null)
224 // FIXME: can be smarter here
225 if (names.length==p.count) {
226 int[] ret = new int[p.count];
227 for(int i=0; i<p.count; i++) ret[i] = i;
229 } else if (argTagged==p.count) {
230 int[] ret = new int[argtags.length];
232 for(int i=0; i<argtags.length; i++)
233 ret[i] = argtags[i]==null ? -1 : (j++);
239 public Sequence makeSequence(Production p) {
240 return Sequence.rewritingSequence(new TargetReducer(p, buildSequence(p), "reducer-"+this), p.elements, p.labels, p.drops);
242 public abstract Object plant(Object[] fields, int[] map);
243 public boolean isRaw() { return false; }
244 public Object invokeRaw(Iterable<Tree> t) { return null; }
245 public class TargetReducer implements Functor<Iterable<Tree>,Object> {
246 private Production p;
249 public TargetReducer(Production p, int[] map, String name) {
254 public String toString() { return name; }
255 public Object invoke(Iterable<Tree> t) {
256 if (isRaw()) return invokeRaw(t);
257 ArrayList ret = new ArrayList();
259 if (tc.head() != null && tc.head() instanceof Functor)
260 ret.add(((Functor<Iterable<Tree>,Object>)tc.head()).invoke(tc.children()));
261 else if (tc.numChildren() == 0)
264 System.err.println("FIXME: don't know what to do about " + tc);
268 System.err.println("input tree: " + t);
269 return plant(ret.toArray(new Object[0]), map);
274 public static class TargetClass extends Target {
275 public final Class _class;
276 public TargetClass(Class _class) { this._class = _class; }
277 public String getName() { return _class.getSimpleName(); }
278 public tag getTag() { return (tag)_class.getAnnotation(tag.class); }
279 public nonterminal getNonTerminal() { return (nonterminal)_class.getAnnotation(nonterminal.class); }
280 public String toString() { return _class.getSimpleName(); }
281 public int[] buildSequence(Production p) {
282 Field[] f = _class.getDeclaredFields();
283 String[] names = new String[f.length];
284 arg[] argtags = new arg[f.length];
285 for(int i=0; i<f.length; i++) {
286 names[i] = f[i].getName();
287 argtags[i] = f[i].getAnnotation(arg.class);
289 int[] ret = buildSequence(p, names, argtags);
290 if (ret!=null) return ret;
291 for(Constructor c : _class.getConstructors())
292 if (new TargetConstructor(c).buildSequence(p)!=null)
293 return new TargetConstructor(c).buildSequence(p);
296 public Object plant(Object[] fields, int[] map) {
298 Object ret = _class.newInstance();
299 Field[] f = _class.getFields();
301 for(int i=0; i<f.length; i++)
303 Object tgt = Reflection.lub(fields[map[i]]);
304 if (f[i].getType() == String.class) tgt = stringify(tgt);
306 tgt = coerce(tgt, f[i].getType());
307 System.err.println("setting a " + f[i].getType().getName() + " to " + Reflection.show(tgt));
311 } catch (Exception e) {
313 throw new RuntimeException(e);
318 public static String stringify(Object o) {
319 if (o==null) return "";
320 if (!(o instanceof Object[])) return o.toString();
321 Object[] arr = (Object[])o;
322 StringBuffer ret = new StringBuffer();
323 for(int i=0; i<arr.length; i++)
325 return ret.toString();
328 public static class TargetConstructor extends Target {
329 public final Constructor _ctor;
330 public TargetConstructor(Constructor _ctor) { this._ctor = _ctor; }
331 public String getName() { return _ctor.getName(); }
332 public tag getTag() { return (tag)_ctor.getAnnotation(tag.class); }
333 public nonterminal getNonTerminal() { return (nonterminal)_ctor.getAnnotation(nonterminal.class); }
334 public String toString() { return _ctor.getName(); }
335 public int[] buildSequence(Production p) {
336 Annotation[][] annotations = _ctor.getParameterAnnotations();
337 int len = annotations.length;
339 String name = _ctor.getDeclaringClass().getName();
341 if (name.indexOf('$') > name.lastIndexOf('.')) {
346 String[] names = new String[len];
347 arg[] argtags = new arg[len];
348 for(int i=0; i<names.length; i++)
349 for(Annotation a : annotations[i+ofs])
350 if (a instanceof arg)
351 argtags[i+ofs] = (arg)a;
352 return buildSequence(p, names, argtags);
354 public Object plant(Object[] fields, int[] map) {
356 Class[] argTypes = _ctor.getParameterTypes();
357 Object[] args = new Object[argTypes.length];
359 for(int i=0; i<args.length; i++)
361 Object tgt = Reflection.lub(fields[map[i]]);
362 if (argTypes[i] == String.class) tgt = stringify(tgt);
364 tgt = coerce(tgt, argTypes[i]);
365 System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
368 return _ctor.newInstance(args);
369 } catch (Exception e) {
370 throw new RuntimeException(e);
374 public static class TargetMethod extends Target {
375 public final Method _method;
376 public TargetMethod(Method _method) { this._method = _method; }
377 public String getName() { return _method.getName(); }
378 public String toString() { return _method.getName(); }
379 public tag getTag() { return (tag)_method.getAnnotation(tag.class); }
380 public nonterminal getNonTerminal() { return (nonterminal)_method.getAnnotation(nonterminal.class); }
381 public int[] buildSequence(Production p) {
382 Annotation[][] annotations = _method.getParameterAnnotations();
383 String[] names = new String[annotations.length];
384 arg[] argtags = new arg[annotations.length];
385 for(int i=0; i<names.length; i++)
386 for(Annotation a : annotations[i])
387 if (a instanceof arg)
389 int[] ret = buildSequence(p, names, argtags);
392 public boolean isRaw() { return _method.isAnnotationPresent(raw.class); }
393 public Object invokeRaw(Iterable<Tree> t) {
395 return _method.invoke(null, new Object[] { t });
396 } catch (Exception e) {
397 throw new RuntimeException(e);
400 public Object plant(Object[] fields, int[] map) {
402 Class[] argTypes = _method.getParameterTypes();
403 Object[] args = new Object[argTypes.length];
405 for(int i=0; i<args.length; i++)
407 Object tgt = Reflection.lub(fields[map[i]]);
408 if (argTypes[i] == String.class) tgt = stringify(tgt);
410 tgt = coerce(tgt, argTypes[i]);
411 System.err.println("setting a " + argTypes[i].getName() + " to " + Reflection.show(tgt));
414 System.err.println("invoking " + _method + " with " + Reflection.show(args));
415 return _method.invoke(null, args);
416 } catch (Exception e) {
417 throw new RuntimeException(e);
422 public static Object coerce(Object o, Class c) {
423 if (o==null) return null;
424 if (c.isInstance(o)) return o;
425 if (c == char.class) {
426 return o.toString().charAt(0);
429 if (o.getClass().isArray() &&
430 o.getClass().getComponentType().isArray() &&
431 o.getClass().getComponentType().getComponentType() == String.class &&
433 c.getComponentType() == String.class) {
434 String[] ret = new String[((Object[])o).length];
435 for(int i=0; i<ret.length; i++) {
436 StringBuffer sb = new StringBuffer();
437 for(Object ob : (Object[])(((Object[])o)[i]))
439 ret[i] = sb.toString();
444 if (c.isArray() && (c.getComponentType().isInstance(o))) {
445 Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
450 if (o.getClass().isArray() && c.isArray()) {
452 for(int i=0; i<((Object[])o).length; i++) {
453 Object ob = (((Object[])o)[i]);
455 System.err.println("no hit with " + c.getComponentType().getName() + " on " + Reflection.show(((Object[])o)[i]));
460 System.err.println("hit with " + c.getComponentType().getName());
461 return Array.newInstance(c.getComponentType(), ((Object[])o).length);
467 public static Union cached = null;
468 public static Union make() {
469 if (cached != null) return cached;
471 ReflectiveMeta m = new ReflectiveMeta();
472 Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream("tests/meta.g")).expand1();
473 MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res);
474 MetaGrammar.BuildContext bc = new MetaGrammar.BuildContext(mgf);
475 Union meta = mgf.get("s").build(bc);
476 Tree t = new CharParser(meta).parse(new FileInputStream("tests/meta.g")).expand1();
477 return cached = make(t, "s");
478 } catch (Exception e) {
479 throw new RuntimeException(e);
482 public static Union make(Tree t, String s) { return make(t, s, new ReflectiveMeta()); }
483 public static Union make(Tree t, String s, ReflectiveMeta rm) {
484 Functor<Iterable<Tree>,Object> red = (Functor<Iterable<Tree>,Object>)t.head();
485 MG.Grammar g = (MG.Grammar)red.invoke(t.children());
486 Context cx = new Context(g,rm);
488 for(MG.NonTerminal nt : g.nonterminals) {
489 System.out.println(nt.name);
490 Union el = (Union)cx.get(nt.name);
491 StringBuffer st = new StringBuffer();
493 System.err.println(st);
494 if (nt.name.equals(s)) u = el;
499 public static class MG {
500 public static @tag("grammar") class Grammar {
501 public NonTerminal get(String s) {
502 for(NonTerminal nt : nonterminals)
503 if (nt.name.equals(s))
507 public @arg("NonTerminal") NonTerminal[] nonterminals;
508 public String toString() {
510 for(NonTerminal nt : nonterminals) ret += nt + ", ";
514 public abstract static class Un extends El {
515 public Seq[][] sequences;
516 public void build(Context cx, Union u) {
517 HashSet<Sequence> bad2 = new HashSet<Sequence>();
518 for(int i=0; i<sequences.length; i++) {
519 Seq[] group = sequences[i];
520 Union u2 = new Union();
521 if (sequences.length==1) u2 = u;
522 for(int j=0; j<group.length; j++) {
523 group[j].build(cx, u2, false);
525 if (sequences.length==1) break;
526 Sequence seq = Sequence.singleton(u2);
527 for(Sequence s : bad2) {
532 bad2.add(Sequence.singleton(u2));
536 public static class NonTerminal extends Un {
537 public String name = null;
538 public @nonterminal("NonTerminal") NonTerminal(@arg("Word") String name,
539 @arg("RHS") Seq[][] sequences) {
541 this.sequences = sequences;
543 public Element build(Context cx) { return cx.get(name); }
546 public static class AnonUn extends Un {
547 public @tag("(") AnonUn(Seq[][] sequences) {
548 this.sequences = sequences;
550 public Element build(Context cx) {
551 Union ret = new Union();
557 //public static @tag void range(char c) { }
558 public static class Range {
559 public @tag("range") Range(char only) { first = only; last = only; }
560 public @tag("-") Range(char first, char last) { this.first = first; this.last = last; }
564 public static abstract class El {
565 public String getLabel() { return null; }
566 public String getOwnerTag() { return null; }
567 public boolean drop() { return false; }
568 public abstract Element build(Context cx);
570 public static class Drop extends El {
572 public Drop(El e) { this.e = e; }
573 public String getLabel() { return null; }
574 public boolean drop() { return true; }
575 public String getOwnerTag() { return e.getOwnerTag(); }
576 public Element build(Context cx) { return e.build(cx); }
578 public static class Label extends El {
581 public Label(String label, El e) { this.e = e; this.label = label; }
582 public String getLabel() { return label; }
583 public String getOwnerTag() { return e.getOwnerTag(); }
584 public Element build(Context cx) { return e.build(cx); }
586 public static /*abstract*/ class Seq {
587 HashSet<Seq> and = new HashSet<Seq>();
588 HashSet<Seq> not = new HashSet<Seq>();
593 public Seq(El e) { this(new El[] { e }); }
594 public Seq(El[] elements) { this.elements = elements; }
595 public Seq tag(String tag) { this.tag = tag; return this; }
596 public Seq follow(El follow) { this.follow = follow; return this; }
598 Seq ret = new Seq(elements);
605 public Seq and(Seq s) { and.add(s); s.lame = true; return this; }
606 public Seq andnot(Seq s) { not.add(s); s.lame = true; return this; }
607 public Seq separate(El sep) {
608 El[] elements = new El[this.elements.length * 2 - 1];
609 for(int i=0; i<this.elements.length; i++) {
610 elements[i*2] = this.elements[i];
611 if (i<this.elements.length-1)
612 elements[i*2+1] = new Drop(sep);
614 this.elements = elements;
617 public Sequence build(Context cx, Union u, boolean lame) {
618 Sequence ret = build0(cx, lame || this.lame);
619 for(Seq s : and) { Sequence dork = s.build(cx, u, true); ret = ret.and(dork); }
620 for(Seq s : not) { Sequence dork = s.build(cx, u, true); ret = ret.not(dork); }
625 public Sequence build0(Context cx, boolean lame) {
626 boolean unwrap = false;
627 boolean dropAll = lame;
628 if (tag!=null && tag.equals("[]")) unwrap = true;
629 if (tag!=null && "()".equals(tag)) dropAll = true;
630 Object[] labels = new Object[elements.length];
631 boolean[] drops = new boolean[elements.length];
632 Element[] els = new Element[elements.length];
633 for(int i=0; i<elements.length; i++) {
634 labels[i] = elements[i].getLabel();
635 drops[i] = elements[i].drop();
636 els[i] = elements[i].build(cx);
637 if (elements[i].getOwnerTag() != null)
638 tag = elements[i].getOwnerTag();
641 if (dropAll) ret = Sequence.drop(els, false);
642 else if (unwrap) ret = Sequence.unwrap(els, cx.rm.repeatTag(), drops);
643 else if (tag!=null) {
644 ret = cx.rm.resolveTag(tag, cx.cnt, els, labels, drops);
647 for(int i=0; i<els.length; i++)
649 if (idx==-1) idx = i;
650 else throw new Error("multiple non-dropped elements in sequence: " + Sequence.drop(els,false));
651 if (idx != -1) ret = Sequence.singleton(els, idx);
652 else ret = Sequence.drop(els, false);
654 if (this.follow != null)
655 ret.follow = MetaGrammar.infer(this.follow.build(cx));
656 ret.lame = this.lame;
660 public static @tag("&") Seq and(Seq s, El[] elements) { return s.and(seq(elements)); }
661 public static @tag("&~") Seq andnot(Seq s, El[] elements) { return s.andnot(seq(elements)); }
662 public static @tag("->") Seq arrow(Seq s, El e) { return s.follow(e); }
663 public static @tag("::") Seq tag(String tagname, Seq s) { return s.tag(tagname); }
664 public static @tag("/") Seq slash(Seq s, El e) { return s.separate(e); }
666 public static @tag("ps") Seq seq(El[] elements) { return new Seq(elements); }
667 public static @tag Seq psx(Seq s) { return s; }
668 public static @tag(":") El colon(String s, El e) { return new Label(s, e); }
669 public static @tag(")") void close(String foo) { throw new Error("not supported"); }
670 public static @tag("()") El epsilon() { return new Constant(Union.epsilon); }
672 public static @tag("nonTerminal") class NonTerminalReference extends El {
673 public @arg String nonTerminal;
674 public Element build(Context cx) {
675 return cx.get(nonTerminal);
679 public static class StringLiteral extends Constant {
680 public @tag("literal") StringLiteral(String string) { super(CharRange.string(string)); }
681 public boolean drop() { return true; }
684 public static class CharClass extends El {
686 public @tag("[") CharClass(Range[] ranges) { this.ranges = ranges; }
687 public Element build(Context cx) {
688 edu.berkeley.sbp.util.Range.Set set = new edu.berkeley.sbp.util.Range.Set();
689 for(Range r : ranges)
690 set.add(r.first, r.last);
691 return CharRange.set(set);
695 public static @tag("{") class XTree extends El {
696 public @arg Seq body;
697 public Element build(Context cx) {
702 public static class Rep extends El {
704 public boolean zero, many, max;
705 public Rep(El e, El sep, boolean zero, boolean many, boolean max) {
706 this.e = e; this.sep = sep; this.zero = zero; this.many = many; this.max = max;}
707 public Element build(Context cx) {
709 ? Sequence.repeat(e.build(cx), zero, many, sep==null ? null : sep.build(cx), cx.rm.repeatTag())
711 ? Sequence.repeatMaximal(MetaGrammar.infer(e.build(cx)), zero, many, cx.rm.repeatTag())
712 : Sequence.repeatMaximal(e.build(cx), zero, many, MetaGrammar.infer(sep.build(cx)), cx.rm.repeatTag());
715 public static class Constant extends El {
717 public Constant(Element constant) { this.constant = constant; }
718 public Element build(Context cx) { return constant; }
720 public abstract static class PostProcess extends El {
722 public PostProcess(El e) { this.e = e; }
723 public Element build(Context cx) { return postProcess(e.build(cx)); }
724 public abstract Element postProcess(Element e);
727 // FIXME: it would be nice if we could hoist this into "Rep"
728 public static @tag("++") El plusmax(final El e) { return new Rep(e, null, false, true, true); }
729 public static @tag("+") El plus(final El e) { return new Rep(e, null, false, true, false); }
730 public static @tag("++/") El plusmaxfollow(final El e, final El sep) { return new Rep(e, sep, false, true, true); }
731 public static @tag("+/") El plusfollow(final El e, final El sep) { return new Rep(e, sep, false, true, false); }
732 public static @tag("**") El starmax(final El e) { return new Rep(e, null, true, true, true); }
733 public static @tag("*") El star(final El e) { return new Rep(e, null, true, true, false); }
734 public static @tag("**/") El starmaxfollow(final El e, final El sep) { return new Rep(e, sep, true, true, true); }
735 public static @tag("*/") El starfollow(final El e, final El sep) { return new Rep(e, sep, true, true, false); }
736 public static @tag("?") El question(final El e) { return new Rep(e, null, true, true, false); }
738 public static @tag("!") El bang(final El e) { return new Drop(e); }
740 public static @tag("^") El caret(final String s) {
741 return new Drop(new Constant(CharRange.string(s)) {
742 public String getOwnerTag() { return s; }
746 public static @tag("~") El tilde(final El e) {
747 return new PostProcess(e) {
748 public Element postProcess(Element e) {
749 return MetaGrammar.infer((Topology<Character>)Atom.toAtom(e).complement());
752 public static @tag("^^") void doublecaret(final El e) { throw new Error("not implemented"); }
754 //public static @tag("(") El subexpression(Seq[][] rhs) { return new NonTerminal(rhs); }
756 public static @nonterminal("Word") String word(String s) { return s; }
757 public static @nonterminal("Quoted") String quoted(String s) { return s; }
758 public static @nonterminal("escaped") String c(char c) { return c+""; }
759 public static @tag("\"\"") String emptystring() { return ""; }
760 public static @tag("\n") String retur() { return "\n"; }
761 public static @tag("\r") String lf() { return "\r"; }
764 public static class Context {
765 HashMap<String,Union> map = new HashMap<String,Union>();
766 private MG.Grammar grammar;
767 public String cnt = null;
768 private ReflectiveMeta rm;
769 public Context(MG.Grammar g, ReflectiveMeta rm) {
773 public Union build() {
775 for(MG.NonTerminal nt : grammar.nonterminals) {
776 Union u = get(nt.name);
777 if ("s".equals(nt.name))
782 public Context(Tree t, ReflectiveMeta rm) {
784 Functor<Iterable<Tree>,Object> red = (Functor<Iterable<Tree>,Object>)t.head();
785 this.grammar = (MG.Grammar)red.invoke(t.children());
787 public Union peek(String name) { return map.get(name); }
788 public void put(String name, Union u) { map.put(name, u); }
789 public Union get(String name) {
790 Union ret = map.get(name);
791 if (ret != null) return ret;
792 ret = new Union(name);
794 MG.NonTerminal nt = grammar.get(name);
796 System.err.println("*** warning could not find " + name);