Java Code Examples for jdk.nashorn.internal.ir.LiteralNode#newInstance()

The following examples show how to use jdk.nashorn.internal.ir.LiteralNode#newInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JSONParser.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parse a JSON literal from the token stream
 * @return the JSON literal as a Node
 */
private Expression jsonLiteral() {
    final long literalToken = token;

    switch (type) {
    case STRING:
        return getStringLiteral();
    case ESCSTRING:
    case DECIMAL:
    case FLOATING:
        return getLiteral();
    case FALSE:
        next();
        return LiteralNode.newInstance(literalToken, finish, false);
    case TRUE:
        next();
        return LiteralNode.newInstance(literalToken, finish, true);
    case NULL:
        next();
        return LiteralNode.newInstance(literalToken, finish);
    case LBRACKET:
        return arrayLiteral();
    case LBRACE:
        return objectLiteral();
    /*
     * A.8.1 JSON Lexical Grammar
     *
     * JSONNumber :: See 15.12.1.1
     *    -opt DecimalIntegerLiteral JSONFractionopt ExponentPartopt
     */
    case SUB:
        next();

        final long realToken = token;
        final Object value = getValue();

        if (value instanceof Number) {
            next();
            return new UnaryNode(literalToken, LiteralNode.newInstance(realToken, finish, (Number)value));
        }

        throw error(AbstractParser.message("expected", "number", type.getNameOrType()));
    default:
        break;
    }

    throw error(AbstractParser.message("expected", "json literal", type.getNameOrType()));
}
 
Example 2
Source File: Parser.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
     * ArrayLiteral :
     *      [ Elision? ]
     *      [ ElementList ]
     *      [ ElementList , Elision? ]
     *      [ expression for (LeftHandExpression in expression) ( (if ( Expression ) )? ]
     *
     * ElementList : Elision? AssignmentExpression
     *      ElementList , Elision? AssignmentExpression
     *
     * Elision :
     *      ,
     *      Elision ,
     *
     * See 12.1.4
     * JavaScript 1.8
     *
     * Parse array literal.
     * @return Expression node.
     */
    private LiteralNode<Expression[]> arrayLiteral() {
        // Capture LBRACKET token.
        final long arrayToken = token;
        // LBRACKET tested in caller.
        next();

        // Prepare to accummulating elements.
        final List<Expression> elements = new ArrayList<>();
        // Track elisions.
        boolean elision = true;
loop:
        while (true) {
             switch (type) {
            case RBRACKET:
                next();

                break loop;

            case COMMARIGHT:
                next();

                // If no prior expression
                if (elision) {
                    elements.add(null);
                }

                elision = true;

                break;

            default:
                if (!elision) {
                    throw error(AbstractParser.message("expected.comma", type.getNameOrType()));
                }
                // Add expression element.
                final Expression expression = assignmentExpression(false);

                if (expression != null) {
                    elements.add(expression);
                } else {
                    expect(RBRACKET);
                }

                elision = false;
                break;
            }
        }

        return LiteralNode.newInstance(arrayToken, finish, elements);
    }
 
Example 3
Source File: Parser.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
     * ArrayLiteral :
     *      [ Elision? ]
     *      [ ElementList ]
     *      [ ElementList , Elision? ]
     *      [ expression for (LeftHandExpression in expression) ( (if ( Expression ) )? ]
     *
     * ElementList : Elision? AssignmentExpression
     *      ElementList , Elision? AssignmentExpression
     *
     * Elision :
     *      ,
     *      Elision ,
     *
     * See 12.1.4
     * JavaScript 1.8
     *
     * Parse array literal.
     * @return Expression node.
     */
    private LiteralNode<Expression[]> arrayLiteral() {
        // Capture LBRACKET token.
        final long arrayToken = token;
        // LBRACKET tested in caller.
        next();

        // Prepare to accummulating elements.
        final List<Expression> elements = new ArrayList<>();
        // Track elisions.
        boolean elision = true;
loop:
        while (true) {
             switch (type) {
            case RBRACKET:
                next();

                break loop;

            case COMMARIGHT:
                next();

                // If no prior expression
                if (elision) {
                    elements.add(null);
                }

                elision = true;

                break;

            default:
                if (!elision) {
                    throw error(AbstractParser.message("expected.comma", type.getNameOrType()));
                }
                // Add expression element.
                final Expression expression = assignmentExpression(false);

                if (expression != null) {
                    elements.add(expression);
                } else {
                    expect(RBRACKET);
                }

                elision = false;
                break;
            }
        }

        return LiteralNode.newInstance(arrayToken, finish, elements);
    }
 
Example 4
Source File: JSONParser.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
     * Parse an array literal from the token stream
     * @return the array literal as a Node
     */
    private LiteralNode<Expression[]> arrayLiteral() {
        // Unlike JavaScript array literals, elison is not permitted in JSON.

        // Capture LBRACKET token.
        final long arrayToken = token;
        // LBRACKET tested in caller.
        next();

        LiteralNode<Expression[]> result = null;
        // Prepare to accummulating elements.
        final List<Expression> elements = new ArrayList<>();

loop:
        while (true) {
            switch (type) {
            case RBRACKET:
                next();
                result = LiteralNode.newInstance(arrayToken, finish, elements);
                break loop;

            case COMMARIGHT:
                next();
                // check for trailing comma - not allowed in JSON
                if (type == RBRACKET) {
                    throw error(AbstractParser.message("trailing.comma.in.json", type.getNameOrType()));
                }
                break;

            default:
                // Add expression element.
                elements.add(jsonLiteral());
                // Comma between array elements is mandatory in JSON.
                if (type != COMMARIGHT && type != RBRACKET) {
                   throw error(AbstractParser.message("expected", ", or ]", type.getNameOrType()));
                }
                break;
            }
        }

        return result;
    }
 
Example 5
Source File: FoldConstants.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected LiteralNode<?> eval() {
    final Node rhsNode = parent.getExpression();

    if (!(rhsNode instanceof LiteralNode)) {
        return null;
    }

    if (rhsNode instanceof ArrayLiteralNode) {
        return null;
    }

    final LiteralNode<?> rhs = (LiteralNode<?>)rhsNode;
    final Type rhsType = rhs.getType();
    final boolean rhsInteger = rhsType.isInteger() || rhsType.isBoolean();

    LiteralNode<?> literalNode;

    switch (parent.tokenType()) {
    case ADD:
        if (rhsInteger) {
            literalNode = LiteralNode.newInstance(token, finish, rhs.getInt32());
        } else if (rhsType.isLong()) {
            literalNode = LiteralNode.newInstance(token, finish, rhs.getLong());
        } else {
            literalNode = LiteralNode.newInstance(token, finish, rhs.getNumber());
        }
        break;
    case SUB:
        if (rhsInteger && rhs.getInt32() != 0) { // @see test/script/basic/minuszero.js
            literalNode = LiteralNode.newInstance(token, finish, -rhs.getInt32());
        } else if (rhsType.isLong() && rhs.getLong() != 0L) {
            literalNode = LiteralNode.newInstance(token, finish, -rhs.getLong());
        } else {
            literalNode = LiteralNode.newInstance(token, finish, -rhs.getNumber());
        }
        break;
    case NOT:
        literalNode = LiteralNode.newInstance(token, finish, !rhs.getBoolean());
        break;
    case BIT_NOT:
        literalNode = LiteralNode.newInstance(token, finish, ~rhs.getInt32());
        break;
    default:
        return null;
    }

    return literalNode;
}
 
Example 6
Source File: SplitIntoFunctions.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private static LiteralNode<Number> intLiteral(final int i) {
    return LiteralNode.newInstance(NO_TOKEN, NO_FINISH, i);
}
 
Example 7
Source File: FoldConstants.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected LiteralNode<?> eval() {
    final Node rhsNode = parent.getExpression();

    if (!(rhsNode instanceof LiteralNode)) {
        return null;
    }

    if (rhsNode instanceof ArrayLiteralNode) {
        return null;
    }

    final LiteralNode<?> rhs = (LiteralNode<?>)rhsNode;
    final Type rhsType = rhs.getType();
    final boolean rhsInteger = rhsType.isInteger() || rhsType.isBoolean();

    LiteralNode<?> literalNode;

    switch (parent.tokenType()) {
    case ADD:
        if (rhsInteger) {
            literalNode = LiteralNode.newInstance(token, finish, rhs.getInt32());
        } else if (rhsType.isLong()) {
            literalNode = LiteralNode.newInstance(token, finish, rhs.getLong());
        } else {
            literalNode = LiteralNode.newInstance(token, finish, rhs.getNumber());
        }
        break;
    case SUB:
        if (rhsInteger && rhs.getInt32() != 0) { // @see test/script/basic/minuszero.js
            literalNode = LiteralNode.newInstance(token, finish, -rhs.getInt32());
        } else if (rhsType.isLong() && rhs.getLong() != 0L) {
            literalNode = LiteralNode.newInstance(token, finish, -rhs.getLong());
        } else {
            literalNode = LiteralNode.newInstance(token, finish, -rhs.getNumber());
        }
        break;
    case NOT:
        literalNode = LiteralNode.newInstance(token, finish, !rhs.getBoolean());
        break;
    case BIT_NOT:
        literalNode = LiteralNode.newInstance(token, finish, ~rhs.getInt32());
        break;
    default:
        return null;
    }

    return literalNode;
}
 
Example 8
Source File: Parser.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
     * ArrayLiteral :
     *      [ Elision? ]
     *      [ ElementList ]
     *      [ ElementList , Elision? ]
     *      [ expression for (LeftHandExpression in expression) ( (if ( Expression ) )? ]
     *
     * ElementList : Elision? AssignmentExpression
     *      ElementList , Elision? AssignmentExpression
     *
     * Elision :
     *      ,
     *      Elision ,
     *
     * See 12.1.4
     * JavaScript 1.8
     *
     * Parse array literal.
     * @return Expression node.
     */
    private LiteralNode<Expression[]> arrayLiteral() {
        // Capture LBRACKET token.
        final long arrayToken = token;
        // LBRACKET tested in caller.
        next();

        // Prepare to accummulating elements.
        final List<Expression> elements = new ArrayList<>();
        // Track elisions.
        boolean elision = true;
loop:
        while (true) {
             switch (type) {
            case RBRACKET:
                next();

                break loop;

            case COMMARIGHT:
                next();

                // If no prior expression
                if (elision) {
                    elements.add(null);
                }

                elision = true;

                break;

            default:
                if (!elision) {
                    throw error(AbstractParser.message("expected.comma", type.getNameOrType()));
                }
                // Add expression element.
                final Expression expression = assignmentExpression(false);

                if (expression != null) {
                    elements.add(expression);
                } else {
                    expect(RBRACKET);
                }

                elision = false;
                break;
            }
        }

        return LiteralNode.newInstance(arrayToken, finish, elements);
    }
 
Example 9
Source File: Parser.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * PrimaryExpression :
 *      this
 *      Identifier
 *      Literal
 *      ArrayLiteral
 *      ObjectLiteral
 *      ( Expression )
 *
 *  See 11.1
 *
 * Parse primary expression.
 * @return Expression node.
 */
@SuppressWarnings("fallthrough")
private Expression primaryExpression() {
    // Capture first token.
    final int  primaryLine  = line;
    final long primaryToken = token;

    switch (type) {
    case THIS:
        final String name = type.getName();
        next();
        lc.setFlag(lc.getCurrentFunction(), FunctionNode.USES_THIS);
        return new IdentNode(primaryToken, finish, name);
    case IDENT:
        final IdentNode ident = getIdent();
        if (ident == null) {
            break;
        }
        detectSpecialProperty(ident);
        return ident;
    case OCTAL:
        if (isStrictMode) {
           throw error(AbstractParser.message("strict.no.octal"), token);
        }
    case STRING:
    case ESCSTRING:
    case DECIMAL:
    case HEXADECIMAL:
    case FLOATING:
    case REGEX:
    case XML:
        return getLiteral();
    case EXECSTRING:
        return execString(primaryLine, primaryToken);
    case FALSE:
        next();
        return LiteralNode.newInstance(primaryToken, finish, false);
    case TRUE:
        next();
        return LiteralNode.newInstance(primaryToken, finish, true);
    case NULL:
        next();
        return LiteralNode.newInstance(primaryToken, finish);
    case LBRACKET:
        return arrayLiteral();
    case LBRACE:
        return objectLiteral();
    case LPAREN:
        next();

        final Expression expression = expression();

        expect(RPAREN);

        return expression;

    default:
        // In this context some operator tokens mark the start of a literal.
        if (lexer.scanLiteral(primaryToken, type, lineInfoReceiver)) {
            next();
            return getLiteral();
        }
        if (isNonStrictModeIdent()) {
            return getIdent();
        }
        break;
    }

    return null;
}
 
Example 10
Source File: JSONParser.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parse a JSON literal from the token stream
 * @return the JSON literal as a Node
 */
private Expression jsonLiteral() {
    final long literalToken = token;

    switch (type) {
    case STRING:
        return getStringLiteral();
    case ESCSTRING:
    case DECIMAL:
    case FLOATING:
        return getLiteral();
    case FALSE:
        next();
        return LiteralNode.newInstance(literalToken, finish, false);
    case TRUE:
        next();
        return LiteralNode.newInstance(literalToken, finish, true);
    case NULL:
        next();
        return LiteralNode.newInstance(literalToken, finish);
    case LBRACKET:
        return arrayLiteral();
    case LBRACE:
        return objectLiteral();
    /*
     * A.8.1 JSON Lexical Grammar
     *
     * JSONNumber :: See 15.12.1.1
     *    -opt DecimalIntegerLiteral JSONFractionopt ExponentPartopt
     */
    case SUB:
        next();

        final long realToken = token;
        final Object value = getValue();

        if (value instanceof Number) {
            next();
            return new UnaryNode(literalToken, LiteralNode.newInstance(realToken, finish, (Number)value));
        }

        throw error(AbstractParser.message("expected", "number", type.getNameOrType()));
    default:
        break;
    }

    throw error(AbstractParser.message("expected", "json literal", type.getNameOrType()));
}
 
Example 11
Source File: Parser.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * PrimaryExpression :
 *      this
 *      Identifier
 *      Literal
 *      ArrayLiteral
 *      ObjectLiteral
 *      ( Expression )
 *
 *  See 11.1
 *
 * Parse primary expression.
 * @return Expression node.
 */
@SuppressWarnings("fallthrough")
private Expression primaryExpression() {
    // Capture first token.
    final int  primaryLine  = line;
    final long primaryToken = token;

    switch (type) {
    case THIS:
        final String name = type.getName();
        next();
        lc.setFlag(lc.getCurrentFunction(), FunctionNode.USES_THIS);
        return new IdentNode(primaryToken, finish, name);
    case IDENT:
        final IdentNode ident = getIdent();
        if (ident == null) {
            break;
        }
        detectSpecialProperty(ident);
        return ident;
    case OCTAL:
        if (isStrictMode) {
           throw error(AbstractParser.message("strict.no.octal"), token);
        }
    case STRING:
    case ESCSTRING:
    case DECIMAL:
    case HEXADECIMAL:
    case FLOATING:
    case REGEX:
    case XML:
        return getLiteral();
    case EXECSTRING:
        return execString(primaryLine, primaryToken);
    case FALSE:
        next();
        return LiteralNode.newInstance(primaryToken, finish, false);
    case TRUE:
        next();
        return LiteralNode.newInstance(primaryToken, finish, true);
    case NULL:
        next();
        return LiteralNode.newInstance(primaryToken, finish);
    case LBRACKET:
        return arrayLiteral();
    case LBRACE:
        return objectLiteral();
    case LPAREN:
        next();

        final Expression expression = expression();

        expect(RPAREN);

        return expression;

    default:
        // In this context some operator tokens mark the start of a literal.
        if (lexer.scanLiteral(primaryToken, type, lineInfoReceiver)) {
            next();
            return getLiteral();
        }
        if (isNonStrictModeIdent()) {
            return getIdent();
        }
        break;
    }

    return null;
}
 
Example 12
Source File: Parser.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
     * ArrayLiteral :
     *      [ Elision? ]
     *      [ ElementList ]
     *      [ ElementList , Elision? ]
     *      [ expression for (LeftHandExpression in expression) ( (if ( Expression ) )? ]
     *
     * ElementList : Elision? AssignmentExpression
     *      ElementList , Elision? AssignmentExpression
     *
     * Elision :
     *      ,
     *      Elision ,
     *
     * See 12.1.4
     * JavaScript 1.8
     *
     * Parse array literal.
     * @return Expression node.
     */
    private LiteralNode<Expression[]> arrayLiteral() {
        // Capture LBRACKET token.
        final long arrayToken = token;
        // LBRACKET tested in caller.
        next();

        // Prepare to accummulating elements.
        final List<Expression> elements = new ArrayList<>();
        // Track elisions.
        boolean elision = true;
loop:
        while (true) {
             switch (type) {
            case RBRACKET:
                next();

                break loop;

            case COMMARIGHT:
                next();

                // If no prior expression
                if (elision) {
                    elements.add(null);
                }

                elision = true;

                break;

            default:
                if (!elision) {
                    throw error(AbstractParser.message("expected.comma", type.getNameOrType()));
                }
                // Add expression element.
                final Expression expression = assignmentExpression(false);

                if (expression != null) {
                    elements.add(expression);
                } else {
                    expect(RBRACKET);
                }

                elision = false;
                break;
            }
        }

        return LiteralNode.newInstance(arrayToken, finish, elements);
    }
 
Example 13
Source File: Parser.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * PrimaryExpression :
 *      this
 *      Identifier
 *      Literal
 *      ArrayLiteral
 *      ObjectLiteral
 *      ( Expression )
 *
 *  See 11.1
 *
 * Parse primary expression.
 * @return Expression node.
 */
@SuppressWarnings("fallthrough")
private Expression primaryExpression() {
    // Capture first token.
    final int  primaryLine  = line;
    final long primaryToken = token;

    switch (type) {
    case THIS:
        final String name = type.getName();
        next();
        lc.setFlag(lc.getCurrentFunction(), FunctionNode.USES_THIS);
        return new IdentNode(primaryToken, finish, name);
    case IDENT:
        final IdentNode ident = getIdent();
        if (ident == null) {
            break;
        }
        detectSpecialProperty(ident);
        return ident;
    case OCTAL:
        if (isStrictMode) {
           throw error(AbstractParser.message("strict.no.octal"), token);
        }
    case STRING:
    case ESCSTRING:
    case DECIMAL:
    case HEXADECIMAL:
    case FLOATING:
    case REGEX:
    case XML:
        return getLiteral();
    case EXECSTRING:
        return execString(primaryLine, primaryToken);
    case FALSE:
        next();
        return LiteralNode.newInstance(primaryToken, finish, false);
    case TRUE:
        next();
        return LiteralNode.newInstance(primaryToken, finish, true);
    case NULL:
        next();
        return LiteralNode.newInstance(primaryToken, finish);
    case LBRACKET:
        return arrayLiteral();
    case LBRACE:
        return objectLiteral();
    case LPAREN:
        next();

        final Expression expression = expression();

        expect(RPAREN);

        return expression;

    default:
        // In this context some operator tokens mark the start of a literal.
        if (lexer.scanLiteral(primaryToken, type, lineInfoReceiver)) {
            next();
            return getLiteral();
        }
        if (isNonStrictModeIdent()) {
            return getIdent();
        }
        break;
    }

    return null;
}
 
Example 14
Source File: SplitIntoFunctions.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private static LiteralNode<Number> intLiteral(final int i) {
    return LiteralNode.newInstance(NO_TOKEN, NO_FINISH, i);
}
 
Example 15
Source File: Parser.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * PrimaryExpression :
 *      this
 *      Identifier
 *      Literal
 *      ArrayLiteral
 *      ObjectLiteral
 *      ( Expression )
 *
 *  See 11.1
 *
 * Parse primary expression.
 * @return Expression node.
 */
@SuppressWarnings("fallthrough")
private Expression primaryExpression() {
    // Capture first token.
    final int  primaryLine  = line;
    final long primaryToken = token;

    switch (type) {
    case THIS:
        final String name = type.getName();
        next();
        return new IdentNode(primaryToken, finish, name);
    case IDENT:
        final IdentNode ident = getIdent();
        if (ident == null) {
            break;
        }
        detectSpecialProperty(ident);
        return ident;
    case OCTAL:
        if (isStrictMode) {
           throw error(AbstractParser.message("strict.no.octal"), token);
        }
    case STRING:
    case ESCSTRING:
    case DECIMAL:
    case HEXADECIMAL:
    case FLOATING:
    case REGEX:
    case XML:
        return getLiteral();
    case EXECSTRING:
        return execString(primaryLine, primaryToken);
    case FALSE:
        next();
        return LiteralNode.newInstance(primaryToken, finish, false);
    case TRUE:
        next();
        return LiteralNode.newInstance(primaryToken, finish, true);
    case NULL:
        next();
        return LiteralNode.newInstance(primaryToken, finish);
    case LBRACKET:
        return arrayLiteral();
    case LBRACE:
        return objectLiteral();
    case LPAREN:
        next();

        final Expression expression = expression();

        expect(RPAREN);

        return expression;

    default:
        // In this context some operator tokens mark the start of a literal.
        if (lexer.scanLiteral(primaryToken, type, lineInfoReceiver)) {
            next();
            return getLiteral();
        }
        if (isNonStrictModeIdent()) {
            return getIdent();
        }
        break;
    }

    return null;
}
 
Example 16
Source File: Parser.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
     * ArrayLiteral :
     *      [ Elision? ]
     *      [ ElementList ]
     *      [ ElementList , Elision? ]
     *      [ expression for (LeftHandExpression in expression) ( (if ( Expression ) )? ]
     *
     * ElementList : Elision? AssignmentExpression
     *      ElementList , Elision? AssignmentExpression
     *
     * Elision :
     *      ,
     *      Elision ,
     *
     * See 12.1.4
     * JavaScript 1.8
     *
     * Parse array literal.
     * @return Expression node.
     */
    private LiteralNode<Expression[]> arrayLiteral() {
        // Capture LBRACKET token.
        final long arrayToken = token;
        // LBRACKET tested in caller.
        next();

        // Prepare to accummulating elements.
        final List<Expression> elements = new ArrayList<>();
        // Track elisions.
        boolean elision = true;
loop:
        while (true) {
             switch (type) {
            case RBRACKET:
                next();

                break loop;

            case COMMARIGHT:
                next();

                // If no prior expression
                if (elision) {
                    elements.add(null);
                }

                elision = true;

                break;

            default:
                if (!elision) {
                    throw error(AbstractParser.message("expected.comma", type.getNameOrType()));
                }
                // Add expression element.
                final Expression expression = assignmentExpression(false);

                if (expression != null) {
                    elements.add(expression);
                } else {
                    expect(RBRACKET);
                }

                elision = false;
                break;
            }
        }

        return LiteralNode.newInstance(arrayToken, finish, elements);
    }
 
Example 17
Source File: Parser.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
/**
     * ArrayLiteral :
     *      [ Elision? ]
     *      [ ElementList ]
     *      [ ElementList , Elision? ]
     *      [ expression for (LeftHandExpression in expression) ( (if ( Expression ) )? ]
     *
     * ElementList : Elision? AssignmentExpression
     *      ElementList , Elision? AssignmentExpression
     *
     * Elision :
     *      ,
     *      Elision ,
     *
     * See 12.1.4
     * JavaScript 1.8
     *
     * Parse array literal.
     * @return Expression node.
     */
    private LiteralNode<Expression[]> arrayLiteral() {
        // Capture LBRACKET token.
        final long arrayToken = token;
        // LBRACKET tested in caller.
        next();

        // Prepare to accummulating elements.
        final List<Expression> elements = new ArrayList<>();
        // Track elisions.
        boolean elision = true;
loop:
        while (true) {
             switch (type) {
            case RBRACKET:
                next();

                break loop;

            case COMMARIGHT:
                next();

                // If no prior expression
                if (elision) {
                    elements.add(null);
                }

                elision = true;

                break;

            default:
                if (!elision) {
                    throw error(AbstractParser.message("expected.comma", type.getNameOrType()));
                }
                // Add expression element.
                final Expression expression = assignmentExpression(false);

                if (expression != null) {
                    elements.add(expression);
                } else {
                    expect(RBRACKET);
                }

                elision = false;
                break;
            }
        }

        return LiteralNode.newInstance(arrayToken, finish, elements);
    }
 
Example 18
Source File: SplitIntoFunctions.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static LiteralNode<Number> intLiteral(final int i) {
    return LiteralNode.newInstance(NO_TOKEN, NO_FINISH, i);
}
 
Example 19
Source File: FoldConstants.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected LiteralNode<?> eval() {
    final Node rhsNode = parent.getExpression();

    if (!(rhsNode instanceof LiteralNode)) {
        return null;
    }

    if (rhsNode instanceof ArrayLiteralNode) {
        return null;
    }

    final LiteralNode<?> rhs = (LiteralNode<?>)rhsNode;
    final Type rhsType = rhs.getType();
    final boolean rhsInteger = rhsType.isInteger() || rhsType.isBoolean();

    LiteralNode<?> literalNode;

    switch (parent.tokenType()) {
    case ADD:
        if (rhsInteger) {
            literalNode = LiteralNode.newInstance(token, finish, rhs.getInt32());
        } else if (rhsType.isLong()) {
            literalNode = LiteralNode.newInstance(token, finish, rhs.getLong());
        } else {
            literalNode = LiteralNode.newInstance(token, finish, rhs.getNumber());
        }
        break;
    case SUB:
        if (rhsInteger && rhs.getInt32() != 0) { // @see test/script/basic/minuszero.js
            literalNode = LiteralNode.newInstance(token, finish, -rhs.getInt32());
        } else if (rhsType.isLong() && rhs.getLong() != 0L) {
            literalNode = LiteralNode.newInstance(token, finish, -rhs.getLong());
        } else {
            literalNode = LiteralNode.newInstance(token, finish, -rhs.getNumber());
        }
        break;
    case NOT:
        literalNode = LiteralNode.newInstance(token, finish, !rhs.getBoolean());
        break;
    case BIT_NOT:
        literalNode = LiteralNode.newInstance(token, finish, ~rhs.getInt32());
        break;
    default:
        return null;
    }

    return literalNode;
}
 
Example 20
Source File: Parser.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
     * ArrayLiteral :
     *      [ Elision? ]
     *      [ ElementList ]
     *      [ ElementList , Elision? ]
     *      [ expression for (LeftHandExpression in expression) ( (if ( Expression ) )? ]
     *
     * ElementList : Elision? AssignmentExpression
     *      ElementList , Elision? AssignmentExpression
     *
     * Elision :
     *      ,
     *      Elision ,
     *
     * See 12.1.4
     * JavaScript 1.8
     *
     * Parse array literal.
     * @return Expression node.
     */
    private LiteralNode<Expression[]> arrayLiteral() {
        // Capture LBRACKET token.
        final long arrayToken = token;
        // LBRACKET tested in caller.
        next();

        // Prepare to accummulating elements.
        final List<Expression> elements = new ArrayList<>();
        // Track elisions.
        boolean elision = true;
loop:
        while (true) {
             switch (type) {
            case RBRACKET:
                next();

                break loop;

            case COMMARIGHT:
                next();

                // If no prior expression
                if (elision) {
                    elements.add(null);
                }

                elision = true;

                break;

            default:
                if (!elision) {
                    throw error(AbstractParser.message("expected.comma", type.getNameOrType()));
                }
                // Add expression element.
                final Expression expression = assignmentExpression(false);

                if (expression != null) {
                    elements.add(expression);
                } else {
                    expect(RBRACKET);
                }

                elision = false;
                break;
            }
        }

        return LiteralNode.newInstance(arrayToken, finish, elements);
    }