checkpoint
authoradam <adam@megacz.com>
Sun, 15 Jan 2006 11:11:35 +0000 (06:11 -0500)
committeradam <adam@megacz.com>
Sun, 15 Jan 2006 11:11:35 +0000 (06:11 -0500)
darcs-hash:20060115111135-5007d-b37831cc7c89c8151a48f062e3d8a0be5ee4cbc9.gz

src/edu/berkeley/sbp/Input.java
src/edu/berkeley/sbp/Parser.java
src/edu/berkeley/sbp/chr/CharInput.java [new file with mode: 0644]
src/edu/berkeley/sbp/chr/CharParser.java
src/edu/berkeley/sbp/misc/CartesianInput.java
src/edu/berkeley/sbp/misc/CartesianLocation.java [new file with mode: 0644]
src/edu/berkeley/sbp/tib/Tib.java
src/edu/berkeley/sbp/util/IntegerTopology.java

index d8374cd..a2c9baa 100644 (file)
@@ -6,11 +6,14 @@ import java.lang.ref.*;
 import edu.berkeley.sbp.*;
 import edu.berkeley.sbp.util.*;
 
-/** a token of input -- note that this represents an <i>actual input token</i> rather than an <tt>Element</tt> which <i>matches</i> a token */
-public interface Input<Tok> {
+/** a stream of tokens to be parsed */
+public interface Input<Token> {
 
-    public Tok next(int numstates, int resets, int waits) throws IOException;
-    public abstract Location getLocation();
+    /** returns the token just beyond the current location and advances beyond it */
+    public Token    next(int numstates, int resets, int waits) throws IOException;
+
+    /** returns the location the input stream is currently at */
+    public Location getLocation();
 
     /** a location *between tokens* in the input stream */
     public static interface Location {
index 75f3ace..632729e 100644 (file)
@@ -42,13 +42,6 @@ public abstract class Parser<Tok, Result> {
 
         public final Walk.Cache cache = this;
 
-        public void optimize(Functor<Tok,Integer> f) {
-            for(State<Tok> state : all_states.values()) {
-                state.oreductions = state.reductions.optimize(f);
-                state.oshifts = state.shifts.optimize(f);
-            }
-        }
-
         private void walk(Element e, HashSet<Element> hs) {
             if (e==null) return;
             if (hs.contains(e)) return;
@@ -109,6 +102,11 @@ public abstract class Parser<Tok, Result> {
                     if (p.element() != null && p.element() instanceof Atom)
                         state.shifts.addAll(state.gotoSetTerminals.subset(((Atom)p.element())));
                 }
+            if (top instanceof IntegerTopology)
+                for(State<Tok> state : all_states.values()) {
+                    state.oreductions = state.reductions.optimize(((IntegerTopology)top).functor());
+                    state.oshifts = state.shifts.optimize(((IntegerTopology)top).functor());
+                }
         }
 
         private boolean isRightNullable(Position p) {
diff --git a/src/edu/berkeley/sbp/chr/CharInput.java b/src/edu/berkeley/sbp/chr/CharInput.java
new file mode 100644 (file)
index 0000000..1178e42
--- /dev/null
@@ -0,0 +1,30 @@
+package edu.berkeley.sbp.chr;
+import java.io.*;
+import java.util.*;
+import java.lang.reflect.*;
+import java.lang.ref.*;
+import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.util.*;
+import edu.berkeley.sbp.misc.*;
+import edu.berkeley.sbp.Input.Location;
+
+public class CharInput extends CartesianInput<Character> {
+    private final Reader r;
+    
+    public CharInput(String s)                { this(new StringReader(s)); }
+    public CharInput(Reader r)                { this(r, null); }
+    public CharInput(Reader r,      String s) { this.r = r; }
+    public CharInput(InputStream i)           { this(i, null); }
+    public CharInput(InputStream i, String s) { this(new InputStreamReader(i), s); }
+    
+    boolean cr = false;
+    public boolean   isCR() { return cr; }
+    public Character next() throws IOException {
+        cr = false;
+        int i = r.read();
+        if (i==-1) return null;
+        char c = (char)i;
+        cr = c=='\n';
+        return c;
+    }
+}
index db235cd..6ad2a7e 100644 (file)
@@ -9,39 +9,14 @@ import edu.berkeley.sbp.misc.*;
 import edu.berkeley.sbp.Input.Location;
 
 public class CharParser extends Parser<Character,String> {
-    public Forest<String> parse(InputStream is) throws IOException, ParseFailed {
-        return super.parse(new Stream(is));
-    }
-    public Forest<String> parse(Reader r) throws IOException, ParseFailed {
-        return super.parse(new Stream(r));
-    }
 
-    public CharParser(Union u) {
-        super(u, new CharTopology());
-        pt.optimize(new CharTopology());
-    }
+    public Forest<String> parse(InputStream is) throws IOException, ParseFailed { return super.parse(new CharInput(is)); }
+    public Forest<String> parse(Reader r)       throws IOException, ParseFailed { return super.parse(new CharInput(r)); }
+
+    public CharParser(Union u) { super(u, new CharTopology()); }
+
     public Forest<String> shiftToken(Character ct, Location loc) {
         return Forest.create(loc, ct.toString(), null, false, false);
     }
 
-    private static class Stream extends CartesianInput<Character> {
-        private final Reader r;
-        
-        public Stream(String s)                { this(new StringReader(s)); }
-        public Stream(Reader r)                { this(r, null); }
-        public Stream(Reader r,      String s) { this.r = r; }
-        public Stream(InputStream i)           { this(i, null); }
-        public Stream(InputStream i, String s) { this(new InputStreamReader(i), s); }
-
-        boolean cr = false;
-        public boolean   isCR() { return cr; }
-        public Character next() throws IOException {
-            cr = false;
-            int i = r.read();
-            if (i==-1) return null;
-            char c = (char)i;
-            cr = c=='\n';
-            return c;
-        }
-    }
 }
index 4930173..928b236 100644 (file)
@@ -39,13 +39,6 @@ public abstract class CartesianInput<Tok> implements Input<Tok> {
         return t;
     }
 
-    public static class Location implements Input.Location {
-        public final int line;
-        public final int col;
-        public String toString()            { return line + ":" + col; }
-        public Location(int line, int col)  { this.line = line; this.col = col; }
-    }
-
     private class LocWrap implements Input.Location {
         public final int line;
         public final int col;
diff --git a/src/edu/berkeley/sbp/misc/CartesianLocation.java b/src/edu/berkeley/sbp/misc/CartesianLocation.java
new file mode 100644 (file)
index 0000000..b5d7bb7
--- /dev/null
@@ -0,0 +1,14 @@
+package edu.berkeley.sbp.misc;
+import java.io.*;
+import java.util.*;
+import java.lang.reflect.*;
+import java.lang.ref.*;
+import edu.berkeley.sbp.*;
+import edu.berkeley.sbp.util.*;
+
+public class CartesianLocation implements Input.Location {
+    public final int line;
+    public final int col;
+    public String toString()                     { return line + ":" + col; }
+    public CartesianLocation(int line, int col)  { this.line = line; this.col = col; }
+}
index 6693600..b2383ac 100644 (file)
@@ -42,7 +42,7 @@ public class Tib implements Input<Character> {
 
     int _row = 1;
     int _col = 0;
-    public Input.Location getLocation() { return new CartesianInput.Location(_row, _col); }
+    public Input.Location getLocation() { return new CartesianLocation(_row, _col); }
     private BufferedReader br;
 
     char left = CharRange.left;
index 71e9df9..73de988 100644 (file)
@@ -15,6 +15,7 @@ public class IntegerTopology<V> implements Topology<V> {
     private int toInt(V v) { return f==null?((IntegerMappable)v).toInt():f.invoke(v); }
 
     public Range.Set getRanges()         { return new Range.Set(rs); }
+    public Functor<V,Integer> functor() { return f; }
 
     public IntegerTopology(Functor<V,Integer> f)               { this(f, new Range.Set()); }
     public IntegerTopology(Functor<V,Integer> f, V a)          { this(f, f==null?((IntegerMappable)a).toInt():f.invoke(a)); }