/** * Assert that the actual token has the same type and lexeme as the expected token. Note that this * does not assert anything about the offsets of the tokens (although the lengths will be equal). * * @param expectedToken the token that was expected * @param actualToken the token that was found * @throws AssertionFailedError if the two tokens are not the same */ public static void assertMatches(Token expectedToken, Token actualToken) { assertEquals(expectedToken.getType(), actualToken.getType()); if (expectedToken instanceof KeywordToken) { assertInstanceOf(KeywordToken.class, actualToken); assertEquals( ((KeywordToken) expectedToken).getKeyword(), ((KeywordToken) actualToken).getKeyword()); } else if (expectedToken instanceof StringToken) { assertInstanceOf(StringToken.class, actualToken); assertEquals( ((StringToken) expectedToken).getLexeme(), ((StringToken) actualToken).getLexeme()); } }
/** * Assert that the tokens in the actual stream of tokens have the same types and lexemes as the * tokens in the expected stream of tokens. Note that this does not assert anything about the * offsets of the tokens (although the lengths will be equal). * * @param expectedStream the head of the stream of tokens that were expected * @param actualStream the head of the stream of tokens that were actually found * @throws AssertionFailedError if the two streams of tokens are not the same */ public static void assertAllMatch(Token expectedStream, Token actualStream) { Token left = expectedStream; Token right = actualStream; while (left.getType() != TokenType.EOF && right.getType() != TokenType.EOF) { assertMatches(left, right); left = left.getNext(); right = right.getNext(); } }
/** * Return {@code true} if this expression is cascaded. If it is, then the target of this * expression is not stored locally but is stored in the nearest ancestor that is a {@link * CascadeExpression}. * * @return {@code true} if this expression is cascaded */ public boolean isCascaded() { return period != null && period.getType() == TokenType.PERIOD_PERIOD; }
/** * Safely visit the given node, printing the suffix after the node if it is non-{@code null}. * * @param suffix the suffix to be printed if there is a node to visit * @param node the node to be visited */ private void visit(Token token, String suffix) { if (token != null) { writer.print(token.getLexeme()); writer.print(suffix); } }
private boolean matches(Token token, Keyword keyword) { return token != null && token.getType() == TokenType.KEYWORD && ((KeywordToken) token).getKeyword() == keyword; }