-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringTokenFactory.java
54 lines (45 loc) · 1.32 KB
/
StringTokenFactory.java
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
/**
* A special kind of TokenFactory
* that looks for tokens that exactly match a given string.
*/
public class StringTokenFactory extends TokenFactory {
private final String tokenText;
private String text;
private int startFrom;
/**
* Create a factory that looks for the given tokenText.
* @param tokenText the exact text of the kind of token we can produce
*/
public StringTokenFactory(final String tokenText) {
super();
this.tokenText = tokenText;
}
/**
* Get the text of the tokens we can produce.
* @return The exact text of the kind of token we can produce
*/
public String getTokenText() {
return tokenText;
}
@Override
public void setText(final String text) {
this.text = text;
}
@Override
public int getTokenLength() {
return tokenText.length();
}
@Override
public boolean find(final int startFrom) {
this.startFrom = startFrom;
final boolean found = text.regionMatches(startFrom, tokenText, 0, tokenText.length());
return found;
}
/**
* Get the position at which we last tried to find a token.
* @return The start position of the last call to find(...)
*/
protected int getTokenStartPosition() {
return startFrom;
}
}