1 /* 2 * [The "BSD licence"] 3 * Copyright (c) 2005-2008 Terence Parr 4 * All rights reserved. 5 * 6 * Conversion to C#: 7 * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 namespace Antlr.Runtime.Tree 34 { 35 using IList = System.Collections.IList; 36 37 [System.Serializable] 38 public class RewriteRuleSubtreeStream : RewriteRuleElementStream 39 { RewriteRuleSubtreeStream( ITreeAdaptor adaptor, string elementDescription )40 public RewriteRuleSubtreeStream( ITreeAdaptor adaptor, string elementDescription ) 41 : base( adaptor, elementDescription ) 42 { 43 } 44 45 /** <summary>Create a stream with one element</summary> */ RewriteRuleSubtreeStream( ITreeAdaptor adaptor, string elementDescription, object oneElement )46 public RewriteRuleSubtreeStream( ITreeAdaptor adaptor, string elementDescription, object oneElement ) 47 : base( adaptor, elementDescription, oneElement ) 48 { 49 } 50 51 /** <summary>Create a stream, but feed off an existing list</summary> */ RewriteRuleSubtreeStream( ITreeAdaptor adaptor, string elementDescription, IList elements )52 public RewriteRuleSubtreeStream( ITreeAdaptor adaptor, string elementDescription, IList elements ) 53 : base( adaptor, elementDescription, elements ) 54 { 55 } 56 57 /** <summary> 58 * Treat next element as a single node even if it's a subtree. 59 * This is used instead of next() when the result has to be a 60 * tree root node. Also prevents us from duplicating recently-added 61 * children; e.g., ^(type ID)+ adds ID to type and then 2nd iteration 62 * must dup the type node, but ID has been added. 63 * </summary> 64 * 65 * <remarks> 66 * Referencing a rule result twice is ok; dup entire tree as 67 * we can't be adding trees as root; e.g., expr expr. 68 * 69 * Hideous code duplication here with super.next(). Can't think of 70 * a proper way to refactor. This needs to always call dup node 71 * and super.next() doesn't know which to call: dup node or dup tree. 72 * </remarks> 73 */ NextNode()74 public virtual object NextNode() 75 { 76 //System.Console.WriteLine("nextNode: elements={0}, singleElement={1}", elements, ((ITree)singleElement).ToStringTree()); 77 int n = Count; 78 if ( dirty || ( cursor >= n && n == 1 ) ) 79 { 80 // if out of elements and size is 1, dup (at most a single node 81 // since this is for making root nodes). 82 object el = NextCore(); 83 return adaptor.DupNode( el ); 84 } 85 // test size above then fetch 86 object tree = NextCore(); 87 while (adaptor.IsNil(tree) && adaptor.GetChildCount(tree) == 1) 88 tree = adaptor.GetChild(tree, 0); 89 //System.Console.WriteLine("_next={0}", ((ITree)tree).ToStringTree()); 90 object el2 = adaptor.DupNode(tree); // dup just the root (want node here) 91 return el2; 92 } 93 Dup( object el )94 protected override object Dup( object el ) 95 { 96 return adaptor.DupTree( el ); 97 } 98 } 99 } 100