java syntax parser
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / tokenizer / Tokenizer.java
index 97f1224..8a42776 100644 (file)
@@ -2,12 +2,15 @@ package eu.svjatoslav.inspector.tokenizer;
 
 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) {
@@ -19,7 +22,15 @@ public class Tokenizer {
                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) {
@@ -55,6 +66,23 @@ public class Tokenizer {
 
        }
 
+       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;