View Javadoc
Minimize
Table

1   /*
2    * Copyright 2003-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.codehaus.groovy.ast.expr;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.codehaus.groovy.ast.GroovyCodeVisitor;
23  import org.codehaus.groovy.ast.VariableScope;
24  
25  /**
26   * This class represents a list of expressions used to
27   * create closures. Example:
28   * <code>
29   * def foo = (1;2;;)
30   * </code>
31   * The right side is a ClosureListExpression consisting of
32   * two ConstantExpressions for the values 1 and 2, and two
33   * EmptyStatement entries. The ClosureListExpression defines a new 
34   * variable scope. All created Closures share this scope.
35   * 
36   * 
37   * @author Jochen Theodorou
38   */
39  public class ClosureListExpression extends ListExpression {
40  
41      private VariableScope scope;
42      
43      public ClosureListExpression(List<Expression> expressions) {
44          super(expressions);
45          scope = new VariableScope();
46      }
47      
48      public ClosureListExpression() {
49          this(new ArrayList<Expression>(3));
50      }
51      
52      public void visit(GroovyCodeVisitor visitor) {
53          visitor.visitClosureListExpression(this);
54      }
55      
56      public Expression transformExpression(ExpressionTransformer transformer) {
57          Expression ret = new ClosureListExpression(transformExpressions(getExpressions(), transformer));
58          ret.setSourcePosition(this);
59          return ret;       
60      }
61      
62      public void setVariableScope(VariableScope scope) {
63          this.scope = scope;
64      }
65      
66      public VariableScope getVariableScope() {
67          return scope;
68      }
69      
70      public String getText() {
71          StringBuffer buffer = new StringBuffer("(");
72          boolean first = true;
73          for (Expression expression : getExpressions()) {
74              if (first) {
75                  first = false;
76              } else {
77                  buffer.append("; ");
78              }
79              
80              buffer.append(expression.getText());
81          }
82          buffer.append(")");
83          return buffer.toString();
84      }
85  }