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; }
/** * Creates list of statements from Strings, each statement on a new line starting from 0. */ protected static List<Statement> createStatementsFromStrings(String... values) { List<Statement> result = new ArrayList<>(); for (int i = 0; i < values.length; i++) { result.add(new Statement(i, i, values[i])); } return result; }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptEmpty() { new Statement(new ArrayList<Token>()); }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNull() { new Statement(null); }
@Test public void shouldCreateStatementFromListOfTokens() { Statement statement = new Statement(Arrays.asList(new Token("a", 1, 1), new Token("b", 2, 1))); assertThat(statement.getValue(), is("ab")); assertThat(statement.getStartLine(), is(1)); assertThat(statement.getEndLine(), is(2)); }
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; }