2 * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.commons.string.tokenizer;
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
10 public class Terminator {
13 public final TerminationStrategy termination;
14 public final String group;
15 public boolean active = true;
16 public final Pattern pattern;
18 public Terminator(TerminationStrategy termination, String regexp, String group) {
19 this.termination = termination;
22 this.pattern = Pattern.compile("^"+regexp);
25 public Matcher match(String source, int index) {
26 Matcher matcher = pattern.matcher(source);
27 matcher.region(index, source.length());
32 public String toString() {
33 return "Terminator{" +
34 "regexp='" + regexp + '\'' +
35 ", termination=" + termination +
36 ", group='" + group + '\'' +
37 ", active=" + active +
41 public enum TerminationStrategy {
43 * Preserve token that is identified within Terminator and return it for processing. For example when
44 * building language parser, it could be used for statements that you want to capture.
49 * While tokens that are marked by Terminator are identified, they are dropped and not returned for consumption.
50 * For example, when building language parser, you might use such strategy for whitespace and comments.
51 * That is, those tokens act as separators between actually useful tokens, but you don't want to consume such
52 * separators or comments in your code.