import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.DROP;
import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.PRESERVE;
+import static java.lang.System.out;
public class Tokenizer {
+ /**
+ * Stack of token indexes. This allows to walk back in history and un-consume the token.
+ */
private final Stack<Integer> tokenIndexes = new Stack<>();
+
+ /**
+ * Terminators that will be searched for by given tokenizer within given source string.
+ */
private final List<Terminator> terminators = new ArrayList<>();
- private String source;
+
+ private String source; // string to be tokenized
+
private int currentIndex = 0;
private int cachedTerminatorIndex = -1;
return this;
}
+ public Tokenizer addTerminator(Terminator terminator) {
+ terminators.add(terminator);
+ return this;
+ }
+
public Tokenizer addTerminator(final String startSequence,
final String endSequence, final Terminator.TerminationStrategy terminationStrategy) {
terminators.add(new Terminator(startSequence, endSequence, terminationStrategy));
+ "\" but got \"" + match.token + "\" instead.");
}
-
/**
- *
* @return next @TokenizerMatch or <code>null</code> if end of input is reached.
* @throws InvalidSyntaxException
*/
return getOrFindTokenTerminator() == null;
}
- public boolean hasMoreTokens() {
+ public boolean hasMoreContent() {
return currentIndex < source.length();
}
currentIndex = tokenIndexes.pop();
}
+ /**
+ * For debugging
+ */
+ public void enlistRemainingTokens(){
+ int redTokenCount = 0;
+
+ try {
+ while (hasMoreContent()) {
+ out.println(getNextToken().toString());
+ redTokenCount++;
+ }
+ } catch (InvalidSyntaxException e){
+ out.println("There is syntax exception");
+ }
+
+ // restore pointer to original location
+ for (int i = 0; i< redTokenCount; i++ ) unreadToken();
+ }
+
+
public void skipUntilDataEnd() {
tokenIndexes.push(currentIndex);
currentIndex = source.length();