Search in sources :

Example 1 with ExpressionStatement

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

the class Parser method let.

// have to pass in 'let' kwd position to compute kid offsets properly
private AstNode let(boolean isStatement, int pos) throws IOException {
    LetNode pn = new LetNode(pos);
    pn.setLineno(ts.lineno);
    if (mustMatchToken(Token.LP, "msg.no.paren.after.let"))
        pn.setLp(ts.tokenBeg - pos);
    pushScope(pn);
    try {
        VariableDeclaration vars = variables(Token.LET, ts.tokenBeg, isStatement);
        pn.setVariables(vars);
        if (mustMatchToken(Token.RP, "msg.no.paren.let")) {
            pn.setRp(ts.tokenBeg - pos);
        }
        if (isStatement && peekToken() == Token.LC) {
            // let statement
            consumeToken();
            // position stmt at LC
            int beg = ts.tokenBeg;
            AstNode stmt = statements();
            mustMatchToken(Token.RC, "msg.no.curly.let");
            stmt.setLength(ts.tokenEnd - beg);
            pn.setLength(ts.tokenEnd - pos);
            pn.setBody(stmt);
            pn.setType(Token.LET);
        } else {
            // let expression
            AstNode expr = expr();
            pn.setLength(getNodeEnd(expr) - pos);
            pn.setBody(expr);
            if (isStatement) {
                // let expression in statement context
                ExpressionStatement es = new ExpressionStatement(pn, !insideFunction());
                es.setLineno(pn.getLineno());
                return es;
            }
        }
    } finally {
        popScope();
    }
    return pn;
}
Also used : LetNode(org.mozilla.javascript.ast.LetNode) ExpressionStatement(org.mozilla.javascript.ast.ExpressionStatement) VariableDeclaration(org.mozilla.javascript.ast.VariableDeclaration) AstNode(org.mozilla.javascript.ast.AstNode)

Example 2 with ExpressionStatement

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

the class Parser method nameOrLabel.

/**
 * Found a name in a statement context.  If it's a label, we gather
 * up any following labels and the next non-label statement into a
 * {@link LabeledStatement} "bundle" and return that.  Otherwise we parse
 * an expression and return it wrapped in an {@link ExpressionStatement}.
 */
private AstNode nameOrLabel() throws IOException {
    if (currentToken != Token.NAME)
        throw codeBug();
    int pos = ts.tokenBeg;
    // set check for label and call down to primaryExpr
    currentFlaggedToken |= TI_CHECK_LABEL;
    AstNode expr = expr();
    if (expr.getType() != Token.LABEL) {
        AstNode n = new ExpressionStatement(expr, !insideFunction());
        n.lineno = expr.lineno;
        return n;
    }
    LabeledStatement bundle = new LabeledStatement(pos);
    recordLabel((Label) expr, bundle);
    bundle.setLineno(ts.lineno);
    // look for more labels
    AstNode stmt = null;
    while (peekToken() == Token.NAME) {
        currentFlaggedToken |= TI_CHECK_LABEL;
        expr = expr();
        if (expr.getType() != Token.LABEL) {
            stmt = new ExpressionStatement(expr, !insideFunction());
            autoInsertSemicolon(stmt);
            break;
        }
        recordLabel((Label) expr, bundle);
    }
    // no more labels; now parse the labeled statement
    try {
        currentLabel = bundle;
        if (stmt == null) {
            stmt = statementHelper();
        }
    } finally {
        currentLabel = null;
        // remove the labels for this statement from the global set
        for (Label lb : bundle.getLabels()) {
            labelSet.remove(lb.getName());
        }
    }
    // If stmt has parent assigned its position already is relative
    // (See bug #710225)
    bundle.setLength(stmt.getParent() == null ? getNodeEnd(stmt) - pos : getNodeEnd(stmt));
    bundle.setStatement(stmt);
    return bundle;
}
Also used : LabeledStatement(org.mozilla.javascript.ast.LabeledStatement) ExpressionStatement(org.mozilla.javascript.ast.ExpressionStatement) Label(org.mozilla.javascript.ast.Label) AstNode(org.mozilla.javascript.ast.AstNode)

Example 3 with ExpressionStatement

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

the class RhinoJavaScriptBuilder method expressionStatement.

/**
 * {@inheritDoc}
 */
@Override
public AstNode expressionStatement(AstNode expr) {
    ExpressionStatement e = new ExpressionStatement();
    e.setExpression(expr);
    return e;
}
Also used : ExpressionStatement(org.mozilla.javascript.ast.ExpressionStatement)

Example 4 with ExpressionStatement

use of org.mozilla.javascript.ast.ExpressionStatement 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)

Example 5 with ExpressionStatement

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

the class Parser method statementHelper.

private AstNode statementHelper() throws IOException {
    // If the statement is set, then it's been told its label by now.
    if (currentLabel != null && currentLabel.getStatement() != null)
        currentLabel = null;
    AstNode pn = null;
    int tt = peekToken(), pos = ts.tokenBeg;
    switch(tt) {
        case Token.IF:
            return ifStatement();
        case Token.SWITCH:
            return switchStatement();
        case Token.WHILE:
            return whileLoop();
        case Token.DO:
            return doLoop();
        case Token.FOR:
            return forLoop();
        case Token.TRY:
            return tryStatement();
        case Token.THROW:
            pn = throwStatement();
            break;
        case Token.BREAK:
            pn = breakStatement();
            break;
        case Token.CONTINUE:
            pn = continueStatement();
            break;
        case Token.WITH:
            if (this.inUseStrictDirective) {
                reportError("msg.no.with.strict");
            }
            return withStatement();
        case Token.CONST:
        case Token.VAR:
            consumeToken();
            int lineno = ts.lineno;
            pn = variables(currentToken, ts.tokenBeg, true);
            pn.setLineno(lineno);
            break;
        case Token.LET:
            pn = letStatement();
            if (pn instanceof VariableDeclaration && peekToken() == Token.SEMI)
                break;
            return pn;
        case Token.RETURN:
        case Token.YIELD:
            pn = returnOrYield(tt, false);
            break;
        case Token.DEBUGGER:
            consumeToken();
            pn = new KeywordLiteral(ts.tokenBeg, ts.tokenEnd - ts.tokenBeg, tt);
            pn.setLineno(ts.lineno);
            break;
        case Token.LC:
            return block();
        case Token.ERROR:
            consumeToken();
            return makeErrorNode();
        case Token.SEMI:
            consumeToken();
            pos = ts.tokenBeg;
            pn = new EmptyStatement(pos, ts.tokenEnd - pos);
            pn.setLineno(ts.lineno);
            return pn;
        case Token.FUNCTION:
            consumeToken();
            return function(FunctionNode.FUNCTION_EXPRESSION_STATEMENT);
        case Token.DEFAULT:
            pn = defaultXmlNamespace();
            break;
        case Token.NAME:
            pn = nameOrLabel();
            if (pn instanceof ExpressionStatement)
                break;
            // LabeledStatement
            return pn;
        default:
            lineno = ts.lineno;
            pn = new ExpressionStatement(expr(), !insideFunction());
            pn.setLineno(lineno);
            break;
    }
    autoInsertSemicolon(pn);
    return pn;
}
Also used : KeywordLiteral(org.mozilla.javascript.ast.KeywordLiteral) ExpressionStatement(org.mozilla.javascript.ast.ExpressionStatement) EmptyStatement(org.mozilla.javascript.ast.EmptyStatement) VariableDeclaration(org.mozilla.javascript.ast.VariableDeclaration) AstNode(org.mozilla.javascript.ast.AstNode)

Aggregations

ExpressionStatement (org.mozilla.javascript.ast.ExpressionStatement)6 AstNode (org.mozilla.javascript.ast.AstNode)5 VariableDeclaration (org.mozilla.javascript.ast.VariableDeclaration)2 EmptyStatement (org.mozilla.javascript.ast.EmptyStatement)1 FunctionNode (org.mozilla.javascript.ast.FunctionNode)1 KeywordLiteral (org.mozilla.javascript.ast.KeywordLiteral)1 Label (org.mozilla.javascript.ast.Label)1 LabeledStatement (org.mozilla.javascript.ast.LabeledStatement)1 LetNode (org.mozilla.javascript.ast.LetNode)1 Name (org.mozilla.javascript.ast.Name)1 ReturnStatement (org.mozilla.javascript.ast.ReturnStatement)1 UnaryExpression (org.mozilla.javascript.ast.UnaryExpression)1 Yield (org.mozilla.javascript.ast.Yield)1