Search in sources :

Example 1 with Comment

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

the class EcmascriptParser method parse.

public EcmascriptNode<AstRoot> parse(final Reader reader) {
    try {
        final List<ParseProblem> parseProblems = new ArrayList<>();
        final String sourceCode = IOUtils.toString(reader);
        final AstRoot astRoot = parseEcmascript(sourceCode, parseProblems);
        final EcmascriptTreeBuilder treeBuilder = new EcmascriptTreeBuilder(sourceCode, parseProblems);
        EcmascriptNode<AstRoot> tree = treeBuilder.build(astRoot);
        suppressMap = new HashMap<>();
        if (astRoot.getComments() != null) {
            for (Comment comment : astRoot.getComments()) {
                int nopmd = comment.getValue().indexOf(suppressMarker);
                if (nopmd > -1) {
                    String suppression = comment.getValue().substring(nopmd + suppressMarker.length());
                    EcmascriptNode<Comment> node = treeBuilder.build(comment);
                    suppressMap.put(node.getBeginLine(), suppression);
                }
            }
        }
        return tree;
    } catch (IOException e) {
        throw new ParseException(e);
    }
}
Also used : Comment(org.mozilla.javascript.ast.Comment) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ParseProblem(org.mozilla.javascript.ast.ParseProblem) ParseException(net.sourceforge.pmd.lang.ast.ParseException) AstRoot(org.mozilla.javascript.ast.AstRoot)

Example 2 with Comment

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

the class Parser method objectLiteral.

private ObjectLiteral objectLiteral() throws IOException {
    int pos = ts.tokenBeg, lineno = ts.lineno;
    int afterComma = -1;
    List<ObjectProperty> elems = new ArrayList<ObjectProperty>();
    Set<String> getterNames = null;
    Set<String> setterNames = null;
    if (this.inUseStrictDirective) {
        getterNames = new HashSet<String>();
        setterNames = new HashSet<String>();
    }
    Comment objJsdocNode = getAndResetJsDoc();
    commaLoop: for (; ; ) {
        String propertyName = null;
        int entryKind = PROP_ENTRY;
        int tt = peekToken();
        Comment jsdocNode = getAndResetJsDoc();
        if (tt == Token.RC) {
            if (afterComma != -1)
                warnTrailingComma(pos, elems, afterComma);
            break commaLoop;
        } else {
            AstNode pname = objliteralProperty();
            if (pname == null) {
                propertyName = null;
                reportError("msg.bad.prop");
            } else {
                propertyName = ts.getString();
                int ppos = ts.tokenBeg;
                consumeToken();
                // This code path needs to handle both destructuring object
                // literals like:
                // var {get, b} = {get: 1, b: 2};
                // and getters like:
                // var x = {get 1() { return 2; };
                // So we check a whitelist of tokens to check if we're at the
                // first case. (Because of keywords, the second case may be
                // many tokens.)
                int peeked = peekToken();
                if (peeked != Token.COMMA && peeked != Token.COLON && peeked != Token.RC) {
                    if (peeked == Token.LP) {
                        entryKind = METHOD_ENTRY;
                    } else if (pname.getType() == Token.NAME) {
                        if ("get".equals(propertyName)) {
                            entryKind = GET_ENTRY;
                        } else if ("set".equals(propertyName)) {
                            entryKind = SET_ENTRY;
                        }
                    }
                    if (entryKind == GET_ENTRY || entryKind == SET_ENTRY) {
                        pname = objliteralProperty();
                        if (pname == null) {
                            reportError("msg.bad.prop");
                        }
                        consumeToken();
                    }
                    if (pname == null) {
                        propertyName = null;
                    } else {
                        propertyName = ts.getString();
                        ObjectProperty objectProp = methodDefinition(ppos, pname, entryKind);
                        pname.setJsDocNode(jsdocNode);
                        elems.add(objectProp);
                    }
                } else {
                    pname.setJsDocNode(jsdocNode);
                    elems.add(plainProperty(pname, tt));
                }
            }
        }
        if (this.inUseStrictDirective && propertyName != null) {
            switch(entryKind) {
                case PROP_ENTRY:
                case METHOD_ENTRY:
                    if (getterNames.contains(propertyName) || setterNames.contains(propertyName)) {
                        addError("msg.dup.obj.lit.prop.strict", propertyName);
                    }
                    getterNames.add(propertyName);
                    setterNames.add(propertyName);
                    break;
                case GET_ENTRY:
                    if (getterNames.contains(propertyName)) {
                        addError("msg.dup.obj.lit.prop.strict", propertyName);
                    }
                    getterNames.add(propertyName);
                    break;
                case SET_ENTRY:
                    if (setterNames.contains(propertyName)) {
                        addError("msg.dup.obj.lit.prop.strict", propertyName);
                    }
                    setterNames.add(propertyName);
                    break;
            }
        }
        // Eat any dangling jsdoc in the property.
        getAndResetJsDoc();
        if (matchToken(Token.COMMA)) {
            afterComma = ts.tokenEnd;
        } else {
            break commaLoop;
        }
    }
    mustMatchToken(Token.RC, "msg.no.brace.prop");
    ObjectLiteral pn = new ObjectLiteral(pos, ts.tokenEnd - pos);
    if (objJsdocNode != null) {
        pn.setJsDocNode(objJsdocNode);
    }
    pn.setElements(elems);
    pn.setLineno(lineno);
    return pn;
}
Also used : ObjectProperty(org.mozilla.javascript.ast.ObjectProperty) Comment(org.mozilla.javascript.ast.Comment) ObjectLiteral(org.mozilla.javascript.ast.ObjectLiteral) ArrayList(java.util.ArrayList) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode)

Example 3 with Comment

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

the class Parser method getAndResetJsDoc.

private Comment getAndResetJsDoc() {
    Comment saved = currentJsDocComment;
    currentJsDocComment = null;
    return saved;
}
Also used : Comment(org.mozilla.javascript.ast.Comment)

Example 4 with Comment

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

the class Parser method tryStatement.

private TryStatement tryStatement() throws IOException {
    if (currentToken != Token.TRY)
        codeBug();
    consumeToken();
    // Pull out JSDoc info and reset it before recursing.
    Comment jsdocNode = getAndResetJsDoc();
    int tryPos = ts.tokenBeg, lineno = ts.lineno, finallyPos = -1;
    if (peekToken() != Token.LC) {
        reportError("msg.no.brace.try");
    }
    AstNode tryBlock = statement();
    int tryEnd = getNodeEnd(tryBlock);
    List<CatchClause> clauses = null;
    boolean sawDefaultCatch = false;
    int peek = peekToken();
    if (peek == Token.CATCH) {
        while (matchToken(Token.CATCH)) {
            int catchLineNum = ts.lineno;
            if (sawDefaultCatch) {
                reportError("msg.catch.unreachable");
            }
            int catchPos = ts.tokenBeg, lp = -1, rp = -1, guardPos = -1;
            if (mustMatchToken(Token.LP, "msg.no.paren.catch"))
                lp = ts.tokenBeg;
            mustMatchToken(Token.NAME, "msg.bad.catchcond");
            Name varName = createNameNode();
            Comment jsdocNodeForName = getAndResetJsDoc();
            if (jsdocNodeForName != null) {
                varName.setJsDocNode(jsdocNodeForName);
            }
            String varNameString = varName.getIdentifier();
            if (inUseStrictDirective) {
                if ("eval".equals(varNameString) || "arguments".equals(varNameString)) {
                    reportError("msg.bad.id.strict", varNameString);
                }
            }
            AstNode catchCond = null;
            if (matchToken(Token.IF)) {
                guardPos = ts.tokenBeg;
                catchCond = expr();
            } else {
                sawDefaultCatch = true;
            }
            if (mustMatchToken(Token.RP, "msg.bad.catchcond"))
                rp = ts.tokenBeg;
            mustMatchToken(Token.LC, "msg.no.brace.catchblock");
            Block catchBlock = (Block) statements();
            tryEnd = getNodeEnd(catchBlock);
            CatchClause catchNode = new CatchClause(catchPos);
            catchNode.setVarName(varName);
            catchNode.setCatchCondition(catchCond);
            catchNode.setBody(catchBlock);
            if (guardPos != -1) {
                catchNode.setIfPosition(guardPos - catchPos);
            }
            catchNode.setParens(lp, rp);
            catchNode.setLineno(catchLineNum);
            if (mustMatchToken(Token.RC, "msg.no.brace.after.body"))
                tryEnd = ts.tokenEnd;
            catchNode.setLength(tryEnd - catchPos);
            if (clauses == null)
                clauses = new ArrayList<CatchClause>();
            clauses.add(catchNode);
        }
    } else if (peek != Token.FINALLY) {
        mustMatchToken(Token.FINALLY, "msg.try.no.catchfinally");
    }
    AstNode finallyBlock = null;
    if (matchToken(Token.FINALLY)) {
        finallyPos = ts.tokenBeg;
        finallyBlock = statement();
        tryEnd = getNodeEnd(finallyBlock);
    }
    TryStatement pn = new TryStatement(tryPos, tryEnd - tryPos);
    pn.setTryBlock(tryBlock);
    pn.setCatchClauses(clauses);
    pn.setFinallyBlock(finallyBlock);
    if (finallyPos != -1) {
        pn.setFinallyPosition(finallyPos - tryPos);
    }
    pn.setLineno(lineno);
    if (jsdocNode != null) {
        pn.setJsDocNode(jsdocNode);
    }
    return pn;
}
Also used : Comment(org.mozilla.javascript.ast.Comment) TryStatement(org.mozilla.javascript.ast.TryStatement) ArrayList(java.util.ArrayList) Block(org.mozilla.javascript.ast.Block) CatchClause(org.mozilla.javascript.ast.CatchClause) XmlString(org.mozilla.javascript.ast.XmlString) AstNode(org.mozilla.javascript.ast.AstNode) Name(org.mozilla.javascript.ast.Name)

Example 5 with Comment

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

the class Parser method parenExpr.

private AstNode parenExpr() throws IOException {
    boolean wasInForInit = inForInit;
    inForInit = false;
    try {
        Comment jsdocNode = getAndResetJsDoc();
        int lineno = ts.lineno;
        int begin = ts.tokenBeg;
        AstNode e = (peekToken() == Token.RP ? new EmptyExpression(begin) : 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;
    }
}
Also used : ParenthesizedExpression(org.mozilla.javascript.ast.ParenthesizedExpression) Comment(org.mozilla.javascript.ast.Comment) AstNode(org.mozilla.javascript.ast.AstNode) EmptyExpression(org.mozilla.javascript.ast.EmptyExpression)

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