Search in sources :

Example 6 with EmptyExpression

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

the class IRFactory method transformTry.

private Node transformTry(TryStatement node) {
    decompiler.addToken(Token.TRY);
    decompiler.addEOL(Token.LC);
    Node tryBlock = transform(node.getTryBlock());
    decompiler.addEOL(Token.RC);
    Node catchBlocks = new Block();
    for (CatchClause cc : node.getCatchClauses()) {
        decompiler.addToken(Token.CATCH);
        decompiler.addToken(Token.LP);
        String varName = cc.getVarName().getIdentifier();
        decompiler.addName(varName);
        Node catchCond = null;
        AstNode ccc = cc.getCatchCondition();
        if (ccc != null) {
            decompiler.addName(" ");
            decompiler.addToken(Token.IF);
            catchCond = transform(ccc);
        } else {
            catchCond = new EmptyExpression();
        }
        decompiler.addToken(Token.RP);
        decompiler.addEOL(Token.LC);
        Node body = transform(cc.getBody());
        decompiler.addEOL(Token.RC);
        catchBlocks.addChildToBack(createCatch(varName, catchCond, body, cc.getLineno()));
    }
    Node finallyBlock = null;
    if (node.getFinallyBlock() != null) {
        decompiler.addToken(Token.FINALLY);
        decompiler.addEOL(Token.LC);
        finallyBlock = transform(node.getFinallyBlock());
        decompiler.addEOL(Token.RC);
    }
    return createTryCatchFinally(tryBlock, catchBlocks, finallyBlock, node.getLineno());
}
Also used : ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) Block(org.mozilla.javascript.ast.Block) CatchClause(org.mozilla.javascript.ast.CatchClause) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression)

Example 7 with EmptyExpression

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

the class Parser method forLoopInit.

private AstNode forLoopInit(int tt) throws IOException {
    try {
        // checked by variables() and relExpr()
        inForInit = true;
        AstNode init = null;
        if (tt == Token.SEMI) {
            init = new EmptyExpression(ts.tokenBeg, 1);
            init.setLineno(ts.lineno);
        } else if (tt == Token.VAR || tt == Token.LET) {
            consumeToken();
            init = variables(tt, ts.tokenBeg, false);
        } else {
            init = expr();
            markDestructuring(init);
        }
        return init;
    } finally {
        inForInit = false;
    }
}
Also used : AstNode(org.mozilla.javascript.ast.AstNode) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression)

Example 8 with EmptyExpression

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

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

the class Parser method xmlInitializer.

private AstNode xmlInitializer() throws IOException {
    if (currentToken != Token.LT)
        codeBug();
    int pos = ts.tokenBeg, tt = ts.getFirstXMLToken();
    if (tt != Token.XML && tt != Token.XMLEND) {
        reportError("msg.syntax");
        return makeErrorNode();
    }
    XmlLiteral pn = new XmlLiteral(pos);
    pn.setLineno(ts.lineno);
    for (; ; tt = ts.getNextXMLToken()) {
        switch(tt) {
            case Token.XML:
                pn.addFragment(new XmlString(ts.tokenBeg, ts.getString()));
                mustMatchToken(Token.LC, "msg.syntax");
                int beg = ts.tokenBeg;
                AstNode expr = (peekToken() == Token.RC) ? new EmptyExpression(beg, ts.tokenEnd - beg) : expr();
                mustMatchToken(Token.RC, "msg.syntax");
                XmlExpression xexpr = new XmlExpression(beg, expr);
                xexpr.setIsXmlAttribute(ts.isXMLAttribute());
                xexpr.setLength(ts.tokenEnd - beg);
                pn.addFragment(xexpr);
                break;
            case Token.XMLEND:
                pn.addFragment(new XmlString(ts.tokenBeg, ts.getString()));
                return pn;
            default:
                reportError("msg.syntax");
                return makeErrorNode();
        }
    }
}
Also used : XmlLiteral(org.mozilla.javascript.ast.XmlLiteral) XmlString(org.mozilla.javascript.ast.XmlString) XmlExpression(org.mozilla.javascript.ast.XmlExpression) AstNode(org.mozilla.javascript.ast.AstNode) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression)

Example 10 with EmptyExpression

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

the class Parser method forLoop.

private Loop forLoop() throws IOException {
    if (currentToken != Token.FOR)
        codeBug();
    consumeToken();
    int forPos = ts.tokenBeg, lineno = ts.lineno;
    boolean isForEach = false, isForIn = false, isForOf = false;
    int eachPos = -1, inPos = -1, lp = -1, rp = -1;
    // init is also foo in 'foo in object'
    AstNode init = null;
    // cond is also object in 'foo in object'
    AstNode cond = null;
    AstNode incr = null;
    Loop pn = null;
    Scope tempScope = new Scope();
    // decide below what AST class to use
    pushScope(tempScope);
    try {
        // See if this is a for each () instead of just a for ()
        if (matchToken(Token.NAME)) {
            if ("each".equals(ts.getString())) {
                isForEach = true;
                eachPos = ts.tokenBeg - forPos;
            } else {
                reportError("msg.no.paren.for");
            }
        }
        if (mustMatchToken(Token.LP, "msg.no.paren.for"))
            lp = ts.tokenBeg - forPos;
        int tt = peekToken();
        init = forLoopInit(tt);
        if (matchToken(Token.IN)) {
            isForIn = true;
            inPos = ts.tokenBeg - forPos;
            // object over which we're iterating
            cond = expr();
        } else if (compilerEnv.getLanguageVersion() >= Context.VERSION_ES6 && matchToken(Token.NAME) && "of".equals(ts.getString())) {
            isForOf = true;
            inPos = ts.tokenBeg - forPos;
            // object over which we're iterating
            cond = expr();
        } else {
            // ordinary for-loop
            mustMatchToken(Token.SEMI, "msg.no.semi.for");
            if (peekToken() == Token.SEMI) {
                // no loop condition
                cond = new EmptyExpression(ts.tokenBeg, 1);
                cond.setLineno(ts.lineno);
            } else {
                cond = expr();
            }
            mustMatchToken(Token.SEMI, "msg.no.semi.for.cond");
            int tmpPos = ts.tokenEnd;
            if (peekToken() == Token.RP) {
                incr = new EmptyExpression(tmpPos, 1);
                incr.setLineno(ts.lineno);
            } else {
                incr = expr();
            }
        }
        if (mustMatchToken(Token.RP, "msg.no.paren.for.ctrl"))
            rp = ts.tokenBeg - forPos;
        if (isForIn || isForOf) {
            ForInLoop fis = new ForInLoop(forPos);
            if (init instanceof VariableDeclaration) {
                // check that there was only one variable given
                if (((VariableDeclaration) init).getVariables().size() > 1) {
                    reportError("msg.mult.index");
                }
            }
            if (isForOf && isForEach) {
                reportError("msg.invalid.for.each");
            }
            fis.setIterator(init);
            fis.setIteratedObject(cond);
            fis.setInPosition(inPos);
            fis.setIsForEach(isForEach);
            fis.setEachPosition(eachPos);
            fis.setIsForOf(isForOf);
            pn = fis;
        } else {
            ForLoop fl = new ForLoop(forPos);
            fl.setInitializer(init);
            fl.setCondition(cond);
            fl.setIncrement(incr);
            pn = fl;
        }
        // replace temp scope with the new loop object
        currentScope.replaceWith(pn);
        popScope();
        // We have to parse the body -after- creating the loop node,
        // so that the loop node appears in the loopSet, allowing
        // break/continue statements to find the enclosing loop.
        enterLoop(pn);
        try {
            AstNode body = statement();
            pn.setLength(getNodeEnd(body) - forPos);
            pn.setBody(body);
        } finally {
            exitLoop();
        }
    } finally {
        if (currentScope == tempScope) {
            popScope();
        }
    }
    pn.setParens(lp, rp);
    pn.setLineno(lineno);
    return pn;
}
Also used : WhileLoop(org.mozilla.javascript.ast.WhileLoop) ForLoop(org.mozilla.javascript.ast.ForLoop) GeneratorExpressionLoop(org.mozilla.javascript.ast.GeneratorExpressionLoop) ArrayComprehensionLoop(org.mozilla.javascript.ast.ArrayComprehensionLoop) Loop(org.mozilla.javascript.ast.Loop) ForInLoop(org.mozilla.javascript.ast.ForInLoop) DoLoop(org.mozilla.javascript.ast.DoLoop) ForInLoop(org.mozilla.javascript.ast.ForInLoop) Scope(org.mozilla.javascript.ast.Scope) ForLoop(org.mozilla.javascript.ast.ForLoop) VariableDeclaration(org.mozilla.javascript.ast.VariableDeclaration) AstNode(org.mozilla.javascript.ast.AstNode) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression)

Aggregations

AstNode (org.mozilla.javascript.ast.AstNode)10 EmptyExpression (org.mozilla.javascript.ast.EmptyExpression)10 XmlString (org.mozilla.javascript.ast.XmlString)4 FunctionNode (org.mozilla.javascript.ast.FunctionNode)3 LetNode (org.mozilla.javascript.ast.LetNode)3 ScriptNode (org.mozilla.javascript.ast.ScriptNode)3 ParenthesizedExpression (org.mozilla.javascript.ast.ParenthesizedExpression)2 XmlExpression (org.mozilla.javascript.ast.XmlExpression)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ArrayComprehensionLoop (org.mozilla.javascript.ast.ArrayComprehensionLoop)1 ArrayLiteral (org.mozilla.javascript.ast.ArrayLiteral)1 Block (org.mozilla.javascript.ast.Block)1 CatchClause (org.mozilla.javascript.ast.CatchClause)1 Comment (org.mozilla.javascript.ast.Comment)1 DoLoop (org.mozilla.javascript.ast.DoLoop)1 ErrorNode (org.mozilla.javascript.ast.ErrorNode)1 ForInLoop (org.mozilla.javascript.ast.ForInLoop)1