/** * @return always true */ @Override public boolean matchToken(TokenQueue tokenQueue, List<Token> matchedTokenList) { int last = matchedTokenList.size() - 1; tokenQueue.pushForward(Collections.singletonList(matchedTokenList.get(last))); matchedTokenList.remove(last); return true; }
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 public void shouldPushForward() { TokenQueue tokenQueue = mock(TokenQueue.class); TokenMatcher matcher = mock(TokenMatcher.class); List<Statement> output = mock(List.class); StatementChannel channel = StatementChannel.create(matcher); assertThat(channel.consume(tokenQueue, output), is(false)); ArgumentCaptor<List> matchedTokenList = ArgumentCaptor.forClass(List.class); verify(matcher).matchToken(eq(tokenQueue), matchedTokenList.capture()); verifyNoMoreInteractions(matcher); verify(tokenQueue).pushForward(matchedTokenList.getValue()); verifyNoMoreInteractions(tokenQueue); verifyNoMoreInteractions(output); }
@Test public void shouldMatch() { TokenQueue tokenQueue = spy(new TokenQueue()); Token token = new Token("a", 0, 0); List<Token> output = new ArrayList<>(Arrays.asList(token)); ForgetLastTokenMatcher matcher = new ForgetLastTokenMatcher(); assertThat(matcher.matchToken(tokenQueue, output), is(true)); assertThat(output.size(), is(0)); verify(tokenQueue).pushForward(eq(Collections.singletonList(token))); }
@Test public void shouldPushTokenAtBegining() { Token pushedToken = new Token("push", 1, 0); List<Token> pushedTokenList = new ArrayList<>(); pushedTokenList.add(pushedToken); tokenQueue.pushForward(pushedTokenList); assertThat(tokenQueue.peek(), is(pushedToken)); assertThat(tokenQueue.size(), is(4)); }
/** * @return always true */ @Override public boolean matchToken(TokenQueue tokenQueue, List<Token> matchedTokenList) { int last = matchedTokenList.size() - 1; tokenQueue.pushForward(Collections.singletonList(matchedTokenList.get(last))); matchedTokenList.remove(last); return true; }
public boolean consume(TokenQueue tokenQueue, List<Statement> output) { List<Token> matchedTokenList = new ArrayList<Token>(); 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; }