@Test public void should_create_tokens_and_trivias() { char[] input = "foo bar".toCharArray(); TokenExpression tokenMatcher = mockTokenMatcher(GenericTokenType.IDENTIFIER); TokenExpression triviaMatcher = mockTokenMatcher(GenericTokenType.COMMENT); MutableParsingRule ruleMatcher = mockRuleMatcher("rule"); AstNodeType realAstNodeType = mock(AstNodeType.class); when(ruleMatcher.getRealAstNodeType()).thenReturn(realAstNodeType); ParseNode triviaNode = new ParseNode(0, 4, Collections.<ParseNode>emptyList(), triviaMatcher); ParseNode tokenNode = new ParseNode(4, 7, Collections.<ParseNode>emptyList(), tokenMatcher); ParseNode parseTreeRoot = new ParseNode(0, 7, ImmutableList.of(triviaNode, tokenNode), ruleMatcher); InputBuffer inputBuffer = new ImmutableInputBuffer(input); ParsingResult parsingResult = new ParsingResult(inputBuffer, true, parseTreeRoot, null); AstNode astNode = AstCreator.create(parsingResult, new LocatedText(null, input)); System.out.println(AstXmlPrinter.print(astNode)); assertThat(astNode.getType()).isSameAs(realAstNodeType); assertThat(astNode.getName()).isEqualTo("rule"); assertThat(astNode.getFromIndex()).isEqualTo(0); assertThat(astNode.getToIndex()).isEqualTo(7); assertThat(astNode.hasChildren()).isTrue(); assertThat(astNode.getTokens()).hasSize(1); Token token = astNode.getTokens().get(0); assertThat(astNode.getToken()).isSameAs(token); assertThat(token.getValue()).isEqualTo("bar"); assertThat(token.getOriginalValue()).isEqualTo("bar"); assertThat(token.getLine()).isEqualTo(1); assertThat(token.getColumn()).isEqualTo(4); assertThat(token.getType()).isEqualTo(GenericTokenType.IDENTIFIER); assertThat(token.getTrivia()).hasSize(1); Trivia trivia = token.getTrivia().get(0); Token triviaToken = trivia.getToken(); assertThat(triviaToken.getValue()).isEqualTo("foo "); assertThat(triviaToken.getOriginalValue()).isEqualTo("foo "); assertThat(triviaToken.getLine()).isEqualTo(1); assertThat(triviaToken.getColumn()).isEqualTo(0); assertThat(triviaToken.getType()).isEqualTo(GenericTokenType.COMMENT); }
private boolean containsComment(AstNode suiteNode) { for (Token token : suiteNode.getTokens()) { for (Trivia trivia : token.getTrivia()) { if (trivia.isComment()) { return true; } } } return false; }
@Test public void should_create_tokens_without_TokenMatcher() { char[] input = "foobar".toCharArray(); ParseNode firstTerminal = new ParseNode(0, 3, Collections.<ParseNode>emptyList(), null); ParseNode secondTerminal = new ParseNode(3, 6, Collections.<ParseNode>emptyList(), null); MutableParsingRule ruleMatcher = mockRuleMatcher("rule"); AstNodeType realAstNodeType = mock(AstNodeType.class); when(ruleMatcher.getRealAstNodeType()).thenReturn(realAstNodeType); ParseNode parseTreeRoot = new ParseNode(0, 6, ImmutableList.of(firstTerminal, secondTerminal), ruleMatcher); InputBuffer inputBuffer = new ImmutableInputBuffer(input); ParsingResult parsingResult = new ParsingResult(inputBuffer, true, parseTreeRoot, null); AstNode astNode = AstCreator.create(parsingResult, new LocatedText(null, input)); System.out.println(AstXmlPrinter.print(astNode)); assertThat(astNode.getType()).isSameAs(realAstNodeType); assertThat(astNode.getName()).isEqualTo("rule"); assertThat(astNode.getFromIndex()).isEqualTo(0); assertThat(astNode.getToIndex()).isEqualTo(6); assertThat(astNode.hasChildren()).isTrue(); assertThat(astNode.getTokens()).hasSize(2); Token token = astNode.getTokens().get(0); assertThat(astNode.getToken()).isSameAs(token); assertThat(token.getValue()).isEqualTo("foo"); assertThat(token.getOriginalValue()).isEqualTo("foo"); assertThat(token.getLine()).isEqualTo(1); assertThat(token.getColumn()).isEqualTo(0); assertThat(token.getType()).isSameAs(AstCreator.UNDEFINED_TOKEN_TYPE); assertThat(token.getType().getName()).isEqualTo("TOKEN"); token = astNode.getTokens().get(1); assertThat(token.getValue()).isEqualTo("bar"); assertThat(token.getOriginalValue()).isEqualTo("bar"); assertThat(token.getLine()).isEqualTo(1); assertThat(token.getColumn()).isEqualTo(3); assertThat(token.getType()).isSameAs(AstCreator.UNDEFINED_TOKEN_TYPE); }
private static boolean isUselessSuperCall( AstNode node, String methodName, List<String> parameters) { StringBuilder sb = new StringBuilder(); for (Token token : node.getTokens()) { sb.append(token.getOriginalValue()); } String actual = sb.toString(); String expected = "super." + methodName + "(" + Joiner.on(',').join(parameters) + ");"; return actual.equals(expected) || actual.equals("return" + expected); }
private void checkAssignment(AstNode astNode) { AstNode memberExpr = astNode.getFirstChild(); AstNode variable = memberExpr.getFirstChild(); if (memberExpr.getNumberOfChildren() == 1 && variable.is(PHPGrammar.VARIABLE_WITHOUT_OBJECTS)) { String varName = variable.getTokenOriginalValue(); if (memberExpr.getTokens().size() == 1 && !CheckUtils.isSuperGlobal(varName) && !globalVariableNames.contains(varName)) { getContext().createLineViolation(this, "Move this variable into a class.", astNode); globalVariableNames.add(varName); } } }