Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/InfixExpression.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 representing an infix (binary operator) expression.
13 * The operator is the node's {@link Token} type.
14 */
15 public class InfixExpression extends AstNode {
16
17 protected AstNode left;
18 protected AstNode right;
19 protected int operatorPosition = -1;
20
21 public InfixExpression() {
22 }
23
24 public InfixExpression(int pos) {
25 super(pos);
26 }
27
28 public InfixExpression(int pos, int len) {
29 super(pos, len);
30 }
31
32 public InfixExpression(int pos, int len,
33 AstNode left,
34 AstNode right) {
35 super(pos, len);
36 setLeft(left);
37 setRight(right);
38 }
39
40 /**
41 * Constructs a new {@code InfixExpression}. Updates bounds to include
42 * left and right nodes.
43 */
44 public InfixExpression(AstNode left, AstNode right) {
45 setLeftAndRight(left, right);
46 }
47
48 /**
49 * Constructs a new {@code InfixExpression}.
50 * @param operatorPos the <em>absolute</em> position of the operator
51 */
52 public InfixExpression(int operator, AstNode left,
53 AstNode right, int operatorPos) {
54 setType(operator);
55 setOperatorPosition(operatorPos - left.getPosition());
56 setLeftAndRight(left, right);
57 }
58
59 public void setLeftAndRight(AstNode left, AstNode right) {
60 assertNotNull(left);
61 assertNotNull(right);
62 // compute our bounds while children have absolute positions
63 int beg = left.getPosition();
64 int end = right.getPosition() + right.getLength();
65 setBounds(beg, end);
66
67 // this updates their positions to be parent-relative
68 setLeft(left);
69 setRight(right);
70 }
71
72 /**
73 * Returns operator token &ndash; alias for {@link #getType}
74 */
75 public int getOperator() {
76 return getType();
77 }
78
79 /**
80 * Sets operator token &ndash; like {@link #setType}, but throws
81 * an exception if the operator is invalid.
82 * @throws IllegalArgumentException if operator is not a valid token
83 * code
84 */
85 public void setOperator(int operator) {
86 if (!Token.isValidToken(operator))
87 throw new IllegalArgumentException("Invalid token: " + operator);
88 setType(operator);
89 }
90
91 /**
92 * Returns the left-hand side of the expression
93 */
94 public AstNode getLeft() {
95 return left;
96 }
97
98 /**
99 * Sets the left-hand side of the expression, and sets its
100 * parent to this node.
101 * @param left the left-hand side of the expression
102 * @throws IllegalArgumentException} if left is {@code null}
103 */
104 public void setLeft(AstNode left) {
105 assertNotNull(left);
106 this.left = left;
107 // line number should agree with source position
108 setLineno(left.getLineno());
109 left.setParent(this);
110 }
111
112 /**
113 * Returns the right-hand side of the expression
114 * @return the right-hand side. It's usually an
115 * {@link AstNode} node, but can also be a {@link FunctionNode}
116 * representing Function expressions.
117 */
118 public AstNode getRight() {
119 return right;
120 }
121
122 /**
123 * Sets the right-hand side of the expression, and sets its parent to this
124 * node.
125 * @throws IllegalArgumentException} if right is {@code null}
126 */
127 public void setRight(AstNode right) {
128 assertNotNull(right);
129 this.right = right;
130 right.setParent(this);
131 }
132
133 /**
134 * Returns relative offset of operator token
135 */
136 public int getOperatorPosition() {
137 return operatorPosition;
138 }
139
140 /**
141 * Sets operator token's relative offset
142 * @param operatorPosition offset in parent of operator token
143 */
144 public void setOperatorPosition(int operatorPosition) {
145 this.operatorPosition = operatorPosition;
146 }
147
148 @Override
149 public boolean hasSideEffects() {
150 // the null-checks are for malformed expressions in IDE-mode
151 switch (getType()) {
152 case Token.COMMA:
153 return right != null && right.hasSideEffects();
154 case Token.AND:
155 case Token.OR:
156 return left != null && left.hasSideEffects()
157 || (right != null && right.hasSideEffects());
158 default:
159 return super.hasSideEffects();
160 }
161 }
162
163 @Override
164 public String toSource(int depth) {
165 StringBuilder sb = new StringBuilder();
166 sb.append(makeIndent(depth));
167 sb.append(left.toSource());
168 sb.append(" ");
169 sb.append(operatorToString(getType()));
170 sb.append(" ");
171 sb.append(right.toSource());
172 return sb.toString();
173 }
174
175 /**
176 * Visits this node, the left operand, and the right operand.
177 */
178 @Override
179 public void visit(NodeVisitor v) {
180 if (v.visit(this)) {
181 left.visit(v);
182 right.visit(v);
183 }
184 }
185 }