1org.antlr.runtime.tree.RewriteRuleSubtreeStream = function() {
2    var sup = org.antlr.runtime.tree.RewriteRuleSubtreeStream.superclass;
3    sup.constructor.apply(this, arguments);
4};
5
6org.antlr.lang.extend(org.antlr.runtime.tree.RewriteRuleSubtreeStream,
7                  org.antlr.runtime.tree.RewriteRuleElementStream, {
8	/** Treat next element as a single node even if it's a subtree.
9	 *  This is used instead of next() when the result has to be a
10	 *  tree root node.  Also prevents us from duplicating recently-added
11	 *  children; e.g., ^(type ID)+ adds ID to type and then 2nd iteration
12	 *  must dup the type node, but ID has been added.
13	 *
14	 *  Referencing a rule result twice is ok; dup entire tree as
15	 *  we can't be adding trees as root; e.g., expr expr.
16	 *
17	 *  Hideous code duplication here with super.next().  Can't think of
18	 *  a proper way to refactor.  This needs to always call dup node
19	 *  and super.next() doesn't know which to call: dup node or dup tree.
20	 */
21    nextNode: function() {
22		var n = this.size(),
23            el;
24		if ( this.dirty || (this.cursor>=n && n===1) ) {
25			// if out of elements and size is 1, dup (at most a single node
26			// since this is for making root nodes).
27			el = this._next();
28			return this.adaptor.dupNode(el);
29		}
30		// test size above then fetch
31		el = this._next();
32		return el;
33	},
34
35    dup: function(el) {
36		return this.adaptor.dupTree(el);
37	}
38});