hoisted getLocation() out of Token and into Token.Stream
authoradam <adam@megacz.com>
Mon, 2 Jan 2006 07:16:13 +0000 (02:16 -0500)
committeradam <adam@megacz.com>
Mon, 2 Jan 2006 07:16:13 +0000 (02:16 -0500)
darcs-hash:20060102071613-5007d-1ca4d808d065cfa45cb60702c110e7517ebaf84b.gz

src/edu/berkeley/sbp/Element.java
src/edu/berkeley/sbp/GSS.java
src/edu/berkeley/sbp/Parser.java
src/edu/berkeley/sbp/Token.java
src/edu/berkeley/sbp/misc/CharToken.java
src/edu/berkeley/sbp/tib/Tib.java

index 7e140f9..c94adbd 100644 (file)
@@ -10,10 +10,11 @@ import java.lang.ref.*;
 /** the root superclass for all components of the grammar (terminals, nonterminals, literals, etc) */
 public abstract class Element {
 
 /** the root superclass for all components of the grammar (terminals, nonterminals, literals, etc) */
 public abstract class Element {
 
+    /** if this element always matches exactly one token, return a topology covering exactly those possible tokens, otherwise <tt>null</tt> */
     abstract Topology toAtom();
     abstract Topology toAtom();
-    public Topology noFollow() { return null; }
+
     Forest epsilonForm() { throw new Error("no epsilon form: " + this); }
     Forest epsilonForm() { throw new Error("no epsilon form: " + this); }
-    final boolean possiblyEpsilon(Walk.Cache cache)   {
+    final boolean possiblyEpsilon(Walk.Cache cache) {
         Boolean ret = cache==null ? null : cache.possiblyEpsilon.get(this);
         if (ret != null) return ret.booleanValue();
         ret = new Walk.PossiblyEpsilon().walk(this) ? Boolean.TRUE : Boolean.FALSE;
         Boolean ret = cache==null ? null : cache.possiblyEpsilon.get(this);
         if (ret != null) return ret.booleanValue();
         ret = new Walk.PossiblyEpsilon().walk(this) ? Boolean.TRUE : Boolean.FALSE;
index 2d9f6ff..69d9eed 100644 (file)
@@ -59,9 +59,11 @@ class GSS {
 
         boolean closed = false;
 
 
         boolean closed = false;
 
-        public Phase(Phase previous, Token token) {
+        private Token.Location location;
+        public Phase(Phase previous, Token token, Token.Location location) {
             this.pos = previous==null ? 0 : previous.pos+1;
             this.token = token;
             this.pos = previous==null ? 0 : previous.pos+1;
             this.token = token;
+            this.location = location;
         }
 
         public boolean isDone() { return token == null; }
         }
 
         public boolean isDone() { return token == null; }
@@ -72,7 +74,7 @@ class GSS {
                 throw new Parser.Failed(error, getLocation());
         }
 
                 throw new Parser.Failed(error, getLocation());
         }
 
-        public Token.Location getLocation() { return token==null ? null : token.getLocation(); }
+        public Token.Location getLocation() { return location; }
 
         /** add a new node (merging with existing nodes if possible)
          *  @param parent             the parent of the new node
 
         /** add a new node (merging with existing nodes if possible)
          *  @param parent             the parent of the new node
index a54fa08..e8e0dc8 100644 (file)
@@ -30,7 +30,7 @@ public abstract class Parser<T extends Token, R> {
     protected Parser(Union u)  { this.pt = new Table(u, top()); }
     protected Parser(Table pt) { this.pt = pt; }
 
     protected Parser(Union u)  { this.pt = new Table(u, top()); }
     protected Parser(Table pt) { this.pt = pt; }
 
-    public abstract Forest<R> shiftedToken(T t);
+    public abstract Forest<R> shiftedToken(T t, Token.Location loc);
     public abstract Topology<T> top();
 
 
     public abstract Topology<T> top();
 
 
@@ -40,12 +40,14 @@ public abstract class Parser<T extends Token, R> {
     /** parse <tt>input</tt>, using the table <tt>pt</tt> to drive the parser */
     public Forest<R> parse(Token.Stream<T> input) throws IOException, Failed {
         GSS gss = new GSS();
     /** parse <tt>input</tt>, using the table <tt>pt</tt> to drive the parser */
     public Forest<R> parse(Token.Stream<T> input) throws IOException, Failed {
         GSS gss = new GSS();
-        GSS.Phase current = gss.new Phase(null, input.next());
+        Token.Location loc = input.getLocation();
+        GSS.Phase current = gss.new Phase(null, input.next(), loc);
         current.newNode(null, null, pt.start, true, null);
         for(;;) {
         current.newNode(null, null, pt.start, true, null);
         for(;;) {
-            GSS.Phase next = gss.new Phase(current, input.next());
+            loc = input.getLocation();
+            GSS.Phase next = gss.new Phase(current, input.next(), loc);
             current.reduce();
             current.reduce();
-            Forest forest = current.token==null ? null : shiftedToken((T)current.token);
+            Forest forest = current.token==null ? null : shiftedToken((T)current.token, loc);
             current.shift(next, forest);
             if (current.isDone()) return (Forest<R>)current.finalResult;
             current.checkFailure();
             current.shift(next, forest);
             if (current.isDone()) return (Forest<R>)current.finalResult;
             current.checkFailure();
index faa02b9..41ef3d1 100644 (file)
@@ -13,17 +13,16 @@ public interface Token {
     /** this is declared abstract as a way of forcing subclasses to provide a thoughtful implementation */
     public abstract String toString();
 
     /** this is declared abstract as a way of forcing subclasses to provide a thoughtful implementation */
     public abstract String toString();
 
-    public abstract Location getLocation();
-
     /** a sequence of input tokens; returns null when EOF is reached */
     public static interface Stream<T extends Token> {
         public T next() throws IOException;
     /** a sequence of input tokens; returns null when EOF is reached */
     public static interface Stream<T extends Token> {
         public T next() throws IOException;
+        public abstract Location getLocation();
     }
 
     /** a location within the input stream */
     public static interface Location {
         public String toString();
     }
     }
 
     /** a location within the input stream */
     public static interface Location {
         public String toString();
     }
-
 }
 
 }
 
+
index eba6cbe..8804fee 100644 (file)
@@ -16,8 +16,8 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
     public static class CharToStringParser extends Parser<CharToken,String> {
         public CharToStringParser(Union u) { super(u); }
         public Topology<CharToken> top() { return new IntegerTopology<CharToken>(); }
     public static class CharToStringParser extends Parser<CharToken,String> {
         public CharToStringParser(Union u) { super(u); }
         public Topology<CharToken> top() { return new IntegerTopology<CharToken>(); }
-        public Forest<String> shiftedToken(CharToken ct) {
-            return Forest.create(ct.getLocation(), ct.result(), null, null, false, false);
+        public Forest<String> shiftedToken(CharToken ct, Token.Location loc) {
+            return Forest.create(loc, ct.result(), null, null, false, false);
         }
     }
 
         }
     }
 
@@ -60,10 +60,10 @@ 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 CharToken left(int row, int col) { return new CharToken((char)9998, 0, 0) { public String toString() { return "{"; } }; }
-    public static CharToken right(int row, int col) { return new CharToken((char)9999, 0, 0) { public String toString() { return "}"; } }; }
     public static final Atom leftBrace  = new CharRange(new IntegerTopology<CharToken>(9998)) { public String toString() { return "{"; } };
     public static final Atom rightBrace = new CharRange(new IntegerTopology<CharToken>(9999)) { public String toString() { return "}"; } };
     public static final Atom leftBrace  = new CharRange(new IntegerTopology<CharToken>(9998)) { public String toString() { return "{"; } };
     public static final Atom rightBrace = new CharRange(new IntegerTopology<CharToken>(9999)) { public String toString() { return "}"; } };
+    public static final CharToken left       = new CharToken((char)9998);
+    public static final CharToken right      = new CharToken((char)9999);
 
     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));
 
     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));
@@ -100,12 +100,9 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
     // Private //////////////////////////////////////////////////////////////////////////////
 
     public final char c;
     // Private //////////////////////////////////////////////////////////////////////////////
 
     public final char c;
-    public final Location location;
-    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; }
-    public String  toString()                      { return "\'"+StringUtil.escapify(c+"")+"\'"; }
+    public CharToken(char c)        { this.c = c; }
+    public String result()          { return c+""; }
+    public String  toString()       { return "\'"+StringUtil.escapify(c+"")+"\'"; }
 
     //////////////////////////////////////////////////////////////////////////////////////////
 
 
     //////////////////////////////////////////////////////////////////////////////////////////
 
@@ -113,12 +110,11 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
 
     // Statics //////////////////////////////////////////////////////////////////////////////
 
 
     // Statics //////////////////////////////////////////////////////////////////////////////
 
-    static class CartesianLocation implements Location {
+    public static class CartesianLocation implements 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; }
         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; }
-        public String getContext() { return ""; }
     }
     
     /** an implementation of Token.Stream for sequences of characters */
     }
     
     /** an implementation of Token.Stream for sequences of characters */
@@ -156,11 +152,14 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable {
         }
 
         long then = 0;
         }
 
         long then = 0;
+        private Token.Location location = new LocWrap(1, 1);
+        public Token.Location getLocation() { return location; }
         public Token next() throws IOException {
             int i = r.read();
             if (i==-1) return null;
             char c = (char)i;
         public Token next() throws IOException {
             int i = r.read();
             if (i==-1) return null;
             char c = (char)i;
-            Token ret = new CharToken(c, new LocWrap(line, col));
+            location = new LocWrap(line, col);
+            Token ret = new CharToken(c);
             String s = line + "";
             while(s.length() < 4) s = " " + s;
             s = "line "+s+", col " + col;
             String s = line + "";
             while(s.length() < 4) s = " " + s;
             s = "line "+s+", col " + col;
index 39f6071..8ee70ca 100644 (file)
@@ -40,6 +40,7 @@ public class Tib implements Token.Stream<CharToken> {
 
     int _row = 0;
     int _col = 0;
 
     int _row = 0;
     int _col = 0;
+    public Token.Location getLocation() { return new CharToken.CartesianLocation(_row, _col); }
     public CharToken next() throws IOException {
         if (cur==null) return null;
         if (s != null) {
     public CharToken next() throws IOException {
         if (cur==null) return null;
         if (s != null) {
@@ -47,7 +48,7 @@ public class Tib implements Token.Stream<CharToken> {
                 char c = s.charAt(spos++);
                 if (c=='\n') { _row++; _col = 0; }
                 else _col++;
                 char c = s.charAt(spos++);
                 if (c=='\n') { _row++; _col = 0; }
                 else _col++;
-                return new CharToken(c, _row, _col);
+                return new CharToken(c);
             }
             s = null;
         }
             }
             s = null;
         }
@@ -55,7 +56,7 @@ public class Tib implements Token.Stream<CharToken> {
             pos = cur.iip+1;
             cur = cur.parent;
             if (cur==null) return null;
             pos = cur.iip+1;
             cur = cur.parent;
             if (cur==null) return null;
-            return CharToken.right(_row, _col);
+            return CharToken.right;
         }
         Object o = cur.child(pos++);
         if (o instanceof String) {
         }
         Object o = cur.child(pos++);
         if (o instanceof String) {
@@ -75,7 +76,7 @@ public class Tib implements Token.Stream<CharToken> {
         }
         cur = (Block)o;
         pos = 0;
         }
         cur = (Block)o;
         pos = 0;
-        return CharToken.left(_row, _col);
+        return CharToken.left;
     }
 
     public static Block parse(BufferedReader br) throws Invalid, IOException {
     }
 
     public static Block parse(BufferedReader br) throws Invalid, IOException {