From: adam Date: Sun, 25 Dec 2005 10:28:52 +0000 (-0500) Subject: added StringToken X-Git-Tag: tag_for_25-Mar~484 X-Git-Url: http://git.megacz.com/?p=sbp.git;a=commitdiff_plain;h=4c5cadfb5604449d04e2cdaaae7b9a61795c9044;hp=f2611533a5703f1256b5ed3a57e36b3909dedad0 added StringToken darcs-hash:20051225102852-5007d-9bd3d55edabd4107e8d3fe0b712b65483a9fe313.gz --- diff --git a/src/edu/berkeley/sbp/misc/StringToken.java b/src/edu/berkeley/sbp/misc/StringToken.java new file mode 100644 index 0000000..fac11ab --- /dev/null +++ b/src/edu/berkeley/sbp/misc/StringToken.java @@ -0,0 +1,42 @@ +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.*; +import edu.berkeley.sbp.util.*; +import edu.berkeley.sbp.*; + +/** an implementation of Token for streams of Java char values */ +public class StringToken implements Token { + + public static final StringToken left = new StringToken(null, null) { public boolean equals(Object o) { return this==o; } }; + public static final StringToken right = new StringToken(null, null) { public boolean equals(Object o) { return this==o; } }; + + public static final Atom leftBrace = new Atom(new DiscreteTopology(left)) { public String toString() { return "{"; } }; + public static final Atom rightBrace = new Atom(new DiscreteTopology(right)) { public String toString() { return "}"; } }; + + private static class StringAtom extends Atom { + private String s; + public StringAtom(String s) { super(new DiscreteTopology(new StringToken(s, null))); this.s = s; } + public String toString() { return "[atom \""+s+"\"]"; } + } + + /** returns an element which exactly matches the string given */ + public static Element string(String s) { + return new StringAtom(s); + } + + public static Topology top() { return new DiscreteTopology(); } + + public final String s; + public final Location location; + private StringToken(String s, Location loc) { this.s = s; this.location = loc; } + public String result() { return s; } + public Location getLocation() { return location; } + public String toString() { return "\'"+StringUtil.escapify(s)+"\'"; } + + public boolean equals(Object o) { return o!=null && o instanceof StringToken && s.equals(((StringToken)o).s); } + public int hashCode() { return s==null ? 0 : s.hashCode(); } +}