Search in sources :

Example 1 with ImportDeclarationTree

use of com.google.javascript.jscomp.parsing.parser.trees.ImportDeclarationTree in project closure-compiler by google.

the class Parser method parseImportDeclaration.

private ParseTree parseImportDeclaration() {
    SourcePosition start = getTreeStartLocation();
    eat(TokenType.IMPORT);
    // import ModuleSpecifier ;
    if (peek(TokenType.STRING)) {
        LiteralToken moduleSpecifier = eat(TokenType.STRING).asLiteral();
        eatPossiblyImplicitSemiColon();
        return new ImportDeclarationTree(getTreeLocation(start), null, null, null, moduleSpecifier);
    }
    // import ImportedDefaultBinding from ModuleSpecifier
    // import NameSpaceImport from ModuleSpecifier
    // import NamedImports from ModuleSpecifier ;
    // import ImportedDefaultBinding , NameSpaceImport from ModuleSpecifier ;
    // import ImportedDefaultBinding , NamedImports from ModuleSpecifier ;
    IdentifierToken defaultBindingIdentifier = null;
    IdentifierToken nameSpaceImportIdentifier = null;
    ImmutableList<ParseTree> identifierSet = null;
    boolean parseExplicitNames = true;
    if (peekId()) {
        defaultBindingIdentifier = eatId();
        if (peek(TokenType.COMMA)) {
            eat(TokenType.COMMA);
        } else {
            parseExplicitNames = false;
        }
    } else if (Keywords.isKeyword(peekType())) {
        Token keyword = nextToken();
        reportError(keyword, "cannot use keyword '%s' here.", keyword);
    }
    if (parseExplicitNames) {
        if (peek(TokenType.STAR)) {
            eat(TokenType.STAR);
            eatPredefinedString(PredefinedName.AS);
            nameSpaceImportIdentifier = eatId();
        } else {
            identifierSet = parseImportSpecifierSet();
        }
    }
    eatPredefinedString(PredefinedName.FROM);
    Token moduleStr = eat(TokenType.STRING);
    LiteralToken moduleSpecifier = (moduleStr == null) ? null : moduleStr.asLiteral();
    eatPossiblyImplicitSemiColon();
    return new ImportDeclarationTree(getTreeLocation(start), defaultBindingIdentifier, identifierSet, nameSpaceImportIdentifier, moduleSpecifier);
}
Also used : ImportDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.ImportDeclarationTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Aggregations

ImportDeclarationTree (com.google.javascript.jscomp.parsing.parser.trees.ImportDeclarationTree)1 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)1 SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)1