checkpoint
authoradam <adam@megacz.com>
Thu, 15 Dec 2005 05:11:36 +0000 (00:11 -0500)
committeradam <adam@megacz.com>
Thu, 15 Dec 2005 05:11:36 +0000 (00:11 -0500)
darcs-hash:20051215051136-5007d-15d333d53fb94628adcf8b39c0e1743434f82d84.gz

src/edu/berkeley/sbp/misc/CharToken.java
src/edu/berkeley/sbp/misc/RegressionTests.java
src/edu/berkeley/sbp/tib/Tib.java

index 93ae7bb..e73ccdd 100644 (file)
@@ -50,6 +50,8 @@ 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)));
     }
 
+    public static final CharToken left  = new CharToken((char)-3, 0, 0);
+    public static final CharToken right = new CharToken((char)-4, 0, 0);
     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 "}"; } };
 
@@ -89,7 +91,7 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
 
     public final char c;
     public final Location location;
-    CharToken(char c, int line, int col)   { this(c, new CartesianLocation(line, col)); }
+    public CharToken(char c, int line, int col)   { this(c, new CartesianLocation(line, col)); }
     private CharToken(char c, Location loc)        { this.c = c; this.location = loc; }
     public String result()                         { return c+""; }
     public Location getLocation()                  { return location; }
index 67152b6..717c784 100644 (file)
@@ -73,12 +73,10 @@ public class RegressionTests {
         public final String input;
         public final String[] output;
         public final Union grammar;
-        public TestCase(String input, String[] output, Union grammar, boolean tib) {
+        public TestCase(String input, String[] output, Union grammar, boolean tib) throws IOException {
             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+"\": ":"")
-                ;
+                ? new Tib(input)
+                : new CharToken.Stream(new StringReader(input), input.indexOf('\n')==-1?"\""+input+"\": ":"");
             this.input = input;
             this.output = output;
             this.grammar = grammar;
@@ -127,9 +125,12 @@ 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 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 TestCase testcase(String input, String[] output, Union grammar) throws IOException {
+            return new TestCase(input, output,        grammar, false); }
+        public TestCase testcase(String input,                  Union grammar) throws IOException {
+            return new TestCase(input, new String[0], grammar, false); }
+        public TestCase  tibcase(String input,                  Union grammar) throws IOException {
+            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");
index 9a546e9..7b3270f 100644 (file)
@@ -3,8 +3,8 @@
 // You may not use this file except in compliance with the License.
 
 package edu.berkeley.sbp.tib;
-//import org.ibex.util.*;
-//import org.ibex.io.*;
+import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.misc.*;
 import java.util.*;
 import java.io.*;
 
@@ -25,37 +25,47 @@ import java.io.*;
  *   possible (it is unclear whether or not the associated grammar is
  *   context-free).
  */
-public class Tib /*implements Token.Stream<CharToken>*/ {
-    /*
-    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(BufferedReader br) { cur = parse(br).toString(0,-1); }
+public class Tib implements Token.Stream<CharToken> {
+
+    public Tib(String s) throws IOException, Invalid { this(new StringReader(s)); }
+    public Tib(Reader r) throws IOException, Invalid { this(new BufferedReader(r)); }
+    public Tib(InputStream is) throws IOException, Invalid { this(new BufferedReader(new InputStreamReader(is))); }
+    public Tib(BufferedReader br) throws IOException, Invalid { cur = parse(br); }
 
-    boolean left = false;
-    boolean right = false;
     private Block cur;
+    private String s = null;
     int pos = 0;
+    int spos = 0;
 
-    public Token next() throws IOException {
-        if (left) { left = false; return new CharToken(-3); }
-        if (right) { right = false; return new CharToken(-4); }
-
+    public CharToken next() throws IOException {
         if (pos >= cur.size()) {
             pos = cur.iip+1;
             cur = cur.parent;
-            return new CharToken(-4);
+            return CharToken.right;
         }
 
+        if (s != null) {
+            if (spos < s.length())
+                return new CharToken(s.charAt(spos++), 0, 0);
+            s = null;
+        }
         Object o = cur.child(pos++);
-        if (o instanceof String) return new StringToken((String)o);
-        if (o.isLiteral()) return ((Block.Literal)o).text();
-        cur = (Block)b;
+        if (o instanceof String) {
+            spos = 0;
+            s = (String)o;
+            return next();
+        }
+        if (((Block)o).isLiteral()) {
+            spos = 0;
+            s = ((Block.Literal)o).text();
+            return next();
+        }
+        cur = (Block)o;
         pos = 0;
-        return new CharToken(-3);
+        return CharToken.left;
     }
 
-    public static Block parse(BufferedReader br) throws Invalid {
+    public static Block parse(BufferedReader br) throws Invalid, IOException {
         int row=0, col=0;
         try {
             boolean blankLine = false;
@@ -104,21 +114,15 @@ public class Tib /*implements Token.Stream<CharToken>*/ {
         }
     }
 
-    public static class Block implements Token {
+    public static class Block /*implements Token*/ {
                       Block  parent;
         public  final int    row;
         public  final int    col;
         public final int iip;
-        private final Vec    children = new Vec();
+        private final Vector children = new Vector();
         private       String pending  = "";
 
-        public Tree<String> result() {
-            // FIXME
-        }
-
-        public Location getLocation() { return new Location.Cartesian(row, col); }
-        public boolean isEOF() { return false; }
-
+        //public Location getLocation() { return /*new Location.Cartesian(row, col)*/null; }
         public int    size() { return children.size(); }
         public Object child(int i) { return children.elementAt(i); }
         public boolean isLiteral() {  return false; }
@@ -237,7 +241,7 @@ public class Tib /*implements Token.Stream<CharToken>*/ {
     // Exceptions //////////////////////////////////////////////////////////////////////////////
 
     private static class InternalException extends RuntimeException { public InternalException(String s) { super(s); } }
-    public static class Invalid extends IOException {
+    public static class Invalid extends /*IOException*/RuntimeException {
         public Invalid(InternalException ie, int row, int col) {
             super(ie.getMessage() + " at " + row + ":" + col);
         }
@@ -245,7 +249,7 @@ public class Tib /*implements Token.Stream<CharToken>*/ {
 
     // Testing //////////////////////////////////////////////////////////////////////////////
 
-    public static void main(String[] s) throws Exception { System.out.println(parse(new Stream(System.in)).toString(-1)); }
+    public static void main(String[] s) throws Exception { System.out.println(parse(new BufferedReader(new InputStreamReader(System.in))).toString(-1)); }
     
     // Utilities //////////////////////////////////////////////////////////////////////////////
 
@@ -268,6 +272,6 @@ public class Tib /*implements Token.Stream<CharToken>*/ {
         }
         return ret.toString();
     }
-    */
+
 }