From: adam Date: Sun, 2 Jul 2006 23:08:32 +0000 (-0400) Subject: checkpoint X-Git-Tag: tag_for_25-Mar~229 X-Git-Url: http://git.megacz.com/?p=sbp.git;a=commitdiff_plain;h=ac0f7d32be59d02b1f8d48d2b23232adf8871b46 checkpoint darcs-hash:20060702230832-5007d-06c5b64a9c1a5ecf1abf090755c4f76009d0f4c1.gz --- diff --git a/src/edu/berkeley/sbp/misc/Demo.java b/src/edu/berkeley/sbp/misc/Demo.java index 92c9194..6384939 100644 --- a/src/edu/berkeley/sbp/misc/Demo.java +++ b/src/edu/berkeley/sbp/misc/Demo.java @@ -33,6 +33,15 @@ public class Demo { 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; @@ -47,7 +56,7 @@ public class Demo { MG.Seq.class, MG.NonTerminalReference.class, MG.StringLiteral.class, - MG.Tree.class, + MG.XTree.class, MG.CharClass.class }); } @@ -136,6 +145,8 @@ public class Demo { */ @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. @@ -230,6 +241,8 @@ public class Demo { 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; @@ -239,6 +252,7 @@ public class Demo { } 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 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); @@ -624,10 +679,12 @@ public class Demo { 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; } @@ -639,7 +696,7 @@ public class Demo { } } - 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(); diff --git a/src/edu/berkeley/sbp/misc/RegressionTests.java b/src/edu/berkeley/sbp/misc/RegressionTests.java index 7a8e502..b186b43 100644 --- a/src/edu/berkeley/sbp/misc/RegressionTests.java +++ b/src/edu/berkeley/sbp/misc/RegressionTests.java @@ -7,6 +7,7 @@ import edu.berkeley.sbp.misc.*; 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 { @@ -30,12 +31,13 @@ public class RegressionTests { } System.err.println("parsing " + s[0]); - Tree res = new CharParser(MetaGrammar.make()).parse(new FileInputStream(s[0])).expand1(); - Union meta = MetaGrammar.make(res, "s"); + Tree 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); @@ -56,7 +58,8 @@ public class RegressionTests { } 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(); @@ -84,17 +87,49 @@ public class RegressionTests { } } + 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() { @@ -167,13 +202,13 @@ public class RegressionTests { public static class TestCaseBuilder extends StringWalker { public Object walk(Tree 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); diff --git a/src/edu/berkeley/sbp/misc/StringWalker.java b/src/edu/berkeley/sbp/misc/StringWalker.java index ae150e6..db85259 100644 --- a/src/edu/berkeley/sbp/misc/StringWalker.java +++ b/src/edu/berkeley/sbp/misc/StringWalker.java @@ -8,7 +8,9 @@ import java.lang.reflect.*; public abstract class StringWalker extends TreeWalker { public void walk(String tag) { } public Object walk(Tree 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) { diff --git a/tests/regression.tc b/tests/regression.tc index 48a9bae..bd7d107 100644 --- a/tests/regression.tc +++ b/tests/regression.tc @@ -391,9 +391,9 @@ testcase { } -testcase { - input "ab"; - - s = a:"a" b:"b" -} +//testcase { +// input "ab"; +// +// s = a:"a" b:"b" +//} diff --git a/tests/testcase.g b/tests/testcase.g index b1321b1..ba23723 100644 --- a/tests/testcase.g +++ b/tests/testcase.g @@ -1,10 +1,6 @@ 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