Search in sources :

Example 71 with AstNode

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

the class Parser method propertyAccess.

/**
 * Handles any construct following a "." or ".." operator.
 * @param pn the left-hand side (target) of the operator.  Never null.
 * @return a PropertyGet, XmlMemberGet, or ErrorNode
 */
private AstNode propertyAccess(int tt, AstNode pn) throws IOException {
    if (pn == null)
        codeBug();
    int memberTypeFlags = 0, lineno = ts.lineno, dotPos = ts.tokenBeg;
    consumeToken();
    if (tt == Token.DOTDOT) {
        mustHaveXML();
        memberTypeFlags = Node.DESCENDANTS_FLAG;
    }
    if (!compilerEnv.isXmlAvailable()) {
        int maybeName = nextToken();
        if (maybeName != Token.NAME && !(compilerEnv.isReservedKeywordAsIdentifier() && TokenStream.isKeyword(ts.getString(), compilerEnv.getLanguageVersion(), inUseStrictDirective))) {
            reportError("msg.no.name.after.dot");
        }
        Name name = createNameNode(true, Token.GETPROP);
        PropertyGet pg = new PropertyGet(pn, name, dotPos);
        pg.setLineno(lineno);
        return pg;
    }
    // right side of . or .. operator
    AstNode ref = null;
    int token = nextToken();
    switch(token) {
        case Token.THROW:
            // needed for generator.throw();
            saveNameTokenData(ts.tokenBeg, "throw", ts.lineno);
            ref = propertyName(-1, "throw", memberTypeFlags);
            break;
        case Token.NAME:
            // handles: name, ns::name, ns::*, ns::[expr]
            ref = propertyName(-1, ts.getString(), memberTypeFlags);
            break;
        case Token.MUL:
            // handles: *, *::name, *::*, *::[expr]
            saveNameTokenData(ts.tokenBeg, "*", ts.lineno);
            ref = propertyName(-1, "*", memberTypeFlags);
            break;
        case Token.XMLATTR:
            // handles: '@attr', '@ns::attr', '@ns::*', '@ns::*',
            // '@::attr', '@::*', '@*', '@*::attr', '@*::*'
            ref = attributeAccess();
            break;
        case Token.RESERVED:
            {
                String name = ts.getString();
                saveNameTokenData(ts.tokenBeg, name, ts.lineno);
                ref = propertyName(-1, name, memberTypeFlags);
                break;
            }
        default:
            if (compilerEnv.isReservedKeywordAsIdentifier()) {
                // allow keywords as property names, e.g. ({if: 1})
                String name = Token.keywordToName(token);
                if (name != null) {
                    saveNameTokenData(ts.tokenBeg, name, ts.lineno);
                    ref = propertyName(-1, name, memberTypeFlags);
                    break;
                }
            }
            reportError("msg.no.name.after.dot");
            return makeErrorNode();
    }
    boolean xml = ref instanceof XmlRef;
    InfixExpression result = xml ? new XmlMemberGet() : new PropertyGet();
    if (xml && tt == Token.DOT)
        result.setType(Token.DOT);
    int pos = pn.getPosition();
    result.setPosition(pos);
    result.setLength(getNodeEnd(ref) - pos);
    result.setOperatorPosition(dotPos - pos);
    result.setLineno(pn.getLineno());
    // do this after setting position
    result.setLeft(pn);
    result.setRight(ref);
    return result;
}
Also used : XmlRef(org.mozilla.javascript.ast.XmlRef) PropertyGet(org.mozilla.javascript.ast.PropertyGet) InfixExpression(org.mozilla.javascript.ast.InfixExpression) XmlMemberGet(org.mozilla.javascript.ast.XmlMemberGet) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode) Name(org.mozilla.javascript.ast.Name)

Example 72 with AstNode

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

Example 73 with AstNode

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

the class Parser method eqExpr.

private AstNode eqExpr() throws IOException {
    AstNode pn = relExpr();
    for (; ; ) {
        int tt = peekToken(), opPos = ts.tokenBeg;
        switch(tt) {
            case Token.EQ:
            case Token.NE:
            case Token.SHEQ:
            case Token.SHNE:
                consumeToken();
                int parseToken = tt;
                if (compilerEnv.getLanguageVersion() == Context.VERSION_1_2) {
                    // JavaScript 1.2 uses shallow equality for == and != .
                    if (tt == Token.EQ)
                        parseToken = Token.SHEQ;
                    else if (tt == Token.NE)
                        parseToken = Token.SHNE;
                }
                pn = new InfixExpression(parseToken, pn, relExpr(), opPos);
                continue;
        }
        break;
    }
    return pn;
}
Also used : InfixExpression(org.mozilla.javascript.ast.InfixExpression) AstNode(org.mozilla.javascript.ast.AstNode)

Example 74 with AstNode

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

the class Parser method mulExpr.

private AstNode mulExpr() throws IOException {
    AstNode pn = unaryExpr();
    for (; ; ) {
        int tt = peekToken(), opPos = ts.tokenBeg;
        switch(tt) {
            case Token.MUL:
            case Token.DIV:
            case Token.MOD:
                consumeToken();
                pn = new InfixExpression(tt, pn, unaryExpr(), opPos);
                continue;
        }
        break;
    }
    return pn;
}
Also used : InfixExpression(org.mozilla.javascript.ast.InfixExpression) AstNode(org.mozilla.javascript.ast.AstNode)

Example 75 with AstNode

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

the class Parser method orExpr.

private AstNode orExpr() throws IOException {
    AstNode pn = andExpr();
    if (matchToken(Token.OR)) {
        int opPos = ts.tokenBeg;
        pn = new InfixExpression(Token.OR, pn, orExpr(), opPos);
    }
    return pn;
}
Also used : InfixExpression(org.mozilla.javascript.ast.InfixExpression) 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