PythonTreeMaker { public IfStatement ifStatement(AstNode astNode) { Token ifToken = toPyToken(astNode.getTokens().get(0)); AstNode condition = astNode.getFirstChild(PythonGrammar.NAMED_EXPR_TEST); Token colon = toPyToken(astNode.getFirstChild(PythonPunctuator.COLON).getToken()); AstNode suite = astNode.getFirstChild(PythonGrammar.SUITE); StatementList body = getStatementListFromSuite(suite); AstNode elseSuite = astNode.getLastChild(PythonGrammar.SUITE); ElseClause elseClause = null; if (elseSuite.getPreviousSibling().getPreviousSibling().is(PythonKeyword.ELSE)) { elseClause = elseClause(elseSuite); } List<IfStatement> elifBranches = astNode.getChildren(PythonKeyword.ELIF).stream() .map(this::elifStatement) .collect(Collectors.toList()); return new IfStatementImpl(ifToken, expression(condition), colon, suiteNewLine(suite), suiteIndent(suite), body, suiteDedent(suite), elifBranches, elseClause); }  FileInput fileInput(AstNode astNode); void setParents(Tree root); AnnotatedAssignment annotatedAssignment(StatementWithSeparator statementWithSeparator); PrintStatement printStatement(StatementWithSeparator statementWithSeparator); ExecStatement execStatement(StatementWithSeparator statementWithSeparator); AssertStatement assertStatement(StatementWithSeparator statementWithSeparator); PassStatement passStatement(StatementWithSeparator statementWithSeparator); DelStatement delStatement(StatementWithSeparator statementWithSeparator); ReturnStatement returnStatement(StatementWithSeparator statementWithSeparator); YieldStatement yieldStatement(StatementWithSeparator statementWithSeparator); YieldExpression yieldExpression(AstNode astNode); RaiseStatement raiseStatement(StatementWithSeparator statementWithSeparator); BreakStatement breakStatement(StatementWithSeparator statementWithSeparator); ContinueStatement continueStatement(StatementWithSeparator statementWithSeparator); ImportStatement importStatement(StatementWithSeparator statementWithSeparator); GlobalStatement globalStatement(StatementWithSeparator statementWithSeparator); NonlocalStatement nonlocalStatement(StatementWithSeparator statementWithSeparator); IfStatement ifStatement(AstNode astNode); FunctionDef funcDefStatement(AstNode astNode); ClassDef classDefStatement(AstNode astNode); ForStatement forStatement(AstNode astNode); WhileStatementImpl whileStatement(AstNode astNode); ExpressionStatement expressionStatement(StatementWithSeparator statementWithSeparator); AssignmentStatement assignment(StatementWithSeparator statementWithSeparator); CompoundAssignmentStatement compoundAssignment(StatementWithSeparator statementWithSeparator); TryStatement tryStatement(AstNode astNode); WithStatement withStatement(AstNode astNode); Expression expression(AstNode astNode); Argument argument(AstNode astNode); LambdaExpression lambdaExpression(AstNode astNode);  }
@Test public void IfStatement() { setRootRule(PythonGrammar.IF_STMT); IfStatement pyIfStatementTree = parse("if x: pass", treeMaker::ifStatement); assertThat(pyIfStatementTree.keyword().value()).isEqualTo("if"); assertThat(pyIfStatementTree.condition()).isInstanceOf(Expression.class); assertThat(pyIfStatementTree.isElif()).isFalse(); assertThat(pyIfStatementTree.elifBranches()).isEmpty(); assertThat(pyIfStatementTree.elseBranch()).isNull(); assertThat(pyIfStatementTree.body().statements()).hasSize(1); assertThat(pyIfStatementTree.children()).hasSize(4); pyIfStatementTree = parse("if x: pass\nelse: pass", treeMaker::ifStatement); assertThat(pyIfStatementTree.keyword().value()).isEqualTo("if"); assertThat(pyIfStatementTree.condition()).isInstanceOf(Expression.class); assertThat(pyIfStatementTree.isElif()).isFalse(); assertThat(pyIfStatementTree.elifBranches()).isEmpty(); ElseClause elseBranch = pyIfStatementTree.elseBranch(); assertThat(elseBranch.firstToken().value()).isEqualTo("else"); assertThat(elseBranch.lastToken().value()).isEqualTo("pass"); assertThat(elseBranch).isNotNull(); assertThat(elseBranch.elseKeyword().value()).isEqualTo("else"); assertThat(elseBranch.body().statements()).hasSize(1); assertThat(pyIfStatementTree.children()).hasSize(5); pyIfStatementTree = parse("if x: pass\nelif y: pass", treeMaker::ifStatement); assertThat(pyIfStatementTree.keyword().value()).isEqualTo("if"); assertThat(pyIfStatementTree.condition()).isInstanceOf(Expression.class); assertThat(pyIfStatementTree.isElif()).isFalse(); assertThat(pyIfStatementTree.elseBranch()).isNull(); assertThat(pyIfStatementTree.elifBranches()).hasSize(1); IfStatement elif = pyIfStatementTree.elifBranches().get(0); assertThat(elif.condition()).isInstanceOf(Expression.class); assertThat(elif.firstToken().value()).isEqualTo("elif"); assertThat(elif.lastToken().value()).isEqualTo("pass"); assertThat(elif.isElif()).isTrue(); assertThat(elif.elseBranch()).isNull(); assertThat(elif.elifBranches()).isEmpty(); assertThat(elif.body().statements()).hasSize(1); assertThat(pyIfStatementTree.children()).hasSize(5); pyIfStatementTree = parse("if x:\n pass", treeMaker::ifStatement); assertThat(pyIfStatementTree.keyword().value()).isEqualTo("if"); assertThat(pyIfStatementTree.condition()).isInstanceOf(Expression.class); assertThat(pyIfStatementTree.isElif()).isFalse(); assertThat(pyIfStatementTree.elseBranch()).isNull(); assertThat(pyIfStatementTree.elifBranches()).isEmpty(); assertThat(pyIfStatementTree.body().statements()).hasSize(1); pyIfStatementTree = parse("if x:\n pass\n pass", treeMaker::ifStatement); assertThat(pyIfStatementTree.body().statements()).hasSize(2); }