-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenScanner.java
More file actions
155 lines (117 loc) · 2.98 KB
/
TokenScanner.java
File metadata and controls
155 lines (117 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringWriter;
/**
* Provides a Token Iterator for a given Reader.
* <p>
* Revised slightly to only return word tokens.
*
* @see Token
*/
public class TokenScanner implements Iterator<String> {
private Reader r;
private int c;
/**
* Creates a TokenScanner for a given Reader.
* <p>
* As an Iterator, the TokenScanner should only read from the Reader as much
* as is necessary to determine getNext() and next(). The TokenScanner should
* NOT read the entire stream and compute all of the Tokens in advance.
* <p>
*
* @param in the source Reader for character data
* @throws IOException when the provided Reader is null
*/
public TokenScanner(java.io.Reader in) throws IOException {
if (in == null) {
throw new NullPointerException();
}
else {
this.r = in;
c = r.read(); //starts off as first character of line
}
}
/**
* Determines whether a given character is a valid word character.
* <p>
* Valid word characters are letters (according to
* Character.isLetter) and single quote '\''.
*
* @param c the character to check
* @return true if the character is a word character
*/
public static boolean isWordCharacter(int c) {
char ch = (char) c;
return (Character.isLetter(ch) || ch == '\'');
}
/**
* Determines whether a given String is a valid word
* <p>
* Valid words are not null or the empty string. They
* only contain word characters.
*
* @param s the string to check
* @return true if the string is a word
*/
public static boolean isWord(String s) {
if (s == null || s.equals("")) {
return false;
}
else {
boolean wurd = true;
for (int i = 0; i < s.length(); i ++) {
int w = (int) s.charAt(i);
wurd = wurd && isWordCharacter(w);
}
return wurd;
}
}
/**
* Determines whether there is another token available.
*/
public boolean hasNext() {
return (c != -1);
}
/**
* Returns the next token, or throws a NoSuchElementException if none remain.
*/
public String next() {
char ch = (char) c;
String token = "" + ch;
StringWriter writer = new StringWriter();
try {
if (isWordCharacter(c)) {
while (isWordCharacter(c) && hasNext()){
writer.write(c);
c = r.read();
}
token = writer.toString();
return token;
}
else {
while (!(isWordCharacter(c)) && hasNext()){
writer.write(c);
c = r.read();
}
token = writer.toString();
if (isWord(token)) {
return token;
}
else {
if (hasNext()) {//System.out.println(next());
return next();
}
else return "heyyy";
}
}
}
catch (IOException e) {
throw new NoSuchElementException();
}
}
public void remove() {
throw new UnsupportedOperationException();
}
}