Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/CatchClause.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.Token;
10
11 /**
12 * Node representing a catch-clause of a try-statement.
13 * Node type is {@link Token#CATCH}.
14 *
15 * <pre><i>CatchClause</i> :
16 * <b>catch</b> ( <i><b>Identifier</b></i> [<b>if</b> Expression] ) Block</pre>
17 */
18 public class CatchClause extends AstNode {
19
20 private Name varName;
21 private AstNode catchCondition;
22 private Block body;
23 private int ifPosition = -1;
24 private int lp = -1;
25 private int rp = -1;
26
27 {
28 type = Token.CATCH;
29 }
30
31 public CatchClause() {
32 }
33
34 public CatchClause(int pos) {
35 super(pos);
36 }
37
38 public CatchClause(int pos, int len) {
39 super(pos, len);
40 }
41
42 /**
43 * Returns catch variable node
44 * @return catch variable
45 */
46 public Name getVarName() {
47 return varName;
48 }
49
50 /**
51 * Sets catch variable node, and sets its parent to this node.
52 * @param varName catch variable
53 * @throws IllegalArgumentException if varName is {@code null}
54 */
55 public void setVarName(Name varName) {
56 assertNotNull(varName);
57 this.varName = varName;
58 varName.setParent(this);
59 }
60
61 /**
62 * Returns catch condition node, if present
63 * @return catch condition node, {@code null} if not present
64 */
65 public AstNode getCatchCondition() {
66 return catchCondition;
67 }
68
69 /**
70 * Sets catch condition node, and sets its parent to this node.
71 * @param catchCondition catch condition node. Can be {@code null}.
72 */
73 public void setCatchCondition(AstNode catchCondition) {
74 this.catchCondition = catchCondition;
75 if (catchCondition != null)
76 catchCondition.setParent(this);
77 }
78
79 /**
80 * Returns catch body
81 */
82 public Block getBody() {
83 return body;
84 }
85
86 /**
87 * Sets catch body, and sets its parent to this node.
88 * @throws IllegalArgumentException if body is {@code null}
89 */
90 public void setBody(Block body) {
91 assertNotNull(body);
92 this.body = body;
93 body.setParent(this);
94 }
95
96 /**
97 * Returns left paren position
98 */
99 public int getLp() {
100 return lp;
101 }
102
103 /**
104 * Sets left paren position
105 */
106 public void setLp(int lp) {
107 this.lp = lp;
108 }
109
110 /**
111 * Returns right paren position
112 */
113 public int getRp() {
114 return rp;
115 }
116
117 /**
118 * Sets right paren position
119 */
120 public void setRp(int rp) {
121 this.rp = rp;
122 }
123
124 /**
125 * Sets both paren positions
126 */
127 public void setParens(int lp, int rp) {
128 this.lp = lp;
129 this.rp = rp;
130 }
131
132 /**
133 * Returns position of "if" keyword
134 * @return position of "if" keyword, if present, or -1
135 */
136 public int getIfPosition() {
137 return ifPosition;
138 }
139
140 /**
141 * Sets position of "if" keyword
142 * @param ifPosition position of "if" keyword, if present, or -1
143 */
144 public void setIfPosition(int ifPosition) {
145 this.ifPosition = ifPosition;
146 }
147
148 @Override
149 public String toSource(int depth) {
150 StringBuilder sb = new StringBuilder();
151 sb.append(makeIndent(depth));
152 sb.append("catch (");
153 sb.append(varName.toSource(0));
154 if (catchCondition != null) {
155 sb.append(" if ");
156 sb.append(catchCondition.toSource(0));
157 }
158 sb.append(") ");
159 sb.append(body.toSource(0));
160 return sb.toString();
161 }
162
163 /**
164 * Visits this node, the catch var name node, the condition if
165 * non-{@code null}, and the catch body.
166 */
167 @Override
168 public void visit(NodeVisitor v) {
169 if (v.visit(this)) {
170 varName.visit(v);
171 if (catchCondition != null) {
172 catchCondition.visit(v);
173 }
174 body.visit(v);
175 }
176 }
177 }