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

The following examples show how to use org.mozilla.javascript.ast.AstNode#getPosition() . 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 void autoInsertSemicolon(AstNode pn) throws IOException {
    int ttFlagged = peekFlaggedToken();
    int pos = pn.getPosition();
    switch (ttFlagged & CLEAR_TI_MASK) {
      case Token.SEMI:
          // Consume ';' as a part of expression
          consumeToken();
          // extend the node bounds to include the semicolon.
          pn.setLength(ts.tokenEnd - pos);
          break;
      case Token.ERROR:
      case Token.EOF:
      case Token.RC:
          // Autoinsert ;
          warnMissingSemi(pos, nodeEnd(pn));
          break;
      default:
          if ((ttFlagged & TI_AFTER_EOL) == 0) {
              // Report error if no EOL or autoinsert ; otherwise
              reportError("msg.no.semi.stmt");
          } else {
              warnMissingSemi(pos, nodeEnd(pn));
          }
          break;
    }
}
 
Example 2
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
private AstNode expr()
    throws IOException
{
    AstNode pn = assignExpr();
    int pos = pn.getPosition();
    while (matchToken(Token.COMMA)) {
        int opPos = ts.tokenBeg;
        if (compilerEnv.isStrictMode() && !pn.hasSideEffects())
            addStrictWarning("msg.no.side.effects", "",
                             pos, nodeEnd(pn) - pos);
        if (peekToken() == Token.YIELD)
            reportError("msg.yield.parenthesized");
        pn = new InfixExpression(Token.COMMA, pn, assignExpr(), opPos);
    }
    return pn;
}
 
Example 3
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private AstNode statement()
    throws IOException
{
    int pos = ts.tokenBeg;
    try {
        AstNode pn = statementHelper();
        if (pn != null) {
            if (compilerEnv.isStrictMode() && !pn.hasSideEffects()) {
                int beg = pn.getPosition();
                beg = Math.max(beg, lineBeginningFor(beg));
                addStrictWarning(pn instanceof EmptyStatement
                                 ? "msg.extra.trailing.semi"
                                 : "msg.no.side.effects",
                                 "", beg, nodeEnd(pn) - beg);
            }
            return pn;
        }
    } catch (ParserException e) {
        // an ErrorNode was added to the ErrorReporter
    }

    // error:  skip ahead to a probable statement boundary
    guessingStatementEnd: for (;;) {
        int tt = peekTokenOrEOL();
        consumeToken();
        switch (tt) {
          case Token.ERROR:
          case Token.EOF:
          case Token.EOL:
          case Token.SEMI:
            break guessingStatementEnd;
        }
    }
    // We don't make error nodes explicitly part of the tree;
    // they get added to the ErrorReporter.  May need to do
    // something different here.
    return new EmptyStatement(pos, ts.tokenBeg - pos);
}
 
Example 4
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 5 votes vote down vote up
private AstNode condExpr()
    throws IOException
{
    AstNode pn = orExpr();
    if (matchToken(Token.HOOK)) {
        int line = ts.lineno;
        int qmarkPos = ts.tokenBeg, colonPos = -1;
        /*
         * Always accept the 'in' operator in the middle clause of a ternary,
         * where it's unambiguous, even if we might be parsing the init of a
         * for statement.
         */
        boolean wasInForInit = inForInit;
        inForInit = false;
        AstNode ifTrue;
        try {
            ifTrue = assignExpr();
        } finally {
            inForInit = wasInForInit;
        }
        if (mustMatchToken(Token.COLON, "msg.no.colon.cond"))
            colonPos = ts.tokenBeg;
        AstNode ifFalse = assignExpr();
        int beg = pn.getPosition(), len = getNodeEnd(ifFalse) - beg;
        ConditionalExpression ce = new ConditionalExpression(beg, len);
        ce.setLineno(line);
        ce.setTestExpression(pn);
        ce.setTrueExpression(ifTrue);
        ce.setFalseExpression(ifFalse);
        ce.setQuestionMarkPosition(qmarkPos - beg);
        ce.setColonPosition(colonPos - beg);
        pn = ce;
    }
    return pn;
}
 
Example 5
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
private int getNodeEnd(AstNode n) {
    return n.getPosition() + n.getLength();
}
 
Example 6
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
private AstNode arrowFunction(AstNode params) throws IOException {
    int baseLineno = ts.lineno;  // line number where source starts
    int functionSourceStart = params != null ? params.getPosition() : -1;  // start of "function" kwd

    FunctionNode fnNode = new FunctionNode(functionSourceStart);
    fnNode.setFunctionType(FunctionNode.ARROW_FUNCTION);
    fnNode.setJsDocNode(getAndResetJsDoc());

    // Would prefer not to call createDestructuringAssignment until codegen,
    // but the symbol definitions have to happen now, before body is parsed.
    Map<String, Node> destructuring = new HashMap<String, Node>();
    Set<String> paramNames = new HashSet<String>();

    PerFunctionVariables savedVars = new PerFunctionVariables(fnNode);
    try {
        if (params instanceof ParenthesizedExpression) {
            fnNode.setParens(0, params.getLength());
            AstNode p = ((ParenthesizedExpression)params).getExpression();
            if (!(p instanceof EmptyExpression)) {
                arrowFunctionParams(fnNode, p, destructuring, paramNames);
            }
        } else {
            arrowFunctionParams(fnNode, params, destructuring, paramNames);
        }

        if (!destructuring.isEmpty()) {
            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);
        }
            
        fnNode.setBody(parseFunctionBody(FunctionNode.ARROW_FUNCTION, fnNode));
        fnNode.setEncodedSourceBounds(functionSourceStart, ts.tokenEnd);
        fnNode.setLength(ts.tokenEnd - functionSourceStart);
    } finally {
        savedVars.restore();
    }

    if (fnNode.isGenerator()) {
        reportError("msg.arrowfunction.generator");
        return makeErrorNode();
    }

    fnNode.setSourceName(sourceURI);
    fnNode.setBaseLineno(baseLineno);
    fnNode.setEndLineno(ts.lineno);

    return fnNode;
}
 
Example 7
Source File: Parser.java    From JsDroidCmd with Mozilla Public License 2.0 4 votes vote down vote up
private int nodeEnd(AstNode node) {
    return node.getPosition() + node.getLength();
}