Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/WithStatement.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 * With statement. Node type is {@link Token#WITH}.<p>
13 *
14 * <pre><i>WithStatement</i> :
15 * <b>with</b> ( Expression ) Statement ;</pre>
16 */
17 public class WithStatement extends AstNode {
18
19 private AstNode expression;
20 private AstNode statement;
21 private int lp = -1;
22 private int rp = -1;
23
24 {
25 type = Token.WITH;
26 }
27
28 public WithStatement() {
29 }
30
31 public WithStatement(int pos) {
32 super(pos);
33 }
34
35 public WithStatement(int pos, int len) {
36 super(pos, len);
37 }
38
39 /**
40 * Returns object expression
41 */
42 public AstNode getExpression() {
43 return expression;
44 }
45
46 /**
47 * Sets object expression (and its parent link)
48 * @throws IllegalArgumentException} if expression is {@code null}
49 */
50 public void setExpression(AstNode expression) {
51 assertNotNull(expression);
52 this.expression = expression;
53 expression.setParent(this);
54 }
55
56 /**
57 * Returns the statement or block
58 */
59 public AstNode getStatement() {
60 return statement;
61 }
62
63 /**
64 * Sets the statement (and sets its parent link)
65 * @throws IllegalArgumentException} if statement is {@code null}
66 */
67 public void setStatement(AstNode statement) {
68 assertNotNull(statement);
69 this.statement = statement;
70 statement.setParent(this);
71 }
72
73 /**
74 * Returns left paren offset
75 */
76 public int getLp() {
77 return lp;
78 }
79
80 /**
81 * Sets left paren offset
82 */
83 public void setLp(int lp) {
84 this.lp = lp;
85 }
86
87 /**
88 * Returns right paren offset
89 */
90 public int getRp() {
91 return rp;
92 }
93
94 /**
95 * Sets right paren offset
96 */
97 public void setRp(int rp) {
98 this.rp = rp;
99 }
100
101 /**
102 * Sets both paren positions
103 */
104 public void setParens(int lp, int rp) {
105 this.lp = lp;
106 this.rp = rp;
107 }
108
109 @Override
110 public String toSource(int depth) {
111 StringBuilder sb = new StringBuilder();
112 sb.append(makeIndent(depth));
113 sb.append("with (");
114 sb.append(expression.toSource(0));
115 sb.append(") ");
116 if (statement.getType() == Token.BLOCK) {
117 sb.append(statement.toSource(depth).trim());
118 sb.append("\n");
119 } else {
120 sb.append("\n").append(statement.toSource(depth + 1));
121 }
122 return sb.toString();
123 }
124
125 /**
126 * Visits this node, then the with-object, then the body statement.
127 */
128 @Override
129 public void visit(NodeVisitor v) {
130 if (v.visit(this)) {
131 expression.visit(v);
132 statement.visit(v);
133 }
134 }
135 }