Search in sources :

Example 51 with AstNode

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

the class Parser method objliteralProperty.

private AstNode objliteralProperty() throws IOException {
    AstNode pname;
    int tt = peekToken();
    switch(tt) {
        case Token.NAME:
            pname = createNameNode();
            break;
        case Token.STRING:
            pname = createStringLiteral();
            break;
        case Token.NUMBER:
            pname = new NumberLiteral(ts.tokenBeg, ts.getString(), ts.getNumber());
            break;
        default:
            if (compilerEnv.isReservedKeywordAsIdentifier() && TokenStream.isKeyword(ts.getString(), compilerEnv.getLanguageVersion(), inUseStrictDirective)) {
                // convert keyword to property name, e.g. ({if: 1})
                pname = createNameNode();
                break;
            }
            return null;
    }
    return pname;
}
Also used : AstNode(org.mozilla.javascript.ast.AstNode) NumberLiteral(org.mozilla.javascript.ast.NumberLiteral)

Example 52 with AstNode

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

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

the class Parser method argumentList.

private List<AstNode> argumentList() throws IOException {
    if (matchToken(Token.RP))
        return null;
    List<AstNode> result = new ArrayList<AstNode>();
    boolean wasInForInit = inForInit;
    inForInit = false;
    try {
        do {
            if (peekToken() == Token.YIELD) {
                reportError("msg.yield.parenthesized");
            }
            AstNode en = assignExpr();
            if (peekToken() == Token.FOR) {
                try {
                    result.add(generatorExpression(en, 0, true));
                } catch (IOException ex) {
                // #TODO
                }
            } else {
                result.add(en);
            }
        } while (matchToken(Token.COMMA));
    } finally {
        inForInit = wasInForInit;
    }
    mustMatchToken(Token.RP, "msg.no.paren.arg");
    return result;
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) AstNode(org.mozilla.javascript.ast.AstNode)

Example 54 with AstNode

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

the class Parser method arrowFunction.

private AstNode arrowFunction(AstNode params) throws IOException {
    // line number where source starts
    int baseLineno = ts.lineno;
    // start of "function" kwd
    int functionSourceStart = params != null ? params.getPosition() : -1;
    FunctionNode fnNode = new FunctionNode(functionSourceStart);
    fnNode.setFunctionType(FunctionNode.ARROW_FUNCTION);
    fnNode.setJsDocNode(getAndResetJsDoc());
    // Would prefer not to call createDestructuringAssignment until codegen,
    // but the symbol definitions have to happen now, before body is parsed.
    Map<String, Node> destructuring = new HashMap<String, Node>();
    Set<String> paramNames = new HashSet<String>();
    PerFunctionVariables savedVars = new PerFunctionVariables(fnNode);
    try {
        if (params instanceof ParenthesizedExpression) {
            fnNode.setParens(0, params.getLength());
            AstNode p = ((ParenthesizedExpression) params).getExpression();
            if (!(p instanceof EmptyExpression)) {
                arrowFunctionParams(fnNode, p, destructuring, paramNames);
            }
        } else {
            arrowFunctionParams(fnNode, params, destructuring, paramNames);
        }
        if (!destructuring.isEmpty()) {
            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);
        }
        fnNode.setBody(parseFunctionBody(FunctionNode.ARROW_FUNCTION, fnNode));
        fnNode.setEncodedSourceBounds(functionSourceStart, ts.tokenEnd);
        fnNode.setLength(ts.tokenEnd - functionSourceStart);
    } finally {
        savedVars.restore();
    }
    if (fnNode.isGenerator()) {
        reportError("msg.arrowfunction.generator");
        return makeErrorNode();
    }
    fnNode.setSourceName(sourceURI);
    fnNode.setBaseLineno(baseLineno);
    fnNode.setEndLineno(ts.lineno);
    return fnNode;
}
Also used : ParenthesizedExpression(org.mozilla.javascript.ast.ParenthesizedExpression) HashMap(java.util.HashMap) 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) FunctionNode(org.mozilla.javascript.ast.FunctionNode) XmlString(org.mozilla.javascript.ast.XmlString) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression) Map(java.util.Map) HashMap(java.util.HashMap) AstNode(org.mozilla.javascript.ast.AstNode) HashSet(java.util.HashSet)

Example 55 with AstNode

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

the class Parser method whileLoop.

private WhileLoop whileLoop() throws IOException {
    if (currentToken != Token.WHILE)
        codeBug();
    consumeToken();
    int pos = ts.tokenBeg;
    WhileLoop pn = new WhileLoop(pos);
    pn.setLineno(ts.lineno);
    enterLoop(pn);
    try {
        ConditionData data = condition();
        pn.setCondition(data.condition);
        pn.setParens(data.lp - pos, data.rp - pos);
        AstNode body = statement();
        pn.setLength(getNodeEnd(body) - pos);
        pn.setBody(body);
    } finally {
        exitLoop();
    }
    return pn;
}
Also used : WhileLoop(org.mozilla.javascript.ast.WhileLoop) 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