+
+ Terminator tokenTerminator = findTokenTerminator();
+
+ if (tokenTerminator.termination == PRESERVE){
+ return buildPreservedToken(token, tokenTerminator);
+ } else if (tokenTerminator.termination == DROP){
+ if (hasAccumulatedToken(token)){
+ currentIndex++;
+ return new TokenizerMatch(token.toString(), "", tokenTerminator);
+ } else {
+ currentIndex++;
+ }
+ }
+ }
+
+ }
+
+ 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);