public class Terminator {
final String startSequence;
- final boolean ignoreTerminator;
String endSequence;
+ TerminationStrategy termination;
- public Terminator(final String startPattern, final boolean ignoreTerminator) {
+ public Terminator(final String startPattern, TerminationStrategy termination) {
startSequence = startPattern;
- this.ignoreTerminator = ignoreTerminator;
+ this.termination = termination;
}
- public Terminator(final String startSequence, final String endSequence,
- final boolean ignoreTerminator) {
-
+ public Terminator(final String startSequence, final String endSequence, TerminationStrategy termination) {
this.startSequence = startSequence;
this.endSequence = endSequence;
- this.ignoreTerminator = ignoreTerminator;
+ this.termination = termination;
}
+ enum TerminationStrategy {
+ PRESERVE,
+ DROP;
+ }
}
import java.util.List;
import java.util.Stack;
+import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.DROP;
+
public class Tokenizer {
final Stack<Integer> tokenIndexes = new Stack<>();
}
public Tokenizer addTerminator(final String startSequence,
- final boolean ignoreTerminator) {
- terminators.add(new Terminator(startSequence, ignoreTerminator));
+ final Terminator.TerminationStrategy terminationStrategy) {
+ terminators.add(new Terminator(startSequence, terminationStrategy));
return this;
}
public Tokenizer addTerminator(final String startSequence,
- final String endSequence, final boolean ignoreTerminator) {
- terminators.add(new Terminator(startSequence, endSequence,
- ignoreTerminator));
+ final String endSequence, final Terminator.TerminationStrategy terminationStrategy) {
+ terminators.add(new Terminator(startSequence, endSequence, terminationStrategy));
return this;
}
for (final Terminator terminator : terminators)
if (sequenceMatches(terminator.startSequence))
- if (terminator.ignoreTerminator) {
+ if (terminator.termination == DROP) {
currentIndex += terminator.startSequence.length();
if (terminator.endSequence != null)
}
- public boolean isNextToken(final String token) {
+ public boolean consumeIfNextToken(final String token) {
if (token.equals(getNextToken().token))
return true;