Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/LetNode.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 * AST node for let statements and expressions.
13 * Node type is {@link Token#LET} or {@link Token#LETEXPR}.<p>
14 *
15 * <pre> <i>LetStatement</i>:
16 * <b>let</b> ( VariableDeclarationList ) Block
17 * <i>LetExpression</i>:
18 * <b>let</b> ( VariableDeclarationList ) Expression</pre>
19 *
20 * Note that standalone let-statements with no parens or body block,
21 * such as {@code let x=6, y=7;}, are represented as a
22 * {@link VariableDeclaration} node of type {@code Token.LET},
23 * wrapped with an {@link ExpressionStatement}.<p>
24 */
25 public class LetNode extends Scope {
26
27 private VariableDeclaration variables;
28 private AstNode body;
29 private int lp = -1;
30 private int rp = -1;
31
32 {
33 type = Token.LETEXPR;
34 }
35
36 public LetNode() {
37 }
38
39 public LetNode(int pos) {
40 super(pos);
41 }
42
43 public LetNode(int pos, int len) {
44 super(pos, len);
45 }
46
47 /**
48 * Returns variable list
49 */
50 public VariableDeclaration getVariables() {
51 return variables;
52 }
53
54 /**
55 * Sets variable list. Sets list parent to this node.
56 * @throws IllegalArgumentException if variables is {@code null}
57 */
58 public void setVariables(VariableDeclaration variables) {
59 assertNotNull(variables);
60 this.variables = variables;
61 variables.setParent(this);
62 }
63
64 /**
65 * Returns body statement or expression. Body is {@code null} if the
66 * form of the let statement is similar to a VariableDeclaration, with no
67 * curly-brace. (This form is used to define let-bound variables in the
68 * scope of the current block.)<p>
69 *
70 * @return the body form
71 */
72 public AstNode getBody() {
73 return body;
74 }
75
76 /**
77 * Sets body statement or expression. Also sets the body parent to this
78 * node.
79 * @param body the body statement or expression. May be
80 * {@code null}.
81 */
82 public void setBody(AstNode body) {
83 this.body = body;
84 if (body != null)
85 body.setParent(this);
86 }
87
88 /**
89 * Returns left paren position, -1 if missing
90 */
91 public int getLp() {
92 return lp;
93 }
94
95 /**
96 * Sets left paren position
97 */
98 public void setLp(int lp) {
99 this.lp = lp;
100 }
101
102 /**
103 * Returns right paren position, -1 if missing
104 */
105 public int getRp() {
106 return rp;
107 }
108
109 /**
110 * Sets right paren position
111 */
112 public void setRp(int rp) {
113 this.rp = rp;
114 }
115
116 /**
117 * Sets both paren positions
118 */
119 public void setParens(int lp, int rp) {
120 this.lp = lp;
121 this.rp = rp;
122 }
123
124 @Override
125 public String toSource(int depth) {
126 String pad = makeIndent(depth);
127 StringBuilder sb = new StringBuilder();
128 sb.append(pad);
129 sb.append("let (");
130 printList(variables.getVariables(), sb);
131 sb.append(") ");
132 if (body != null) {
133 sb.append(body.toSource(depth));
134 }
135 return sb.toString();
136 }
137
138 /**
139 * Visits this node, the variable list, and if present, the body
140 * expression or statement.
141 */
142 @Override
143 public void visit(NodeVisitor v) {
144 if (v.visit(this)) {
145 variables.visit(v);
146 if (body != null) {
147 body.visit(v);
148 }
149 }
150 }
151 }