PythonTreeMaker { private Expression binaryExpression(AstNode astNode) { List<AstNode> children = astNode.getChildren(); Expression result = expression(children.get(0)); for (int i = 1; i < astNode.getNumberOfChildren(); i += 2) { AstNode operator = children.get(i); Expression rightOperand = expression(operator.getNextSibling()); AstNode not = operator.getFirstChild(PythonKeyword.NOT); Token notToken = not == null ? null : toPyToken(not.getToken()); if (PythonKeyword.IN.equals(operator.getLastToken().getType())) { result = new InExpressionImpl(result, notToken, toPyToken(operator.getLastToken()), rightOperand); } else if (PythonKeyword.IS.equals(operator.getToken().getType())) { result = new IsExpressionImpl(result, toPyToken(operator.getToken()), notToken, rightOperand); } else { result = new BinaryExpressionImpl(result, toPyToken(operator.getToken()), rightOperand); } } return result; }  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 binary_expressions() { setRootRule(PythonGrammar.TEST); BinaryExpression simplePlus = binaryExpression("a + b"); assertThat(simplePlus.leftOperand()).isInstanceOf(Name.class); assertThat(simplePlus.operator().value()).isEqualTo("+"); assertThat(simplePlus.rightOperand()).isInstanceOf(Name.class); assertThat(simplePlus.getKind()).isEqualTo(Tree.Kind.PLUS); assertThat(simplePlus.children()).hasSize(3); BinaryExpression compoundPlus = binaryExpression("a + b - c"); assertThat(compoundPlus.leftOperand()).isInstanceOf(BinaryExpression.class); assertThat(compoundPlus.children()).hasSize(3); assertThat(compoundPlus.operator().value()).isEqualTo("-"); assertThat(compoundPlus.rightOperand()).isInstanceOf(Name.class); assertThat(compoundPlus.getKind()).isEqualTo(Tree.Kind.MINUS); BinaryExpression compoundPlusLeft = (BinaryExpression) compoundPlus.leftOperand(); assertThat(compoundPlusLeft.operator().value()).isEqualTo("+"); assertThat(binaryExpression("a * b").getKind()).isEqualTo(Tree.Kind.MULTIPLICATION); assertThat(binaryExpression("a / b").getKind()).isEqualTo(Tree.Kind.DIVISION); assertThat(binaryExpression("a assertThat(binaryExpression("a % b").getKind()).isEqualTo(Tree.Kind.MODULO); assertThat(binaryExpression("a @ b").getKind()).isEqualTo(Tree.Kind.MATRIX_MULTIPLICATION); assertThat(binaryExpression("a >> b").getKind()).isEqualTo(Tree.Kind.SHIFT_EXPR); assertThat(binaryExpression("a << b").getKind()).isEqualTo(Tree.Kind.SHIFT_EXPR); assertThat(binaryExpression("a & b").getKind()).isEqualTo(Tree.Kind.BITWISE_AND); assertThat(binaryExpression("a | b").getKind()).isEqualTo(Tree.Kind.BITWISE_OR); assertThat(binaryExpression("a ^ b").getKind()).isEqualTo(Tree.Kind.BITWISE_XOR); assertThat(binaryExpression("a ** b").getKind()).isEqualTo(Tree.Kind.POWER); assertThat(binaryExpression("a == b").getKind()).isEqualTo(Tree.Kind.COMPARISON); assertThat(binaryExpression("a >= b").getKind()).isEqualTo(Tree.Kind.COMPARISON); assertThat(binaryExpression("a <= b").getKind()).isEqualTo(Tree.Kind.COMPARISON); assertThat(binaryExpression("a > b").getKind()).isEqualTo(Tree.Kind.COMPARISON); assertThat(binaryExpression("a < b").getKind()).isEqualTo(Tree.Kind.COMPARISON); assertThat(binaryExpression("a != b").getKind()).isEqualTo(Tree.Kind.COMPARISON); assertThat(binaryExpression("a <> b").getKind()).isEqualTo(Tree.Kind.COMPARISON); assertThat(binaryExpression("a and b").getKind()).isEqualTo(Tree.Kind.AND); assertThat(binaryExpression("a or b").getKind()).isEqualTo(Tree.Kind.OR); }
@Test public void in_expressions() { setRootRule(PythonGrammar.TEST); InExpression in = (InExpression) binaryExpression("1 in [a]"); assertThat(in.getKind()).isEqualTo(Tree.Kind.IN); assertThat(in.operator().value()).isEqualTo("in"); assertThat(in.leftOperand().getKind()).isEqualTo(Tree.Kind.NUMERIC_LITERAL); assertThat(in.rightOperand().getKind()).isEqualTo(Tree.Kind.LIST_LITERAL); assertThat(in.notToken()).isNull(); InExpression notIn = (InExpression) binaryExpression("1 not in [a]"); assertThat(notIn.getKind()).isEqualTo(Tree.Kind.IN); assertThat(notIn.operator().value()).isEqualTo("in"); assertThat(notIn.notToken()).isNotNull(); assertThat(notIn.children()).containsExactly(notIn.leftOperand(), notIn.notToken(), notIn.operator(), notIn.rightOperand()); }
@Test public void is_expressions() { setRootRule(PythonGrammar.TEST); IsExpression is = (IsExpression) binaryExpression("a is 1"); assertThat(is.getKind()).isEqualTo(Tree.Kind.IS); assertThat(is.operator().value()).isEqualTo("is"); assertThat(is.leftOperand().getKind()).isEqualTo(Tree.Kind.NAME); assertThat(is.rightOperand().getKind()).isEqualTo(Tree.Kind.NUMERIC_LITERAL); assertThat(is.notToken()).isNull(); IsExpression notIs = (IsExpression) binaryExpression("a is not 1"); assertThat(notIs.getKind()).isEqualTo(Tree.Kind.IS); assertThat(notIs.operator().value()).isEqualTo("is"); assertThat(notIs.notToken()).isNotNull(); assertThat(notIs.children()).containsExactly(notIs.leftOperand(), notIs.operator(), notIs.notToken(), notIs.rightOperand()); }