Search in sources :

Example 66 with SourcePosition

use of com.google.javascript.jscomp.parsing.parser.util.SourcePosition in project closure-compiler by google.

the class Parser method parseInterfaceElement.

private ParseTree parseInterfaceElement() {
    SourcePosition start = getTreeStartLocation();
    boolean isGenerator = eatOpt(TokenType.STAR) != null;
    IdentifierToken name = null;
    TokenType type = peekType();
    if (type == TokenType.NEW) {
        // ConstructSignature
        return parseCallSignature(true);
    } else if (type == TokenType.IDENTIFIER || Keywords.isKeyword(type)) {
        name = eatIdOrKeywordAsId();
    } else if (type == TokenType.OPEN_SQUARE) {
        // IndexSignature
        return parseIndexSignature();
    } else if (type == TokenType.OPEN_ANGLE || type == TokenType.OPEN_PAREN) {
        // CallSignature
        return parseCallSignature(false);
    }
    boolean isOptional = false;
    if (peek(TokenType.QUESTION)) {
        eat(TokenType.QUESTION);
        isOptional = true;
    }
    if (peek(TokenType.OPEN_PAREN) || peek(TokenType.OPEN_ANGLE)) {
        // Method signature.
        ParseTree function = parseMethodSignature(start, name, false, isGenerator, isOptional, null);
        return function;
    } else {
        // Property signature.
        ParseTree declaredType = maybeParseColonType();
        return new MemberVariableTree(getTreeLocation(start), name, false, isOptional, null, declaredType);
    }
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree) ComputedPropertyMemberVariableTree(com.google.javascript.jscomp.parsing.parser.trees.ComputedPropertyMemberVariableTree) MemberVariableTree(com.google.javascript.jscomp.parsing.parser.trees.MemberVariableTree)

Example 67 with SourcePosition

use of com.google.javascript.jscomp.parsing.parser.util.SourcePosition in project closure-compiler by google.

the class Parser method parseNamespaceName.

private NamespaceNameTree parseNamespaceName() {
    SourcePosition start = getTreeStartLocation();
    IdentifierToken token = eatId();
    return new NamespaceNameTree(getTreeLocation(start), buildIdentifierPath(token));
}
Also used : NamespaceNameTree(com.google.javascript.jscomp.parsing.parser.trees.NamespaceNameTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition)

Example 68 with SourcePosition

use of com.google.javascript.jscomp.parsing.parser.util.SourcePosition in project closure-compiler by google.

the class Parser method parseType.

private ParseTree parseType() {
    SourcePosition start = getTreeStartLocation();
    if (!peekId() && !EnumSet.of(TokenType.VOID, TokenType.OPEN_PAREN, TokenType.OPEN_CURLY, TokenType.TYPEOF).contains(peekType())) {
        reportError("Unexpected token '%s' in type expression", peekType());
        return new TypeNameTree(getTreeLocation(start), ImmutableList.of("error"));
    }
    ParseTree typeExpression = parseFunctionTypeExpression();
    if (!peek(TokenType.BAR)) {
        return typeExpression;
    }
    ImmutableList.Builder<ParseTree> unionType = ImmutableList.builder();
    unionType.add(typeExpression);
    do {
        eat(TokenType.BAR);
        unionType.add(parseArrayTypeExpression());
    } while (peek(TokenType.BAR));
    return new UnionTypeTree(getTreeLocation(start), unionType.build());
}
Also used : TypeNameTree(com.google.javascript.jscomp.parsing.parser.trees.TypeNameTree) ImmutableList(com.google.common.collect.ImmutableList) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) UnionTypeTree(com.google.javascript.jscomp.parsing.parser.trees.UnionTypeTree) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 69 with SourcePosition

use of com.google.javascript.jscomp.parsing.parser.util.SourcePosition in project closure-compiler by google.

the class Parser method parseExportDeclaration.

/*
  ExportDeclaration :
    export * FromClause ;
    export ExportClause [NoReference] FromClause ;
    export ExportClause ;
    export VariableStatement
    export Declaration[Default]
    export default AssignmentExpression ;
  ExportClause [NoReference] :
    { }
    { ExportsList [?NoReference] }
    { ExportsList [?NoReference] , }
  ExportsList [NoReference] :
    ExportSpecifier [?NoReference]
    ExportsList [?NoReference] , ExportSpecifier [?NoReference]
  ExportSpecifier [NoReference] :
    [~NoReference] IdentifierReference
    [~NoReference] IdentifierReference as IdentifierName
    [+NoReference] IdentifierName
    [+NoReference] IdentifierName as IdentifierName
   */
private ParseTree parseExportDeclaration(boolean isAmbient) {
    SourcePosition start = getTreeStartLocation();
    boolean isDefault = false;
    boolean isExportAll = false;
    boolean isExportSpecifier = false;
    boolean needsSemiColon = true;
    eat(TokenType.EXPORT);
    ParseTree export = null;
    ImmutableList<ParseTree> exportSpecifierList = null;
    switch(peekType()) {
        case STAR:
            isExportAll = true;
            nextToken();
            break;
        case FUNCTION:
            // TODO(bradfordcsmith): handle async functions here
            export = isAmbient ? parseAmbientFunctionDeclaration() : parseFunctionDeclaration();
            needsSemiColon = isAmbient;
            break;
        case CLASS:
            export = parseClassDeclaration(isAmbient);
            needsSemiColon = false;
            break;
        case INTERFACE:
            export = parseInterfaceDeclaration();
            needsSemiColon = false;
            break;
        case ENUM:
            export = parseEnumDeclaration();
            needsSemiColon = false;
            break;
        case MODULE:
        case NAMESPACE:
            export = parseNamespaceDeclaration(isAmbient);
            needsSemiColon = false;
            break;
        case DECLARE:
            export = parseAmbientDeclaration();
            needsSemiColon = false;
            break;
        case DEFAULT:
            isDefault = true;
            nextToken();
            export = parseExpression();
            needsSemiColon = false;
            break;
        case OPEN_CURLY:
            isExportSpecifier = true;
            exportSpecifierList = parseExportSpecifierSet();
            break;
        case TYPE:
            export = parseTypeAlias();
            break;
        case VAR:
        case LET:
        case CONST:
        default:
            // unreachable, parse as a var decl to get a parse error.
            export = isAmbient ? parseAmbientVariableDeclarationList() : parseVariableDeclarationList();
            break;
    }
    LiteralToken moduleSpecifier = null;
    if (isExportAll || (isExportSpecifier && peekPredefinedString(PredefinedName.FROM))) {
        eatPredefinedString(PredefinedName.FROM);
        moduleSpecifier = (LiteralToken) eat(TokenType.STRING);
    } else if (isExportSpecifier) {
        for (ParseTree tree : exportSpecifierList) {
            IdentifierToken importedName = tree.asExportSpecifier().importedName;
            if (isKeyword(importedName.value)) {
                reportError(importedName, "cannot use keyword '%s' here.", importedName.value);
            }
        }
    }
    if (needsSemiColon || peekImplicitSemiColon()) {
        eatPossibleImplicitSemiColon();
    }
    return new ExportDeclarationTree(getTreeLocation(start), isDefault, isExportAll, export, exportSpecifierList, moduleSpecifier);
}
Also used : ExportDeclarationTree(com.google.javascript.jscomp.parsing.parser.trees.ExportDeclarationTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 70 with SourcePosition

use of com.google.javascript.jscomp.parsing.parser.util.SourcePosition in project closure-compiler by google.

the class CodeGenerator method addNonJsDoc_nonTrailing.

private void addNonJsDoc_nonTrailing(Node node, NonJSDocComment nonJSDocComment) {
    String content = nonJSDocComment.getCommentString();
    SourcePosition commentEndPosition = nonJSDocComment.getEndPosition();
    // source lines are 1-indexed
    int nodeLineNumber = node.getLineno() - 1;
    if (nonJSDocComment.isEndingAsLineComment()) {
        // Non trailing line comments can not be on the same line as the node.
        checkState(commentEndPosition.line < nodeLineNumber, "Non trailing line comments can not be on the same line as the node.");
        add(content + "\n");
    } else {
        if (nodeLineNumber == commentEndPosition.line) {
            // e.g. ``` /* comment */ let x; ```
            add(content + " ");
        } else {
            // e.g.
            // ```
            // /* comment */
            // let x;
            // ```
            add(content + "\n");
        }
    }
}
Also used : SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition)

Aggregations

SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)112 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)81 ImmutableList (com.google.common.collect.ImmutableList)15 BinaryOperatorTree (com.google.javascript.jscomp.parsing.parser.trees.BinaryOperatorTree)13 FormalParameterListTree (com.google.javascript.jscomp.parsing.parser.trees.FormalParameterListTree)7 FunctionDeclarationTree (com.google.javascript.jscomp.parsing.parser.trees.FunctionDeclarationTree)6 DefaultParameterTree (com.google.javascript.jscomp.parsing.parser.trees.DefaultParameterTree)5 ArgumentListTree (com.google.javascript.jscomp.parsing.parser.trees.ArgumentListTree)4 BlockTree (com.google.javascript.jscomp.parsing.parser.trees.BlockTree)4 IdentifierExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.IdentifierExpressionTree)4 MissingPrimaryExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.MissingPrimaryExpressionTree)4 OptionalMemberExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.OptionalMemberExpressionTree)4 OptionalMemberLookupExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.OptionalMemberLookupExpressionTree)4 ArrayLiteralExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.ArrayLiteralExpressionTree)3 GenericTypeListTree (com.google.javascript.jscomp.parsing.parser.trees.GenericTypeListTree)3 ObjectLiteralExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.ObjectLiteralExpressionTree)3 OptChainCallExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.OptChainCallExpressionTree)3 TemplateLiteralExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.TemplateLiteralExpressionTree)3 VariableDeclarationTree (com.google.javascript.jscomp.parsing.parser.trees.VariableDeclarationTree)3 FormatString (com.google.errorprone.annotations.FormatString)2