1  $ template make_prods()
  2  $
  3  $ // the first production name that goes into Start
  4  $ param baseprod = {//production/@ename}
  5
  6  $ // generate all production files
  7  $ foreach {//production}
  8  $   output $basedir + '/node/' + @ename + '.java'
  9  /* This file was generated by SableCC (http://www.sablecc.org/). */
 10
 11  package $package.node;
 12
 13  public abstract class @ename extends Node
 14  {
 15  }
 16  $   end
 17  $ end
 18
 19  $ // generate all alternative files
 20  $ foreach {//alt}
 21  $   output $basedir + '/node/' + @ename + '.java'
 22  /* This file was generated by SableCC (http://www.sablecc.org/). */
 23
 24  package $package.node;
 25
 26  import java.util.*;
 27  import $package.analysis.*;
 28
 29  public final class @ename extends ${../@ename}
 30  {
 31  $ // define subnodes
 32  $ foreach {elem}
 33  $   if @is_list
 34      private final List _@{name}_ = new TypedList(new @{ename}_Cast());
 35  $   else
 36      private @etype _@{name}_;
 37  $   end
 38  $ end
 39
 40  $ // define default empty constructor
 41  $ if {elem}
 42      public @ename ()
 43      {
 44      }
 45  $ end
 46
 47  $ // define constructor
 48      public @ename (
 49  $ foreach {elem}
 50  $   if {@is_list}
 51              List _@{name}_[-sep ','-]
 52  $   else
 53              @etype _@{name}_[-sep ','-]
 54  $   end
 55  $ end
 56      )
 57      {
 58  $ foreach {elem}
 59  $   if @is_list
 60          this._@{name}_.clear();
 61          this._@{name}_.addAll(_@{name}_);
 62  $   else
 63          set@ename (_@{name}_);
 64  $   end
 65  $ end
 66      }
 67
 68  $ // define clone()
 69      public Object clone()
 70      {
 71          return new @ename (
 72  $ foreach {elem}
 73  $   if @is_list
 74              cloneList (_@{name}_)[-sep ','-]
 75  $   else
 76              (@etype)cloneNode (_@{name}_)[-sep ','-]
 77  $   end
 78  $ end
 79          );
 80      }
 81
 82  $ // define apply()
 83      public void apply(Switch sw)
 84      {
 85          ((Analysis) sw).case@ename(this);
 86      }
 87
 88  $ // define accessors
 89  $ foreach {elem}
 90  $   if @is_list
 91      public List get@ename ()
 92      {
 93          return _@{name}_;
 94      }
 95
 96      public void set@ename (List list)
 97      {
 98          _@{name}_.clear();
 99          _@{name}_.addAll(list);
100      }
101  $   else
102      public @etype get@ename ()
103      {
104          return _@{name}_;
105      }
106
107      public void set@ename (@etype node)
108      {
109          if(_@{name}_ != null)
110          {
111              _@{name}_.parent(null);
112          }
113
114          if(node != null)
115          {
116              if(node.parent() != null)
117              {
118                  node.parent().removeChild(node);
119              }
120
121              node.parent(this);
122          }
123
124          _@{name}_ = node;
125      }
126  $   end
127  $ end foreach
128
129  $ // define toString()
130      public String toString()
131      {
132          return ""
133  $ foreach {elem}
134              + toString (_@{name}_)
135  $ end
136          ;
137      }
138
139  $ // define removeChild()
140      void removeChild(Node child)
141      {
142  $ foreach {elem}
143  $   if @is_list
144          if ( _@{name}_.remove(child))
145          {
146              return;
147          }
148  $   else
149          if ( _@{name}_ == child )
150          {
151              _@{name}_ = null;
152              return;
153          }
154  $   end
155  $ end
156      }
157
158  $ // define replaceChild()
159      void replaceChild(Node oldChild, Node newChild)
160      {
161  $ foreach {elem}
162  $   if @is_list
163          for(ListIterator i = _@{name}_.listIterator(); i.hasNext();)
164          {
165              if(i.next() == oldChild)
166              {
167                  if(newChild != null)
168                  {
169                      i.set(newChild);
170                      oldChild.parent(null);
171                      return;
172                  }
173
174                  i.remove();
175                  oldChild.parent(null);
176                  return;
177              }
178          }
179  $   else
180          if ( _@{name}_ == oldChild )
181          {
182              set@ename ((@etype) newChild);
183              return;
184          }
185  $   end
186  $ end foreach
187      }
188
189  $ //define cast inner classes for any list elements
190  $ foreach {elem[@is_list]}
191      private class @{ename}_Cast implements Cast
192      {
193          public Object cast(Object o)
194          {
195              @etype node = (@etype) o;
196
197              if((node.parent() != null) &&
198                  (node.parent() != ${../@ename}.this))
199              {
200                  node.parent().removeChild(node);
201              }
202
203              if((node.parent() == null) ||
204                  (node.parent() != ${../@ename}.this))
205              {
206                  node.parent(${../@ename}.this);
207              }
208
209              return node;
210          }
211
212          public Object unCast(Object o)
213          {
214              @etype node = (@etype) o;
215              if ( node.parent() == ${../@ename}.this )
216                  node.parent(null);
217              return node;
218          }
219      }
220  $ end foreach
221  }
222  $   end output
223  $ end foreach
224
225  $ // generate node/Start.java
226  $   output $basedir + '/node/Start.java'
227  /* This file was generated by SableCC (http://www.sablecc.org/). */
228
229  package @package.node;
230
231  import @package.analysis.*;
232
233  public final class Start extends Node
234  {
235      private $baseprod _base_;
236      private EOF _eof_;
237
238      public Start()
239      {
240      }
241
242      public Start(
243          $baseprod _base_,
244          EOF _eof_)
245      {
246          set$baseprod(_base_);
247          setEOF(_eof_);
248      }
249
250      public Object clone()
251      {
252          return new Start(
253              ($baseprod) cloneNode(_base_),
254              (EOF) cloneNode(_eof_));
255      }
256
257      public void apply(Switch sw)
258      {
259          ((Analysis) sw).caseStart(this);
260      }
261
262      public $baseprod get$baseprod()
263      {
264          return _base_;
265      }
266      public void set$baseprod($baseprod node)
267      {
268          if(_base_ != null)
269          {
270              _base_.parent(null);
271          }
272
273          if(node != null)
274          {
275              if(node.parent() != null)
276              {
277                  node.parent().removeChild(node);
278              }
279
280              node.parent(this);
281          }
282
283          _base_ = node;
284      }
285
286      public EOF getEOF()
287      {
288          return _eof_;
289      }
290      public void setEOF(EOF node)
291      {
292          if(_eof_ != null)
293          {
294              _eof_.parent(null);
295          }
296
297          if(node != null)
298          {
299              if(node.parent() != null)
300              {
301                  node.parent().removeChild(node);
302              }
303
304              node.parent(this);
305          }
306
307          _eof_ = node;
308      }
309
310      void removeChild(Node child)
311      {
312          if(_base_ == child)
313          {
314              _base_ = null;
315              return;
316          }
317
318          if(_eof_ == child)
319          {
320              _eof_ = null;
321              return;
322          }
323      }
324      void replaceChild(Node oldChild, Node newChild)
325      {
326          if(_base_ == oldChild)
327          {
328              set$baseprod(($baseprod) newChild);
329              return;
330          }
331
332          if(_eof_ == oldChild)
333          {
334              setEOF((EOF) newChild);
335              return;
336          }
337      }
338
339      public String toString()
340      {
341          return "" +
342              toString(_base_) +
343              toString(_eof_);
344      }
345  }
346  $   end output
347  $ end template

Generated with vim2html
Copyright © 2003 by Chip Cuccio <http://norlug.org/~chipster/?finger>