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