+ private void skipUntilTerminatorEnd(Terminator terminator) throws InvalidSyntaxException {
+ if (terminator.hasEndSequence())
+ currentIndex = getEndSequenceIndex(terminator) + terminator.endSequence.length();
+ else
+ currentIndex += terminator.startSequence.length();
+ }
+
+ /**
+ * @throws InvalidSyntaxException if end sequence is not found as is expected by given token.
+ */
+ private TokenizerMatch buildPreservedToken(StringBuilder token, Terminator terminator)
+ throws InvalidSyntaxException {
+ if (hasAccumulatedToken(token))
+ return new TokenizerMatch(token.toString(), null, terminator);
+
+ if (terminator.hasEndSequence())
+ return buildTokenWithExpectedENdSequence(terminator);
+ else
+ return buildTokenWithoutEndSequence(terminator);
+ }
+
+ private TokenizerMatch buildTokenWithoutEndSequence(Terminator terminator) {
+ currentIndex += terminator.startSequence.length();
+ return new TokenizerMatch(terminator.startSequence, null, terminator);
+ }
+
+ private TokenizerMatch buildTokenWithExpectedENdSequence(Terminator terminator) throws InvalidSyntaxException {
+ int endSequenceIndex = getEndSequenceIndex(terminator);
+ String reminder = source.substring(currentIndex + terminator.startSequence.length(), endSequenceIndex);
+ currentIndex = endSequenceIndex + terminator.endSequence.length();
+
+ return new TokenizerMatch(terminator.startSequence, reminder, terminator);
+ }
+
+ /**
+ * @throws InvalidSyntaxException if end of input is reached without finding expected end sequence.
+ */
+ private int getEndSequenceIndex(Terminator terminator) throws InvalidSyntaxException {
+ int endSequenceIndex = source.indexOf(terminator.endSequence,
+ currentIndex + terminator.startSequence.length());
+
+ if (endSequenceIndex < 0)
+ throw new InvalidSyntaxException("Expected \"" + terminator.endSequence + "\" but not found.");
+
+ return endSequenceIndex;
+ }
+