Debian Codegraph @

rhino/src/org/mozilla/javascript/ast/ExpressionStatement.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 expression in a statement context. The node type is
13 * {@link Token#EXPR_VOID} if inside a function, or else
14 * {@link Token#EXPR_RESULT} if inside a script.
15 */
16 public class ExpressionStatement extends AstNode {
17
18 private AstNode expr;
19
20 {
21 type = Token.EXPR_VOID;
22 }
23
24 /**
25 * Called by the parser to set node type to EXPR_RESULT
26 * if this node is not within a Function.
27 */
28 public void setHasResult() {
29 type = Token.EXPR_RESULT;
30 }
31
32 public ExpressionStatement() {
33 }
34
35 /**
36 * Constructs a new {@code ExpressionStatement} wrapping
37 * the specified expression. Sets this node's position to the
38 * position of the wrapped node, and sets the wrapped node's
39 * position to zero. Sets this node's length to the length of
40 * the wrapped node.
41 * @param expr the wrapped expression
42 * @param hasResult {@code true} if this expression has side
43 * effects. If true, sets node type to EXPR_RESULT, else to EXPR_VOID.
44 */
45 public ExpressionStatement(AstNode expr, boolean hasResult) {
46 this(expr);
47 if (hasResult) setHasResult();
48 }
49
50 /**
51 * Constructs a new {@code ExpressionStatement} wrapping
52 * the specified expression. Sets this node's position to the
53 * position of the wrapped node, and sets the wrapped node's
54 * position to zero. Sets this node's length to the length of
55 * the wrapped node.
56 * @param expr the wrapped expression
57 */
58 public ExpressionStatement(AstNode expr) {
59 this(expr.getPosition(), expr.getLength(), expr);
60 }
61
62 public ExpressionStatement(int pos, int len) {
63 super(pos, len);
64 }
65
66 /**
67 * Constructs a new {@code ExpressionStatement}
68 * @param expr the wrapped {@link AstNode}.
69 * The {@code ExpressionStatement}'s bounds are set to those of expr,
70 * and expr's parent is set to this node.
71 * @throws IllegalArgumentException if {@code expr} is null
72 */
73 public ExpressionStatement(int pos, int len, AstNode expr) {
74 super(pos, len);
75 setExpression(expr);
76 }
77
78 /**
79 * Returns the wrapped expression
80 */
81 public AstNode getExpression() {
82 return expr;
83 }
84
85 /**
86 * Sets the wrapped expression, and sets its parent to this node.
87 * @throws IllegalArgumentException} if expression is {@code null}
88 */
89 public void setExpression(AstNode expression) {
90 assertNotNull(expression);
91 expr = expression;
92 expression.setParent(this);
93 setLineno(expression.getLineno());
94 }
95
96 /**
97 * Returns true if this node has side effects
98 * @throws IllegalStateException if expression has not yet
99 * been set.
100 */
101 @Override
102 public boolean hasSideEffects() {
103 return type == Token.EXPR_RESULT || expr.hasSideEffects();
104 }
105
106 @Override
107 public String toSource(int depth) {
108 StringBuilder sb = new StringBuilder();
109 sb.append(expr.toSource(depth));
110 sb.append(";\n");
111 return sb.toString();
112 }
113
114 /**
115 * Visits this node, then the wrapped statement.
116 */
117 @Override
118 public void visit(NodeVisitor v) {
119 if (v.visit(this)) {
120 expr.visit(v);
121 }
122 }
123 }