Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/AstRoot.java

raw symbols SCIP
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.Node;
10 import org.mozilla.javascript.Token;
11
12 import java.util.SortedSet;
13 import java.util.TreeSet;
14
15 /**
16 * Node for the root of a parse tree. It contains the statements and functions
17 * in the script, and a list of {@link Comment} nodes associated with the script
18 * as a whole. Node type is {@link Token#SCRIPT}. <p>
19 *
20 * Note that the tree itself does not store errors. To collect the parse errors
21 * and warnings, pass an {@link org.mozilla.javascript.ErrorReporter} to the
22 * {@link org.mozilla.javascript.Parser} via the
23 * {@link org.mozilla.javascript.CompilerEnvirons}.
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 * Returns comment set
42 * @return comment set, sorted by start position. Can be {@code null}.
43 */
44 public SortedSet<Comment> getComments() {
45 return comments;
46 }
47
48 /**
49 * Sets comment list, and updates the parent of each entry to point
50 * to this node. Replaces any existing comments.
51 * @param comments comment list. can be {@code null}.
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 * Add a comment to the comment set.
66 * @param comment the comment node.
67 * @throws IllegalArgumentException if comment is {@code null}
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 * Visits the comment nodes in the order they appear in the source code.
80 * The comments are not visited by the {@link #visit} function - you must
81 * use this function to visit them.
82 * @param visitor the callback object. It is passed each comment node.
83 * The return value is ignored.
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 * Visits the AST nodes, then the comment nodes.
95 * This method is equivalent to calling {@link #visit}, then
96 * {@link #visitComments}. The return value
97 * is ignored while visiting comment nodes.
98 * @param visitor the callback object.
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 * A debug-printer that includes comments (at the end).
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 * Debugging function to check that the parser has set the parent
126 * link for every node in the tree.
127 * @throws IllegalStateException if a parent link is missing
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 }