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

The following examples show how to use org.mozilla.javascript.ast.AstNode#setLineno() . 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: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
private AstNode forLoopInit(int tt) throws IOException {
    try {
        inForInit = true;  // checked by variables() and relExpr()
        AstNode init = null;
        if (tt == Token.SEMI) {
            init = new EmptyExpression(ts.tokenBeg, 1);
            init.setLineno(ts.lineno);
        } else if (tt == Token.VAR || tt == Token.LET) {
            consumeToken();
            init = variables(tt, ts.tokenBeg, false);
        } else {
            init = expr();
            markDestructuring(init);
        }
        return init;
    } finally {
        inForInit = false;
    }
}
 
Example 2
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private AstNode statements(AstNode parent) throws IOException {
    if (currentToken != Token.LC  // assertion can be invalid in bad code
        && !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;
}
 
Example 3
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private AstNode letStatement()
    throws IOException
{
    if (currentToken != Token.LET) codeBug();
    consumeToken();
    int lineno = ts.lineno, pos = ts.tokenBeg;
    AstNode pn;
    if (peekToken() == Token.LP) {
        pn = let(true, pos);
    } else {
        pn = variables(Token.LET, pos, true);  // else, e.g.: let x=6, y=7;
    }
    pn.setLineno(lineno);
    return pn;
}
 
Example 4
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
private AstNode returnOrYield(int tt, boolean exprContext)
    throws IOException
{
    if (!insideFunction()) {
        reportError(tt == Token.RETURN ? "msg.bad.return"
                                       : "msg.bad.yield");
    }
    consumeToken();
    int lineno = ts.lineno, pos = ts.tokenBeg, end = ts.tokenEnd;

    AstNode e = null;
    // This is ugly, but we don't want to require a semicolon.
    switch (peekTokenOrEOL()) {
      case Token.SEMI: case Token.RC:  case Token.RB:    case Token.RP:
      case Token.EOF:  case Token.EOL: case Token.ERROR: case Token.YIELD:
        break;
      default:
        e = expr();
        end = getNodeEnd(e);
    }

    int before = endFlags;
    AstNode ret;

    if (tt == Token.RETURN) {
        endFlags |= e == null ? Node.END_RETURNS : Node.END_RETURNS_VALUE;
        ret = new ReturnStatement(pos, end - pos, e);

        // see if we need a strict mode warning
        if (nowAllSet(before, endFlags,
                Node.END_RETURNS|Node.END_RETURNS_VALUE))
            addStrictWarning("msg.return.inconsistent", "", pos, end - pos);
    } else {
        if (!insideFunction())
            reportError("msg.bad.yield");
        endFlags |= Node.END_YIELDS;
        ret = new Yield(pos, end - pos, e);
        setRequiresActivation();
        setIsGenerator();
        if (!exprContext) {
            ret = new ExpressionStatement(ret);
        }
    }

    // see if we are mixing yields and value returns.
    if (insideFunction()
        && nowAllSet(before, endFlags,
                Node.END_YIELDS|Node.END_RETURNS_VALUE)) {
        Name name = ((FunctionNode)currentScriptOrFn).getFunctionName();
        if (name == null || name.length() == 0)
            addError("msg.anon.generator.returns", "");
        else
            addError("msg.generator.returns", name.getIdentifier());
    }

    ret.setLineno(lineno);
    return ret;
}
 
Example 5
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
private AstNode unaryExpr()
    throws IOException
{
    AstNode node;
    int tt = peekToken();
    int line = ts.lineno;

    switch(tt) {
      case Token.VOID:
      case Token.NOT:
      case Token.BITNOT:
      case Token.TYPEOF:
          consumeToken();
          node = new UnaryExpression(tt, ts.tokenBeg, unaryExpr());
          node.setLineno(line);
          return node;

      case Token.ADD:
          consumeToken();
          // Convert to special POS token in parse tree
          node = new UnaryExpression(Token.POS, ts.tokenBeg, unaryExpr());
          node.setLineno(line);
          return node;

      case Token.SUB:
          consumeToken();
          // Convert to special NEG token in parse tree
          node = new UnaryExpression(Token.NEG, ts.tokenBeg, unaryExpr());
          node.setLineno(line);
          return node;

      case Token.INC:
      case Token.DEC:
          consumeToken();
          UnaryExpression expr = new UnaryExpression(tt, ts.tokenBeg,
                                                     memberExpr(true));
          expr.setLineno(line);
          checkBadIncDec(expr);
          return expr;

      case Token.DELPROP:
          consumeToken();
          node = new UnaryExpression(tt, ts.tokenBeg, unaryExpr());
          node.setLineno(line);
          return node;

      case Token.ERROR:
          consumeToken();
          return makeErrorNode();

      case Token.LT:
          // XML stream encountered in expression.
          if (compilerEnv.isXmlAvailable()) {
              consumeToken();
              return memberExprTail(true, xmlInitializer());
          }
          // Fall thru to the default handling of RELOP

      default:
          AstNode pn = memberExpr(true);
          // Don't look across a newline boundary for a postfix incop.
          tt = peekTokenOrEOL();
          if (!(tt == Token.INC || tt == Token.DEC)) {
              return pn;
          }
          consumeToken();
          UnaryExpression uexpr =
                  new UnaryExpression(tt, ts.tokenBeg, pn, true);
          uexpr.setLineno(line);
          checkBadIncDec(uexpr);
          return uexpr;
    }
}
 
Example 6
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Parse a new-expression, or if next token isn't {@link Token#NEW},
 * a primary expression.
 * @param allowCallSyntax passed down to {@link #memberExprTail}
 */
private AstNode memberExpr(boolean allowCallSyntax)
    throws IOException
{
    int tt = peekToken(), lineno = ts.lineno;
    AstNode pn;

    if (tt != Token.NEW) {
        pn = primaryExpr();
    } else {
        consumeToken();
        int pos = ts.tokenBeg;
        NewExpression nx = new NewExpression(pos);

        AstNode target = memberExpr(false);
        int end = getNodeEnd(target);
        nx.setTarget(target);

        int lp = -1;
        if (matchToken(Token.LP)) {
            lp = ts.tokenBeg;
            List<AstNode> args = argumentList();
            if (args != null && args.size() > ARGC_LIMIT)
                reportError("msg.too.many.constructor.args");
            int rp = ts.tokenBeg;
            end = ts.tokenEnd;
            if (args != null)
                nx.setArguments(args);
            nx.setParens(lp - pos, rp - pos);
        }

        // Experimental syntax: allow an object literal to follow a new
        // expression, which will mean a kind of anonymous class built with
        // the JavaAdapter.  the object literal will be passed as an
        // additional argument to the constructor.
        if (matchToken(Token.LC)) {
            ObjectLiteral initializer = objectLiteral();
            end = getNodeEnd(initializer);
            nx.setInitializer(initializer);
        }
        nx.setLength(end - pos);
        pn = nx;
    }
    pn.setLineno(lineno);
    AstNode tail = memberExprTail(allowCallSyntax, pn);
    return tail;
}