Search in sources :

Example 1 with Comment

use of com.google.javascript.jscomp.parsing.parser.trees.Comment in project closure-compiler by google.

the class Scanner method skipMultiLineComment.

private void skipMultiLineComment() {
    int startOffset = index;
    // '/'
    nextChar();
    // '*'
    nextChar();
    while (!isAtEnd() && (peekChar() != '*' || peekChar(1) != '/')) {
        nextChar();
    }
    if (!isAtEnd()) {
        nextChar();
        nextChar();
        Comment.Type type = Comment.Type.BLOCK;
        if (index - startOffset > 4) {
            if (this.contents.charAt(startOffset + 2) == '*') {
                type = Comment.Type.JSDOC;
            } else if (this.contents.charAt(startOffset + 2) == '!') {
                type = Comment.Type.IMPORTANT;
            }
        }
        SourceRange range = lineNumberScanner.getSourceRange(startOffset, index);
        String value = this.contents.substring(startOffset, index);
        recordComment(type, range, value);
    } else {
        reportError("unterminated comment");
    }
}
Also used : Comment(com.google.javascript.jscomp.parsing.parser.trees.Comment) FormatString(com.google.errorprone.annotations.FormatString) SourceRange(com.google.javascript.jscomp.parsing.parser.util.SourceRange)

Example 2 with Comment

use of com.google.javascript.jscomp.parsing.parser.trees.Comment in project closure-compiler by google.

the class ParserRunner method parse.

public static ParseResult parse(StaticSourceFile sourceFile, String sourceString, Config config, ErrorReporter errorReporter) {
    // TODO(johnlenz): unify "SourceFile", "Es6ErrorReporter" and "Config"
    String sourceName = sourceFile.getName();
    try {
        SourceFile file = new SourceFile(sourceName, sourceString);
        boolean keepGoing = config.runMode() == Config.RunMode.KEEP_GOING;
        Es6ErrorReporter es6ErrorReporter = new Es6ErrorReporter(errorReporter, keepGoing);
        com.google.javascript.jscomp.parsing.parser.Parser.Config es6config = newParserConfig(config);
        Parser p = new Parser(es6config, es6ErrorReporter, file);
        ProgramTree tree = p.parseProgram();
        Node root = null;
        List<Comment> comments = ImmutableList.of();
        FeatureSet features = p.getFeatures();
        if (tree != null && (!es6ErrorReporter.hadError() || keepGoing)) {
            IRFactory factory = IRFactory.transformTree(tree, sourceFile, config, errorReporter);
            root = factory.getResultNode();
            features = features.union(factory.getFeatures());
            root.putProp(Node.FEATURE_SET, features);
            if (config.jsDocParsingMode().shouldParseDescriptions()) {
                comments = p.getComments();
            }
        }
        return new ParseResult(root, comments, features, p.getSourceMapURL());
    } catch (Throwable t) {
        throw new RuntimeException("Exception parsing \"" + sourceName + "\"", t);
    }
}
Also used : Comment(com.google.javascript.jscomp.parsing.parser.trees.Comment) ProgramTree(com.google.javascript.jscomp.parsing.parser.trees.ProgramTree) Node(com.google.javascript.rhino.Node) Parser(com.google.javascript.jscomp.parsing.parser.Parser) FeatureSet(com.google.javascript.jscomp.parsing.parser.FeatureSet) StaticSourceFile(com.google.javascript.rhino.StaticSourceFile) SourceFile(com.google.javascript.jscomp.parsing.parser.SourceFile)

Example 3 with Comment

use of com.google.javascript.jscomp.parsing.parser.trees.Comment in project closure-compiler by google.

the class IRFactory method parseNonJSDocCommentAt.

/**
 * Creates a single NonJSDocComment from every comment associated with this node; or null if there
 * are no such comments.
 *
 * <p>It would be legal to replace all comments associated with this node with that one string.
 */
@Nullable
private NonJSDocComment parseNonJSDocCommentAt(SourcePosition pos, boolean isInline) {
    if (config.jsDocParsingMode() != JsDocParsing.INCLUDE_ALL_COMMENTS) {
        return null;
    }
    if (!this.nonJsdocTracker.hasPendingCommentBefore(pos)) {
        return null;
    }
    StringBuilder result = new StringBuilder();
    Comment firstComment = this.nonJsdocTracker.current();
    Comment lastComment = null;
    while (this.nonJsdocTracker.hasPendingCommentBefore(pos)) {
        Comment currentComment = this.nonJsdocTracker.current();
        if (lastComment != null) {
            for (int blankCount = currentComment.location.start.line - lastComment.location.end.line; blankCount > 0; blankCount--) {
                result.append("\n");
            }
        }
        result.append(currentComment.value);
        lastComment = currentComment;
        this.nonJsdocTracker.advance();
    }
    NonJSDocComment nonJSDocComment = new NonJSDocComment(firstComment.location.start, lastComment.location.end, result.toString());
    nonJSDocComment.setEndsAsLineComment(lastComment.type == Comment.Type.LINE);
    nonJSDocComment.setIsInline(isInline);
    return nonJSDocComment;
}
Also used : NonJSDocComment(com.google.javascript.rhino.NonJSDocComment) Comment(com.google.javascript.jscomp.parsing.parser.trees.Comment) NonJSDocComment(com.google.javascript.rhino.NonJSDocComment) Nullable(javax.annotation.Nullable)

Example 4 with Comment

use of com.google.javascript.jscomp.parsing.parser.trees.Comment in project closure-compiler by google.

the class IRFactory method getJSDocCommentAt.

private Comment getJSDocCommentAt(SourcePosition pos) {
    Comment closestPreviousComment = null;
    while (this.jsdocTracker.hasPendingCommentBefore(pos)) {
        closestPreviousComment = this.jsdocTracker.current();
        this.jsdocTracker.advance();
    }
    return closestPreviousComment;
}
Also used : NonJSDocComment(com.google.javascript.rhino.NonJSDocComment) Comment(com.google.javascript.jscomp.parsing.parser.trees.Comment)

Example 5 with Comment

use of com.google.javascript.jscomp.parsing.parser.trees.Comment in project closure-compiler by google.

the class IRFactory method transformTree.

public static IRFactory transformTree(ProgramTree tree, StaticSourceFile sourceFile, Config config, ErrorReporter errorReporter) {
    IRFactory irFactory = new IRFactory(sourceFile, config, errorReporter, tree.sourceComments);
    // don't call transform as we don't want standard jsdoc handling.
    Node n = irFactory.transformDispatcher.process(tree);
    irFactory.setSourceInfo(n, tree);
    if (tree.sourceComments != null) {
        for (Comment comment : tree.sourceComments) {
            if ((comment.type == Comment.Type.JSDOC || comment.type == Comment.Type.IMPORTANT) && !irFactory.parsedComments.contains(comment)) {
                irFactory.handlePossibleFileOverviewJsDoc(comment);
            }
        }
    }
    irFactory.setFileOverviewJsDoc(n);
    irFactory.validateAll(n);
    irFactory.resultNode = n;
    return irFactory;
}
Also used : NonJSDocComment(com.google.javascript.rhino.NonJSDocComment) Comment(com.google.javascript.jscomp.parsing.parser.trees.Comment) Node(com.google.javascript.rhino.Node)

Aggregations

Comment (com.google.javascript.jscomp.parsing.parser.trees.Comment)9 Node (com.google.javascript.rhino.Node)4 NonJSDocComment (com.google.javascript.rhino.NonJSDocComment)3 Compiler (com.google.javascript.jscomp.Compiler)2 CompilerOptions (com.google.javascript.jscomp.CompilerOptions)2 SourceFile (com.google.javascript.jscomp.SourceFile)2 Config (com.google.javascript.jscomp.parsing.Config)2 ParserRunner (com.google.javascript.jscomp.parsing.ParserRunner)2 InputId (com.google.javascript.rhino.InputId)2 FormatString (com.google.errorprone.annotations.FormatString)1 BasicErrorManager (com.google.javascript.jscomp.BasicErrorManager)1 CheckLevel (com.google.javascript.jscomp.CheckLevel)1 GatherModuleMetadata (com.google.javascript.jscomp.GatherModuleMetadata)1 JSError (com.google.javascript.jscomp.JSError)1 ModuleMetadata (com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata)1 FeatureSet (com.google.javascript.jscomp.parsing.parser.FeatureSet)1 Parser (com.google.javascript.jscomp.parsing.parser.Parser)1 SourceFile (com.google.javascript.jscomp.parsing.parser.SourceFile)1 ProgramTree (com.google.javascript.jscomp.parsing.parser.trees.ProgramTree)1 SourceRange (com.google.javascript.jscomp.parsing.parser.util.SourceRange)1