1 4 package com.genimen.djeneric.tools.generator.core; 5 6 import com.genimen.djeneric.tools.generator.core.nodes.AndOrNode; 7 import com.genimen.djeneric.tools.generator.core.nodes.BodyNode; 8 import com.genimen.djeneric.tools.generator.core.nodes.BoolNode; 9 import com.genimen.djeneric.tools.generator.core.nodes.CalculatedExpressionNode; 10 import com.genimen.djeneric.tools.generator.core.nodes.CharNode; 11 import com.genimen.djeneric.tools.generator.core.nodes.CommentNode; 12 import com.genimen.djeneric.tools.generator.core.nodes.ConstNode; 13 import com.genimen.djeneric.tools.generator.core.nodes.ElseNode; 14 import com.genimen.djeneric.tools.generator.core.nodes.ExpressionNode; 15 import com.genimen.djeneric.tools.generator.core.nodes.FilterNode; 16 import com.genimen.djeneric.tools.generator.core.nodes.FloatNode; 17 import com.genimen.djeneric.tools.generator.core.nodes.ForNode; 18 import com.genimen.djeneric.tools.generator.core.nodes.IfNode; 19 import com.genimen.djeneric.tools.generator.core.nodes.IndentNode; 20 import com.genimen.djeneric.tools.generator.core.nodes.IntegerNode; 21 import com.genimen.djeneric.tools.generator.core.nodes.ModuleNode; 22 import com.genimen.djeneric.tools.generator.core.nodes.NullNode; 23 import com.genimen.djeneric.tools.generator.core.nodes.OperatorNode; 24 import com.genimen.djeneric.tools.generator.core.nodes.OrderByNode; 25 import com.genimen.djeneric.tools.generator.core.nodes.PropertyNode; 26 import com.genimen.djeneric.tools.generator.core.nodes.PropertyOrFunctionNode; 27 import com.genimen.djeneric.tools.generator.core.nodes.SetNode; 28 import com.genimen.djeneric.tools.generator.core.nodes.StringNode; 29 import com.genimen.djeneric.tools.generator.core.nodes.SubExpressionNode; 30 import com.genimen.djeneric.tools.generator.core.nodes.TemplateNode; 31 import com.genimen.djeneric.tools.generator.core.nodes.UnstructuredNode; 32 import com.genimen.djeneric.tools.generator.core.util.ParseContext; 33 import com.genimen.djeneric.util.DjLogger; 34 35 public class SimpleNode implements Node 36 { 37 protected Node parent; 38 protected Node[] children; 39 protected int id; 40 protected DjentelParserEngine parser; 41 public int beginLine = 0, beginColumn = 0; 42 43 protected String name = null; 44 45 public SimpleNode(int i) 46 { 47 id = i; 48 } 49 50 public SimpleNode(DjentelParserEngine p, int i) 51 { 52 this(i); 53 parser = p; 54 } 55 56 public String getName() 57 { 58 return "SimpleNode"; 59 } 60 61 public void setLineInfo(Token token) 62 { 63 beginLine = token.beginLine; 64 beginColumn = token.beginColumn; 65 } 66 67 public void setLineInfo(SimpleNode node) 68 { 69 beginLine = node.beginLine; 70 beginColumn = node.beginColumn; 71 } 72 73 public void setLineInfo(int line, int column) 74 { 75 beginLine = line; 76 beginColumn = column; 77 } 78 79 public static Node jjtCreate(int id) 80 { 81 SimpleNode newNode; 82 83 if (id == DjentelParserEngineTreeConstants.JJTFETCHUNSTRUCTURED) newNode = new UnstructuredNode(id); 84 else if (id == DjentelParserEngineTreeConstants.JJTFORSTATEMENT) newNode = new ForNode(id); 85 else if (id == DjentelParserEngineTreeConstants.JJTSETSTATEMENT) newNode = new SetNode(id); 86 else if (id == DjentelParserEngineTreeConstants.JJTIFSTATEMENT) newNode = new IfNode(id); 87 else if (id == DjentelParserEngineTreeConstants.JJTBODY) newNode = new BodyNode(id); 88 else if (id == DjentelParserEngineTreeConstants.JJTELSESTATEMENT) newNode = new ElseNode(id); 89 else if (id == DjentelParserEngineTreeConstants.JJTEXPRESSION) newNode = new ExpressionNode(id); 90 else if (id == DjentelParserEngineTreeConstants.JJTSUBEXPRESSION) newNode = new SubExpressionNode(id); 91 else if (id == DjentelParserEngineTreeConstants.JJTANDOR) newNode = new AndOrNode(id); 92 else if (id == DjentelParserEngineTreeConstants.JJTCALCULATEDEXPRESSION) newNode = new CalculatedExpressionNode(id); 93 else if (id == DjentelParserEngineTreeConstants.JJTOPERATOR) newNode = new OperatorNode(id); 94 else if (id == DjentelParserEngineTreeConstants.JJTOBJECTPATH) newNode = new PropertyNode(id); 95 else if (id == DjentelParserEngineTreeConstants.JJTPROPERTYORFUNCTION) newNode = new PropertyOrFunctionNode(id); 96 else if (id == DjentelParserEngineTreeConstants.JJTBOOLLITERAL) newNode = new BoolNode(id); 97 else if (id == DjentelParserEngineTreeConstants.JJTCHARLITERAL) newNode = new CharNode(id); 98 else if (id == DjentelParserEngineTreeConstants.JJTFLOATLITERAL) newNode = new FloatNode(id); 99 else if (id == DjentelParserEngineTreeConstants.JJTINTEGERLITERAL) newNode = new IntegerNode(id); 100 else if (id == DjentelParserEngineTreeConstants.JJTNULLLITERAL) newNode = new NullNode(id); 101 else if (id == DjentelParserEngineTreeConstants.JJTSTRINGLITERAL) newNode = new StringNode(id); 102 else if (id == DjentelParserEngineTreeConstants.JJTMODULEDEFINITION) newNode = new ModuleNode(id); 103 else if (id == DjentelParserEngineTreeConstants.JJTBUILDTREE) newNode = new TemplateNode(id); 104 else if (id == DjentelParserEngineTreeConstants.JJTCONST) newNode = new ConstNode(id); 105 else if (id == DjentelParserEngineTreeConstants.JJTCOMMENTSTATEMENT) newNode = new CommentNode(id); 106 else if (id == DjentelParserEngineTreeConstants.JJTINDENT) newNode = new IndentNode(id); 107 else if (id == DjentelParserEngineTreeConstants.JJTFILTER) newNode = new FilterNode(id); 108 else if (id == DjentelParserEngineTreeConstants.JJTORDERBY) newNode = new OrderByNode(id); 109 else newNode = new SimpleNode(id); 110 111 return newNode; 112 } 113 114 public static Node jjtCreate(DjentelParserEngine p, int id) 115 { 116 return new SimpleNode(p, id); 117 } 118 119 public void jjtOpen() 120 { 121 } 122 123 public void jjtClose() 124 { 125 } 126 127 public void setParent(SimpleNode n) 128 { 129 parent = n; 130 } 131 132 public void jjtSetParent(Node n) 133 { 134 parent = n; 135 } 136 137 public SimpleNode getParent() 138 { 139 return (SimpleNode) parent; 140 } 141 142 public Node jjtGetParent() 143 { 144 return parent; 145 } 146 147 public void addChild(SimpleNode n) 148 { 149 jjtAddChild(n); 150 n.setParent(this); 151 } 152 153 public void addChild(SimpleNode n, int i) 154 { 155 jjtAddChild(n, i); 156 n.setParent(this); 157 } 158 159 public void jjtAddChild(Node n) 160 { 161 int idx = 0; 162 if (children != null) idx = children.length; 163 jjtAddChild(n, idx); 164 } 165 166 public void jjtAddChild(Node n, int i) 167 { 168 if (children == null) 169 { 170 children = new Node[i + 1]; 171 } 172 else if (i >= children.length) 173 { 174 Node c[] = new Node[i + 1]; 175 System.arraycopy(children, 0, c, 0, children.length); 176 children = c; 177 } 178 children[i] = n; 179 } 180 181 public void replaceChild(int i, SimpleNode n) 182 { 183 children[i] = n; 184 n.setParent(this); 185 } 186 187 public void replaceChild(SimpleNode node, SimpleNode newNode) 188 { 189 children[indexOf(node)] = newNode; 190 newNode.setParent(this); 191 } 192 193 public int indexOf(SimpleNode node) 194 { 195 for (int i = 0; i < children.length; i++) 196 { 197 if (children[i] == node) return i; 198 } 199 return -1; 200 } 201 202 public SimpleNode getChild(int i) 203 { 204 return (SimpleNode) children[i]; 205 } 206 207 public Node jjtGetChild(int i) 208 { 209 return children[i]; 210 } 211 212 public void removeChild(int at) 213 { 214 jjtRemoveChild(at); 215 } 216 217 public void jjtRemoveChild(int at) 218 { 219 Node c[] = new Node[children.length - 1]; 220 int idx = 0; 221 for (int i = 0; i < children.length; i++) 222 { 223 if (i != at) c[idx++] = children[i]; 224 } 225 children = c; 226 } 227 228 public void removeChild(Node node) 229 { 230 jjtRemoveChild(node); 231 } 232 233 public void jjtRemoveChild(Node node) 234 { 235 int at = -1; 236 for (int i = 0; i < children.length; i++) 237 { 238 if (children[i] == node) 239 { 240 at = i; 241 break; 242 } 243 } 244 if (at == -1) return; 245 jjtRemoveChild(at); 246 } 247 248 public int getChildCount() 249 { 250 return jjtGetNumChildren(); 251 } 252 253 public int jjtGetNumChildren() 254 { 255 return (children == null) ? 0 : children.length; 256 } 257 258 265 public String toString() 266 { 267 if (name != null) return name; 268 return DjentelParserEngineTreeConstants.jjtNodeName[id]; 269 } 270 271 public void setName(String nm) 272 { 273 name = nm; 274 } 275 276 public String toString(String prefix) 277 { 278 return prefix + toString(); 279 } 280 281 285 public void dump(String prefix) 286 { 287 DjLogger.log(toString(prefix)); 288 if (children != null) 289 { 290 for (int i = 0; i < children.length; ++i) 291 { 292 SimpleNode n = (SimpleNode) children[i]; 293 if (n != null) 294 { 295 n.dump(prefix + " "); 296 } 297 } 298 } 299 } 300 301 public String evaluate(ParseContext context) throws ParseException 302 { 303 throw new ParseException("Can not evaluate a " + getClass().getName(), beginLine, beginColumn); 304 } 305 306 public Float toFloat(Object o) 307 { 308 if (o instanceof Float ) return (Float ) o; 309 return new Float (o.toString()); 310 } 311 312 public Integer toInteger(Object o) 313 { 314 if (o instanceof Integer ) return (Integer ) o; 315 if (o instanceof Float ) return new Integer (((Float ) o).intValue()); 316 return new Integer (o.toString()); 317 } 318 319 public String classOnly(Object o) 320 { 321 if (o == null) return "null"; 322 String full = o.getClass().getName(); 323 int idx = full.lastIndexOf("."); 324 if (idx == -1) return full; 325 return full.substring(idx + 1); 326 } 327 328 public int getColumn() 329 { 330 return beginColumn; 331 } 332 333 public int getLine() 334 { 335 return beginLine; 336 } 337 338 } 339 | Popular Tags |