Search in sources :

Example 31 with AstNode

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

the class Parser method shiftExpr.

private AstNode shiftExpr() throws IOException {
    AstNode pn = addExpr();
    for (; ; ) {
        int tt = peekToken(), opPos = ts.tokenBeg;
        switch(tt) {
            case Token.LSH:
            case Token.URSH:
            case Token.RSH:
                consumeToken();
                pn = new InfixExpression(tt, pn, addExpr(), opPos);
                continue;
        }
        break;
    }
    return pn;
}
Also used : InfixExpression(org.mozilla.javascript.ast.InfixExpression) AstNode(org.mozilla.javascript.ast.AstNode)

Example 32 with AstNode

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

the class Parser method parseFunctionParams.

private void parseFunctionParams(FunctionNode fnNode) throws IOException {
    if (matchToken(Token.RP)) {
        fnNode.setRp(ts.tokenBeg - fnNode.getPosition());
        return;
    }
    // Would prefer not to call createDestructuringAssignment until codegen,
    // but the symbol definitions have to happen now, before body is parsed.
    Map<String, Node> destructuring = null;
    Set<String> paramNames = new HashSet<String>();
    do {
        int tt = peekToken();
        if (tt == Token.LB || tt == Token.LC) {
            AstNode expr = destructuringPrimaryExpr();
            markDestructuring(expr);
            fnNode.addParam(expr);
            // variables from the destructuring assignment
            if (destructuring == null) {
                destructuring = new HashMap<String, Node>();
            }
            String pname = currentScriptOrFn.getNextTempName();
            defineSymbol(Token.LP, pname, false);
            destructuring.put(pname, expr);
        } else {
            if (mustMatchToken(Token.NAME, "msg.no.parm")) {
                Name paramNameNode = createNameNode();
                Comment jsdocNodeForName = getAndResetJsDoc();
                if (jsdocNodeForName != null) {
                    paramNameNode.setJsDocNode(jsdocNodeForName);
                }
                fnNode.addParam(paramNameNode);
                String paramName = ts.getString();
                defineSymbol(Token.LP, paramName);
                if (this.inUseStrictDirective) {
                    if ("eval".equals(paramName) || "arguments".equals(paramName)) {
                        reportError("msg.bad.id.strict", paramName);
                    }
                    if (paramNames.contains(paramName))
                        addError("msg.dup.param.strict", paramName);
                    paramNames.add(paramName);
                }
            } else {
                fnNode.addParam(makeErrorNode());
            }
        }
    } while (matchToken(Token.COMMA));
    if (destructuring != null) {
        Node destructuringNode = new Node(Token.COMMA);
        // Add assignment helper for each destructuring parameter
        for (Map.Entry<String, Node> param : destructuring.entrySet()) {
            Node assign = createDestructuringAssignment(Token.VAR, param.getValue(), createName(param.getKey()));
            destructuringNode.addChildToBack(assign);
        }
        fnNode.putProp(Node.DESTRUCTURING_PARAMS, destructuringNode);
    }
    if (mustMatchToken(Token.RP, "msg.no.paren.after.parms")) {
        fnNode.setRp(ts.tokenBeg - fnNode.getPosition());
    }
}
Also used : Comment(org.mozilla.javascript.ast.Comment) ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) ErrorNode(org.mozilla.javascript.ast.ErrorNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) XmlString(org.mozilla.javascript.ast.XmlString) Map(java.util.Map) HashMap(java.util.HashMap) AstNode(org.mozilla.javascript.ast.AstNode) HashSet(java.util.HashSet) Name(org.mozilla.javascript.ast.Name)

Example 33 with AstNode

use of org.mozilla.javascript.ast.AstNode 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 34 with AstNode

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

the class Parser method letStatement.

private AstNode letStatement() throws IOException {
    if (currentToken != Token.LET)
        codeBug();
    consumeToken();
    int lineno = ts.lineno, pos = ts.tokenBeg;
    AstNode pn;
    if (peekToken() == Token.LP) {
        pn = let(true, pos);
    } else {
        // else, e.g.: let x=6, y=7;
        pn = variables(Token.LET, pos, true);
    }
    pn.setLineno(lineno);
    return pn;
}
Also used : AstNode(org.mozilla.javascript.ast.AstNode)

Example 35 with AstNode

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

the class RhinoJavaScriptBuilder method switchStatement.

/**
 * {@inheritDoc}
 */
@Override
public AstNode switchStatement(AstNode expr, Iterable<AstNode> cases) {
    SwitchStatement s = new SwitchStatement();
    s.setExpression(expr);
    for (AstNode c : cases) {
        // the user must make sure it sends the correct types. TODO the code can check and build the correct type if
        // needed
        s.addCase((SwitchCase) c);
    }
    return s;
}
Also used : SwitchStatement(org.mozilla.javascript.ast.SwitchStatement) AstNode(org.mozilla.javascript.ast.AstNode)

Aggregations

AstNode (org.mozilla.javascript.ast.AstNode)80 FunctionNode (org.mozilla.javascript.ast.FunctionNode)22 LetNode (org.mozilla.javascript.ast.LetNode)22 ScriptNode (org.mozilla.javascript.ast.ScriptNode)19 XmlString (org.mozilla.javascript.ast.XmlString)15 InfixExpression (org.mozilla.javascript.ast.InfixExpression)13 EmptyExpression (org.mozilla.javascript.ast.EmptyExpression)9 Comment (org.mozilla.javascript.ast.Comment)8 Name (org.mozilla.javascript.ast.Name)8 ArrayList (java.util.ArrayList)6 Block (org.mozilla.javascript.ast.Block)5 ExpressionStatement (org.mozilla.javascript.ast.ExpressionStatement)5 VariableDeclaration (org.mozilla.javascript.ast.VariableDeclaration)5 ArrayComprehensionLoop (org.mozilla.javascript.ast.ArrayComprehensionLoop)4 ErrorNode (org.mozilla.javascript.ast.ErrorNode)4 GeneratorExpressionLoop (org.mozilla.javascript.ast.GeneratorExpressionLoop)4 DoLoop (org.mozilla.javascript.ast.DoLoop)3 Scope (org.mozilla.javascript.ast.Scope)3 VariableInitializer (org.mozilla.javascript.ast.VariableInitializer)3 IOException (java.io.IOException)2