System.out.println(t.toPrettyString());
}
+ public static class ReflectiveMetaPlain extends ReflectiveMeta {
+ public Object repeatTag() { return null; }
+ public Sequence tryResolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
+ return null; }
+ public Sequence resolveTag(String tag, String nonTerminalName, Element[] els, Object[] labels, boolean[] drops) {
+ return Sequence.rewritingSequence(tag, els, labels, drops);
+ }
+ }
+
public static class ReflectiveMeta extends MetaGrammar.Meta {
private final Class _cl;
private final Class[] _inner;
MG.Seq.class,
MG.NonTerminalReference.class,
MG.StringLiteral.class,
- MG.Tree.class,
+ MG.XTree.class,
MG.CharClass.class
});
}
*/
@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.
return Sequence.rewritingSequence(new TargetReducer(p), p.elements, p.labels, p.drops);
}
public abstract Object plant(Object[] fields, int[] map);
+ public boolean isRaw() { return false; }
+ public Object invokeRaw(Tree t) { return null; }
public class TargetReducer implements Reducer {
private Production p;
private int[] map;
}
public String toString() { return "reducer-"+Target.this; }
public Object reduce(Tree t) {
+ if (isRaw()) return invokeRaw(t);
Object[] objects = new Object[t.numChildren()];
for(int i=0; i<t.numChildren(); i++) {
Tree tc = t.child(i);
int[] ret = buildSequence(p, names, argtags);
return ret;
}
+ public boolean isRaw() { return _method.isAnnotationPresent(raw.class); }
+ public Object invokeRaw(Tree t) {
+ try {
+ return _method.invoke(null, new Object[] { t });
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
public Object plant(Object[] fields, int[] map) {
try {
Class[] argTypes = _method.getParameterTypes();
if (c == char.class) {
return o.toString().charAt(0);
}
+
+ if (o.getClass().isArray() &&
+ o.getClass().getComponentType().isArray() &&
+ o.getClass().getComponentType().getComponentType() == String.class &&
+ c.isArray() &&
+ c.getComponentType() == String.class) {
+ String[] ret = new String[((Object[])o).length];
+ for(int i=0; i<ret.length; i++) {
+ StringBuffer sb = new StringBuffer();
+ for(Object ob : (Object[])(((Object[])o)[i]))
+ sb.append(ob);
+ ret[i] = sb.toString();
+ }
+ return ret;
+ }
+
if (c.isArray() && (c.getComponentType().isInstance(o))) {
Object[] ret = (Object[])Array.newInstance(c.getComponentType(), 1);
ret[0] = o;
return ret;
}
+
if (o.getClass().isArray() && c.isArray()) {
boolean ok = true;
for(int i=0; i<((Object[])o).length; i++) {
return o;
}
- public static Union make(Tree t, String s) {
- ReflectiveMeta rm = new ReflectiveMeta();
+ public static Union cached = null;
+ public static Union make() {
+ if (cached != null) return cached;
+ try {
+ ReflectiveMeta m = new ReflectiveMeta();
+ Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream("tests/meta.g")).expand1();
+ MetaGrammar.Meta.MetaGrammarFile mgf = m.new MetaGrammarFile(res);
+ MetaGrammar.BuildContext bc = new MetaGrammar.BuildContext(mgf);
+ Union meta = mgf.get("s").build(bc);
+ Tree t = new CharParser(meta).parse(new FileInputStream("tests/meta.g")).expand1();
+ return cached = make(t, "s");
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ public static Union make(Tree t, String s) { return make(t, s, new ReflectiveMeta()); }
+ public static Union make(Tree t, String s, ReflectiveMeta rm) {
Reducer red = (Reducer)t.head();
MG.Grammar g = (MG.Grammar)red.reduce(t);
Context cx = new Context(g,rm);
Union u = null;
for(MG.NonTerminal nt : g.nonterminals) {
+ System.out.println(nt.name);
Union el = (Union)cx.get(nt.name);
StringBuffer st = new StringBuffer();
el.toString(st);
return cx.get(nonTerminal);
}
}
+
public static class StringLiteral extends Constant {
public @tag("literal") StringLiteral(String string) { super(CharRange.string(string)); }
public boolean drop() { return true; }
}
+
public static class CharClass extends El {
Range[] ranges;
public @tag("[") CharClass(Range[] ranges) { this.ranges = ranges; }
}
}
- public static @tag("{") class Tree extends El {
+ public static @tag("{") class XTree extends El {
public @arg Seq body;
public Element build(Context cx) {
throw new Error();
import edu.berkeley.sbp.tib.*;
import edu.berkeley.sbp.chr.*;
import edu.berkeley.sbp.util.*;
+import static edu.berkeley.sbp.misc.Demo.*;
public class RegressionTests {
}
System.err.println("parsing " + s[0]);
- Tree<String> res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1();
- Union meta = MetaGrammar.make(res, "s");
+ Tree<String> res = new CharParser(Demo.make()).parse(new FileInputStream(s[0])).expand1();
+ Union meta = Demo.make(res, "s");
System.err.println("parsing " + s[1]);
SequenceInputStream sis = new SequenceInputStream(new FileInputStream(s[0]), new FileInputStream(s[1]));
res = new CharParser(meta).parse(sis).expand1();
- Union testcasegrammar = MetaGrammar.make(res, "ts");
+
+ Union testcasegrammar = Demo.make(res, "ts", new TestCaseMaker());
if (testcasegrammar==null) return;
CharParser parser = new CharParser(testcasegrammar);
}
System.err.println("expanding...");
- TestCase[] expanded = (TestCase[])new TestCaseBuilder().walk(r2.expand1());
+ Tree t = r2.expand1();
+ TestCase[] expanded = (TestCase[])((Demo.Reducer)t.head()).reduce(t);
System.err.println("executing...");
for(TestCase tc : expanded) {
tc.execute();
}
}
+ public static class TestCaseMaker extends ReflectiveMeta {
+ public TestCaseMaker() {
+ super(TestCaseMakerHelper.class, new Class[] {
+ MG.Grammar.class,
+ MG.NonTerminal.class,
+ MG.AnonUn.class,
+ MG.Range.class,
+ MG.El.class,
+ MG.Seq.class,
+ MG.NonTerminalReference.class,
+ MG.StringLiteral.class,
+ MG.XTree.class,
+ MG.CharClass.class
+ });
+ }
+ public static class TestCaseMakerHelper extends MG {
+ public static @tag("grammaro") @raw Object grammaro(Tree t) {
+ System.out.println("working on " + t);
+ return Demo.make(t.child(0), "s", new ReflectiveMetaPlain());
+ }
+ //public static @tag("tca") Object tca(Object[] o) throws IOException {
+ //return new TestCase((String)o[0], (String[])o[1], (Union)o[2], false, false); }
+ public static @tag("tca") Object tca(String input, String[] output, Union u) throws IOException {
+ return new TestCase(input, output, u, false, false); }
+ public static @tag("tcb") Object tca(String input, Union u) throws IOException {
+ return new TestCase(input, new String[0], u, false, false); }
+ public static @tag("ts") TestCase[] go(TestCase[] cases) { return cases; }
+ public static @tag("o") Object o(Object[] s) { return s; }
+ }
+ }
+
public static class TestCase {
private final boolean tib;
private final boolean jav;
public /*final*/ String input;
public final String[] output;
public final Union grammar;
+
public TestCase(String input, String[] output, Union grammar, boolean tib, boolean jav) throws IOException {
this.tib = tib;
this.jav = jav;
this.input = input;
- this.output = output;
+ this.output = output==null ? new String[0] : output;
this.grammar = grammar;
}
public String toString() {
public static class TestCaseBuilder extends StringWalker {
public Object walk(Tree<String> tree) {
try {
- if ("grammar".equals(tree.head())) return MetaGrammar.make(tree, "s");
+ if ("grammaro".equals(tree.head())) return Demo.make(tree, "s");
else if ("output".equals(tree.head())) return MetaGrammar.string(tree.children());
else if ("input".equals(tree.head())) return MetaGrammar.string(tree.children());
else if ("testcase".equals(tree.head())) {
String input = MetaGrammar.string(tree.child(0));
String[] output = tree.numChildren()>2 ? ((String[])walk(tree, 1)) : new String[0];
- Union grammar = MetaGrammar.make(tree.child(tree.numChildren()-1), "s");
+ Union grammar = Demo.make(tree.child(tree.numChildren()-1), "s");
TestCase tc = new TestCase(input, output, grammar, false, false);
return tc;
} else if ("ts".equals(tree.head())) return walk(tree, 0);
public abstract class StringWalker extends TreeWalker<String> {
public void walk(String tag) { }
public Object walk(Tree<String> tree) {
- walk(tree.head());
+ Object o = tree.head();
+ if (!(o instanceof String)) o = null; /* FIXME */
+ walk((String)o);
return super.walk(tree);
}
public Object walk(String tag, Object[] tokens) {
}
-testcase {
- input "ab";
-
- s = a:"a" b:"b"
-}
+//testcase {
+// input "ab";
+//
+// s = a:"a" b:"b"
+//}
ts = ts:: !ws tests !ws
tests = test */ ws
-test = ^"testcase" "{" input output +/ ws (grammar::Grammar) "}" /ws
- | ^"testcase" "{" input (grammar::Grammar) "}" /ws
- | ^"tibcase" "{" input output +/ ws (grammar::Grammar) "}" /ws
- | ^"tibcase" "{" input (grammar::Grammar) "}" /ws
- | ^"javacase" "{" input output +/ ws (grammar::Grammar) "}" /ws
- | ^"javacase" "{" input (grammar::Grammar) "}" /ws
-output = ^"output" Quoted ";" /ws
-input = "input" Quoted ";" /ws
+test = tca:: "testcase" "{" input (o::(output +/ ws)) (grammaro::(grammar::Grammar)) "}" /ws
+ | tcb:: "testcase" "{" input (grammaro::(grammar::Grammar)) "}" /ws
+output = "output" Quoted ";" /ws
+input = "input" Quoted ";" /ws