PythonTreeMaker { private static ImportFrom importFromStatement(AstNode astNode, Separators separators) { Token importKeyword = toPyToken(astNode.getFirstChild(PythonKeyword.IMPORT).getToken()); Token fromKeyword = toPyToken(astNode.getFirstChild(PythonKeyword.FROM).getToken()); List<Token> dottedPrefixForModule = punctuators(astNode, PythonPunctuator.DOT); AstNode moduleNode = astNode.getFirstChild(PythonGrammar.DOTTED_NAME); DottedName moduleName = null; if (moduleNode != null) { moduleName = dottedName(moduleNode); } AstNode importAsnames = astNode.getFirstChild(PythonGrammar.IMPORT_AS_NAMES); List<AliasedName> aliasedImportNames = null; boolean isWildcardImport = true; if (importAsnames != null) { aliasedImportNames = importAsnames.getChildren(PythonGrammar.IMPORT_AS_NAME).stream() .map(PythonTreeMaker::aliasedName) .collect(Collectors.toList()); isWildcardImport = false; } Token wildcard = null; if (isWildcardImport) { wildcard = toPyToken(astNode.getFirstChild(PythonPunctuator.MUL).getToken()); } return new ImportFromImpl(fromKeyword, dottedPrefixForModule, moduleName, importKeyword, aliasedImportNames, wildcard, separators); }  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 importFromStatement() { setRootRule(PythonGrammar.IMPORT_STMT); AstNode astNode = p.parse("from foo import f"); StatementWithSeparator statementWithSeparator = new StatementWithSeparator(astNode, null); ImportFrom importStatement = (ImportFrom) treeMaker.importStatement(statementWithSeparator); assertThat(importStatement).isNotNull(); assertThat(importStatement.firstToken().value()).isEqualTo("from"); assertThat(importStatement.lastToken().value()).isEqualTo("f"); assertThat(importStatement.importKeyword().value()).isEqualTo("import"); assertThat(importStatement.dottedPrefixForModule()).isEmpty(); assertThat(importStatement.fromKeyword().value()).isEqualTo("from"); assertThat(importStatement.module().names().get(0).name()).isEqualTo("foo"); assertThat(importStatement.isWildcardImport()).isFalse(); assertThat(importStatement.wildcard()).isNull(); assertThat(importStatement.importedNames()).hasSize(1); AliasedName aliasedNameTree = importStatement.importedNames().get(0); assertThat(aliasedNameTree.asKeyword()).isNull(); assertThat(aliasedNameTree.alias()).isNull(); assertThat(aliasedNameTree.dottedName().names().get(0).name()).isEqualTo("f"); assertThat(importStatement.children()).hasSize(4); assertThat(importStatement.children()).containsExactly(importStatement.fromKeyword(), importStatement.module(), importStatement.importKeyword(), aliasedNameTree); astNode = p.parse("from .foo import f"); statementWithSeparator = new StatementWithSeparator(astNode, null); importStatement = (ImportFrom) treeMaker.importStatement(statementWithSeparator); assertThat(importStatement.dottedPrefixForModule()).hasSize(1); assertThat(importStatement.dottedPrefixForModule().get(0).value()).isEqualTo("."); assertThat(importStatement.module().names().get(0).name()).isEqualTo("foo"); aliasedNameTree = importStatement.importedNames().get(0); assertThat(importStatement.children()).hasSize(5); assertThat(importStatement.children()).containsExactly(importStatement.fromKeyword(), importStatement.dottedPrefixForModule().get(0), importStatement.module(), importStatement.importKeyword(), aliasedNameTree); astNode = p.parse("from ..foo import f"); statementWithSeparator = new StatementWithSeparator(astNode, null); importStatement = (ImportFrom) treeMaker.importStatement(statementWithSeparator); assertThat(importStatement.dottedPrefixForModule()).hasSize(2); assertThat(importStatement.dottedPrefixForModule().get(0).value()).isEqualTo("."); assertThat(importStatement.dottedPrefixForModule().get(1).value()).isEqualTo("."); assertThat(importStatement.module().names().get(0).name()).isEqualTo("foo"); assertThat(importStatement.children()).hasSize(6); astNode = p.parse("from . import f"); statementWithSeparator = new StatementWithSeparator(astNode, null); importStatement = (ImportFrom) treeMaker.importStatement(statementWithSeparator); assertThat(importStatement.dottedPrefixForModule()).hasSize(1); assertThat(importStatement.dottedPrefixForModule().get(0).value()).isEqualTo("."); assertThat(importStatement.module()).isNull(); assertThat(importStatement.children()).hasSize(4); astNode = p.parse("from foo import f as g"); statementWithSeparator = new StatementWithSeparator(astNode, null); importStatement = (ImportFrom) treeMaker.importStatement(statementWithSeparator); assertThat(importStatement.importedNames()).hasSize(1); aliasedNameTree = importStatement.importedNames().get(0); assertThat(aliasedNameTree.asKeyword().value()).isEqualTo("as"); assertThat(aliasedNameTree.alias().name()).isEqualTo("g"); assertThat(aliasedNameTree.dottedName().names().get(0).name()).isEqualTo("f"); assertThat(importStatement.children()).hasSize(4); astNode = p.parse("from foo import f as g, h"); statementWithSeparator = new StatementWithSeparator(astNode, null); importStatement = (ImportFrom) treeMaker.importStatement(statementWithSeparator); assertThat(importStatement.importedNames()).hasSize(2); AliasedName aliasedNameTree1 = importStatement.importedNames().get(0); assertThat(aliasedNameTree1.asKeyword().value()).isEqualTo("as"); assertThat(aliasedNameTree1.alias().name()).isEqualTo("g"); assertThat(aliasedNameTree1.dottedName().names().get(0).name()).isEqualTo("f"); assertThat(importStatement.children()).hasSize(5); List<Tree> aliasedNameChildren = aliasedNameTree1.children(); assertThat(aliasedNameChildren).hasSize(3); assertThat(aliasedNameChildren).containsExactly(aliasedNameTree1.dottedName(), aliasedNameTree1.asKeyword(), aliasedNameTree1.alias()); AliasedName aliasedNameTree2 = importStatement.importedNames().get(1); assertThat(aliasedNameTree2.asKeyword()).isNull(); assertThat(aliasedNameTree2.alias()).isNull(); assertThat(aliasedNameTree2.dottedName().names().get(0).name()).isEqualTo("h"); astNode = p.parse("from foo import *"); statementWithSeparator = new StatementWithSeparator(astNode, null); importStatement = (ImportFrom) treeMaker.importStatement(statementWithSeparator); assertThat(importStatement.importedNames()).isEmpty(); assertThat(importStatement.isWildcardImport()).isTrue(); assertThat(importStatement.wildcard().value()).isEqualTo("*"); assertThat(importStatement.children()).hasSize(4); assertThat(importStatement.children()).containsExactly(importStatement.fromKeyword(), importStatement.module(), importStatement.importKeyword(), importStatement.wildcard()); }