Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/ElementGet.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 an indexed property reference, such as {@code foo['bar']} or
13 * {@code foo[2]}. This is sometimes called an "element-get" operation, hence
14 * the name of the node.<p>
15 *
16 * Node type is {@link Token#GETELEM}.<p>
17 *
18 * The node bounds extend from the beginning position of the target through the
19 * closing right-bracket. In the presence of a syntax error, the right bracket
20 * position is -1, and the node ends at the end of the element expression.
21 */
22 public class ElementGet extends AstNode {
23
24 private AstNode target;
25 private AstNode element;
26 private int lb = -1;
27 private int rb = -1;
28
29 {
30 type = Token.GETELEM;
31 }
32
33 public ElementGet() {
34 }
35
36 public ElementGet(int pos) {
37 super(pos);
38 }
39
40 public ElementGet(int pos, int len) {
41 super(pos, len);
42 }
43
44 public ElementGet(AstNode target, AstNode element) {
45 setTarget(target);
46 setElement(element);
47 }
48
49 /**
50 * Returns the object on which the element is being fetched.
51 */
52 public AstNode getTarget() {
53 return target;
54 }
55
56 /**
57 * Sets target object, and sets its parent to this node.
58 * @param target expression evaluating to the object upon which
59 * to do the element lookup
60 * @throws IllegalArgumentException if target is {@code null}
61 */
62 public void setTarget(AstNode target) {
63 assertNotNull(target);
64 this.target = target;
65 target.setParent(this);
66 }
67
68 /**
69 * Returns the element being accessed
70 */
71 public AstNode getElement() {
72 return element;
73 }
74
75 /**
76 * Sets the element being accessed, and sets its parent to this node.
77 * @throws IllegalArgumentException if element is {@code null}
78 */
79 public void setElement(AstNode element) {
80 assertNotNull(element);
81 this.element = element;
82 element.setParent(this);
83 }
84
85 /**
86 * Returns left bracket position
87 */
88 public int getLb() {
89 return lb;
90 }
91
92 /**
93 * Sets left bracket position
94 */
95 public void setLb(int lb) {
96 this.lb = lb;
97 }
98
99 /**
100 * Returns right bracket position, -1 if missing
101 */
102 public int getRb() {
103 return rb;
104 }
105
106 /**
107 * Sets right bracket position, -1 if not present
108 */
109 public void setRb(int rb) {
110 this.rb = rb;
111 }
112
113 public void setParens(int lb, int rb) {
114 this.lb = lb;
115 this.rb = rb;
116 }
117
118 @Override
119 public String toSource(int depth) {
120 StringBuilder sb = new StringBuilder();
121 sb.append(makeIndent(depth));
122 sb.append(target.toSource(0));
123 sb.append("[");
124 sb.append(element.toSource(0));
125 sb.append("]");
126 return sb.toString();
127 }
128
129 /**
130 * Visits this node, the target, and the index expression.
131 */
132 @Override
133 public void visit(NodeVisitor v) {
134 if (v.visit(this)) {
135 target.visit(v);
136 element.visit(v);
137 }
138 }
139 }