View Javadoc

1   /*
2    * ASTASForStatement.java
3    * 
4    * Copyright (c) 2006 David Holroyd
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package uk.co.badgersinfoil.metaas.impl;
20  
21  import org.asdt.core.internal.antlr.AS3Parser;
22  import uk.co.badgersinfoil.metaas.dom.Expression;
23  import uk.co.badgersinfoil.metaas.dom.ASForStatement;
24  import uk.co.badgersinfoil.metaas.dom.ScriptElement;
25  import uk.co.badgersinfoil.metaas.dom.StatementContainer;
26  import uk.co.badgersinfoil.metaas.impl.antlr.LinkedListTree;
27  
28  
29  public class ASTASForStatement extends ContainerDelegate implements ASForStatement {
30  
31  	private static final int INDEX_INIT = 0;
32  	private static final int INDEX_CONDITION = 1;
33  	private static final int INDEX_UPDATE = 2;
34  	private static final int INDEX_STATEMENT = 3;
35  
36  	public ASTASForStatement(LinkedListTree ast) {
37  		super(ast);
38  	}
39  
40  	public String getInitString() {
41  		if (!hasInit()) {
42  			return null;
43  		}
44  		return ASTUtils.stringifyNode(findInit());
45  	}
46  
47  	public ScriptElement getInit() {
48  		if (!hasInit()) {
49  			return null;
50  		}
51  		LinkedListTree init = findInit().getFirstChild();
52  		switch (init.getType()) {
53  		    case AS3Parser.VAR:
54  		    case AS3Parser.CONST:
55  			return new ASTASDeclarationStatement(init);
56  		    default:
57  			return ExpressionBuilder.build(init);
58  		}
59  	}
60  
61  	public String getConditionString() {
62  		if (!hasCondition()) {
63  			return null;
64  		}
65  		return ASTUtils.stringifyNode(findCondition().getFirstChild());
66  	}
67  
68  	public Expression getCondition() {
69  		if (!hasCondition()) {
70  			return null;
71  		}
72  		return ExpressionBuilder.build(findCondition().getFirstChild());
73  	}
74  
75  	public String getUpdateString() {
76  		if (!hasUpdate()) {
77  			return null;
78  		}
79  		return ASTUtils.stringifyNode(findUpdate().getFirstChild());
80  	}
81  
82  	public Expression getUpdate() {
83  		if (!hasUpdate()) {
84  			return null;
85  		}
86  		return ExpressionBuilder.build(findUpdate().getFirstChild());
87  	}
88  
89  	private boolean hasCondition() {
90  		return findCondition().getChildCount() > 0;
91  	}
92  
93  	private boolean hasInit() {
94  		return findInit().getChildCount() > 0;
95  	}
96  
97  	private boolean hasUpdate() {
98  		return findUpdate().getChildCount() > 0;
99  	}
100 
101 	private LinkedListTree getChild(int index) {
102 		return (LinkedListTree)ast.getChild(index);
103 	}
104 
105 	protected StatementContainer getStatementContainer() {
106 		return new ASTStatementList(getChild(INDEX_STATEMENT));
107 	}
108 
109 	private LinkedListTree findInit() {
110 		return getChild(INDEX_INIT);
111 	}
112 
113 	private LinkedListTree findCondition() {
114 		return getChild(INDEX_CONDITION);
115 	}
116 
117 	private LinkedListTree findUpdate() {
118 		return getChild(INDEX_UPDATE);
119 	}
120 
121 	public void setCondition(String expr) {
122 		if (expr == null) {
123 			deleteAnyChild(findCondition());
124 		} else {
125 			LinkedListTree cond = AS3FragmentParser.parseForCond(expr);
126 			ast.setChildWithTokens(INDEX_CONDITION, cond);
127 		}
128 	}
129 
130 	public void setCondition(Expression expr) {
131 		LinkedListTree cond = findCondition();
132 		if (expr == null) {
133 			deleteAnyChild(cond);
134 		} else {
135 			setFirstChild(cond, ast(expr));
136 		}
137 	}
138 
139 	public void setInit(String expr) {
140 		if (expr == null) {
141 			deleteAnyChild(findInit());
142 		} else {
143 			LinkedListTree init = AS3FragmentParser.parseForInit(expr);
144 			ast.setChildWithTokens(INDEX_INIT, init);
145 		}
146 	}
147 
148 	public void setUpdate(String expr) {
149 		if (expr == null) {
150 			deleteAnyChild(findUpdate());
151 		} else {
152 			LinkedListTree update = AS3FragmentParser.parseForIter(expr);
153 			ast.setChildWithTokens(INDEX_UPDATE, update);
154 		}
155 	}
156 
157 	public void setUpdate(Expression expr) {
158 		if (expr == null) {
159 			deleteAnyChild(findUpdate());
160 		} else {
161 			setFirstChild(findUpdate(), ast(expr));
162 		}
163 	}
164 
165 	private void deleteAnyChild(LinkedListTree tree) {
166 		if (tree.getChildCount() > 0) {
167 			tree.deleteChild(0);
168 		}
169 	}
170 
171 	private static void setFirstChild(LinkedListTree parent, LinkedListTree child) {
172 		if (parent.getChildCount() == 0) {
173 			parent.addChildWithTokens(child);
174 		} else {
175 			parent.setChildWithTokens(0, child);
176 		}
177 	}
178 }