| 1 | /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 | |
| 7 | package org.mozilla.javascript.ast; |
| 8 | |
| 9 | import org.mozilla.javascript.Token; |
| 10 | |
| 11 | /** |
| 12 | * Node representing comments. |
| 13 | * Node type is {@link Token#COMMENT}.<p> |
| 14 | * |
| 15 | * <p>JavaScript effectively has five comment types: |
| 16 | * <ol> |
| 17 | * <li>// line comments</li> |
| 18 | * <li>/<span class="none">* block comments *\/</li> |
| 19 | * <li>/<span class="none">** jsdoc comments *\/</li> |
| 20 | * <li><!-- html-open line comments</li> |
| 21 | * <li>^\\s*--> html-close line comments</li> |
| 22 | * </ol> |
| 23 | * |
| 24 | * <p>The first three should be familiar to Java programmers. JsDoc comments |
| 25 | * are really just block comments with some conventions about the formatting |
| 26 | * within the comment delimiters. Line and block comments are described in the |
| 27 | * Ecma-262 specification. <p> |
| 28 | * |
| 29 | * <p>SpiderMonkey and Rhino also support HTML comment syntax, but somewhat |
| 30 | * counterintuitively, the syntax does not produce a block comment. Instead, |
| 31 | * everything from the string <!-- through the end of the line is considered |
| 32 | * a comment, and if the token --> is the first non-whitespace on the line, |
| 33 | * then the line is considered a line comment. This is to support parsing |
| 34 | * JavaScript in <script> HTML tags that has been "hidden" from very old |
| 35 | * browsers by surrounding it with HTML comment delimiters. <p> |
| 36 | * |
| 37 | * Note the node start position for Comment nodes is still relative to the |
| 38 | * parent, but Comments are always stored directly in the AstRoot node, so |
| 39 | * they are also effectively absolute offsets. |
| 40 | */ |
| 41 | public class Comment extends AstNode { |
| 42 | |
| 43 | private String value; |
| 44 | private Token.CommentType commentType; |
| 45 | |
| 46 | { |
| 47 | type = Token.COMMENT; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Constructs a new Comment |
| 52 | * @param pos the start position |
| 53 | * @param len the length including delimiter(s) |
| 54 | * @param type the comment type |
| 55 | * @param value the value of the comment, as a string |
| 56 | */ |
| 57 | public Comment(int pos, int len, Token.CommentType type, String value) { |
| 58 | super(pos, len); |
| 59 | commentType = type; |
| 60 | this.value = value; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns the comment style |
| 65 | */ |
| 66 | public Token.CommentType getCommentType() { |
| 67 | return commentType; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Sets the comment style |
| 72 | * @param type the comment style, a |
| 73 | * {@link org.mozilla.javascript.Token.CommentType} |
| 74 | */ |
| 75 | public void setCommentType(Token.CommentType type) { |
| 76 | this.commentType = type; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Returns a string of the comment value. |
| 81 | */ |
| 82 | public String getValue() { |
| 83 | return value; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public String toSource(int depth) { |
| 88 | StringBuilder sb = new StringBuilder(getLength() + 10); |
| 89 | sb.append(makeIndent(depth)); |
| 90 | sb.append(value); |
| 91 | return sb.toString(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Comment nodes are not visited during normal visitor traversals, |
| 96 | * but comply with the {@link AstNode#visit} interface. |
| 97 | */ |
| 98 | @Override |
| 99 | public void visit(NodeVisitor v) { |
| 100 | v.visit(this); |
| 101 | } |
| 102 | } |