Search in sources :

Example 66 with AstNode

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

the class Parser method arrayComprehensionLoop.

private ArrayComprehensionLoop arrayComprehensionLoop() throws IOException {
    if (nextToken() != Token.FOR)
        codeBug();
    int pos = ts.tokenBeg;
    int eachPos = -1, lp = -1, rp = -1, inPos = -1;
    boolean isForIn = false, isForOf = false;
    ArrayComprehensionLoop pn = new ArrayComprehensionLoop(pos);
    pushScope(pn);
    try {
        if (matchToken(Token.NAME)) {
            if (ts.getString().equals("each")) {
                eachPos = ts.tokenBeg - pos;
            } else {
                reportError("msg.no.paren.for");
            }
        }
        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);
        }
        switch(nextToken()) {
            case Token.IN:
                inPos = ts.tokenBeg - pos;
                isForIn = true;
                break;
            case Token.NAME:
                if ("of".equals(ts.getString())) {
                    if (eachPos != -1) {
                        reportError("msg.invalid.for.each");
                    }
                    inPos = ts.tokenBeg - pos;
                    isForOf = true;
                    break;
                }
            // fallthru
            default:
                reportError("msg.in.after.for.name");
        }
        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.setEachPosition(eachPos);
        pn.setIsForEach(eachPos != -1);
        pn.setParens(lp, rp);
        pn.setIsForOf(isForOf);
        return pn;
    } finally {
        popScope();
    }
}
Also used : ArrayComprehensionLoop(org.mozilla.javascript.ast.ArrayComprehensionLoop) AstNode(org.mozilla.javascript.ast.AstNode)

Example 67 with AstNode

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

Example 68 with AstNode

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

the class Parser method bitOrExpr.

private AstNode bitOrExpr() throws IOException {
    AstNode pn = bitXorExpr();
    while (matchToken(Token.BITOR)) {
        int opPos = ts.tokenBeg;
        pn = new InfixExpression(Token.BITOR, pn, bitXorExpr(), opPos);
    }
    return pn;
}
Also used : InfixExpression(org.mozilla.javascript.ast.InfixExpression) AstNode(org.mozilla.javascript.ast.AstNode)

Example 69 with AstNode

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

the class Parser method statements.

// This function does not match the closing RC: the caller matches
// the RC so it can provide a suitable error message if not matched.
// This means it's up to the caller to set the length of the node to
// include the closing RC.  The node start pos is set to the
// absolute buffer start position, and the caller should fix it up
// to be relative to the parent node.  All children of this block
// node are given relative start positions and correct lengths.
private AstNode statements(AstNode parent) throws IOException {
    if (// assertion can be invalid in bad code
    currentToken != Token.LC && !compilerEnv.isIdeMode())
        codeBug();
    int pos = ts.tokenBeg;
    AstNode block = parent != null ? parent : new Block(pos);
    block.setLineno(ts.lineno);
    int tt;
    while ((tt = peekToken()) > Token.EOF && tt != Token.RC) {
        block.addChild(statement());
    }
    block.setLength(ts.tokenBeg - pos);
    return block;
}
Also used : Block(org.mozilla.javascript.ast.Block) AstNode(org.mozilla.javascript.ast.AstNode)

Example 70 with AstNode

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

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