Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/UnaryExpression.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 unary operators such as {@code ++},
13 * {@code ~}, {@code typeof} and {@code delete}. The type field
14 * is set to the appropriate Token type for the operator. The node length spans
15 * from the operator to the end of the operand (for prefix operators) or from
16 * the start of the operand to the operator (for postfix).<p>
17 *
18 * The {@code default xml namespace = &lt;expr&gt;} statement in E4X
19 * (JavaScript 1.6) is represented as a {@code UnaryExpression} of node
20 * type {@link Token#DEFAULTNAMESPACE}, wrapped with an
21 * {@link ExpressionStatement}.
22 */
23 public class UnaryExpression extends AstNode {
24
25 private AstNode operand;
26 private boolean isPostfix;
27
28 public UnaryExpression() {
29 }
30
31 public UnaryExpression(int pos) {
32 super(pos);
33 }
34
35 /**
36 * Constructs a new postfix UnaryExpression
37 */
38 public UnaryExpression(int pos, int len) {
39 super(pos, len);
40 }
41
42 /**
43 * Constructs a new prefix UnaryExpression.
44 */
45 public UnaryExpression(int operator, int operatorPosition,
46 AstNode operand) {
47 this(operator, operatorPosition, operand, false);
48 }
49
50 /**
51 * Constructs a new UnaryExpression with the specified operator
52 * and operand. It sets the parent of the operand, and sets its own bounds
53 * to encompass the operator and operand.
54 * @param operator the node type
55 * @param operatorPosition the absolute position of the operator.
56 * @param operand the operand expression
57 * @param postFix true if the operator follows the operand. Int
58 * @throws IllegalArgumentException} if {@code operand} is {@code null}
59 */
60 public UnaryExpression(int operator, int operatorPosition,
61 AstNode operand, boolean postFix) {
62 assertNotNull(operand);
63 int beg = postFix ? operand.getPosition() : operatorPosition;
64 // JavaScript only has ++ and -- postfix operators, so length is 2
65 int end = postFix
66 ? operatorPosition + 2
67 : operand.getPosition() + operand.getLength();
68 setBounds(beg, end);
69 setOperator(operator);
70 setOperand(operand);
71 isPostfix = postFix;
72 }
73
74 /**
75 * Returns operator token &ndash; alias for {@link #getType}
76 */
77 public int getOperator() {
78 return type;
79 }
80
81 /**
82 * Sets operator &ndash; same as {@link #setType}, but throws an
83 * exception if the operator is invalid
84 * @throws IllegalArgumentException if operator is not a valid
85 * Token code
86 */
87 public void setOperator(int operator) {
88 if (!Token.isValidToken(operator))
89 throw new IllegalArgumentException("Invalid token: " + operator);
90 setType(operator);
91 }
92
93 public AstNode getOperand() {
94 return operand;
95 }
96
97 /**
98 * Sets the operand, and sets its parent to be this node.
99 * @throws IllegalArgumentException} if {@code operand} is {@code null}
100 */
101 public void setOperand(AstNode operand) {
102 assertNotNull(operand);
103 this.operand = operand;
104 operand.setParent(this);
105 }
106
107 /**
108 * Returns whether the operator is postfix
109 */
110 public boolean isPostfix() {
111 return isPostfix;
112 }
113
114 /**
115 * Returns whether the operator is prefix
116 */
117 public boolean isPrefix() {
118 return !isPostfix;
119 }
120
121 /**
122 * Sets whether the operator is postfix
123 */
124 public void setIsPostfix(boolean isPostfix) {
125 this.isPostfix = isPostfix;
126 }
127
128 @Override
129 public String toSource(int depth) {
130 StringBuilder sb = new StringBuilder();
131 sb.append(makeIndent(depth));
132 int type = getType();
133 if (!isPostfix) {
134 sb.append(operatorToString(type));
135 if (type == Token.TYPEOF || type == Token.DELPROP || type == Token.VOID) {
136 sb.append(" ");
137 }
138 }
139 sb.append(operand.toSource());
140 if (isPostfix) {
141 sb.append(operatorToString(type));
142 }
143 return sb.toString();
144 }
145
146 /**
147 * Visits this node, then the operand.
148 */
149 @Override
150 public void visit(NodeVisitor v) {
151 if (v.visit(this)) {
152 operand.visit(v);
153 }
154 }
155 }