/** * @return always true */ @Override public boolean matchToken(TokenQueue tokenQueue, List<Token> matchedTokenList) { matcher.matchToken(tokenQueue, matchedTokenList); return true; }
public static TokenMatcher anyToken() { // TODO Godin: we can return singleton instance return new AnyTokenMatcher(); }
public static TokenMatcher forgetLastToken() { // TODO Godin: we can return singleton instance return new ForgetLastTokenMatcher(); }
@Test public void shouldNotCreateStatement() { TokenQueue tokenQueue = spy(new TokenQueue(Arrays.asList(new Token("a", 1, 1)))); TokenMatcher matcher = spy(new AnyTokenMatcher()); StatementChannel channel = StatementChannel.create(matcher); List<Statement> output = mock(List.class); assertThat(channel.consume(tokenQueue, output), is(true)); verify(matcher).matchToken(eq(tokenQueue), anyList()); verifyNoMoreInteractions(matcher); verify(output).add(any()); verifyNoMoreInteractions(output); }
@Test public void shouldMatch() { TokenQueue tokenQueue = spy(new TokenQueue()); TokenMatcher delegate = mock(TokenMatcher.class); OptTokenMatcher matcher = new OptTokenMatcher(delegate); List<Token> output = mock(List.class); assertThat(matcher.matchToken(tokenQueue, output), is(true)); verify(delegate).matchToken(tokenQueue, output); verifyNoMoreInteractions(delegate); verifyNoMoreInteractions(tokenQueue); verifyNoMoreInteractions(output); }
public static TokenMatcher from(String token) { return new ExactTokenMatcher(token); }
public static TokenMatcher bridge(String lToken, String rToken) { return new BridgeTokenMatcher(lToken, rToken); }
public static TokenMatcher to(String... tokens) { return new UptoTokenMatcher(tokens); }
public static TokenMatcher opt(TokenMatcher optMatcher) { return new OptTokenMatcher(optMatcher); }
@Test public void shouldNotMatchWhenNoLeft() { Token t1 = new Token("a", 1, 1); TokenQueue tokenQueue = spy(new TokenQueue(Arrays.asList(t1))); List<Token> output = mock(List.class); BridgeTokenMatcher matcher = new BridgeTokenMatcher("(", ")"); assertThat(matcher.matchToken(tokenQueue, output), is(false)); verify(tokenQueue).isNextTokenValue("("); verifyNoMoreInteractions(tokenQueue); verifyNoMoreInteractions(output); }
@Test public void shouldNotMatch() { Token t1 = new Token("a", 1, 1); Token t2 = new Token("b", 2, 1); TokenQueue tokenQueue = spy(new TokenQueue(Arrays.asList(t1, t2))); List<Token> output = mock(List.class); ExactTokenMatcher matcher = new ExactTokenMatcher("b"); assertThat(matcher.matchToken(tokenQueue, output), is(false)); verify(tokenQueue).isNextTokenValue("b"); verifyNoMoreInteractions(tokenQueue); verifyNoMoreInteractions(output); }
@Test public void shouldCreateStatement() { Token token = new Token("a", 1, 1); TokenQueue tokenQueue = spy(new TokenQueue(Arrays.asList(token))); TokenMatcher matcher = spy(new AnyTokenMatcher()); StatementChannel channel = StatementChannel.create(matcher); List<Statement> output = mock(List.class); assertThat(channel.consume(tokenQueue, output), is(true)); verify(matcher).matchToken(eq(tokenQueue), anyList()); verifyNoMoreInteractions(matcher); ArgumentCaptor<Statement> statement = ArgumentCaptor.forClass(Statement.class); verify(output).add(statement.capture()); assertThat(statement.getValue().getValue(), is("a")); assertThat(statement.getValue().getStartLine(), is(1)); assertThat(statement.getValue().getEndLine(), is(1)); verifyNoMoreInteractions(output); }
public static TokenMatcher token(String token) { return new ExactTokenMatcher(token); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullAsLeft() { new BridgeTokenMatcher(null, ")"); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNull() { new UptoTokenMatcher(null); }
public boolean consume(TokenQueue tokenQueue, List<Statement> output) { List<Token> matchedTokenList = new ArrayList<>(); for (TokenMatcher tokenMatcher : tokenMatchers) { if (!tokenMatcher.matchToken(tokenQueue, matchedTokenList)) { tokenQueue.pushForward(matchedTokenList); return false; } } // all matchers were successful, so now build the statement // matchedTokenList.size() check is for case with ForgiveLastTokenMatcher if (!blackHole && !matchedTokenList.isEmpty()) { output.add(new Statement(matchedTokenList)); } return true; }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNull() { new OptTokenMatcher(null); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNull() { new ExactTokenMatcher(null); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullAsRight() { new BridgeTokenMatcher("(", null); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptEmpty() { new UptoTokenMatcher(new String[] {}); }