001    /* This file was generated with JastAdd2 (http://jastadd.org) version 2.1.3 */
002    package AST;
003    
004    import java.util.*;
005    /**
006     * @ast node
007     * @production ASTNode;
008    
009     */
010    public class ASTNode<T extends ASTNode> extends beaver.Symbol  implements Cloneable, Iterable<T> {
011      /**
012       * @apilevel internal
013       */
014      public ASTNode<T> clone() throws CloneNotSupportedException {
015        ASTNode node = (ASTNode) super.clone();
016        return node;
017      }
018      /**
019       * @apilevel internal
020       */
021      public ASTNode<T> copy() {
022        try {
023          ASTNode node = (ASTNode) clone();
024          node.parent = null;
025          if(children != null) {
026            node.children = (ASTNode[]) children.clone();
027          }
028          return node;
029        } catch (CloneNotSupportedException e) {
030          throw new Error("Error: clone not supported for " + getClass().getName());
031        }
032      }
033      /**
034       * Create a deep copy of the AST subtree at this node.
035       * The copy is dangling, i.e. has no parent.
036       * @return dangling copy of the subtree at this node
037       * @apilevel low-level
038       */
039      public ASTNode<T> fullCopy() {
040        ASTNode tree = (ASTNode) copy();
041        if (children != null) {
042          for (int i = 0; i < children.length; ++i) {
043            ASTNode child = (ASTNode) children[i];
044            if(child != null) {
045              child = child.fullCopy();
046              tree.setChild(child, i);
047            }
048          }
049        }
050        return tree;
051      }
052      /**
053       * @aspect Exercises
054       * @declaredat /home/csz-naf/examples/StateMachine/spec/Exercises.jrag:109
055       */
056      Set<State> asSet(State o) {
057        HashSet<State> result = new HashSet<State>();
058        result.add(o);
059        return result;
060      }
061      /**
062       * @aspect Exercises
063       * @declaredat /home/csz-naf/examples/StateMachine/spec/Exercises.jrag:115
064       */
065      Set<State> union(Set<State> s1, Set<State> s2) {
066        HashSet<State> result = new HashSet<State>();
067        for (State s: s1) result.add(s);
068        for (State s: s2) result.add(s);
069        return result;
070      }
071      /**
072       */
073      public ASTNode() {
074        super();
075        init$Children();
076      }
077      /**
078       * Initializes the child array to the correct size.
079       * Initializes List and Opt nta children.
080       * @apilevel internal
081       * @ast method
082       */
083      public void init$Children() {
084      }
085      /**
086       * @apilevel internal
087       */
088      private int childIndex;
089      /**
090       * @apilevel low-level
091       */
092      public int getIndexOfChild(ASTNode node) {
093        if (node == null) {
094          return -1;
095        }
096        if (node.childIndex < numChildren && node == children[node.childIndex]) {
097          return node.childIndex;
098        }
099        for(int i = 0; children != null && i < children.length; i++) {
100          if(children[i] == node) {
101            node.childIndex = i;
102            return i;
103          }
104        }
105        return -1;
106      }
107      /**
108       * @apilevel internal
109       */
110      public static final boolean generatedWithCircularEnabled = true;
111      /**
112       * @apilevel internal
113       */
114      public static final boolean generatedWithCacheCycle = true;
115      /**
116       * @apilevel internal
117       */
118      public static final boolean generatedWithComponentCheck = false;
119      /**
120       * Parent pointer
121       * @apilevel low-level
122       */
123      protected ASTNode parent;
124      /**
125       * Child array
126       * @apilevel low-level
127       */
128      protected ASTNode[] children;
129      /**
130       * @apilevel internal
131       */
132      protected static ASTNode$State state = new ASTNode$State();
133      /**
134       * @apilevel internal
135       */
136      public final ASTNode$State state() {
137        return state;
138      }
139      /**
140       * @apilevel low-level
141       */
142      public T getChild(int i) {
143    
144        // No rewrites
145        ASTNode child = getChildNoTransform(i);
146        return (T) child;
147    
148      }
149      /**
150       * @apilevel low-level
151       */
152      public void addChild(T node) {
153        setChild(node, getNumChildNoTransform());
154      }
155      /**
156       * <p><em>This method does not invoke AST transformations.</em></p>
157       * @apilevel low-level
158       */
159      public final T getChildNoTransform(int i) {
160        if (children == null) {
161          return null;
162        }
163        T child = (T)children[i];
164        return child;
165      }
166      /**
167       * @apilevel low-level
168       */
169      protected int numChildren;
170      /**
171       * @apilevel low-level
172       */
173      protected int numChildren() {
174        return numChildren;
175      }
176      /**
177       * @apilevel low-level
178       */
179      public int getNumChild() {
180        return numChildren();
181      }
182      /**
183       * <p><em>This method does not invoke AST transformations.</em></p>
184       * @apilevel low-level
185       */
186      public final int getNumChildNoTransform() {
187        return numChildren();
188      }
189      /**
190       * @apilevel low-level
191       */
192      public void setChild(ASTNode node, int i) {
193        if(children == null) {
194          children = new ASTNode[(i+1>4 || !(this instanceof List))?i+1:4];
195        } else if (i >= children.length) {
196          ASTNode c[] = new ASTNode[i << 1];
197          System.arraycopy(children, 0, c, 0, children.length);
198          children = c;
199        }
200        children[i] = node;
201        if(i >= numChildren) {
202          numChildren = i+1;
203        }
204        if(node != null) {
205          node.setParent(this);
206          node.childIndex = i;
207        }
208      }
209      /**
210       * @apilevel low-level
211       */
212      public void insertChild(ASTNode node, int i) {
213        if(children == null) {
214          children = new ASTNode[(i+1>4 || !(this instanceof List))?i+1:4];
215          children[i] = node;
216        } else {
217          ASTNode c[] = new ASTNode[children.length + 1];
218          System.arraycopy(children, 0, c, 0, i);
219          c[i] = node;
220          if(i < children.length) {
221            System.arraycopy(children, i, c, i+1, children.length-i);
222            for(int j = i+1; j < c.length; ++j) {
223              if(c[j] != null) {
224                c[j].childIndex = j;
225              }
226            }
227          }
228          children = c;
229        }
230        numChildren++;
231        if(node != null) {
232          node.setParent(this);
233          node.childIndex = i;
234        }
235      }
236      /**
237       * @apilevel low-level
238       */
239      public void removeChild(int i) {
240        if(children != null) {
241          ASTNode child = (ASTNode) children[i];
242          if(child != null) {
243            child.parent = null;
244            child.childIndex = -1;
245          }
246          // Adding a check of this instance to make sure its a List, a move of children doesn't make
247          // any sense for a node unless its a list. Also, there is a problem if a child of a non-List node is removed
248          // and siblings are moved one step to the right, with null at the end.
249          if (this instanceof List || this instanceof Opt) {
250            System.arraycopy(children, i+1, children, i, children.length-i-1);
251            children[children.length-1] = null;
252            numChildren--;
253            // fix child indices
254            for(int j = i; j < numChildren; ++j) {
255              if(children[j] != null) {
256                child = (ASTNode) children[j];
257                child.childIndex = j;
258              }
259            }
260          } else {
261            children[i] = null;
262          }
263        }
264      }
265      /**
266       * @apilevel low-level
267       */
268      public ASTNode getParent() {
269        ;
270        return (ASTNode) parent;
271      }
272      /**
273       * @apilevel low-level
274       */
275      public void setParent(ASTNode node) {
276        parent = node;
277      }
278      /**
279       * Line and column information.
280       */
281      protected int startLine;
282      /**
283       */
284      protected short startColumn;
285      /**
286       */
287      protected int endLine;
288      /**
289       */
290      protected short endColumn;
291      /**
292       */
293      public int getStartLine() {
294        return startLine;
295      }
296      /**
297       */
298      public short getStartColumn() {
299        return startColumn;
300      }
301      /**
302       */
303      public int getEndLine() {
304        return endLine;
305      }
306      /**
307       */
308      public short getEndColumn() {
309        return endColumn;
310      }
311      /**
312       */
313      public void setStart(int startLine, short startColumn) {
314        this.startLine = startLine;
315        this.startColumn = startColumn;
316      }
317      /**
318       */
319      public void setEnd(int endLine, short endColumn) {
320        this.endLine = endLine;
321        this.endColumn = endColumn;
322      }
323      /**
324       * @apilevel low-level
325       */
326      public java.util.Iterator<T> iterator() {
327        return new java.util.Iterator<T>() {
328          private int counter = 0;
329          public boolean hasNext() {
330            return counter < getNumChild();
331          }
332          public T next() {
333            if(hasNext())
334              return (T)getChild(counter++);
335            else
336              return null;
337          }
338          public void remove() {
339            throw new UnsupportedOperationException();
340          }
341        };
342      }
343      /**
344       * @apilevel low-level
345       */
346      public void flushCache() {
347      }
348      /**
349       * @apilevel internal
350       */
351      public void flushCollectionCache() {
352      }
353      /**
354       * @aspect <NoAspect>
355       * @declaredat /home/csz-naf/examples/StateMachine/spec/Exercises.jrag:66
356       */
357        protected void collect_contributors_StateMachine_numberOfTransitionsColl() {
358        for(int i = 0; i < getNumChild(); i++) {
359          getChild(i).collect_contributors_StateMachine_numberOfTransitionsColl();
360        }
361      }
362      protected void contributeTo_StateMachine_StateMachine_numberOfTransitionsColl(Counter collection) {
363      }
364    
365      /**
366       * @aspect <NoAspect>
367       * @declaredat /home/csz-naf/examples/StateMachine/spec/Exercises.jrag:83
368       */
369        protected void collect_contributors_StateMachine_errors() {
370        for(int i = 0; i < getNumChild(); i++) {
371          getChild(i).collect_contributors_StateMachine_errors();
372        }
373      }
374      protected void contributeTo_StateMachine_StateMachine_errors(Set<String> collection) {
375      }
376    
377      /**
378       * @aspect <NoAspect>
379       * @declaredat /home/csz-naf/examples/StateMachine/spec/Exercises.jrag:48
380       */
381        protected void collect_contributors_State_altSuccessors() {
382        for(int i = 0; i < getNumChild(); i++) {
383          getChild(i).collect_contributors_State_altSuccessors();
384        }
385      }
386      protected void contributeTo_State_State_altSuccessors(Set<State> collection) {
387      }
388    
389      /**
390       * @aspect <NoAspect>
391       * @declaredat /home/csz-naf/examples/StateMachine/spec/Exercises.jrag:57
392       */
393        protected void collect_contributors_State_predecessors() {
394        for(int i = 0; i < getNumChild(); i++) {
395          getChild(i).collect_contributors_State_predecessors();
396        }
397      }
398      protected void contributeTo_State_State_predecessors(Set<State> collection) {
399      }
400    
401      /**
402       * @aspect <NoAspect>
403       * @declaredat /home/csz-naf/examples/StateMachine/spec/Exercises.jrag:102
404       */
405        protected void collect_contributors_State_altReachable() {
406        for(int i = 0; i < getNumChild(); i++) {
407          getChild(i).collect_contributors_State_altReachable();
408        }
409      }
410      protected void contributeTo_State_State_altReachable(Set<State> collection) {
411      }
412    
413      /**
414       * @aspect <NoAspect>
415       * @declaredat /home/csz-naf/examples/StateMachine/spec/Graph.jrag:6
416       */
417        protected void collect_contributors_State_transitions() {
418        for(int i = 0; i < getNumChild(); i++) {
419          getChild(i).collect_contributors_State_transitions();
420        }
421      }
422      protected void contributeTo_State_State_transitions(Set<Transition> collection) {
423      }
424    
425      /**
426       * @apilevel internal
427       */
428      public State Define_State_lookupForward(ASTNode caller, ASTNode child, String label) {
429        return getParent().Define_State_lookupForward(this, caller, label);
430      }
431      /**
432       * @apilevel internal
433       */
434      public Set<Transition> Define_Set_Transition__transitionsOf(ASTNode caller, ASTNode child, State s) {
435        return getParent().Define_Set_Transition__transitionsOf(this, caller, s);
436      }
437      /**
438       * @apilevel internal
439       */
440      public StateMachine Define_StateMachine_theMachine(ASTNode caller, ASTNode child) {
441        return getParent().Define_StateMachine_theMachine(this, caller);
442      }
443      /**
444       * @apilevel internal
445       */
446      public State Define_State_lookup(ASTNode caller, ASTNode child, String label) {
447        return getParent().Define_State_lookup(this, caller, label);
448      }
449    }