| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript.ast; |
| 8 |
|
| 9 |
import org.mozilla.javascript.Node; |
| 10 |
import org.mozilla.javascript.Token; |
| 11 |
|
| 12 |
import java.util.SortedSet; |
| 13 |
import java.util.TreeSet; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public class AstRoot extends ScriptNode { |
| 26 |
|
| 27 |
private SortedSet<Comment> comments; |
| 28 |
|
| 29 |
{ |
| 30 |
type = Token.SCRIPT; |
| 31 |
} |
| 32 |
|
| 33 |
public AstRoot() { |
| 34 |
} |
| 35 |
|
| 36 |
public AstRoot(int pos) { |
| 37 |
super(pos); |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public SortedSet<Comment> getComments() { |
| 45 |
return comments; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public void setComments(SortedSet<Comment> comments) { |
| 54 |
if (comments == null) { |
| 55 |
this.comments = null; |
| 56 |
} else { |
| 57 |
if (this.comments != null) |
| 58 |
this.comments.clear(); |
| 59 |
for (Comment c : comments) |
| 60 |
addComment(c); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public void addComment(Comment comment) { |
| 70 |
assertNotNull(comment); |
| 71 |
if (comments == null) { |
| 72 |
comments = new TreeSet<Comment>(new AstNode.PositionComparator()); |
| 73 |
} |
| 74 |
comments.add(comment); |
| 75 |
comment.setParent(this); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public void visitComments(NodeVisitor visitor) { |
| 86 |
if (comments != null) { |
| 87 |
for (Comment c : comments) { |
| 88 |
visitor.visit(c); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public void visitAll(NodeVisitor visitor) { |
| 101 |
visit(visitor); |
| 102 |
visitComments(visitor); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public String toSource(int depth) { |
| 107 |
StringBuilder sb = new StringBuilder(); |
| 108 |
for (Node node : this) { |
| 109 |
sb.append(((AstNode)node).toSource(depth)); |
| 110 |
} |
| 111 |
return sb.toString(); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
@Override |
| 118 |
public String debugPrint() { |
| 119 |
DebugPrintVisitor dpv = new DebugPrintVisitor(new StringBuilder(1000)); |
| 120 |
visitAll(dpv); |
| 121 |
return dpv.toString(); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public void checkParentLinks() { |
| 130 |
this.visit(new NodeVisitor() { |
| 131 |
public boolean visit(AstNode node) { |
| 132 |
int type = node.getType(); |
| 133 |
if (type == Token.SCRIPT) |
| 134 |
return true; |
| 135 |
if (node.getParent() == null) |
| 136 |
throw new IllegalStateException |
| 137 |
("No parent for node: " + node |
| 138 |
+ "\n" + node.toSource(0)); |
| 139 |
return true; |
| 140 |
} |
| 141 |
}); |
| 142 |
} |
| 143 |
} |