Search in sources :

Example 6 with Comment

use of org.mozilla.javascript.ast.Comment 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 7 with Comment

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

the class Parser method assignExpr.

private AstNode assignExpr() throws IOException {
    int tt = peekToken();
    if (tt == Token.YIELD) {
        return returnOrYield(tt, true);
    }
    AstNode pn = condExpr();
    boolean hasEOL = false;
    tt = peekTokenOrEOL();
    if (tt == Token.EOL) {
        hasEOL = true;
        tt = peekToken();
    }
    if (Token.FIRST_ASSIGN <= tt && tt <= Token.LAST_ASSIGN) {
        consumeToken();
        // Pull out JSDoc info and reset it before recursing.
        Comment jsdocNode = getAndResetJsDoc();
        markDestructuring(pn);
        int opPos = ts.tokenBeg;
        pn = new Assignment(tt, pn, assignExpr(), opPos);
        if (jsdocNode != null) {
            pn.setJsDocNode(jsdocNode);
        }
    } else if (tt == Token.SEMI) {
        // For example: /** @type Number */ C.prototype.x;
        if (currentJsDocComment != null) {
            pn.setJsDocNode(getAndResetJsDoc());
        }
    } else if (!hasEOL && tt == Token.ARROW) {
        consumeToken();
        pn = arrowFunction(pn);
    }
    return pn;
}
Also used : Assignment(org.mozilla.javascript.ast.Assignment) Comment(org.mozilla.javascript.ast.Comment) AstNode(org.mozilla.javascript.ast.AstNode)

Example 8 with Comment

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

the class Parser method withStatement.

private WithStatement withStatement() throws IOException {
    if (currentToken != Token.WITH)
        codeBug();
    consumeToken();
    Comment withComment = getAndResetJsDoc();
    int lineno = ts.lineno, pos = ts.tokenBeg, lp = -1, rp = -1;
    if (mustMatchToken(Token.LP, "msg.no.paren.with"))
        lp = ts.tokenBeg;
    AstNode obj = expr();
    if (mustMatchToken(Token.RP, "msg.no.paren.after.with"))
        rp = ts.tokenBeg;
    AstNode body = statement();
    WithStatement pn = new WithStatement(pos, getNodeEnd(body) - pos);
    pn.setJsDocNode(withComment);
    pn.setExpression(obj);
    pn.setStatement(body);
    pn.setParens(lp, rp);
    pn.setLineno(lineno);
    return pn;
}
Also used : Comment(org.mozilla.javascript.ast.Comment) WithStatement(org.mozilla.javascript.ast.WithStatement) AstNode(org.mozilla.javascript.ast.AstNode)

Example 9 with Comment

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

the class Parser method variables.

/**
 * Parse a 'var' or 'const' statement, or a 'var' init list in a for
 * statement.
 * @param declType A token value: either VAR, CONST, or LET depending on
 * context.
 * @param pos the position where the node should start.  It's sometimes
 * the var/const/let keyword, and other times the beginning of the first
 * token in the first variable declaration.
 * @return the parsed variable list
 */
private VariableDeclaration variables(int declType, int pos, boolean isStatement) throws IOException {
    int end;
    VariableDeclaration pn = new VariableDeclaration(pos);
    pn.setType(declType);
    pn.setLineno(ts.lineno);
    Comment varjsdocNode = getAndResetJsDoc();
    if (varjsdocNode != null) {
        pn.setJsDocNode(varjsdocNode);
    }
    // var {b: s2, a: s1} = foo, x = 6, y, [s3, s4] = bar;
    for (; ; ) {
        AstNode destructuring = null;
        Name name = null;
        int tt = peekToken(), kidPos = ts.tokenBeg;
        end = ts.tokenEnd;
        if (tt == Token.LB || tt == Token.LC) {
            // Destructuring assignment, e.g., var [a,b] = ...
            destructuring = destructuringPrimaryExpr();
            end = getNodeEnd(destructuring);
            if (!(destructuring instanceof DestructuringForm))
                reportError("msg.bad.assign.left", kidPos, end - kidPos);
            markDestructuring(destructuring);
        } else {
            // Simple variable name
            mustMatchToken(Token.NAME, "msg.bad.var");
            name = createNameNode();
            name.setLineno(ts.getLineno());
            if (inUseStrictDirective) {
                String id = ts.getString();
                if ("eval".equals(id) || "arguments".equals(ts.getString())) {
                    reportError("msg.bad.id.strict", id);
                }
            }
            defineSymbol(declType, ts.getString(), inForInit);
        }
        int lineno = ts.lineno;
        Comment jsdocNode = getAndResetJsDoc();
        AstNode init = null;
        if (matchToken(Token.ASSIGN)) {
            init = assignExpr();
            end = getNodeEnd(init);
        }
        VariableInitializer vi = new VariableInitializer(kidPos, end - kidPos);
        if (destructuring != null) {
            if (init == null && !inForInit) {
                reportError("msg.destruct.assign.no.init");
            }
            vi.setTarget(destructuring);
        } else {
            vi.setTarget(name);
        }
        vi.setInitializer(init);
        vi.setType(declType);
        vi.setJsDocNode(jsdocNode);
        vi.setLineno(lineno);
        pn.addVariable(vi);
        if (!matchToken(Token.COMMA))
            break;
    }
    pn.setLength(end - pos);
    pn.setIsStatement(isStatement);
    return pn;
}
Also used : Comment(org.mozilla.javascript.ast.Comment) DestructuringForm(org.mozilla.javascript.ast.DestructuringForm) VariableDeclaration(org.mozilla.javascript.ast.VariableDeclaration) XmlString(org.mozilla.javascript.ast.XmlString) VariableInitializer(org.mozilla.javascript.ast.VariableInitializer) AstNode(org.mozilla.javascript.ast.AstNode) Name(org.mozilla.javascript.ast.Name)

Example 10 with Comment

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

the class Parser method parse.

private AstRoot parse() throws IOException {
    int pos = 0;
    AstRoot root = new AstRoot(pos);
    currentScope = currentScriptOrFn = root;
    // line number where source starts
    int baseLineno = ts.lineno;
    // in case source is empty
    int end = pos;
    boolean inDirectivePrologue = true;
    boolean savedStrictMode = inUseStrictDirective;
    inUseStrictDirective = defaultUseStrictDirective;
    if (inUseStrictDirective) {
        root.setInStrictMode(true);
    }
    try {
        for (; ; ) {
            int tt = peekToken();
            if (tt <= Token.EOF) {
                break;
            }
            AstNode n;
            if (tt == Token.FUNCTION) {
                consumeToken();
                try {
                    n = function(calledByCompileFunction ? FunctionNode.FUNCTION_EXPRESSION : FunctionNode.FUNCTION_STATEMENT);
                } catch (ParserException e) {
                    break;
                }
            } else {
                n = statement();
                if (inDirectivePrologue) {
                    String directive = getDirective(n);
                    if (directive == null) {
                        inDirectivePrologue = false;
                    } else if (directive.equals("严格模式")) {
                        inUseStrictDirective = true;
                        root.setInStrictMode(true);
                    }
                }
            }
            end = getNodeEnd(n);
            root.addChildToBack(n);
            n.setParent(root);
        }
    } catch (StackOverflowError ex) {
        String msg = lookupMessage("msg.too.deep.parser.recursion");
        if (!compilerEnv.isIdeMode())
            throw Context.reportRuntimeError(msg, sourceURI, ts.lineno, null, 0);
    } finally {
        inUseStrictDirective = savedStrictMode;
    }
    if (this.syntaxErrorCount != 0) {
        String msg = String.valueOf(this.syntaxErrorCount);
        msg = lookupMessage("msg.got.syntax.errors", msg);
        if (!compilerEnv.isIdeMode())
            throw errorReporter.runtimeError(msg, sourceURI, baseLineno, null, 0);
    }
    // add comments to root in lexical order
    if (scannedComments != null) {
        // If we find a comment beyond end of our last statement or
        // function, extend the root bounds to the end of that comment.
        int last = scannedComments.size() - 1;
        end = Math.max(end, getNodeEnd(scannedComments.get(last)));
        for (Comment c : scannedComments) {
            root.addComment(c);
        }
    }
    root.setLength(end - pos);
    root.setSourceName(sourceURI);
    root.setBaseLineno(baseLineno);
    root.setEndLineno(ts.lineno);
    return root;
}
Also used : Comment(org.mozilla.javascript.ast.Comment) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode) AstRoot(org.mozilla.javascript.ast.AstRoot)

Aggregations

Comment (org.mozilla.javascript.ast.Comment)11 AstNode (org.mozilla.javascript.ast.AstNode)8 XmlString (org.mozilla.javascript.ast.XmlString)5 ArrayList (java.util.ArrayList)3 Name (org.mozilla.javascript.ast.Name)3 AstRoot (org.mozilla.javascript.ast.AstRoot)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ParseException (net.sourceforge.pmd.lang.ast.ParseException)1 Assignment (org.mozilla.javascript.ast.Assignment)1 Block (org.mozilla.javascript.ast.Block)1 CatchClause (org.mozilla.javascript.ast.CatchClause)1 DestructuringForm (org.mozilla.javascript.ast.DestructuringForm)1 EmptyExpression (org.mozilla.javascript.ast.EmptyExpression)1 ErrorNode (org.mozilla.javascript.ast.ErrorNode)1 FunctionNode (org.mozilla.javascript.ast.FunctionNode)1 LetNode (org.mozilla.javascript.ast.LetNode)1 ObjectLiteral (org.mozilla.javascript.ast.ObjectLiteral)1