RegexChannel extends Channel<O> { @Override public final boolean consume(CodeReader code, O output) { if (code.popTo(matcher, tmpBuilder) > 0) { consume(tmpBuilder, output); tmpBuilder.delete(0, tmpBuilder.length()); return true; } return false; } RegexChannel(String regex); @Override final boolean consume(CodeReader code, O output);  }
@Test public void shouldMatch() { ChannelDispatcher<StringBuilder> dispatcher = ChannelDispatcher.builder().addChannel(new MyWordChannel()).addChannel(new BlackholeChannel()).build(); StringBuilder output = new StringBuilder(); dispatcher.consume(new CodeReader("my word"), output); assertThat(output.toString(), is("<w>my</w> <w>word</w>")); }
@Test public void shouldMatchTokenLongerThanBuffer() { ChannelDispatcher<StringBuilder> dispatcher = ChannelDispatcher.builder().addChannel(new MyLiteralChannel()).build(); StringBuilder output = new StringBuilder(); CodeReaderConfiguration codeReaderConfiguration = new CodeReaderConfiguration(); int literalLength = 100000; String veryLongLiteral = String.format(String.format("%%0%dd", literalLength), 0).replace("0", "a"); assertThat(veryLongLiteral.length(), is(100000)); dispatcher.consume(new CodeReader("\">" + veryLongLiteral + "<\"", codeReaderConfiguration), output); assertThat(output.toString(), is("<literal>\">" + veryLongLiteral + "<\"</literal>")); }