jdk.nashorn.internal.parser.TokenType Java Examples

The following examples show how to use jdk.nashorn.internal.parser.TokenType. 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: CodeGenerator.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
private boolean enterAND_OR(final BinaryNode binaryNode) {
    final Expression lhs = binaryNode.lhs();
    final Expression rhs = binaryNode.rhs();

    final Label skip = new Label("skip");

    load(lhs).convert(Type.OBJECT).dup().convert(Type.BOOLEAN);

    if (binaryNode.tokenType() == TokenType.AND) {
        method.ifeq(skip);
    } else {
        method.ifne(skip);
    }

    method.pop();
    load(rhs).convert(Type.OBJECT);
    method.label(skip);
    method.store(binaryNode.getSymbol());

    return false;
}
 
Example #2
Source File: Lower.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveExpressionStatement(final ExpressionStatement expressionStatement) {
    final Expression expr = expressionStatement.getExpression();
    ExpressionStatement node = expressionStatement;

    final FunctionNode currentFunction = lc.getCurrentFunction();

    if (currentFunction.isProgram()) {
        if (!isInternalExpression(expr) && !isEvalResultAssignment(expr)) {
            node = expressionStatement.setExpression(
                new BinaryNode(
                    Token.recast(
                        expressionStatement.getToken(),
                        TokenType.ASSIGN),
                    compilerConstant(RETURN),
                expr));
        }
    }

    return addStatement(node);
}
 
Example #3
Source File: Debug.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Dump a token stream to stdout
 *
 * TODO: most other bugging goes to stderr, change?
 *
 * @param source the source
 * @param lexer  the lexer
 * @param stream the stream to dump
 */
public static void dumpTokens(final Source source, final Lexer lexer, final TokenStream stream) {
    TokenType type;
    int k = 0;
    do {
        while (k > stream.last()) {
            // Get more tokens.
            lexer.lexify();
        }

        final long token = stream.get(k);
        type = Token.descType(token);
        System.out.println("" + k + ": " + Token.toString(source, token, true));
        k++;
    } while(type != EOF);
}
 
Example #4
Source File: Lower.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveExpressionStatement(final ExpressionStatement expressionStatement) {
    final Expression expr = expressionStatement.getExpression();
    ExpressionStatement node = expressionStatement;

    final FunctionNode currentFunction = lc.getCurrentFunction();

    if (currentFunction.isProgram()) {
        if (!isInternalExpression(expr) && !isEvalResultAssignment(expr)) {
            node = expressionStatement.setExpression(
                new BinaryNode(
                    Token.recast(
                        expressionStatement.getToken(),
                        TokenType.ASSIGN),
                    compilerConstant(RETURN),
                expr));
        }
    }

    return addStatement(node);
}
 
Example #5
Source File: OptimisticTypesCalculator.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean enterBinaryNode(final BinaryNode binaryNode) {
    if(binaryNode.isAssignment()) {
        final Expression lhs = binaryNode.lhs();
        if(!binaryNode.isSelfModifying()) {
            tagNeverOptimistic(lhs);
        }
        if(lhs instanceof IdentNode) {
            final Symbol symbol = ((IdentNode)lhs).getSymbol();
            // Assignment to internal symbols is never optimistic, except for self-assignment expressions
            if(symbol.isInternal() && !binaryNode.rhs().isSelfModifying()) {
                tagNeverOptimistic(binaryNode.rhs());
            }
        }
    } else if(binaryNode.isTokenType(TokenType.INSTANCEOF)) {
        tagNeverOptimistic(binaryNode.lhs());
        tagNeverOptimistic(binaryNode.rhs());
    }
    return true;
}
 
Example #6
Source File: SplitIntoFunctions.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveBlock(final Block block) {
    if (!artificialBlock) {
        if (lc.isFunctionBody()) {
            // Prepend declaration-only var statements to the top of the statement list.
            lc.prependStatements(getCurrentFunctionState().varStatements);
        } else if (lc.isSplitBody()) {
            appendSplitReturn(FALLTHROUGH_STATE, NO_LINE_NUMBER);
            if (getCurrentFunctionState().fn.isProgram()) {
                // If we're splitting the program, make sure every shard ends with "return :return" and
                // begins with ":return = :return-in;".
                lc.prependStatement(new ExpressionStatement(NO_LINE_NUMBER, NO_TOKEN, NO_FINISH,
                        new BinaryNode(Token.toDesc(TokenType.ASSIGN, 0, 0), createReturnIdent(), createReturnParamIdent())));
            }
        }
    }
    return block;
}
 
Example #7
Source File: SplitIntoFunctions.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveBlock(final Block block) {
    if (!artificialBlock) {
        if (lc.isFunctionBody()) {
            // Prepend declaration-only var statements to the top of the statement list.
            lc.prependStatements(getCurrentFunctionState().varStatements);
        } else if (lc.isSplitBody()) {
            appendSplitReturn(FALLTHROUGH_STATE, NO_LINE_NUMBER);
            if (getCurrentFunctionState().fn.isProgram()) {
                // If we're splitting the program, make sure every shard ends with "return :return" and
                // begins with ":return = :return-in;".
                lc.prependStatement(new ExpressionStatement(NO_LINE_NUMBER, NO_TOKEN, NO_FINISH,
                        new BinaryNode(Token.toDesc(TokenType.ASSIGN, 0, 0), createReturnIdent(), createReturnParamIdent())));
            }
        }
    }
    return block;
}
 
Example #8
Source File: Debug.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Dump a token stream to stdout
 *
 * TODO: most other bugging goes to stderr, change?
 *
 * @param source the source
 * @param lexer  the lexer
 * @param stream the stream to dump
 */
public static void dumpTokens(final Source source, final Lexer lexer, final TokenStream stream) {
    TokenType type;
    int k = 0;
    do {
        while (k > stream.last()) {
            // Get more tokens.
            lexer.lexify();
        }

        final long token = stream.get(k);
        type = Token.descType(token);
        System.out.println("" + k + ": " + Token.toString(source, token, true));
        k++;
    } while(type != EOF);
}
 
Example #9
Source File: CodeGenerator.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private boolean enterAND_OR(final BinaryNode binaryNode) {
    final Expression lhs = binaryNode.lhs();
    final Expression rhs = binaryNode.rhs();

    final Label skip = new Label("skip");

    load(lhs, Type.OBJECT).dup().convert(Type.BOOLEAN);

    if (binaryNode.tokenType() == TokenType.AND) {
        method.ifeq(skip);
    } else {
        method.ifne(skip);
    }

    method.pop();
    load(rhs, Type.OBJECT);
    method.label(skip);
    method.store(binaryNode.getSymbol());

    return false;
}
 
Example #10
Source File: OptimisticTypesCalculator.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean enterBinaryNode(final BinaryNode binaryNode) {
    if(binaryNode.isAssignment()) {
        final Expression lhs = binaryNode.lhs();
        if(!binaryNode.isSelfModifying()) {
            tagNeverOptimistic(lhs);
        }
        if(lhs instanceof IdentNode) {
            final Symbol symbol = ((IdentNode)lhs).getSymbol();
            // Assignment to internal symbols is never optimistic, except for self-assignment expressions
            if(symbol.isInternal() && !binaryNode.rhs().isSelfModifying()) {
                tagNeverOptimistic(binaryNode.rhs());
            }
        }
    } else if(binaryNode.isTokenType(TokenType.INSTANCEOF)
            || binaryNode.isTokenType(TokenType.EQ_STRICT)
            || binaryNode.isTokenType(TokenType.NE_STRICT)) {
        tagNeverOptimistic(binaryNode.lhs());
        tagNeverOptimistic(binaryNode.rhs());
    }
    return true;
}
 
Example #11
Source File: SplitIntoFunctions.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveBlock(final Block block) {
    if (!artificialBlock) {
        if (lc.isFunctionBody()) {
            // Prepend declaration-only var statements to the top of the statement list.
            lc.prependStatements(getCurrentFunctionState().varStatements);
        } else if (lc.isSplitBody()) {
            appendSplitReturn(FALLTHROUGH_STATE, NO_LINE_NUMBER);
            if (getCurrentFunctionState().fn.isProgram()) {
                // If we're splitting the program, make sure every shard ends with "return :return" and
                // begins with ":return = :return-in;".
                lc.prependStatement(new ExpressionStatement(NO_LINE_NUMBER, NO_TOKEN, NO_FINISH,
                        new BinaryNode(Token.toDesc(TokenType.ASSIGN, 0, 0), createReturnIdent(), createReturnParamIdent())));
            }
        }
    }
    return block;
}
 
Example #12
Source File: Debug.java    From nashorn with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Dump a token stream to stdout
 *
 * TODO: most other bugging goes to stderr, change?
 *
 * @param source the source
 * @param lexer  the lexer
 * @param stream the stream to dump
 */
public static void dumpTokens(final Source source, final Lexer lexer, final TokenStream stream) {
    TokenType type;
    int k = 0;
    do {
        while (k > stream.last()) {
            // Get more tokens.
            lexer.lexify();
        }

        final long token = stream.get(k);
        type = Token.descType(token);
        System.out.println("" + k + ": " + Token.toString(source, token, true));
        k++;
    } while(type != EOF);
}
 
Example #13
Source File: Lower.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Node leaveExpressionStatement(final ExpressionStatement expressionStatement) {
    final Expression expr = expressionStatement.getExpression();
    ExpressionStatement node = expressionStatement;

    final FunctionNode currentFunction = lc.getCurrentFunction();

    if (currentFunction.isProgram()) {
        if (!isInternalExpression(expr) && !isEvalResultAssignment(expr)) {
            node = expressionStatement.setExpression(
                new BinaryNode(
                    Token.recast(
                        expressionStatement.getToken(),
                        TokenType.ASSIGN),
                    compilerConstant(RETURN),
                expr));
        }
    }

    return addStatement(node);
}
 
Example #14
Source File: OptimisticTypesCalculator.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean enterBinaryNode(final BinaryNode binaryNode) {
    if(binaryNode.isAssignment()) {
        final Expression lhs = binaryNode.lhs();
        if(!binaryNode.isSelfModifying()) {
            tagNeverOptimistic(lhs);
        }
        if(lhs instanceof IdentNode) {
            final Symbol symbol = ((IdentNode)lhs).getSymbol();
            // Assignment to internal symbols is never optimistic, except for self-assignment expressions
            if(symbol.isInternal() && !binaryNode.rhs().isSelfModifying()) {
                tagNeverOptimistic(binaryNode.rhs());
            }
        }
    } else if(binaryNode.isTokenType(TokenType.INSTANCEOF)) {
        tagNeverOptimistic(binaryNode.lhs());
        tagNeverOptimistic(binaryNode.rhs());
    }
    return true;
}
 
Example #15
Source File: Debug.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Dump a token stream to stdout
 *
 * TODO: most other bugging goes to stderr, change?
 *
 * @param source the source
 * @param lexer  the lexer
 * @param stream the stream to dump
 */
public static void dumpTokens(final Source source, final Lexer lexer, final TokenStream stream) {
    TokenType type;
    int k = 0;
    do {
        while (k > stream.last()) {
            // Get more tokens.
            lexer.lexify();
        }

        final long token = stream.get(k);
        type = Token.descType(token);
        System.out.println("" + k + ": " + Token.toString(source, token, true));
        k++;
    } while(type != EOF);
}
 
Example #16
Source File: AssignSymbols.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveBinaryNode(final BinaryNode binaryNode) {
    if (binaryNode.isTokenType(TokenType.ASSIGN)) {
        return leaveASSIGN(binaryNode);
    }
    return super.leaveBinaryNode(binaryNode);
}
 
Example #17
Source File: LocalVariableTypesCalculator.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean enterBinaryNode(final BinaryNode binaryNode) {
    // NOTE: regardless of operator's lexical associativity, lhs is always evaluated first.
    final Expression lhs = binaryNode.lhs();
    final LvarType lhsType;
    if (!(lhs instanceof IdentNode && binaryNode.isTokenType(TokenType.ASSIGN))) {
        lhsType = visitExpression(lhs);
    } else {
        // Can't visit IdentNode on LHS of a simple assignment, as visits imply use, and this is def.
        // The type is irrelevant, as only RHS is used to determine the type anyway.
        lhsType = LvarType.UNDEFINED;
    }

    final boolean isLogical = binaryNode.isLogical();
    final Label joinLabel = isLogical ? new Label("") : null;
    if(isLogical) {
        jumpToLabel((JoinPredecessor)lhs, joinLabel);
    }

    final Expression rhs = binaryNode.rhs();
    final LvarType rhsType = visitExpression(rhs);
    if(isLogical) {
        jumpToLabel((JoinPredecessor)rhs, joinLabel);
    }
    joinOnLabel(joinLabel);

    final LvarType type = toLvarType(binaryNode.setOperands(lhsType.typeExpression, rhsType.typeExpression).getType());

    if(binaryNode.isAssignment() && lhs instanceof IdentNode) {
        if(binaryNode.isSelfModifying()) {
            onSelfAssignment((IdentNode)lhs, type);
        } else {
            onAssignment((IdentNode)lhs, type);
        }
    }
    typeStack.push(type);
    return false;
}
 
Example #18
Source File: AssignSymbols.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveBinaryNode(final BinaryNode binaryNode) {
    if (binaryNode.isTokenType(TokenType.ASSIGN)) {
        return leaveASSIGN(binaryNode);
    }
    return super.leaveBinaryNode(binaryNode);
}
 
Example #19
Source File: RecompilableScriptFunctionData.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static long tokenFor(final FunctionNode fn) {
    final int  position  = Token.descPosition(fn.getFirstToken());
    final long lastToken = Token.withDelimiter(fn.getLastToken());
    // EOL uses length field to store the line number
    final int  length    = Token.descPosition(lastToken) - position + (Token.descType(lastToken) == TokenType.EOL ? 0 : Token.descLength(lastToken));

    return Token.toDesc(TokenType.FUNCTION, position, length);
}
 
Example #20
Source File: BinaryNode.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Type getMostOptimisticType() {
    final TokenType tokenType = tokenType();
    if(tokenType == TokenType.ADD || tokenType == TokenType.ASSIGN_ADD) {
        return OPTIMISTIC_UNDECIDED_TYPE;
    } else if (CAN_OVERFLOW.contains(tokenType)) {
        return Type.INT;
    }
    return getMostPessimisticType();
}
 
Example #21
Source File: LocalVariableTypesCalculator.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean enterBinaryNode(final BinaryNode binaryNode) {
    // NOTE: regardless of operator's lexical associativity, lhs is always evaluated first.
    final Expression lhs = binaryNode.lhs();
    final LvarType lhsType;
    if (!(lhs instanceof IdentNode && binaryNode.isTokenType(TokenType.ASSIGN))) {
        lhsType = visitExpression(lhs);
    } else {
        // Can't visit IdentNode on LHS of a simple assignment, as visits imply use, and this is def.
        // The type is irrelevant, as only RHS is used to determine the type anyway.
        lhsType = LvarType.UNDEFINED;
    }

    final boolean isLogical = binaryNode.isLogical();
    final Label joinLabel = isLogical ? new Label("") : null;
    if(isLogical) {
        jumpToLabel((JoinPredecessor)lhs, joinLabel);
    }

    final Expression rhs = binaryNode.rhs();
    final LvarType rhsType = visitExpression(rhs);
    if(isLogical) {
        jumpToLabel((JoinPredecessor)rhs, joinLabel);
    }
    joinOnLabel(joinLabel);

    final LvarType type = toLvarType(binaryNode.setOperands(lhsType.typeExpression, rhsType.typeExpression).getType());

    if(binaryNode.isAssignment() && lhs instanceof IdentNode) {
        if(binaryNode.isSelfModifying()) {
            onSelfAssignment((IdentNode)lhs, type);
        } else {
            onAssignment((IdentNode)lhs, type);
        }
    }
    typeStack.push(type);
    return false;
}
 
Example #22
Source File: OptimisticTypesCalculator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean enterUnaryNode(final UnaryNode unaryNode) {
    if(unaryNode.isTokenType(TokenType.NOT) || unaryNode.isTokenType(TokenType.NEW)) {
        // Operand of boolean negation is never optimistic (always coerced to boolean).
        // Operand of "new" is never optimistic (always coerced to Object).
        tagNeverOptimistic(unaryNode.getExpression());
    }
    return true;
}
 
Example #23
Source File: BinaryNode.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private Type getTypeUncached() {
    if(type == OPTIMISTIC_UNDECIDED_TYPE) {
        return decideType(lhs.getType(), rhs.getType());
    }
    final Type widest = getWidestOperationType();
    if(type == null) {
        return widest;
    }
    if (tokenType() == TokenType.ASSIGN_SHR || tokenType() == TokenType.SHR) {
        return type;
    }
    return Type.narrowest(widest, Type.widest(type, Type.widest(lhs.getType(), rhs.getType())));
}
 
Example #24
Source File: AssignSymbols.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveBinaryNode(final BinaryNode binaryNode) {
    if (binaryNode.isTokenType(TokenType.ASSIGN)) {
        return leaveASSIGN(binaryNode);
    }
    return super.leaveBinaryNode(binaryNode);
}
 
Example #25
Source File: BinaryNode.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Type getMostOptimisticType() {
    final TokenType tokenType = tokenType();
    if(tokenType == TokenType.ADD || tokenType == TokenType.ASSIGN_ADD) {
        return OPTIMISTIC_UNDECIDED_TYPE;
    } else if (CAN_OVERFLOW.contains(tokenType)) {
        return Type.INT;
    }
    return getMostPessimisticType();
}
 
Example #26
Source File: AssignSymbols.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveBinaryNode(final BinaryNode binaryNode) {
    if (binaryNode.isTokenType(TokenType.ASSIGN)) {
        return leaveASSIGN(binaryNode);
    }
    return super.leaveBinaryNode(binaryNode);
}
 
Example #27
Source File: AssignSymbols.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Node leaveBinaryNode(final BinaryNode binaryNode) {
    if (binaryNode.isTokenType(TokenType.ASSIGN)) {
        return leaveASSIGN(binaryNode);
    }
    return super.leaveBinaryNode(binaryNode);
}
 
Example #28
Source File: TernaryNode.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void toString(final StringBuilder sb, final boolean printType) {
    final TokenType tokenType  = tokenType();
    final boolean   testParen  = tokenType.needsParens(getTest().tokenType(), true);
    final boolean   trueParen  = tokenType.needsParens(getTrueExpression().tokenType(), false);
    final boolean   falseParen = tokenType.needsParens(getFalseExpression().tokenType(), false);

    if (testParen) {
        sb.append('(');
    }
    getTest().toString(sb, printType);
    if (testParen) {
        sb.append(')');
    }

    sb.append(" ? ");

    if (trueParen) {
        sb.append('(');
    }
    getTrueExpression().toString(sb, printType);
    if (trueParen) {
        sb.append(')');
    }

    sb.append(" : ");

    if (falseParen) {
        sb.append('(');
    }
    getFalseExpression().toString(sb, printType);
    if (falseParen) {
        sb.append(')');
    }
}
 
Example #29
Source File: LocalVariableTypesCalculator.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean enterBinaryNode(final BinaryNode binaryNode) {
    // NOTE: regardless of operator's lexical associativity, lhs is always evaluated first.
    final Expression lhs = binaryNode.lhs();
    final LvarType lhsType;
    if (!(lhs instanceof IdentNode && binaryNode.isTokenType(TokenType.ASSIGN))) {
        lhsType = visitExpression(lhs);
    } else {
        // Can't visit IdentNode on LHS of a simple assignment, as visits imply use, and this is def.
        // The type is irrelevant, as only RHS is used to determine the type anyway.
        lhsType = LvarType.UNDEFINED;
    }

    final boolean isLogical = binaryNode.isLogical();
    final Label joinLabel = isLogical ? new Label("") : null;
    if(isLogical) {
        jumpToLabel((JoinPredecessor)lhs, joinLabel);
    }

    final Expression rhs = binaryNode.rhs();
    final LvarType rhsType = visitExpression(rhs);
    if(isLogical) {
        jumpToLabel((JoinPredecessor)rhs, joinLabel);
    }
    joinOnLabel(joinLabel);

    final LvarType type = toLvarType(binaryNode.setOperands(lhsType.typeExpression, rhsType.typeExpression).getType());

    if(binaryNode.isAssignment() && lhs instanceof IdentNode) {
        if(binaryNode.isSelfModifying()) {
            onSelfAssignment((IdentNode)lhs, type);
        } else {
            onAssignment((IdentNode)lhs, type);
        }
    }
    typeStack.push(type);
    return false;
}
 
Example #30
Source File: LiteralNode.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Constructor
 *
 * @param token   token
 * @param finish  finish
 * @param value   array literal value, a Node array
 */
protected ArrayLiteralNode(final long token, final int finish, final Expression[] value) {
    super(Token.recast(token, TokenType.ARRAY), finish, value);
    this.elementType = Type.UNKNOWN;
    this.presets     = null;
    this.postsets    = null;
    this.splitRanges = null;
}