From fd97655ce34c3aff1a47bfb2f45775ac711923b1 Mon Sep 17 00:00:00 2001 From: adam Date: Mon, 2 Jan 2006 02:16:13 -0500 Subject: [PATCH] hoisted getLocation() out of Token and into Token.Stream darcs-hash:20060102071613-5007d-1ca4d808d065cfa45cb60702c110e7517ebaf84b.gz --- src/edu/berkeley/sbp/Element.java | 5 +++-- src/edu/berkeley/sbp/GSS.java | 6 ++++-- src/edu/berkeley/sbp/Parser.java | 10 ++++++---- src/edu/berkeley/sbp/Token.java | 5 ++--- src/edu/berkeley/sbp/misc/CharToken.java | 25 ++++++++++++------------- src/edu/berkeley/sbp/tib/Tib.java | 7 ++++--- 6 files changed, 31 insertions(+), 27 deletions(-) diff --git a/src/edu/berkeley/sbp/Element.java b/src/edu/berkeley/sbp/Element.java index 7e140f9..c94adbd 100644 --- a/src/edu/berkeley/sbp/Element.java +++ b/src/edu/berkeley/sbp/Element.java @@ -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 { + /** if this element always matches exactly one token, return a topology covering exactly those possible tokens, otherwise null */ abstract Topology toAtom(); - public Topology noFollow() { return null; } + 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; diff --git a/src/edu/berkeley/sbp/GSS.java b/src/edu/berkeley/sbp/GSS.java index 2d9f6ff..69d9eed 100644 --- a/src/edu/berkeley/sbp/GSS.java +++ b/src/edu/berkeley/sbp/GSS.java @@ -59,9 +59,11 @@ class GSS { 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.location = location; } public boolean isDone() { return token == null; } @@ -72,7 +74,7 @@ class GSS { 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 diff --git a/src/edu/berkeley/sbp/Parser.java b/src/edu/berkeley/sbp/Parser.java index a54fa08..e8e0dc8 100644 --- a/src/edu/berkeley/sbp/Parser.java +++ b/src/edu/berkeley/sbp/Parser.java @@ -30,7 +30,7 @@ public abstract class Parser { protected Parser(Union u) { this.pt = new Table(u, top()); } protected Parser(Table pt) { this.pt = pt; } - public abstract Forest shiftedToken(T t); + public abstract Forest shiftedToken(T t, Token.Location loc); public abstract Topology top(); @@ -40,12 +40,14 @@ public abstract class Parser { /** parse input, using the table pt to drive the parser */ public Forest parse(Token.Stream 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(;;) { - GSS.Phase next = gss.new Phase(current, input.next()); + loc = input.getLocation(); + GSS.Phase next = gss.new Phase(current, input.next(), loc); 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)current.finalResult; current.checkFailure(); diff --git a/src/edu/berkeley/sbp/Token.java b/src/edu/berkeley/sbp/Token.java index faa02b9..41ef3d1 100644 --- a/src/edu/berkeley/sbp/Token.java +++ b/src/edu/berkeley/sbp/Token.java @@ -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(); - public abstract Location getLocation(); - /** a sequence of input tokens; returns null when EOF is reached */ public static interface Stream { public T next() throws IOException; + public abstract Location getLocation(); } /** a location within the input stream */ public static interface Location { public String toString(); } - } + diff --git a/src/edu/berkeley/sbp/misc/CharToken.java b/src/edu/berkeley/sbp/misc/CharToken.java index eba6cbe..8804fee 100644 --- a/src/edu/berkeley/sbp/misc/CharToken.java +++ b/src/edu/berkeley/sbp/misc/CharToken.java @@ -16,8 +16,8 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable { public static class CharToStringParser extends Parser { public CharToStringParser(Union u) { super(u); } public Topology top() { return new IntegerTopology(); } - public Forest shiftedToken(CharToken ct) { - return Forest.create(ct.getLocation(), ct.result(), null, null, false, false); + public Forest 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(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(9998)) { public String toString() { return "{"; } }; public static final Atom rightBrace = new CharRange(new IntegerTopology(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(all)); @@ -100,12 +100,9 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable { // 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 ////////////////////////////////////////////////////////////////////////////// - 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 String getContext() { return ""; } } /** an implementation of Token.Stream for sequences of characters */ @@ -156,11 +152,14 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable { } 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; - 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; diff --git a/src/edu/berkeley/sbp/tib/Tib.java b/src/edu/berkeley/sbp/tib/Tib.java index 39f6071..8ee70ca 100644 --- a/src/edu/berkeley/sbp/tib/Tib.java +++ b/src/edu/berkeley/sbp/tib/Tib.java @@ -40,6 +40,7 @@ public class Tib implements Token.Stream { 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) { @@ -47,7 +48,7 @@ public class Tib implements Token.Stream { 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; } @@ -55,7 +56,7 @@ public class Tib implements Token.Stream { 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) { @@ -75,7 +76,7 @@ public class Tib implements Token.Stream { } cur = (Block)o; pos = 0; - return CharToken.left(_row, _col); + return CharToken.left; } public static Block parse(BufferedReader br) throws Invalid, IOException { -- 1.7.10.4