- /** append Java code to <tt>sb</tt> which evaluates to this instance */
- public void toJava(StringBuffer sb) {
- sb.append("new Tree(null, ");
- sb.append(head==null ? "null" : "\"" + StringUtil.toJavaString(head+"") + "\"");
- sb.append(", new Tree[] { ");
- for(int i=0; i<children.length; i++) {
- if (children[i]==null) sb.append("null");
- else children[i].toJava(sb);
- if (i<children.length-1) sb.append(",\n ");
+ /** get the input region that this tree was parsed from */
+ public Input.Region getRegion() { return location; }
+
+ public Tree(Input.Region loc, NodeType head) { this(loc, head, null); }
+ public Tree(Input.Region loc, NodeType head, Tree<NodeType>[] children) { this(loc, head, children, false); }
+
+ // FIXME: fairly inefficient because we keep copying arrays
+ /** package-private constructor, allows setting the "lift" bit */
+ Tree(Input.Region loc, NodeType head, Tree<NodeType>[] children, boolean lift) {
+ this.location = loc;
+ this.ihead = head;
+ if (lift && children != null && children.length > 0) {
+ Tree<NodeType> last = children[children.length-1];
+ this.children = new Tree[(children.length-1)+last.children.length];
+ System.arraycopy(children, 0, this.children, 0, children.length-1);
+ if (last.children.length > 0)
+ System.arraycopy(last.children, 0, this.children, children.length-1, last.children.length);
+ } else {
+ this.children = ArrayUtil.clone(children, Tree.class);