$ template make_nodes() $ $ // Generate the node/Node.java file $ output $basedir + '/node/Node.java' /* This file was generated by SableCC (http://www.sablecc.org/). */ package @package.node; import java.util.*; import @package.analysis.*; public abstract class Node implements Switchable, Cloneable { private Node parent; public abstract Object clone(); public Node parent() { return parent; } void parent(Node parent) { this.parent = parent; } abstract void removeChild(Node child); abstract void replaceChild(Node oldChild, Node newChild); public void replaceBy(Node node) { if(parent != null) { parent.replaceChild(this, node); } } protected String toString(Node node) { if(node != null) { return node.toString(); } return ""; } protected String toString(List list) { StringBuffer s = new StringBuffer(); for(Iterator i = list.iterator(); i.hasNext();) { s.append(i.next()); } return s.toString(); } protected Node cloneNode(Node node) { if(node != null) { return (Node) node.clone(); } return null; } protected List cloneList(List list) { List clone = new LinkedList(); for(Iterator i = list.iterator(); i.hasNext();) { clone.add(((Node) i.next()).clone()); } return clone; } } $ END OUTPUT $ // Generate the node/NodeCast.java file $ output $basedir + '/node/NodeCast.java' /* This file was generated by SableCC (http://www.sablecc.org/). */ package @package.node; public class NodeCast implements Cast { public final static NodeCast instance = new NodeCast(); private NodeCast() { } public Object cast(Object o) { return (Node) o; } public Object unCast(Object o) { return (Node)o; } } $ end output $ // Generate the node/Cast.java file $ output $basedir + '/node/Cast.java' /* This file was generated by SableCC (http://www.sablecc.org/). */ package @package.node; public interface Cast { Object cast(Object o); // assign ownership Object unCast(Object o); // release ownership } $ end output $ // Generate the node/NoCast.java file $ output $basedir + '/node/NoCast.java' /* This file was generated by SableCC (http://www.sablecc.org/). */ package @package.node; public class NoCast implements Cast { public final static NoCast instance = new NoCast(); private NoCast() { } public Object cast(Object o) { return o; } public Object unCast(Object o) { return o; } } $ end output $ // Generate the node/Switch.java file $ output $basedir + '/node/Switch.java' /* This file was generated by SableCC (http://www.sablecc.org/). */ package @package.node; public interface Switch { } $ end output $ // Generate the node/Switchable.java file $ output $basedir + '/node/Switchable.java' /* This file was generated by SableCC (http://www.sablecc.org/). */ package @package.node; public interface Switchable { void apply(Switch sw); } $ end output $ // Generate the node/TypedList.java file $ output $basedir + '/node/TypedList.java' /* This file was generated by SableCC (http://www.sablecc.org/). */ package @package.node; import java.util.*; public class TypedList implements List { Cast cast; List list = new LinkedList(); public TypedList() { this.cast = NoCast.instance; } public TypedList(Collection c) { this.cast = NoCast.instance; addAll(c); } public TypedList(Cast cast) { this.cast = cast; } public TypedList(Collection c, Cast cast) { this.cast = cast; addAll(c); } public void add(int index, Object element) { list.add(index, cast.cast(element)); } public boolean add(Object o) { return list.add(cast.cast(o)); } public boolean addAll(Collection c) { for ( Iterator it = c.iterator(); it.hasNext(); ) cast.cast(it.next()); return list.addAll(c); } public boolean addAll(int index, Collection c) { for ( Iterator it = c.iterator(); it.hasNext(); ) cast.cast(it.next()); return list.addAll(index, c); } public void clear() { for ( Iterator it = list.iterator(); it.hasNext(); ) cast.unCast(it.next()); list.clear(); } public boolean contains(Object o) { return list.contains(o); } public boolean containsAll(Collection c) { return list.containsAll(c); } public boolean equals(Object o) { return list.equals(o); } public Object get(int index) { return list.get(index); } public int hashCode() { return list.hashCode(); } public int indexOf(Object o) { return list.indexOf(o); } public boolean isEmpty() { return list.isEmpty(); } public int lastIndexOf(Object o) { return list.lastIndexOf(o); } public Object remove(int index) { return cast.unCast(list.remove(index)); } public boolean remove(Object o) { int n = list.indexOf(o); if ( n != -1 ) { remove(n); return true; } else { return false; } } public boolean removeAll(Collection c) { boolean changed = false; for ( Iterator it = c.iterator(); it.hasNext(); ) { Object o = it.next(); if ( remove(o) ) changed = true; } return changed; } public boolean retainAll(Collection c) { boolean changed = false; for ( Iterator it = iterator(); it.hasNext(); ) { Object o = it.next(); if ( !c.contains(o) ) { it.remove(); changed = true; } } return changed; } public Object set(int index, Object element) { return cast.unCast(list.set(index, cast.cast(element))); } public int size() { return list.size(); } public List subList(int fromIndex, int toIndex) { return list.subList(fromIndex, toIndex); } public Object[] toArray() { return list.toArray(); } public Object[] toArray(Object[] a) { return list.toArray(a); } public Iterator iterator() { return listIterator(); } public ListIterator listIterator() { return new TypedListIterator(list.listIterator()); } public ListIterator listIterator(int index) { return new TypedListIterator(list.listIterator(index)); } class TypedListIterator implements ListIterator { private ListIterator it; TypedListIterator (ListIterator it) { this.it = it; } public void add (Object o) { it.add(cast.cast(o)); } public boolean hasNext() { return it.hasNext(); } public boolean hasPrevious() { return it.hasPrevious(); } public Object next() { return it.next(); } public int nextIndex() { return it.nextIndex(); } public Object previous() { return it.previous(); } public int previousIndex() { return it.previousIndex(); } public void remove() { it.previous(); cast.unCast(it.next()); it.remove(); } public void set(Object o) { it.previous(); cast.unCast(it.next()); it.set(cast.cast(o)); } } } $ end output $ end template