Search in sources :

Example 6 with Name

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

the class Parser method continueStatement.

private ContinueStatement continueStatement() throws IOException {
    if (currentToken != Token.CONTINUE)
        codeBug();
    consumeToken();
    int lineno = ts.lineno, pos = ts.tokenBeg, end = ts.tokenEnd;
    Name label = null;
    if (peekTokenOrEOL() == Token.NAME) {
        label = createNameNode();
        end = getNodeEnd(label);
    }
    // matchJumpLabelName only matches if there is one
    LabeledStatement labels = matchJumpLabelName();
    Loop target = null;
    if (labels == null && label == null) {
        if (loopSet == null || loopSet.size() == 0) {
            reportError("msg.continue.outside");
        } else {
            target = loopSet.get(loopSet.size() - 1);
        }
    } else {
        if (labels == null || !(labels.getStatement() instanceof Loop)) {
            reportError("msg.continue.nonloop", pos, end - pos);
        }
        target = labels == null ? null : (Loop) labels.getStatement();
    }
    ContinueStatement pn = new ContinueStatement(pos, end - pos);
    if (// can be null in error-recovery mode
    target != null)
        pn.setTarget(target);
    pn.setLabel(label);
    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) LabeledStatement(org.mozilla.javascript.ast.LabeledStatement) ContinueStatement(org.mozilla.javascript.ast.ContinueStatement) Name(org.mozilla.javascript.ast.Name)

Example 7 with Name

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

the class Parser method parseFunctionParams.

private void parseFunctionParams(FunctionNode fnNode) throws IOException {
    if (matchToken(Token.RP)) {
        fnNode.setRp(ts.tokenBeg - fnNode.getPosition());
        return;
    }
    // Would prefer not to call createDestructuringAssignment until codegen,
    // but the symbol definitions have to happen now, before body is parsed.
    Map<String, Node> destructuring = null;
    Set<String> paramNames = new HashSet<String>();
    do {
        int tt = peekToken();
        if (tt == Token.LB || tt == Token.LC) {
            AstNode expr = destructuringPrimaryExpr();
            markDestructuring(expr);
            fnNode.addParam(expr);
            // variables from the destructuring assignment
            if (destructuring == null) {
                destructuring = new HashMap<String, Node>();
            }
            String pname = currentScriptOrFn.getNextTempName();
            defineSymbol(Token.LP, pname, false);
            destructuring.put(pname, expr);
        } else {
            if (mustMatchToken(Token.NAME, "msg.no.parm")) {
                Name paramNameNode = createNameNode();
                Comment jsdocNodeForName = getAndResetJsDoc();
                if (jsdocNodeForName != null) {
                    paramNameNode.setJsDocNode(jsdocNodeForName);
                }
                fnNode.addParam(paramNameNode);
                String paramName = ts.getString();
                defineSymbol(Token.LP, paramName);
                if (this.inUseStrictDirective) {
                    if ("eval".equals(paramName) || "arguments".equals(paramName)) {
                        reportError("msg.bad.id.strict", paramName);
                    }
                    if (paramNames.contains(paramName))
                        addError("msg.dup.param.strict", paramName);
                    paramNames.add(paramName);
                }
            } else {
                fnNode.addParam(makeErrorNode());
            }
        }
    } while (matchToken(Token.COMMA));
    if (destructuring != null) {
        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);
    }
    if (mustMatchToken(Token.RP, "msg.no.paren.after.parms")) {
        fnNode.setRp(ts.tokenBeg - fnNode.getPosition());
    }
}
Also used : Comment(org.mozilla.javascript.ast.Comment) 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) Map(java.util.Map) HashMap(java.util.HashMap) AstNode(org.mozilla.javascript.ast.AstNode) HashSet(java.util.HashSet) Name(org.mozilla.javascript.ast.Name)

Example 8 with Name

use of org.mozilla.javascript.ast.Name in project hackpad by dropbox.

the class Node method newString.

public static Node newString(int type, String str) {
    Name name = new Name();
    name.setIdentifier(str);
    name.setType(type);
    return name;
}
Also used : Name(org.mozilla.javascript.ast.Name)

Example 9 with Name

use of org.mozilla.javascript.ast.Name in project hackpad by dropbox.

the class Node method toString.

private void toString(ObjToIntMap printIds, StringBuffer sb) {
    if (Token.printTrees) {
        sb.append(Token.name(type));
        if (this instanceof Name) {
            sb.append(' ');
            sb.append(getString());
            Scope scope = getScope();
            if (scope != null) {
                sb.append("[scope: ");
                appendPrintId(scope, printIds, sb);
                sb.append("]");
            }
        } else if (this instanceof Scope) {
            if (this instanceof ScriptNode) {
                ScriptNode sof = (ScriptNode) this;
                if (this instanceof FunctionNode) {
                    FunctionNode fn = (FunctionNode) this;
                    sb.append(' ');
                    sb.append(fn.getName());
                }
                sb.append(" [source name: ");
                sb.append(sof.getSourceName());
                sb.append("] [encoded source length: ");
                sb.append(sof.getEncodedSourceEnd() - sof.getEncodedSourceStart());
                sb.append("] [base line: ");
                sb.append(sof.getBaseLineno());
                sb.append("] [end line: ");
                sb.append(sof.getEndLineno());
                sb.append(']');
            }
            if (((Scope) this).getSymbolTable() != null) {
                sb.append(" [scope ");
                appendPrintId(this, printIds, sb);
                sb.append(": ");
                Iterator<String> iter = ((Scope) this).getSymbolTable().keySet().iterator();
                while (iter.hasNext()) {
                    sb.append(iter.next());
                    sb.append(" ");
                }
                sb.append("]");
            }
        } else if (this instanceof Jump) {
            Jump jump = (Jump) this;
            if (type == Token.BREAK || type == Token.CONTINUE) {
                sb.append(" [label: ");
                appendPrintId(jump.getJumpStatement(), printIds, sb);
                sb.append(']');
            } else if (type == Token.TRY) {
                Node catchNode = jump.target;
                Node finallyTarget = jump.getFinally();
                if (catchNode != null) {
                    sb.append(" [catch: ");
                    appendPrintId(catchNode, printIds, sb);
                    sb.append(']');
                }
                if (finallyTarget != null) {
                    sb.append(" [finally: ");
                    appendPrintId(finallyTarget, printIds, sb);
                    sb.append(']');
                }
            } else if (type == Token.LABEL || type == Token.LOOP || type == Token.SWITCH) {
                sb.append(" [break: ");
                appendPrintId(jump.target, printIds, sb);
                sb.append(']');
                if (type == Token.LOOP) {
                    sb.append(" [continue: ");
                    appendPrintId(jump.getContinue(), printIds, sb);
                    sb.append(']');
                }
            } else {
                sb.append(" [target: ");
                appendPrintId(jump.target, printIds, sb);
                sb.append(']');
            }
        } else if (type == Token.NUMBER) {
            sb.append(' ');
            sb.append(getDouble());
        } else if (type == Token.TARGET) {
            sb.append(' ');
            appendPrintId(this, printIds, sb);
        }
        if (lineno != -1) {
            sb.append(' ');
            sb.append(lineno);
        }
        for (PropListItem x = propListHead; x != null; x = x.next) {
            int type = x.type;
            sb.append(" [");
            sb.append(propToString(type));
            sb.append(": ");
            String value;
            switch(type) {
                case // can't add this as it recurses
                TARGETBLOCK_PROP:
                    value = "target block property";
                    break;
                case // can't add this as it is dull
                LOCAL_BLOCK_PROP:
                    value = "last local block";
                    break;
                case ISNUMBER_PROP:
                    switch(x.intValue) {
                        case BOTH:
                            value = "both";
                            break;
                        case RIGHT:
                            value = "right";
                            break;
                        case LEFT:
                            value = "left";
                            break;
                        default:
                            throw Kit.codeBug();
                    }
                    break;
                case SPECIALCALL_PROP:
                    switch(x.intValue) {
                        case SPECIALCALL_EVAL:
                            value = "eval";
                            break;
                        case SPECIALCALL_WITH:
                            value = "with";
                            break;
                        default:
                            // NON_SPECIALCALL should not be stored
                            throw Kit.codeBug();
                    }
                    break;
                case OBJECT_IDS_PROP:
                    {
                        Object[] a = (Object[]) x.objectValue;
                        value = "[";
                        for (int i = 0; i < a.length; i++) {
                            value += a[i].toString();
                            if (i + 1 < a.length)
                                value += ", ";
                        }
                        value += "]";
                        break;
                    }
                default:
                    Object obj = x.objectValue;
                    if (obj != null) {
                        value = obj.toString();
                    } else {
                        value = String.valueOf(x.intValue);
                    }
                    break;
            }
            sb.append(value);
            sb.append(']');
        }
    }
}
Also used : Scope(org.mozilla.javascript.ast.Scope) ScriptNode(org.mozilla.javascript.ast.ScriptNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) Iterator(java.util.Iterator) ScriptNode(org.mozilla.javascript.ast.ScriptNode) Jump(org.mozilla.javascript.ast.Jump) Name(org.mozilla.javascript.ast.Name)

Example 10 with Name

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

the class IRFactory method transformXmlRef.

private Node transformXmlRef(Node pn, XmlRef node, int memberTypeFlags) {
    if ((memberTypeFlags & Node.ATTRIBUTE_FLAG) != 0)
        decompiler.addToken(Token.XMLATTR);
    Name namespace = node.getNamespace();
    String ns = namespace != null ? namespace.getIdentifier() : null;
    if (ns != null) {
        decompiler.addName(ns);
        decompiler.addToken(Token.COLONCOLON);
    }
    if (node instanceof XmlPropRef) {
        String name = ((XmlPropRef) node).getPropName().getIdentifier();
        decompiler.addName(name);
        return createPropertyGet(pn, ns, name, memberTypeFlags);
    } else {
        decompiler.addToken(Token.LB);
        Node expr = transform(((XmlElemRef) node).getExpression());
        decompiler.addToken(Token.RB);
        return createElementGet(pn, ns, expr, memberTypeFlags);
    }
}
Also used : XmlPropRef(org.mozilla.javascript.ast.XmlPropRef) ScriptNode(org.mozilla.javascript.ast.ScriptNode) AstNode(org.mozilla.javascript.ast.AstNode) LetNode(org.mozilla.javascript.ast.LetNode) FunctionNode(org.mozilla.javascript.ast.FunctionNode) XmlString(org.mozilla.javascript.ast.XmlString) Name(org.mozilla.javascript.ast.Name)

Aggregations

Name (org.mozilla.javascript.ast.Name)23 AstNode (org.mozilla.javascript.ast.AstNode)11 XmlString (org.mozilla.javascript.ast.XmlString)11 FunctionNode (org.mozilla.javascript.ast.FunctionNode)10 ScriptNode (org.mozilla.javascript.ast.ScriptNode)7 LetNode (org.mozilla.javascript.ast.LetNode)5 Comment (org.mozilla.javascript.ast.Comment)3 ErrorNode (org.mozilla.javascript.ast.ErrorNode)3 Jump (org.mozilla.javascript.ast.Jump)3 ObjectProperty (org.mozilla.javascript.ast.ObjectProperty)3 Iterator (java.util.Iterator)2 InfixExpression (org.mozilla.javascript.ast.InfixExpression)2 LabeledStatement (org.mozilla.javascript.ast.LabeledStatement)2 NumberLiteral (org.mozilla.javascript.ast.NumberLiteral)2 PropertyGet (org.mozilla.javascript.ast.PropertyGet)2 Scope (org.mozilla.javascript.ast.Scope)2 StringLiteral (org.mozilla.javascript.ast.StringLiteral)2 XmlPropRef (org.mozilla.javascript.ast.XmlPropRef)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1