Java Code Examples for org.mozilla.javascript.ast.AstNode#getType()

The following examples show how to use org.mozilla.javascript.ast.AstNode#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IRFactory.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private Node transformArrayLiteral(ArrayLiteral node) {
    if (node.isDestructuring()) {
        return node;
    }
    decompiler.addToken(Token.LB);
    List<AstNode> elems = node.getElements();
    Node array = new Node(Token.ARRAYLIT);
    List<Integer> skipIndexes = null;
    for (int i = 0; i < elems.size(); ++i) {
        AstNode elem = elems.get(i);
        if (elem.getType() != Token.EMPTY) {
            array.addChildToBack(transform(elem));
        } else {
            if (skipIndexes == null) {
                skipIndexes = new ArrayList<Integer>();
            }
            skipIndexes.add(i);
        }
        if (i < elems.size() - 1)
            decompiler.addToken(Token.COMMA);
    }
    decompiler.addToken(Token.RB);
    array.putIntProp(Node.DESTRUCTURING_ARRAY_LENGTH,
                     node.getDestructuringLength());
    if (skipIndexes != null) {
        int[] skips = new int[skipIndexes.size()];
        for (int i = 0; i < skipIndexes.size(); i++)
            skips[i] = skipIndexes.get(i);
        array.putProp(Node.SKIP_INDEXES_PROP, skips);
    }
    return array;
}
 
Example 2
Source File: IRFactory.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
void decompile(AstNode node) {
    switch (node.getType()) {
      case Token.ARRAYLIT:
          decompileArrayLiteral((ArrayLiteral)node);
          break;
      case Token.OBJECTLIT:
          decompileObjectLiteral((ObjectLiteral)node);
          break;
      case Token.STRING:
          decompiler.addString(((StringLiteral)node).getValue());
          break;
      case Token.NAME:
          decompiler.addName(((Name)node).getIdentifier());
          break;
      case Token.NUMBER:
          decompiler.addNumber(((NumberLiteral)node).getNumber());
          break;
      case Token.GETPROP:
          decompilePropertyGet((PropertyGet)node);
          break;
      case Token.EMPTY:
          break;
      case Token.GETELEM:
          decompileElementGet((ElementGet) node);
          break;
      case Token.THIS:
          decompiler.addToken(node.getType());
          break;
      default:
          Kit.codeBug("unexpected token: "
                      + Token.typeToName(node.getType()));
    }
}
 
Example 3
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private void arrowFunctionParams(FunctionNode fnNode, AstNode params, Map<String, Node> destructuring, Set<String> paramNames) {
    if (params instanceof ArrayLiteral || params instanceof ObjectLiteral) {
        markDestructuring(params);
        fnNode.addParam(params);
        String pname = currentScriptOrFn.getNextTempName();
        defineSymbol(Token.LP, pname, false);
        destructuring.put(pname, params);
    } else if (params instanceof InfixExpression && params.getType() == Token.COMMA) {
        arrowFunctionParams(fnNode, ((InfixExpression)params).getLeft(), destructuring, paramNames);
        arrowFunctionParams(fnNode, ((InfixExpression)params).getRight(), destructuring, paramNames);
    } else if (params instanceof Name) {
        fnNode.addParam(params);
        String paramName = ((Name)params).getIdentifier();
        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 {
        reportError("msg.no.parm", params.getPosition(), params.getLength());
        fnNode.addParam(makeErrorNode());
    }
}
 
Example 4
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private AstNode parenExpr() throws IOException {
    boolean wasInForInit = inForInit;
    inForInit = false;
    try {
        Comment jsdocNode = getAndResetJsDoc();
        int lineno = ts.lineno;
        int begin = ts.tokenBeg;
        AstNode e = expr();
        if (peekToken() == Token.FOR) {
            return generatorExpression(e, begin);
        }
        ParenthesizedExpression pn = new ParenthesizedExpression(e);
        if (jsdocNode == null) {
            jsdocNode = getAndResetJsDoc();
        }
        if (jsdocNode != null) {
            pn.setJsDocNode(jsdocNode);
        }
        mustMatchToken(Token.RP, "msg.no.paren");
        if (e.getType() == Token.EMPTY && peekToken() != Token.ARROW) {
          reportError("msg.syntax");
          return makeErrorNode();
        }
        pn.setLength(ts.tokenEnd - pn.getPosition());
        pn.setLineno(lineno);
        return pn;
    } finally {
        inForInit = wasInForInit;
    }
}
 
Example 5
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private void checkCallRequiresActivation(AstNode pn) {
    if ((pn.getType() == Token.NAME
         && "eval".equals(((Name)pn).getIdentifier()))
        || (pn.getType() == Token.GETPROP &&
            "eval".equals(((PropertyGet)pn).getProperty().getIdentifier())))
        setRequiresActivation();
}
 
Example 6
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private void checkBadIncDec(UnaryExpression expr) {
    AstNode op = removeParens(expr.getOperand());
    int tt = op.getType();
    if (!(tt == Token.NAME
          || tt == Token.GETPROP
          || tt == Token.GETELEM
          || tt == Token.GET_REF
          || tt == Token.CALL))
        reportError(expr.getType() == Token.INC
                    ? "msg.bad.incr"
                    : "msg.bad.decr");
}
 
Example 7
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
boolean destructuringArray(ArrayLiteral array,
                           int variableType,
                           String tempName,
                           Node parent,
                           List<String> destructuringNames)
{
    boolean empty = true;
    int setOp = variableType == Token.CONST
        ? Token.SETCONST : Token.SETNAME;
    int index = 0;
    for (AstNode n : array.getElements()) {
        if (n.getType() == Token.EMPTY) {
            index++;
            continue;
        }
        Node rightElem = new Node(Token.GETELEM,
                                  createName(tempName),
                                  createNumber(index));
        if (n.getType() == Token.NAME) {
            String name = n.getString();
            parent.addChildToBack(new Node(setOp,
                                          createName(Token.BINDNAME,
                                                     name, null),
                                          rightElem));
            if (variableType != -1) {
                defineSymbol(variableType, name, true);
                destructuringNames.add(name);
            }
        } else {
            parent.addChildToBack
                (destructuringAssignmentHelper
                 (variableType, n,
                  rightElem,
                  currentScriptOrFn.getNextTempName()));
        }
        index++;
        empty = false;
    }
    return empty;
}
 
Example 8
Source File: JavascriptTestability.java    From testability-explorer with Apache License 2.0 5 votes vote down vote up
public boolean visit(AstNode node) {
  if (node.getType() == Token.FUNCTION) {
    if (node.getEnclosingFunction() != null) {
      count++;
    }
  }
  return true;
}
 
Example 9
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Found a name in a statement context.  If it's a label, we gather
 * up any following labels and the next non-label statement into a
 * {@link LabeledStatement} "bundle" and return that.  Otherwise we parse
 * an expression and return it wrapped in an {@link ExpressionStatement}.
 */
private AstNode nameOrLabel()
    throws IOException
{
    if (currentToken != Token.NAME) throw codeBug();
    int pos = ts.tokenBeg;

    // set check for label and call down to primaryExpr
    currentFlaggedToken |= TI_CHECK_LABEL;
    AstNode expr = expr();

    if (expr.getType() != Token.LABEL) {
        AstNode n = new ExpressionStatement(expr, !insideFunction());
        n.lineno = expr.lineno;
        return n;
    }

    LabeledStatement bundle = new LabeledStatement(pos);
    recordLabel((Label)expr, bundle);
    bundle.setLineno(ts.lineno);
    // look for more labels
    AstNode stmt = null;
    while (peekToken() == Token.NAME) {
        currentFlaggedToken |= TI_CHECK_LABEL;
        expr = expr();
        if (expr.getType() != Token.LABEL) {
            stmt = new ExpressionStatement(expr, !insideFunction());
            autoInsertSemicolon(stmt);
            break;
        }
        recordLabel((Label)expr, bundle);
    }

    // no more labels; now parse the labeled statement
    try {
        currentLabel = bundle;
        if (stmt == null) {
            stmt = statementHelper();
        }
    } finally {
        currentLabel = null;
        // remove the labels for this statement from the global set
        for (Label lb : bundle.getLabels()) {
            labelSet.remove(lb.getName());
        }
    }

    // If stmt has parent assigned its position already is relative
    // (See bug #710225)
    bundle.setLength(stmt.getParent() == null
                 ? getNodeEnd(stmt) - pos
                 : getNodeEnd(stmt));
    bundle.setStatement(stmt);
    return bundle;
}
 
Example 10
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
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");
        }

        // Define as a let since we want the scope of the variable to
        // 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;
            }
        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();
    }
}
 
Example 11
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
private GeneratorExpressionLoop generatorExpressionLoop()
    throws IOException
{
    if (nextToken() != Token.FOR) codeBug();
    int pos = ts.tokenBeg;
    int lp = -1, rp = -1, inPos = -1;
    GeneratorExpressionLoop pn = new GeneratorExpressionLoop(pos);

    pushScope(pn);
    try {
        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");
        }

        // Define as a let since we want the scope of the variable to
        // be restricted to the array comprehension
        if (iter.getType() == Token.NAME) {
            defineSymbol(Token.LET, ts.getString(), true);
        }

        if (mustMatchToken(Token.IN, "msg.in.after.for.name"))
            inPos = ts.tokenBeg - pos;
        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.setParens(lp, rp);
        return pn;
    } finally {
        popScope();
    }
}
 
Example 12
Source File: JavaScriptErrorCheckVisitor.java    From compiler with Apache License 2.0 4 votes vote down vote up
@Override
public boolean visit(AstNode node) {
	if (node.getType() == Token.ERROR)
		hasError = true;
	return !hasError;
}