From 3ba82cd7dde22a9ef2c9c094a51ea5df6bcf687e Mon Sep 17 00:00:00 2001 From: adam Date: Tue, 4 Jul 2006 22:54:50 -0400 Subject: [PATCH] checkpoint darcs-hash:20060705025450-5007d-1d5b7ceafb385004cf8abafbb9fcff4143e0f2da.gz --- src/edu/berkeley/sbp/bind/bind.java | 56 ++++++++++++++ src/edu/berkeley/sbp/misc/Demo.java | 95 +++++++----------------- src/edu/berkeley/sbp/misc/RegressionTests.java | 1 + src/edu/berkeley/sbp/tib/TibDoc.java | 1 + 4 files changed, 86 insertions(+), 67 deletions(-) create mode 100644 src/edu/berkeley/sbp/bind/bind.java diff --git a/src/edu/berkeley/sbp/bind/bind.java b/src/edu/berkeley/sbp/bind/bind.java new file mode 100644 index 0000000..e384ca4 --- /dev/null +++ b/src/edu/berkeley/sbp/bind/bind.java @@ -0,0 +1,56 @@ +package edu.berkeley.sbp.bind; + +import edu.berkeley.sbp.util.*; +import edu.berkeley.sbp.*; +import edu.berkeley.sbp.chr.*; +import edu.berkeley.sbp.bind.*; +import java.util.*; +import java.lang.annotation.*; +import java.lang.reflect.*; +import java.io.*; + +public class bind { + /** + * Constructors, classes, and methods with this attribute will + * match every production of the nonterminal called "value()" + * that is arg-compatible. If value() is undefined, then the + * class/constructor/method name is used. + */ + /* + @Retention(RetentionPolicy.RUNTIME) public static @interface nonterminal { String value() default ""; } + */ + + @Retention(RetentionPolicy.RUNTIME) public static @interface raw { } + + /** + * Constructors, classes, and methods with this attribute will + * match every tree tagged with "value()" that is arg-compatible. + * If value() is undefined, then the class/constructor/method + * name is used. + */ + + @Retention(RetentionPolicy.RUNTIME) public static @interface as { String value() default ""; } + + + /** + * If any parameter to a method or field in a class has a named + * arg-tag, that parameter/field matches the child of the tree + * which either has that label or else is a reference to a + * nonterminal with the corresponding name. + * + * The remaining non-named arg-tags match the remaining children + * of the tree in sequential order. + * + * If any arg-tagged parameters/fields remain, the match fails. + * If there were no arg-tagged parameters-fields, it is as if all + * of them were non-named and arg-tagged. + * + * A method/constructor is arg-compatible if all of its arguments + * are arg-compatible. + * + * A class is arg-compatible if all of its fields are + * arg-compatible, or if one of its constructors is arg-compatible. + * + */ + @Retention(RetentionPolicy.RUNTIME) public static @interface arg { String value() default ""; } +} diff --git a/src/edu/berkeley/sbp/misc/Demo.java b/src/edu/berkeley/sbp/misc/Demo.java index 8a3b793..467ff38 100644 --- a/src/edu/berkeley/sbp/misc/Demo.java +++ b/src/edu/berkeley/sbp/misc/Demo.java @@ -2,6 +2,7 @@ package edu.berkeley.sbp.misc; import edu.berkeley.sbp.util.*; import edu.berkeley.sbp.*; import edu.berkeley.sbp.chr.*; +import edu.berkeley.sbp.bind.*; import java.util.*; import java.lang.annotation.*; import java.lang.reflect.*; @@ -113,53 +114,7 @@ public class Demo { } } - - public static class bind { - /** - * Constructors, classes, and methods with this attribute will - * match every production of the nonterminal called "value()" - * that is arg-compatible. If value() is undefined, then the - * class/constructor/method name is used. - */ - /* - @Retention(RetentionPolicy.RUNTIME) public static @interface nonterminal { String value() default ""; } - */ - - @Retention(RetentionPolicy.RUNTIME) public static @interface raw { } - - /** - * Constructors, classes, and methods with this attribute will - * match every tree tagged with "value()" that is arg-compatible. - * If value() is undefined, then the class/constructor/method - * name is used. - */ - - @Retention(RetentionPolicy.RUNTIME) public static @interface as { String value() default ""; } - - - /** - * If any parameter to a method or field in a class has a named - * arg-tag, that parameter/field matches the child of the tree - * which either has that label or else is a reference to a - * nonterminal with the corresponding name. - * - * The remaining non-named arg-tags match the remaining children - * of the tree in sequential order. - * - * If any arg-tagged parameters/fields remain, the match fails. - * If there were no arg-tagged parameters-fields, it is as if all - * of them were non-named and arg-tagged. - * - * A method/constructor is arg-compatible if all of its arguments - * are arg-compatible. - * - * A class is arg-compatible if all of its fields are - * arg-compatible, or if one of its constructors is arg-compatible. - * - */ - @Retention(RetentionPolicy.RUNTIME) public static @interface arg { String value() default ""; } - } - + public static class Production { public String tag; public String nonTerminal; @@ -180,19 +135,19 @@ public class Demo { } public static class Target { - public int[] buildSequence(Production p) { - Annotation[][] annotations = _bindable.getArgAnnotations(); - String[] names = _bindable.getArgNames(); - String name = _bindable.getSimpleName(); - int len = annotations.length; - int ofs = 0; - bind.arg[] argtags = new bind.arg[len]; - for(int i=0; i> t) { return _bindable.impose(new Object[] { t }); } public boolean isCompatible(Production p) { bind.as t = getBindAs(); @@ -244,22 +197,26 @@ public class Demo { } } public Sequence makeSequence(Production p) { - return Sequence.rewritingSequence(new TargetReducer(p, buildSequence(p), "reducer-"+this), + return Sequence.rewritingSequence(new TargetReducer(p, buildSequence(p), "reducer-"+this, _bindable, isRaw()), p.elements, p.labels, p.drops); } - public class TargetReducer implements Tree.TreeFunctor { + public static class TargetReducer implements Tree.TreeFunctor { private Production p; private int[] map; private String name; - public TargetReducer(Production p, int[] map, String name) { + private Reflection.Bindable _bindable; + private boolean _israw; + public TargetReducer(Production p, int[] map, String name, Reflection.Bindable b, boolean raw) { this.p = p; this.map = map; this.name = name; + this._bindable = b; + this._israw = raw; } public String toString() { return name; } public Object invoke(Iterable> t) { - if (isRaw()) return invokeRaw(t); + if (_israw) return _bindable.impose(new Object[] { t }); ArrayList ret = new ArrayList(); for(Tree tc : t) { if (tc.head() != null && tc.head() instanceof Functor) @@ -277,11 +234,13 @@ public class Demo { for(int i=0; i