1 package edu.berkeley.sbp.misc;
4 import java.lang.reflect.*;
5 import java.lang.ref.*;
6 import edu.berkeley.sbp.*;
7 import edu.berkeley.sbp.*;
8 import edu.berkeley.sbp.util.*;
9 import edu.berkeley.sbp.*;
11 /** an implementation of Token for streams of Java <tt>char</tt> values */
12 public class StringToken implements Token {
14 public static final StringToken left = new StringToken(null, null) { public boolean equals(Object o) { return this==o; } };
15 public static final StringToken right = new StringToken(null, null) { public boolean equals(Object o) { return this==o; } };
17 public static final Atom leftBrace = new Atom(new DiscreteTopology<StringToken>(left)) { public String toString() { return "{"; } };
18 public static final Atom rightBrace = new Atom(new DiscreteTopology<StringToken>(right)) { public String toString() { return "}"; } };
20 private static class StringAtom extends Atom {
22 public StringAtom(String s) { super(new DiscreteTopology<StringToken>(new StringToken(s, null))); this.s = s; }
23 public String toString() { return "[atom \""+s+"\"]"; }
26 /** returns an element which exactly matches the string given */
27 public static Element string(String s) {
28 return new StringAtom(s);
31 public static Topology<StringToken> top() { return new DiscreteTopology<StringToken>(); }
33 public final String s;
34 public final Location location;
35 private StringToken(String s, Location loc) { this.s = s; this.location = loc; }
36 public String result() { return s; }
37 public Location getLocation() { return location; }
38 public String toString() { return "\'"+StringUtil.escapify(s)+"\'"; }
40 public boolean equals(Object o) { return o!=null && o instanceof StringToken && s.equals(((StringToken)o).s); }
41 public int hashCode() { return s==null ? 0 : s.hashCode(); }