Search in sources :

Example 61 with AstNode

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

the class Parser method parse.

private AstRoot parse() throws IOException {
    int pos = 0;
    AstRoot root = new AstRoot(pos);
    currentScope = currentScriptOrFn = root;
    // line number where source starts
    int baseLineno = ts.lineno;
    // in case source is empty
    int end = pos;
    boolean inDirectivePrologue = true;
    boolean savedStrictMode = inUseStrictDirective;
    inUseStrictDirective = defaultUseStrictDirective;
    if (inUseStrictDirective) {
        root.setInStrictMode(true);
    }
    try {
        for (; ; ) {
            int tt = peekToken();
            if (tt <= Token.EOF) {
                break;
            }
            AstNode n;
            if (tt == Token.FUNCTION) {
                consumeToken();
                try {
                    n = function(calledByCompileFunction ? FunctionNode.FUNCTION_EXPRESSION : FunctionNode.FUNCTION_STATEMENT);
                } catch (ParserException e) {
                    break;
                }
            } else {
                n = statement();
                if (inDirectivePrologue) {
                    String directive = getDirective(n);
                    if (directive == null) {
                        inDirectivePrologue = false;
                    } else if (directive.equals("严格模式")) {
                        inUseStrictDirective = true;
                        root.setInStrictMode(true);
                    }
                }
            }
            end = getNodeEnd(n);
            root.addChildToBack(n);
            n.setParent(root);
        }
    } catch (StackOverflowError ex) {
        String msg = lookupMessage("msg.too.deep.parser.recursion");
        if (!compilerEnv.isIdeMode())
            throw Context.reportRuntimeError(msg, sourceURI, ts.lineno, null, 0);
    } finally {
        inUseStrictDirective = savedStrictMode;
    }
    if (this.syntaxErrorCount != 0) {
        String msg = String.valueOf(this.syntaxErrorCount);
        msg = lookupMessage("msg.got.syntax.errors", msg);
        if (!compilerEnv.isIdeMode())
            throw errorReporter.runtimeError(msg, sourceURI, baseLineno, null, 0);
    }
    // add comments to root in lexical order
    if (scannedComments != null) {
        // If we find a comment beyond end of our last statement or
        // function, extend the root bounds to the end of that comment.
        int last = scannedComments.size() - 1;
        end = Math.max(end, getNodeEnd(scannedComments.get(last)));
        for (Comment c : scannedComments) {
            root.addComment(c);
        }
    }
    root.setLength(end - pos);
    root.setSourceName(sourceURI);
    root.setBaseLineno(baseLineno);
    root.setEndLineno(ts.lineno);
    return root;
}
Also used : Comment(org.mozilla.javascript.ast.Comment) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode) AstRoot(org.mozilla.javascript.ast.AstRoot)

Example 62 with AstNode

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

the class Parser method destructuringArray.

boolean destructuringArray(ArrayLiteral array, int variableType, String tempName, Node parent, List<String> destructuringNames) {
    boolean empty = true;
    int setOp = variableType == Token.CONST ? Token.SETCONST : Token.SETNAME;
    int index = 0;
    for (AstNode n : array.getElements()) {
        if (n.getType() == Token.EMPTY) {
            index++;
            continue;
        }
        Node rightElem = new Node(Token.GETELEM, createName(tempName), createNumber(index));
        if (n.getType() == Token.NAME) {
            String name = n.getString();
            parent.addChildToBack(new Node(setOp, createName(Token.BINDNAME, name, null), rightElem));
            if (variableType != -1) {
                defineSymbol(variableType, name, true);
                destructuringNames.add(name);
            }
        } else {
            parent.addChildToBack(destructuringAssignmentHelper(variableType, n, rightElem, currentScriptOrFn.getNextTempName()));
        }
        index++;
        empty = false;
    }
    return empty;
}
Also used : 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) AstNode(org.mozilla.javascript.ast.AstNode)

Example 63 with AstNode

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

the class Parser method expr.

private AstNode expr() throws IOException {
    AstNode pn = assignExpr();
    int pos = pn.getPosition();
    while (matchToken(Token.COMMA)) {
        int opPos = ts.tokenBeg;
        if (compilerEnv.isStrictMode() && !pn.hasSideEffects())
            addStrictWarning("msg.no.side.effects", "", pos, nodeEnd(pn) - pos);
        if (peekToken() == Token.YIELD)
            reportError("msg.yield.parenthesized");
        pn = new InfixExpression(Token.COMMA, pn, assignExpr(), opPos);
    }
    return pn;
}
Also used : InfixExpression(org.mozilla.javascript.ast.InfixExpression) AstNode(org.mozilla.javascript.ast.AstNode)

Example 64 with AstNode

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

the class Parser method generatorExpressionLoop.

private GeneratorExpressionLoop generatorExpressionLoop() throws IOException {
    if (nextToken() != Token.FOR)
        codeBug();
    int pos = ts.tokenBeg;
    int lp = -1, rp = -1, inPos = -1;
    GeneratorExpressionLoop pn = new GeneratorExpressionLoop(pos);
    pushScope(pn);
    try {
        if (mustMatchToken(Token.LP, "msg.no.paren.for")) {
            lp = ts.tokenBeg - pos;
        }
        AstNode iter = null;
        switch(peekToken()) {
            case Token.LB:
            case Token.LC:
                // handle destructuring assignment
                iter = destructuringPrimaryExpr();
                markDestructuring(iter);
                break;
            case Token.NAME:
                consumeToken();
                iter = createNameNode();
                break;
            default:
                reportError("msg.bad.var");
        }
        // be restricted to the array comprehension
        if (iter.getType() == Token.NAME) {
            defineSymbol(Token.LET, ts.getString(), true);
        }
        if (mustMatchToken(Token.IN, "msg.in.after.for.name"))
            inPos = ts.tokenBeg - pos;
        AstNode obj = expr();
        if (mustMatchToken(Token.RP, "msg.no.paren.for.ctrl"))
            rp = ts.tokenBeg - pos;
        pn.setLength(ts.tokenEnd - pos);
        pn.setIterator(iter);
        pn.setIteratedObject(obj);
        pn.setInPosition(inPos);
        pn.setParens(lp, rp);
        return pn;
    } finally {
        popScope();
    }
}
Also used : GeneratorExpressionLoop(org.mozilla.javascript.ast.GeneratorExpressionLoop) AstNode(org.mozilla.javascript.ast.AstNode)

Example 65 with AstNode

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

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