+ private TokenizerMatch buildPreservedToken(StringBuilder token, Terminator terminator) throws InvalidSyntaxException {
+ if (hasAccumulatedToken(token))
+ return new TokenizerMatch(token.toString(), "", terminator);
+
+ if (terminator.hasEndSequence()){
+ int endSequenceIndex = source.indexOf(terminator.endSequence,
+ currentIndex + terminator.startSequence.length());
+
+ if (endSequenceIndex < 0)
+ throw new InvalidSyntaxException("Expected \"" + terminator.endSequence + "\" but not found.");
+
+ String reminder = source.substring(currentIndex + terminator.startSequence.length(), endSequenceIndex);
+ currentIndex = endSequenceIndex + terminator.endSequence.length();
+
+ return new TokenizerMatch(terminator.startSequence, reminder, terminator);
+ } else {
+ currentIndex += terminator.startSequence.length();
+ return new TokenizerMatch(terminator.startSequence, "", terminator);
+ }
+ }
+
+ private boolean hasAccumulatedToken(StringBuilder token) {
+ return token.length() > 0;
+ }
+
+ private boolean isOngoingToken() {
+ return findTokenTerminator() == null;
+ }
+
+ public Terminator findTokenTerminator() {
+ for (Terminator terminator : terminators)
+ if (terminator.matches(source, currentIndex))
+ return terminator;
+ return null;
+ }
+
+ public boolean consumeIfNextToken(final String token) throws InvalidSyntaxException {