| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package org.mozilla.javascript.ast; |
| 8 |
|
| 9 |
import org.mozilla.javascript.Token; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 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 |
|
| 37 |
|
| 38 |
public UnaryExpression(int pos, int len) { |
| 39 |
super(pos, len); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public UnaryExpression(int operator, int operatorPosition, |
| 46 |
AstNode operand) { |
| 47 |
this(operator, operatorPosition, operand, false); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public UnaryExpression(int operator, int operatorPosition, |
| 61 |
AstNode operand, boolean postFix) { |
| 62 |
assertNotNull(operand); |
| 63 |
int beg = postFix ? operand.getPosition() : operatorPosition; |
| 64 |
|
| 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 |
|
| 76 |
|
| 77 |
public int getOperator() { |
| 78 |
return type; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 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 |
|
| 99 |
|
| 100 |
|
| 101 |
public void setOperand(AstNode operand) { |
| 102 |
assertNotNull(operand); |
| 103 |
this.operand = operand; |
| 104 |
operand.setParent(this); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public boolean isPostfix() { |
| 111 |
return isPostfix; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public boolean isPrefix() { |
| 118 |
return !isPostfix; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 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 |
|
| 148 |
|
| 149 |
@Override |
| 150 |
public void visit(NodeVisitor v) { |
| 151 |
if (v.visit(this)) { |
| 152 |
operand.visit(v); |
| 153 |
} |
| 154 |
} |
| 155 |
} |