import java.util.ArrayList;
import java.util.List;
+import java.util.Stack;
public class Tokenizer {
private final List<Terminator> terminators = new ArrayList<Terminator>();
private final String source;
+ Stack<Integer> tokenIndexes = new Stack<Integer>();
+
private int currentIndex = 0;
public Tokenizer(final String source) {
terminators.add(new Terminator(terminator, empty));
}
+ public void expectToken(final String value) throws InvalidSyntaxException {
+ final TokenizerMatch match = getToken();
+ if (!value.equals(match.token))
+ throw new InvalidSyntaxException("Expected \"" + value
+ + "\" but got \"" + match.token + "\" instead.");
+ }
+
public TokenizerMatch getToken() {
+ tokenIndexes.push(currentIndex);
final StringBuffer result = new StringBuffer();
while (true) {
}
+ public boolean isNextToken(final String token) {
+ if (token.equals(getToken().token))
+ return true;
+
+ rollbackToken();
+ return false;
+ }
+
+ public void rollbackToken() {
+ currentIndex = tokenIndexes.pop();
+ }
+
+ public void skipUtilEnd() {
+ tokenIndexes.push(currentIndex);
+ currentIndex = source.length();
+ }
+
public boolean terminatorMatches(final Terminator terminator) {
if ((currentIndex + terminator.value.length()) > source.length())
return false;