Search in sources :

Example 1 with ReturnStatement

use of org.mozilla.javascript.ast.ReturnStatement in project st-js by st-js.

the class RhinoJavaScriptBuilder method returnStatement.

/**
 * {@inheritDoc}
 */
@Override
public AstNode returnStatement(AstNode returnValue) {
    ReturnStatement r = new ReturnStatement();
    r.setReturnValue(returnValue);
    return r;
}
Also used : ReturnStatement(org.mozilla.javascript.ast.ReturnStatement)

Example 2 with ReturnStatement

use of org.mozilla.javascript.ast.ReturnStatement in project HL4A by HL4A.

the class Parser method parseFunctionBody.

private AstNode parseFunctionBody(int type, FunctionNode fnNode) throws IOException {
    boolean isExpressionClosure = false;
    if (!matchToken(Token.LC)) {
        if (compilerEnv.getLanguageVersion() < Context.VERSION_1_8 && type != FunctionNode.ARROW_FUNCTION) {
            reportError("msg.no.brace.body");
        } else {
            isExpressionClosure = true;
        }
    }
    boolean isArrow = type == FunctionNode.ARROW_FUNCTION;
    ++nestingOfFunction;
    int pos = ts.tokenBeg;
    // starts at LC position
    Block pn = new Block(pos);
    boolean inDirectivePrologue = true;
    boolean savedStrictMode = inUseStrictDirective;
    // Don't set 'inUseStrictDirective' to false: inherit strict mode.
    pn.setLineno(ts.lineno);
    try {
        if (isExpressionClosure) {
            AstNode returnValue = assignExpr();
            ReturnStatement n = new ReturnStatement(returnValue.getPosition(), returnValue.getLength(), returnValue);
            // expression closure flag is required on both nodes
            n.putProp(Node.EXPRESSION_CLOSURE_PROP, Boolean.TRUE);
            pn.putProp(Node.EXPRESSION_CLOSURE_PROP, Boolean.TRUE);
            if (isArrow) {
                n.putProp(Node.ARROW_FUNCTION_PROP, Boolean.TRUE);
            }
            pn.addStatement(n);
        } else {
            bodyLoop: for (; ; ) {
                AstNode n;
                int tt = peekToken();
                switch(tt) {
                    case Token.ERROR:
                    case Token.EOF:
                    case Token.RC:
                        break bodyLoop;
                    case Token.FUNCTION:
                        consumeToken();
                        n = function(FunctionNode.FUNCTION_STATEMENT);
                        break;
                    default:
                        n = statement();
                        if (inDirectivePrologue) {
                            String directive = getDirective(n);
                            if (directive == null) {
                                inDirectivePrologue = false;
                            } else if (directive.equals("严格模式")) {
                                inUseStrictDirective = true;
                                fnNode.setInStrictMode(true);
                                if (!savedStrictMode) {
                                    setRequiresActivation();
                                }
                            }
                        }
                        break;
                }
                pn.addStatement(n);
            }
        }
    } catch (ParserException e) {
    // Ignore it
    } finally {
        --nestingOfFunction;
        inUseStrictDirective = savedStrictMode;
    }
    int end = ts.tokenEnd;
    getAndResetJsDoc();
    if (!isExpressionClosure && mustMatchToken(Token.RC, "msg.no.brace.after.body"))
        end = ts.tokenEnd;
    pn.setLength(end - pos);
    return pn;
}
Also used : ReturnStatement(org.mozilla.javascript.ast.ReturnStatement) Block(org.mozilla.javascript.ast.Block) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode)

Example 3 with ReturnStatement

use of org.mozilla.javascript.ast.ReturnStatement in project HL4A by HL4A.

the class Parser method returnOrYield.

private AstNode returnOrYield(int tt, boolean exprContext) throws IOException {
    if (!insideFunction()) {
        reportError(tt == Token.RETURN ? "msg.bad.return" : "msg.bad.yield");
    }
    consumeToken();
    int lineno = ts.lineno, pos = ts.tokenBeg, end = ts.tokenEnd;
    AstNode e = null;
    // This is ugly, but we don't want to require a semicolon.
    switch(peekTokenOrEOL()) {
        case Token.SEMI:
        case Token.RC:
        case Token.RB:
        case Token.RP:
        case Token.EOF:
        case Token.EOL:
        case Token.ERROR:
        case Token.YIELD:
            break;
        default:
            e = expr();
            end = getNodeEnd(e);
    }
    int before = endFlags;
    AstNode ret;
    if (tt == Token.RETURN) {
        endFlags |= e == null ? Node.END_RETURNS : Node.END_RETURNS_VALUE;
        ret = new ReturnStatement(pos, end - pos, e);
        // see if we need a strict mode warning
        if (nowAllSet(before, endFlags, Node.END_RETURNS | Node.END_RETURNS_VALUE))
            addStrictWarning("msg.return.inconsistent", "", pos, end - pos);
    } else {
        if (!insideFunction())
            reportError("msg.bad.yield");
        endFlags |= Node.END_YIELDS;
        ret = new Yield(pos, end - pos, e);
        setRequiresActivation();
        setIsGenerator();
        if (!exprContext) {
            ret = new ExpressionStatement(ret);
        }
    }
    // see if we are mixing yields and value returns.
    if (insideFunction() && nowAllSet(before, endFlags, Node.END_YIELDS | Node.END_RETURNS_VALUE)) {
        Name name = ((FunctionNode) currentScriptOrFn).getFunctionName();
        if (name == null || name.length() == 0)
            addError("msg.anon.generator.returns", "");
        else
            addError("msg.generator.returns", name.getIdentifier());
    }
    ret.setLineno(lineno);
    return ret;
}
Also used : ExpressionStatement(org.mozilla.javascript.ast.ExpressionStatement) ReturnStatement(org.mozilla.javascript.ast.ReturnStatement) FunctionNode(org.mozilla.javascript.ast.FunctionNode) Yield(org.mozilla.javascript.ast.Yield) AstNode(org.mozilla.javascript.ast.AstNode) Name(org.mozilla.javascript.ast.Name)

Aggregations

ReturnStatement (org.mozilla.javascript.ast.ReturnStatement)3 AstNode (org.mozilla.javascript.ast.AstNode)2 Block (org.mozilla.javascript.ast.Block)1 ExpressionStatement (org.mozilla.javascript.ast.ExpressionStatement)1 FunctionNode (org.mozilla.javascript.ast.FunctionNode)1 Name (org.mozilla.javascript.ast.Name)1 XmlString (org.mozilla.javascript.ast.XmlString)1 Yield (org.mozilla.javascript.ast.Yield)1