Search in sources :

Example 1 with ElementGet

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

the class Parser method memberExprTail.

/**
 * Parse any number of "(expr)", "[expr]" ".expr", "..expr",
 * or ".(expr)" constructs trailing the passed expression.
 * @param pn the non-null parent node
 * @return the outermost (lexically last occurring) expression,
 * which will have the passed parent node as a descendant
 */
private AstNode memberExprTail(boolean allowCallSyntax, AstNode pn) throws IOException {
    // we no longer return null for errors, so this won't be null
    if (pn == null)
        codeBug();
    int pos = pn.getPosition();
    int lineno;
    tailLoop: for (; ; ) {
        int tt = peekToken();
        switch(tt) {
            case Token.DOT:
            case Token.DOTDOT:
                lineno = ts.lineno;
                pn = propertyAccess(tt, pn);
                pn.setLineno(lineno);
                break;
            case Token.DOTQUERY:
                consumeToken();
                int opPos = ts.tokenBeg, rp = -1;
                lineno = ts.lineno;
                mustHaveXML();
                setRequiresActivation();
                AstNode filter = expr();
                int end = getNodeEnd(filter);
                if (mustMatchToken(Token.RP, "msg.no.paren")) {
                    rp = ts.tokenBeg;
                    end = ts.tokenEnd;
                }
                XmlDotQuery q = new XmlDotQuery(pos, end - pos);
                q.setLeft(pn);
                q.setRight(filter);
                q.setOperatorPosition(opPos);
                q.setRp(rp - pos);
                q.setLineno(lineno);
                pn = q;
                break;
            case Token.LB:
                consumeToken();
                int lb = ts.tokenBeg, rb = -1;
                lineno = ts.lineno;
                AstNode expr = expr();
                end = getNodeEnd(expr);
                if (mustMatchToken(Token.RB, "msg.no.bracket.index")) {
                    rb = ts.tokenBeg;
                    end = ts.tokenEnd;
                }
                ElementGet g = new ElementGet(pos, end - pos);
                g.setTarget(pn);
                g.setElement(expr);
                g.setParens(lb, rb);
                g.setLineno(lineno);
                pn = g;
                break;
            case Token.LP:
                if (!allowCallSyntax) {
                    break tailLoop;
                }
                lineno = ts.lineno;
                consumeToken();
                checkCallRequiresActivation(pn);
                FunctionCall f = new FunctionCall(pos);
                f.setTarget(pn);
                // Assign the line number for the function call to where
                // the paren appeared, not where the name expression started.
                f.setLineno(lineno);
                f.setLp(ts.tokenBeg - pos);
                List<AstNode> args = argumentList();
                if (args != null && args.size() > ARGC_LIMIT)
                    reportError("msg.too.many.function.args");
                f.setArguments(args);
                f.setRp(ts.tokenBeg - pos);
                f.setLength(ts.tokenEnd - pos);
                pn = f;
                break;
            default:
                break tailLoop;
        }
    }
    return pn;
}
Also used : XmlDotQuery(org.mozilla.javascript.ast.XmlDotQuery) List(java.util.List) ArrayList(java.util.ArrayList) FunctionCall(org.mozilla.javascript.ast.FunctionCall) AstNode(org.mozilla.javascript.ast.AstNode) ElementGet(org.mozilla.javascript.ast.ElementGet)

Example 2 with ElementGet

use of org.mozilla.javascript.ast.ElementGet in project st-js by st-js.

the class RhinoJavaScriptBuilder method elementGet.

/**
 * {@inheritDoc}
 */
@Override
public AstNode elementGet(AstNode target, AstNode index) {
    ElementGet prop = new ElementGet();
    prop.setTarget(target);
    prop.setElement(index);
    return prop;
}
Also used : ElementGet(org.mozilla.javascript.ast.ElementGet)

Example 3 with ElementGet

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

the class Parser method simpleAssignment.

// Quickie tutorial for some of the interpreter bytecodes.
// 
// GETPROP - for normal foo.bar prop access; right side is a name
// GETELEM - for normal foo[bar] element access; rhs is an expr
// SETPROP - for assignment when left side is a GETPROP
// SETELEM - for assignment when left side is a GETELEM
// DELPROP - used for delete foo.bar or foo[bar]
// 
// GET_REF, SET_REF, DEL_REF - in general, these mean you're using
// get/set/delete on a right-hand side expression (possibly with no
// explicit left-hand side) that doesn't use the normal JavaScript
// Object (i.e. ScriptableObject) get/set/delete functions, but wants
// to provide its own versions instead.  It will ultimately implement
// Ref, and currently SpecialRef (for __proto__ etc.) and XmlName
// (for E4X XML objects) are the only implementations.  The runtime
// notices these bytecodes and delegates get/set/delete to the object.
// 
// BINDNAME:  used in assignments.  LHS is evaluated first to get a
// specific object containing the property ("binding" the property
// to the object) so that it's always the same object, regardless of
// side effects in the RHS.
protected Node simpleAssignment(Node left, Node right) {
    int nodeType = left.getType();
    switch(nodeType) {
        case Token.NAME:
            String name = ((Name) left).getIdentifier();
            if (inUseStrictDirective && ("eval".equals(name) || "arguments".equals(name))) {
                reportError("msg.bad.id.strict", name);
            }
            left.setType(Token.BINDNAME);
            return new Node(Token.SETNAME, left, right);
        case Token.GETPROP:
        case Token.GETELEM:
            {
                Node obj, id;
                // field, but that seems just as ugly as this casting.
                if (left instanceof PropertyGet) {
                    obj = ((PropertyGet) left).getTarget();
                    id = ((PropertyGet) left).getProperty();
                } else if (left instanceof ElementGet) {
                    obj = ((ElementGet) left).getTarget();
                    id = ((ElementGet) left).getElement();
                } else {
                    // This branch is called during IRFactory transform pass.
                    obj = left.getFirstChild();
                    id = left.getLastChild();
                }
                int type;
                if (nodeType == Token.GETPROP) {
                    type = Token.SETPROP;
                    // TODO(stevey) - see https://bugzilla.mozilla.org/show_bug.cgi?id=492036
                    // The new AST code generates NAME tokens for GETPROP ids where the old parser
                    // generated STRING nodes. If we don't set the type to STRING below, this will
                    // cause java.lang.VerifyError in codegen for code like
                    // "var obj={p:3};[obj.p]=[9];"
                    id.setType(Token.STRING);
                } else {
                    type = Token.SETELEM;
                }
                return new Node(type, obj, id, right);
            }
        case Token.GET_REF:
            {
                Node ref = left.getFirstChild();
                checkMutableReference(ref);
                return new Node(Token.SET_REF, ref, right);
            }
    }
    throw codeBug();
}
Also used : PropertyGet(org.mozilla.javascript.ast.PropertyGet) 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) Name(org.mozilla.javascript.ast.Name) ElementGet(org.mozilla.javascript.ast.ElementGet)

Aggregations

ElementGet (org.mozilla.javascript.ast.ElementGet)3 AstNode (org.mozilla.javascript.ast.AstNode)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ErrorNode (org.mozilla.javascript.ast.ErrorNode)1 FunctionCall (org.mozilla.javascript.ast.FunctionCall)1 FunctionNode (org.mozilla.javascript.ast.FunctionNode)1 LetNode (org.mozilla.javascript.ast.LetNode)1 Name (org.mozilla.javascript.ast.Name)1 PropertyGet (org.mozilla.javascript.ast.PropertyGet)1 ScriptNode (org.mozilla.javascript.ast.ScriptNode)1 XmlDotQuery (org.mozilla.javascript.ast.XmlDotQuery)1 XmlString (org.mozilla.javascript.ast.XmlString)1