checkpoint
authoradam <adam@megacz.com>
Thu, 15 Dec 2005 00:24:38 +0000 (19:24 -0500)
committeradam <adam@megacz.com>
Thu, 15 Dec 2005 00:24:38 +0000 (19:24 -0500)
darcs-hash:20051215002438-5007d-b8ea3e4fac53b69e89bb8b7671b8aa203cb4c6f4.gz

src/edu/berkeley/sbp/misc/CharToken.java
src/edu/berkeley/sbp/misc/MetaGrammar.java
src/edu/berkeley/sbp/misc/ReflectiveWalker.java
src/edu/berkeley/sbp/misc/RegressionTests.java
src/edu/berkeley/sbp/tib/Tib.java
tests/meta.g
tests/regression.tc
tests/testcase.g

index bad4372..eccac6a 100644 (file)
@@ -50,6 +50,9 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
         return new CharRange(new IntegerTopology<CharToken>(new Range.Set(new Range((int)start, (int)end)).complement().intersect(all)));
     }
 
         return new CharRange(new IntegerTopology<CharToken>(new Range.Set(new Range((int)start, (int)end)).complement().intersect(all)));
     }
 
+    public static final Atom leftBrace  = new Atom(new IntegerTopology<CharToken>(-3)) { public String toString() { return "{"; } };
+    public static final Atom rightBrace = new Atom(new IntegerTopology<CharToken>(-4)) { public String toString() { return "}"; } };
+
     private static final Range.Set all = new Range.Set(new Range(0, Character.MAX_VALUE));
     public  static final Atom      any = new CharRange(new IntegerTopology<CharToken>(all));
     public  static final Atom     none = new CharRange(new IntegerTopology<CharToken>());
     private static final Range.Set all = new Range.Set(new Range(0, Character.MAX_VALUE));
     public  static final Atom      any = new CharRange(new IntegerTopology<CharToken>(all));
     public  static final Atom     none = new CharRange(new IntegerTopology<CharToken>());
index 71782dd..d385a55 100644 (file)
@@ -82,13 +82,24 @@ public class MetaGrammar extends ReflectiveWalker {
     }
 
     public Object   _backslash__leftbrace_(String s)      { return SELF; }
     }
 
     public Object   _backslash__leftbrace_(String s)      { return SELF; }
-    public Object   _leftbrace_(String s)                 { return SELF; }
-    public Object   _plus_(final Element r) { return Repeat.many1(r); }
+    public Object   _leftbrace_(PreSequence p) {
+        return nonTerminal("braced"+(anon++),
+                           new PreSequence[][] {
+                               new PreSequence[] {
+                                   new PreSequence(CharToken.leftBrace),
+                                   p,
+                                   new PreSequence(CharToken.rightBrace)
+                               }
+                           },
+                           false,
+                           false);
+    }
 
 
-    public PreSequence rewrite(Object[] o)                                            { return new PreSequence(o, null); }
-    public PreSequence _equals__greater_(Object[] o, String s)                        { return new PreSequence(o, s); }
+    public Object   _plus_(final Element r)                    { return Repeat.many1(r); }
+    public PreSequence rewrite(Object[] o)                     { return new PreSequence(o, null); }
+    public PreSequence _equals__greater_(Object[] o, String s) { return new PreSequence(o, s); }
     public PreSequence seq(Object[] o, Object sep, String tag) { return new PreSequence(sep==null ? o : _slash_(o, sep), tag); }
     public PreSequence seq(Object[] o, Object sep, String tag) { return new PreSequence(sep==null ? o : _slash_(o, sep), tag); }
-    public Object[] seq(Object[] o) { return o; }
+    public Object[] seq(Object[] o)                            { return o; }
     public Object[] _slash_(Object[] o, Object sep) {
         if (o.length <= 1) return o;
         Object[] ret = new Object[o.length * 2 - 1];
     public Object[] _slash_(Object[] o, Object sep) {
         if (o.length <= 1) return o;
         Object[] ret = new Object[o.length * 2 - 1];
@@ -141,6 +152,8 @@ public class MetaGrammar extends ReflectiveWalker {
         public /*final*/ String tag;
         public final Object[] o;
 
         public /*final*/ String tag;
         public final Object[] o;
 
+        public PreSequence(Object o) { this(new Object[] { o }, null); }
+        public PreSequence(Object[] o) { this(o, null); }
         public PreSequence(Object[] o, String tag) { this.o = o; this.tag = tag; }
         boolean[] drops = null;
         public Union    buildUnion() {
         public PreSequence(Object[] o, String tag) { this.o = o; this.tag = tag; }
         boolean[] drops = null;
         public Union    buildUnion() {
index 71e597c..c22e81c 100644 (file)
@@ -53,12 +53,19 @@ public class ReflectiveWalker extends StringWalker {
         }
         return ret.toString();
     }
         }
         return ret.toString();
     }
+    /*
     public Object walk(Tree<String> tree) {
         if (tree.head()!=null) {
             Member m = member("$"+normalize(tree.head()), 0, false);
     public Object walk(Tree<String> tree) {
         if (tree.head()!=null) {
             Member m = member("$"+normalize(tree.head()), 0, false);
+            if (m!=null) {
+                if ((m instanceof Method) && ((Method)m).getReturnType()==Void.TYPE) {
+                    Reflection.fuzzyInvoke(target, m, new Object[0]);
+                }
+            }
         }
         return super.walk(tree);
     }
         }
         return super.walk(tree);
     }
+    */
     public Object walk(String tag, Object[] argo) {
         if (argo.length==0) return super.walk(tag, argo);
         if (argo==null) return tag;
     public Object walk(String tag, Object[] argo) {
         if (argo.length==0) return super.walk(tag, argo);
         if (argo==null) return tag;
index 96c2075..67152b6 100644 (file)
@@ -2,9 +2,8 @@ package edu.berkeley.sbp.misc;
 import java.io.*;
 import java.util.*;
 import edu.berkeley.sbp.*;
 import java.io.*;
 import java.util.*;
 import edu.berkeley.sbp.*;
-import edu.berkeley.sbp.*;
 import edu.berkeley.sbp.misc.*;
 import edu.berkeley.sbp.misc.*;
-import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.tib.*;
 
 public class RegressionTests {
 
 
 public class RegressionTests {
 
@@ -70,10 +69,16 @@ public class RegressionTests {
     }
 
     public static class TestCase {
     }
 
     public static class TestCase {
+        private final Token.Stream inp;
         public final String input;
         public final String[] output;
         public final Union grammar;
         public final String input;
         public final String[] output;
         public final Union grammar;
-        public TestCase(String input, String[] output, Union grammar) {
+        public TestCase(String input, String[] output, Union grammar, boolean tib) {
+            this.inp = tib
+                ? new CharToken.Stream(new StringReader(input), input.indexOf('\n')==-1?"\""+input+"\": ":"")
+                /*: new TibCharToken.Stream(new StringReader(input))*/
+                : new CharToken.Stream(new StringReader(input), input.indexOf('\n')==-1?"\""+input+"\": ":"")
+                ;
             this.input = input;
             this.output = output;
             this.grammar = grammar;
             this.input = input;
             this.output = output;
             this.grammar = grammar;
@@ -85,8 +90,7 @@ public class RegressionTests {
             return ret;
         }
         public boolean execute() throws Exception {
             return ret;
         }
         public boolean execute() throws Exception {
-            Forest<String> res = new Parser(grammar,
-                                            CharToken.top()).parse(new CharToken.Stream(new StringReader(input), input.indexOf('\n')==-1?"\""+input+"\": ":""));
+            Forest<String> res = new Parser(grammar, CharToken.top()).parse(inp);
             Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
             System.out.print("\r");
             if (results.size() == 0 && output.length > 0) {
             Collection<Tree<String>> results = res==null ? new HashSet<Tree<String>>() : res.expand(false);
             System.out.print("\r");
             if (results.size() == 0 && output.length > 0) {
@@ -123,8 +127,9 @@ public class RegressionTests {
     public static class TestCaseBuilder extends MetaGrammar {
         public TestCase[] ts(Object o1, TestCase[] ts, Object o2) { return ts; }
         public TestCase[] ts(TestCase[] ts) { return ts; }
     public static class TestCaseBuilder extends MetaGrammar {
         public TestCase[] ts(Object o1, TestCase[] ts, Object o2) { return ts; }
         public TestCase[] ts(TestCase[] ts) { return ts; }
-        public TestCase testcase(String input, String[] output, Union grammar) { return new TestCase(input, output,        grammar); }
-        public TestCase testcase(String input,                  Union grammar) { return new TestCase(input, new String[0], grammar); }
+        public TestCase testcase(String input, String[] output, Union grammar) { return new TestCase(input, output,        grammar, false); }
+        public TestCase testcase(String input,                  Union grammar) { return new TestCase(input, new String[0], grammar, false); }
+        public TestCase  tibcase(String input,                  Union grammar) { return new TestCase(input, new String[0], grammar, false); }
         public MetaGrammar grammar(Object[] o) { return this; }
         public Object walk(String tag, Object[] args) {
             if ("grammar".equals(tag)) return done("s");
         public MetaGrammar grammar(Object[] o) { return this; }
         public Object walk(String tag, Object[] args) {
             if ("grammar".equals(tag)) return done("s");
index df56855..167ad8b 100644 (file)
@@ -25,15 +25,14 @@ import java.io.*;
  *   possible (it is unclear whether or not the associated grammar is
  *   context-free).
  */
  *   possible (it is unclear whether or not the associated grammar is
  *   context-free).
  */
-public class Tib /*implements Token.Stream<TibToken>*/ {
+public class Tib /*implements Token.Stream<TreeToken<String>>*/ {
     /*
     /*
+    public final String str;
 
 
-    private String str;
-
+    public Tib(String s) { this(new StringReader(s)); }
+    public Tib(Reader r) { this(new BufferedReader(r)); }
     public Tib(InputStream is) { this(new BufferedReader(new InputStreamReader(is))); }
     public Tib(InputStream is) { this(new BufferedReader(new InputStreamReader(is))); }
-    public Tib(BufferedReader br) {
-        Block b = parse(br);
-    }
+    public Tib(BufferedReader br) { str = parse(br).toString(0,-1); }
 
     public Token next() throws IOException {
         if (pos >= str.length()) return Atom.EOF;
 
     public Token next() throws IOException {
         if (pos >= str.length()) return Atom.EOF;
@@ -241,6 +240,6 @@ public class Tib /*implements Token.Stream<TibToken>*/ {
         }
         return ret.toString();
     }
         }
         return ret.toString();
     }
-    */    
+    */
 }
 
 }
 
index 2779b7c..0e3bce7 100644 (file)
@@ -24,7 +24,7 @@ range    ::= ec          => "range"
 e        ::= word                           => "nonTerminal"
            |  quoted                        => "literal"
            |    ^"()"
 e        ::= word                           => "nonTerminal"
            |  quoted                        => "literal"
            |    ^"()"
-           |    ^"{" alternatives "}"   /ws
+           |    ^"{" sequence "}"   /ws
            |    ^"["  (range*) "]"
            |    ^"[~" (range*) "]"
            |    ^"^" quoted             /ws
            |    ^"["  (range*) "]"
            |    ^"[~" (range*) "]"
            |    ^"^" quoted             /ws
index c807a7a..3204e6a 100644 (file)
 //    s ::= ()       => s0
 //}
 
 //    s ::= ()       => s0
 //}
 
+//tibcase {
+//    input "
+//  hello 
+//    there
+//    how
+//     { are }
+//";
+//
+//    s        ::= ws S ws
+//    ws      !::= w*
+//    S        ::= { Hello }
+//    Hello    ::= "hello" { "there" "how" { "are" } }
+//}
+
 testcase {
     input "ab c";
     output "1:{{a b} {c}}";
 testcase {
     input "ab c";
     output "1:{{a b} {c}}";
index d6656a2..b42a410 100644 (file)
@@ -3,5 +3,6 @@ tests    ::= test */ ws
 ws       !::= w*
 test     ::= ^"testcase" "{" input output +/ ws   grammar "}" /ws
            | ^"testcase" "{" input                grammar "}" /ws
 ws       !::= w*
 test     ::= ^"testcase" "{" input output +/ ws   grammar "}" /ws
            | ^"testcase" "{" input                grammar "}" /ws
+           | ^"tibcase"  "{" input                grammar "}" /ws
 output   ::= "output" quoted ";" /ws
 input    ::= "input"  quoted ";" /ws
 output   ::= "output" quoted ";" /ws
 input    ::= "input"  quoted ";" /ws