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  package org.codehaus.groovy.ast.expr;
17  
18  import org.codehaus.groovy.ast.GroovyCodeVisitor;
19  
20  /**
21   * Represents a range expression such as for iterating.
22   * E.g.:
23   * <pre>for i in 0..10 {...}</pre>
24   * 
25   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
26   * @version $Revision: 15194 $
27   */
28  public class RangeExpression extends Expression {
29  
30      private Expression from;
31      private Expression to;
32      private boolean inclusive;
33  
34      public RangeExpression(Expression from, Expression to, boolean inclusive) {
35          this.from = from;
36          this.to = to;
37          this.inclusive = inclusive;
38      }
39  
40      public void visit(GroovyCodeVisitor visitor) {
41          visitor.visitRangeExpression(this);
42      }
43  
44      public Expression transformExpression(ExpressionTransformer transformer) {
45          Expression ret = new RangeExpression(transformer.transform(from), transformer.transform(to), inclusive); 
46          ret.setSourcePosition(this);
47          return ret;
48      }
49  
50      public Expression getFrom() {
51          return from;
52      }
53  
54      public Expression getTo() {
55          return to;
56      }
57  
58      public boolean isInclusive() {
59          return inclusive;
60      }
61  
62      public String getText() {
63          return "(" + from.getText() +
64                 (!isInclusive()? "..<" : ".." ) +
65                 to.getText() + ")";
66      }
67  }