KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > generator > core > util > TreeNormalizer


1 package com.genimen.djeneric.tools.generator.core.util;
2
3 import com.genimen.djeneric.tools.generator.core.ParseException;
4 import com.genimen.djeneric.tools.generator.core.SimpleNode;
5 import com.genimen.djeneric.tools.generator.core.nodes.AndOrNode;
6 import com.genimen.djeneric.tools.generator.core.nodes.CalculatedExpressionNode;
7 import com.genimen.djeneric.tools.generator.core.nodes.ElseNode;
8 import com.genimen.djeneric.tools.generator.core.nodes.ExpressionNode;
9 import com.genimen.djeneric.tools.generator.core.nodes.IfNode;
10 import com.genimen.djeneric.tools.generator.core.nodes.OperatorNode;
11 import com.genimen.djeneric.tools.generator.core.nodes.SubExpressionNode;
12 import com.genimen.djeneric.tools.generator.core.nodes.UnstructuredNode;
13
14 public class TreeNormalizer
15 {
16
17   public TreeNormalizer()
18   {
19   }
20
21   public void normalize(SimpleNode root) throws ParseException
22   {
23     // The order of the following methods is NOT trivial
24
normalizeOperands(root);
25     pruneCalculatedExpressions(root);
26     normalizeAndOr(root);
27     pruneUnary(root);
28     pruneUnstructured(root);
29     checkIfElse(root);
30   }
31
32   private void pushDown(CalculatedExpressionNode expr, String JavaDoc operator)
33   {
34     int i = 1;
35
36     while (i < expr.getChildCount() - 1)
37     {
38       int increment = 1;
39       SimpleNode child = expr.getChild(i);
40       if (child instanceof OperatorNode)
41       {
42         OperatorNode opNode = (OperatorNode) child;
43         if ((operator.equals("+-") && (opNode.getOperator().equals("+") || opNode.getOperator().equals("-")))
44             || (opNode.getOperator().equals(operator)))
45         {
46           moveLeftRightDown(expr, i, opNode);
47           // We shuffled the children; the 'next' is now the current because
48
// the left node is removed from the children list.
49
increment = 0;
50         }
51       }
52       i += increment;
53       // proceed with next node
54
}
55   }
56
57   private void pushDown(ExpressionNode expr, String JavaDoc andor)
58   {
59     int i = 1;
60
61     while (i < expr.getChildCount() - 1)
62     {
63       int increment = 1;
64       SimpleNode child = expr.getChild(i);
65       if (child instanceof AndOrNode)
66       {
67         AndOrNode aoNode = (AndOrNode) child;
68         if (aoNode.getOperator().equals(andor))
69         {
70           moveLeftRightDown(expr, i, aoNode);
71           // We shuffled the children; the 'next' is now the current because
72
// the left node is removed from the children list.
73
increment = 0;
74         }
75       }
76       i += increment;
77       // proceed with next node
78
}
79   }
80
81   private void moveLeftRightDown(SimpleNode expr, int i, SimpleNode opNode)
82   {
83     SimpleNode left = expr.getChild(i - 1);
84     SimpleNode right = expr.getChild(i + 1);
85
86     expr.removeChild(left);
87     expr.removeChild(right);
88
89     left.setParent(opNode);
90     right.setParent(opNode);
91
92     opNode.addChild(left);
93     opNode.addChild(right);
94   }
95
96   private void normalizeOperands(SimpleNode root)
97   {
98     for (int i = 0; i < root.getChildCount(); i++)
99     {
100       normalizeOperands(root.getChild(i));
101     }
102
103     if (root instanceof CalculatedExpressionNode)
104     {
105       pushDown((CalculatedExpressionNode) root, ".");
106       pushDown((CalculatedExpressionNode) root, "/");
107       pushDown((CalculatedExpressionNode) root, "*");
108       pushDown((CalculatedExpressionNode) root, "%");
109       pushDown((CalculatedExpressionNode) root, "+-");
110     }
111   }
112
113   private void normalizeAndOr(SimpleNode root)
114   {
115     for (int i = 0; i < root.getChildCount(); i++)
116     {
117       normalizeAndOr(root.getChild(i));
118     }
119
120     if (root instanceof ExpressionNode)
121     {
122       pushDown((ExpressionNode) root, "&&");
123       pushDown((ExpressionNode) root, "||");
124     }
125   }
126
127   private void checkIfElse(SimpleNode root) throws ParseException
128   {
129     for (int j = 0; j < root.getChildCount(); j++)
130     {
131       checkIfElse(root.getChild(j));
132     }
133
134     int i = 0;
135     while (i < root.getChildCount())
136     {
137       int increment = 1;
138       if (root.getChild(i) instanceof ElseNode)
139       {
140         if (i == 0 || !(root.getChild(i - 1) instanceof IfNode))
141         {
142           throw new ParseException("Else without if encountered at line " + root.getChild(i).beginLine + " column "
143                                    + root.getChild(i).beginColumn, root.getChild(i).beginLine,
144               root.getChild(i).beginColumn);
145         }
146         root.getChild(i - 1).addChild(root.getChild(i).getChild(0));
147         root.getChild(i).setParent(root.getChild(i - 1));
148         root.removeChild(i);
149         increment = 0;
150       }
151       i += increment;
152     }
153
154   }
155
156   private boolean pruneCalculatedExpressions(SimpleNode root) throws ParseException
157   {
158     boolean doneOne = false;
159
160     int i = 0;
161     while (i < root.getChildCount())
162     {
163       if (!pruneCalculatedExpressions(root.getChild(i))) i++;
164     }
165
166     if (root instanceof CalculatedExpressionNode)
167     {
168       SimpleNode parent = root.getParent();
169       if (root.getChildCount() != 1)
170       {
171         throw new ParseException("Calculated expression with childcount != 1 found", parent.beginLine,
172             parent.beginColumn);
173       }
174       parent.replaceChild(root, root.getChild(0));
175       doneOne = true;
176     }
177     return doneOne;
178   }
179
180   private void pruneUnary(SimpleNode root) throws ParseException
181   {
182     int i = 0;
183     while (i < root.getChildCount())
184     {
185       int increment = 1;
186       if (root.getChild(i) instanceof ExpressionNode)
187       {
188         ExpressionNode exprNode = (ExpressionNode) root.getChild(i);
189         if (exprNode.getChildCount() != 1) throw new ParseException("Expression (" + exprNode.toString()
190                                                                     + ") with more than 1 child found",
191             exprNode.beginLine, exprNode.beginColumn);
192
193         SimpleNode child = exprNode.getChild(0);
194         root.replaceChild(i, child);
195         child.setParent(root);
196         increment = 0;
197       }
198       if (root.getChild(i) instanceof SubExpressionNode)
199       {
200         SubExpressionNode exprNode = (SubExpressionNode) root.getChild(i);
201         if (exprNode.isUnary())
202         {
203           SimpleNode child = exprNode.getChild(0);
204           root.replaceChild(i, child);
205           child.setParent(root);
206           increment = 0;
207         }
208       }
209       i += increment;
210     }
211
212     for (int j = 0; j < root.getChildCount(); j++)
213     {
214       pruneUnary(root.getChild(j));
215     }
216   }
217
218   private void pruneUnstructured(SimpleNode root) throws ParseException
219   {
220     int i = 0;
221     while (i < root.getChildCount())
222     {
223       int increment = 1;
224       if (root.getChild(i) instanceof UnstructuredNode)
225       {
226         UnstructuredNode un = (UnstructuredNode) root.getChild(i);
227         boolean kill = false;
228
229         if (un.getData().length() == 0) kill = true;
230         else if ((i > 0) && (i < root.getChildCount() - 1) && (root.getChild(i - 1) instanceof IfNode)
231                  && (root.getChild(i + 1) instanceof ElseNode) && (un.getData().trim().length() == 0))
232         {
233           kill = true;
234         }
235
236         if (kill)
237         {
238           root.removeChild(un);
239           increment = 0;
240         }
241       }
242       i += increment;
243     }
244
245     for (int j = 0; j < root.getChildCount(); j++)
246     {
247       pruneUnstructured(root.getChild(j));
248     }
249   }
250 }
Popular Tags