Home | History | Annotate | Download | only in javaparser
      1 package com.github.javaparser;
      2 
      3 import com.github.javaparser.ast.comments.BlockComment;
      4 import com.github.javaparser.ast.comments.Comment;
      5 import com.github.javaparser.ast.comments.JavadocComment;
      6 import com.github.javaparser.ast.comments.LineComment;
      7 
      8 import static com.github.javaparser.GeneratedJavaParserConstants.*;
      9 import static com.github.javaparser.Position.pos;
     10 
     11 /**
     12  * Base class for {@link com.github.javaparser.GeneratedJavaParserTokenManager}
     13  */
     14 abstract class GeneratedJavaParserTokenManagerBase {
     15     /**
     16      * Create a TokenRange that spans exactly one token
     17      */
     18     private static TokenRange tokenRange(Token token) {
     19         JavaToken javaToken = token.javaToken;
     20         return new TokenRange(javaToken, javaToken);
     21     }
     22 
     23     /**
     24      * Since comments are completely captured in a single token, including their delimiters, deconstruct them here so we
     25      * can turn them into nodes later on.
     26      */
     27     static Comment createCommentFromToken(Token token) {
     28         String commentText = token.image;
     29         if (token.kind == JAVADOC_COMMENT) {
     30             return new JavadocComment(tokenRange(token), commentText.substring(3, commentText.length() - 2));
     31         } else if (token.kind == MULTI_LINE_COMMENT) {
     32             return new BlockComment(tokenRange(token), commentText.substring(2, commentText.length() - 2));
     33         } else if (token.kind == SINGLE_LINE_COMMENT) {
     34             // line comments have their end of line character(s) included, and we don't want that.
     35             Range range = new Range(pos(token.beginLine, token.beginColumn), pos(token.endLine, token.endColumn));
     36             while (commentText.endsWith("\r") || commentText.endsWith("\n")) {
     37                 commentText = commentText.substring(0, commentText.length() - 1);
     38             }
     39             range = range.withEnd(pos(range.begin.line, range.begin.column + commentText.length()));
     40             LineComment comment = new LineComment(tokenRange(token), commentText.substring(2));
     41             comment.setRange(range);
     42             return comment;
     43         }
     44         throw new AssertionError("Unexpectedly got passed a non-comment token.");
     45     }
     46 }
     47