PythonTreeMaker { private static Name name(AstNode astNode) { return new NameImpl(toPyToken(astNode.getFirstChild(GenericTokenType.IDENTIFIER).getToken()), astNode.getParent().is(PythonGrammar.ATOM)); }  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 attributeRef_expression() { setRootRule(PythonGrammar.TEST); QualifiedExpression qualifiedExpression = (QualifiedExpression) parse("foo.bar", treeMaker::expression); assertThat(qualifiedExpression.name().name()).isEqualTo("bar"); Expression qualifier = qualifiedExpression.qualifier(); assertThat(qualifier).isInstanceOf(Name.class); assertThat(((Name) qualifier).name()).isEqualTo("foo"); assertThat(qualifiedExpression.children()).hasSize(3); assertThat(((Name) qualifiedExpression.children().get(0)).name()).isEqualTo("foo"); assertThat(((Token) qualifiedExpression.children().get(1)).type()).isEqualTo(PythonPunctuator.DOT); assertThat(((Name) qualifiedExpression.children().get(2)).name()).isEqualTo("bar"); qualifiedExpression = (QualifiedExpression) parse("foo.bar.baz", treeMaker::expression); assertThat(qualifiedExpression.name().name()).isEqualTo("baz"); assertThat(qualifiedExpression.firstToken().value()).isEqualTo("foo"); assertThat(qualifiedExpression.lastToken().value()).isEqualTo("baz"); assertThat(qualifiedExpression.qualifier()).isInstanceOf(QualifiedExpression.class); QualifiedExpression qualExpr = (QualifiedExpression) qualifiedExpression.qualifier(); assertThat(qualExpr.name().name()).isEqualTo("bar"); assertThat(qualExpr.firstToken().value()).isEqualTo("foo"); assertThat(qualExpr.lastToken().value()).isEqualTo("bar"); assertThat(qualExpr.qualifier()).isInstanceOf(Name.class); Name name = (Name) qualExpr.qualifier(); assertThat(name.name()).isEqualTo("foo"); assertThat(qualifiedExpression.children()).hasSize(3); }
@Test public void subscription_expressions() { setRootRule(PythonGrammar.TEST); SubscriptionExpression expr = (SubscriptionExpression) parse("x[a]", treeMaker::expression); assertThat(expr.getKind()).isEqualTo(Tree.Kind.SUBSCRIPTION); assertThat(((Name) expr.object()).name()).isEqualTo("x"); assertThat(((Name) expr.subscripts().expressions().get(0)).name()).isEqualTo("a"); assertThat(expr.leftBracket().value()).isEqualTo("["); assertThat(expr.rightBracket().value()).isEqualTo("]"); assertThat(expr.children()).hasSize(4); SubscriptionExpression multipleSubscripts = (SubscriptionExpression) parse("x[a, 42]", treeMaker::expression); ExpressionList subscripts = multipleSubscripts.subscripts(); assertThat(subscripts.children()).hasSize(3); assertThat(subscripts.children().get(1)).isSameAs(subscripts.commas().get(0)); assertThat(subscripts.expressions()).extracting(Tree::getKind) .containsExactly(Tree.Kind.NAME, Tree.Kind.NUMERIC_LITERAL); }
@Test public void slice() { setRootRule(PythonGrammar.SUBSCRIPT); SliceItem slice = parse("a:b:c", treeMaker::sliceItem); assertThat(((Name) slice.lowerBound()).name()).isEqualTo("a"); assertThat(((Name) slice.upperBound()).name()).isEqualTo("b"); assertThat(((Name) slice.stride()).name()).isEqualTo("c"); assertThat(slice.boundSeparator().value()).isEqualTo(":"); assertThat(slice.strideSeparator().value()).isEqualTo(":"); assertThat(slice.children()).hasSize(5); SliceItem trivial = parse(":", treeMaker::sliceItem); assertThat(trivial.lowerBound()).isNull(); assertThat(trivial.upperBound()).isNull(); assertThat(trivial.stride()).isNull(); assertThat(trivial.strideSeparator()).isNull(); SliceItem lowerBoundOnly = parse("a:", treeMaker::sliceItem); assertThat(((Name) lowerBoundOnly.lowerBound()).name()).isEqualTo("a"); assertThat(lowerBoundOnly.upperBound()).isNull(); assertThat(lowerBoundOnly.stride()).isNull(); assertThat(lowerBoundOnly.strideSeparator()).isNull(); SliceItem upperBoundOnly = parse(":a", treeMaker::sliceItem); assertThat(upperBoundOnly.lowerBound()).isNull(); assertThat(((Name) upperBoundOnly.upperBound()).name()).isEqualTo("a"); assertThat(upperBoundOnly.stride()).isNull(); assertThat(upperBoundOnly.strideSeparator()).isNull(); SliceItem strideOnly = parse("::a", treeMaker::sliceItem); assertThat(strideOnly.lowerBound()).isNull(); assertThat(strideOnly.upperBound()).isNull(); assertThat(((Name) strideOnly.stride()).name()).isEqualTo("a"); assertThat(strideOnly.strideSeparator()).isNotNull(); SliceItem strideContainingOnlyColon = parse("::", treeMaker::sliceItem); assertThat(strideContainingOnlyColon.lowerBound()).isNull(); assertThat(strideContainingOnlyColon.upperBound()).isNull(); assertThat(strideContainingOnlyColon.strideSeparator()).isNotNull(); }
@Test public void variables() { setRootRule(PythonGrammar.EXPR); Name name = (Name) parse("foo", treeMaker::expression); assertThat(name.isVariable()).isTrue(); QualifiedExpression qualifiedExpressionTree = (QualifiedExpression) parse("a.b", treeMaker::expression); assertThat(qualifiedExpressionTree.name().isVariable()).isFalse(); setRootRule(PythonGrammar.FUNCDEF); FunctionDef functionDefTree = parse("def func(x): pass", treeMaker::funcDefStatement); assertThat(functionDefTree.name().isVariable()).isFalse(); }