Search in sources :

Example 1 with SourceRange

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

the class Scanner method skipSingleLineComment.

private void skipSingleLineComment(Comment.Type type) {
    int startOffset = index;
    while (!isAtEnd() && !isLineTerminator(peekChar())) {
        nextChar();
    }
    SourceRange range = lineNumberScanner.getSourceRange(startOffset, index);
    String value = this.contents.substring(startOffset, index);
    recordComment(type, range, value);
}
Also used : FormatString(com.google.errorprone.annotations.FormatString) SourceRange(com.google.javascript.jscomp.parsing.parser.util.SourceRange)

Example 2 with SourceRange

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

the class Scanner method skipMultiLineComment.

private void skipMultiLineComment() {
    int startOffset = index;
    // '/'
    nextChar();
    // '*'
    nextChar();
    while (!isAtEnd() && (peekChar() != '*' || peekChar(1) != '/')) {
        nextChar();
    }
    if (!isAtEnd()) {
        nextChar();
        nextChar();
        Comment.Type type = Comment.Type.BLOCK;
        if (index - startOffset > 4) {
            if (this.contents.charAt(startOffset + 2) == '*') {
                type = Comment.Type.JSDOC;
            } else if (this.contents.charAt(startOffset + 2) == '!') {
                type = Comment.Type.IMPORTANT;
            }
        }
        SourceRange range = lineNumberScanner.getSourceRange(startOffset, index);
        String value = this.contents.substring(startOffset, index);
        recordComment(type, range, value);
    } else {
        reportError("unterminated comment");
    }
}
Also used : Comment(com.google.javascript.jscomp.parsing.parser.trees.Comment) FormatString(com.google.errorprone.annotations.FormatString) SourceRange(com.google.javascript.jscomp.parsing.parser.util.SourceRange)

Example 3 with SourceRange

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

the class Parser method parseCatch.

private CatchTree parseCatch() {
    SourcePosition start = getTreeStartLocation();
    CatchTree catchBlock;
    eat(TokenType.CATCH);
    ParseTree exception = new EmptyStatementTree(new SourceRange(getTreeEndLocation(), getTreeStartLocation()));
    if (peekToken().type == TokenType.OPEN_PAREN) {
        eat(TokenType.OPEN_PAREN);
        if (peekPatternStart()) {
            exception = parsePattern(PatternKind.INITIALIZER);
        } else {
            exception = parseIdentifierExpression();
        }
        eat(TokenType.CLOSE_PAREN);
    } else {
        recordFeatureUsed(Feature.OPTIONAL_CATCH_BINDING);
    }
    BlockTree catchBody = parseBlock();
    catchBlock = new CatchTree(getTreeLocation(start), exception, catchBody);
    return catchBlock;
}
Also used : EmptyStatementTree(com.google.javascript.jscomp.parsing.parser.trees.EmptyStatementTree) CatchTree(com.google.javascript.jscomp.parsing.parser.trees.CatchTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) BlockTree(com.google.javascript.jscomp.parsing.parser.trees.BlockTree) SourceRange(com.google.javascript.jscomp.parsing.parser.util.SourceRange) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Example 4 with SourceRange

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

the class IRFactory method normalizeNumber.

double normalizeNumber(LiteralToken token) {
    String value = token.value;
    if (value.contains("_")) {
        value = removeNumericSeparators(value, token);
    }
    SourceRange location = token.location;
    int length = value.length();
    checkState(length > 0);
    checkState(value.charAt(0) != '-' && value.charAt(0) != '+');
    if (value.charAt(0) == '.') {
        return Double.parseDouble('0' + value);
    } else if (value.charAt(0) == '0' && length > 1) {
        switch(value.charAt(1)) {
            case '.':
            case 'e':
            case 'E':
                return Double.parseDouble(value);
            case 'b':
            case 'B':
                {
                    maybeWarnForFeature(token, Feature.BINARY_LITERALS);
                    double v = 0;
                    int c = 1;
                    while (++c < length) {
                        v = (v * 2) + binarydigit(value.charAt(c));
                    }
                    return v;
                }
            case 'o':
            case 'O':
                {
                    maybeWarnForFeature(token, Feature.OCTAL_LITERALS);
                    double v = 0;
                    int c = 1;
                    while (++c < length) {
                        v = (v * 8) + octaldigit(value.charAt(c));
                    }
                    return v;
                }
            case 'x':
            case 'X':
                {
                    double v = 0;
                    int c = 1;
                    while (++c < length) {
                        v = (v * 0x10) + hexdigit(value.charAt(c));
                    }
                    return v;
                }
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
                double v = 0;
                int c = 0;
                while (++c < length) {
                    char digit = value.charAt(c);
                    if (isOctalDigit(digit)) {
                        v = (v * 8) + octaldigit(digit);
                    } else {
                        errorReporter.error(INVALID_OCTAL_DIGIT, sourceName, lineno(location.start), charno(location.start));
                        return 0;
                    }
                }
                if (inStrictContext()) {
                    errorReporter.error(INVALID_ES5_STRICT_OCTAL, sourceName, lineno(location.start), charno(location.start));
                } else {
                    errorReporter.warning(INVALID_ES5_STRICT_OCTAL, sourceName, lineno(location.start), charno(location.start));
                }
                return v;
            case '8':
            case '9':
                errorReporter.error(INVALID_OCTAL_DIGIT, sourceName, lineno(location.start), charno(location.start));
                return 0;
            default:
                throw new IllegalStateException("Unexpected character in number literal: " + value.charAt(1));
        }
    } else {
        return Double.parseDouble(value);
    }
}
Also used : SourceRange(com.google.javascript.jscomp.parsing.parser.util.SourceRange)

Example 5 with SourceRange

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

the class Parser method parseParameter.

private ParseTree parseParameter(ParamContext context) {
    SourcePosition start = getTreeStartLocation();
    ParseTree parameter = null;
    if (peek(TokenType.SPREAD)) {
        parameter = parseRestParameter();
    } else if (peekId()) {
        parameter = parseIdentifierExpression();
        if (peek(TokenType.QUESTION)) {
            eat(TokenType.QUESTION);
            parameter = new OptionalParameterTree(getTreeLocation(start), parameter);
        }
    } else if (context != ParamContext.TYPE_EXPRESSION && peekPatternStart()) {
        parameter = parsePattern(PatternKind.INITIALIZER);
    } else {
        throw new IllegalStateException("parseParameterCalled() without confirming a parameter exists.");
    }
    ParseTree typeAnnotation = null;
    SourceRange typeLocation = null;
    if (peek(TokenType.COLON)) {
        if (peek(1, TokenType.STRING)) {
            eat(TokenType.COLON);
            // Specialized Signature
            typeAnnotation = parseLiteralExpression();
        } else {
            typeAnnotation = parseTypeAnnotation();
        }
        typeLocation = getTreeLocation(getTreeStartLocation());
    }
    if (context == ParamContext.IMPLEMENTATION && !parameter.isRestParameter() && peek(TokenType.EQUAL)) {
        eat(TokenType.EQUAL);
        ParseTree defaultValue = parseAssignmentExpression();
        parameter = new DefaultParameterTree(getTreeLocation(start), parameter, defaultValue);
    }
    if (typeAnnotation != null) {
        // Must be a direct child of the parameter list.
        parameter = new TypedParameterTree(typeLocation, parameter, typeAnnotation);
    }
    return parameter;
}
Also used : OptionalParameterTree(com.google.javascript.jscomp.parsing.parser.trees.OptionalParameterTree) TypedParameterTree(com.google.javascript.jscomp.parsing.parser.trees.TypedParameterTree) SourcePosition(com.google.javascript.jscomp.parsing.parser.util.SourcePosition) DefaultParameterTree(com.google.javascript.jscomp.parsing.parser.trees.DefaultParameterTree) SourceRange(com.google.javascript.jscomp.parsing.parser.util.SourceRange) ParseTree(com.google.javascript.jscomp.parsing.parser.trees.ParseTree)

Aggregations

SourceRange (com.google.javascript.jscomp.parsing.parser.util.SourceRange)8 ParseTree (com.google.javascript.jscomp.parsing.parser.trees.ParseTree)5 SourcePosition (com.google.javascript.jscomp.parsing.parser.util.SourcePosition)3 ImmutableList (com.google.common.collect.ImmutableList)2 FormatString (com.google.errorprone.annotations.FormatString)2 NullTree (com.google.javascript.jscomp.parsing.parser.trees.NullTree)2 ArrayLiteralExpressionTree (com.google.javascript.jscomp.parsing.parser.trees.ArrayLiteralExpressionTree)1 BlockTree (com.google.javascript.jscomp.parsing.parser.trees.BlockTree)1 CatchTree (com.google.javascript.jscomp.parsing.parser.trees.CatchTree)1 Comment (com.google.javascript.jscomp.parsing.parser.trees.Comment)1 DefaultParameterTree (com.google.javascript.jscomp.parsing.parser.trees.DefaultParameterTree)1 EmptyStatementTree (com.google.javascript.jscomp.parsing.parser.trees.EmptyStatementTree)1 ForStatementTree (com.google.javascript.jscomp.parsing.parser.trees.ForStatementTree)1 OptionalParameterTree (com.google.javascript.jscomp.parsing.parser.trees.OptionalParameterTree)1 PropertyNameAssignmentTree (com.google.javascript.jscomp.parsing.parser.trees.PropertyNameAssignmentTree)1 TypedParameterTree (com.google.javascript.jscomp.parsing.parser.trees.TypedParameterTree)1