X-Git-Url: http://git.megacz.com/?p=sbp.git;a=blobdiff_plain;f=src%2Fedu%2Fberkeley%2Fsbp%2Fmisc%2FCharToken.java;h=8804feedc307f3122feb0c4d61f58f46b9d1c40c;hp=143c708a1441e2436d4057efc2acd5283f9201f4;hb=fd97655ce34c3aff1a47bfb2f45775ac711923b1;hpb=242a8711e98cbd3ed6d05271bf910fde38e9be9a diff --git a/src/edu/berkeley/sbp/misc/CharToken.java b/src/edu/berkeley/sbp/misc/CharToken.java index 143c708..8804fee 100644 --- a/src/edu/berkeley/sbp/misc/CharToken.java +++ b/src/edu/berkeley/sbp/misc/CharToken.java @@ -13,6 +13,14 @@ public class CharToken implements Token, IntegerTopology.IntegerMappable { // Public ////////////////////////////////////////////////////////////////////////////// + public static class CharToStringParser extends Parser { + public CharToStringParser(Union u) { super(u); } + public Topology top() { return new IntegerTopology(); } + public Forest shiftedToken(CharToken ct, Token.Location loc) { + return Forest.create(loc, ct.result(), null, null, false, false); + } + } + public static class CharRange extends Atom { private String esc(char c) { return StringUtil.escapify(c+"", "[]-~\\\"\'"); } private Topology t; @@ -52,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)); @@ -92,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+"")+"\'"; } ////////////////////////////////////////////////////////////////////////////////////////// @@ -105,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 */ @@ -148,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;