1*16467b97STreehugger Robot // [The "BSD licence"] 2*16467b97STreehugger Robot // Copyright (c) 2006-2007 Kay Roepke 3*16467b97STreehugger Robot // All rights reserved. 4*16467b97STreehugger Robot // 5*16467b97STreehugger Robot // Redistribution and use in source and binary forms, with or without 6*16467b97STreehugger Robot // modification, are permitted provided that the following conditions 7*16467b97STreehugger Robot // are met: 8*16467b97STreehugger Robot // 1. Redistributions of source code must retain the above copyright 9*16467b97STreehugger Robot // notice, this list of conditions and the following disclaimer. 10*16467b97STreehugger Robot // 2. Redistributions in binary form must reproduce the above copyright 11*16467b97STreehugger Robot // notice, this list of conditions and the following disclaimer in the 12*16467b97STreehugger Robot // documentation and/or other materials provided with the distribution. 13*16467b97STreehugger Robot // 3. The name of the author may not be used to endorse or promote products 14*16467b97STreehugger Robot // derived from this software without specific prior written permission. 15*16467b97STreehugger Robot // 16*16467b97STreehugger Robot // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17*16467b97STreehugger Robot // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18*16467b97STreehugger Robot // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19*16467b97STreehugger Robot // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20*16467b97STreehugger Robot // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21*16467b97STreehugger Robot // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22*16467b97STreehugger Robot // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23*16467b97STreehugger Robot // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24*16467b97STreehugger Robot // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25*16467b97STreehugger Robot // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26*16467b97STreehugger Robot 27*16467b97STreehugger Robot 28*16467b97STreehugger Robot #import <Foundation/Foundation.h> 29*16467b97STreehugger Robot #import "IntStream.h" 30*16467b97STreehugger Robot #import "CharStream.h" 31*16467b97STreehugger Robot #import "TokenStream.h" 32*16467b97STreehugger Robot #import "CommonTree.h" 33*16467b97STreehugger Robot #import "CommonTreeAdaptor.h" 34*16467b97STreehugger Robot 35*16467b97STreehugger Robot @protocol TreeNodeStream < IntStream > 36*16467b97STreehugger Robot 37*16467b97STreehugger Robot - (id) initWithTree:(CommonTree *)theTree; 38*16467b97STreehugger Robot 39*16467b97STreehugger Robot /** Get a tree node at an absolute index i; 0..n-1. 40*16467b97STreehugger Robot * If you don't want to buffer up nodes, then this method makes no 41*16467b97STreehugger Robot * sense for you. 42*16467b97STreehugger Robot */ 43*16467b97STreehugger Robot - (id) get:(NSInteger) idx; 44*16467b97STreehugger Robot /** Get tree node at current input pointer + i ahead where i=1 is next node. 45*16467b97STreehugger Robot * i<0 indicates nodes in the past. So LT(-1) is previous node, but 46*16467b97STreehugger Robot * implementations are not required to provide results for k < -1. 47*16467b97STreehugger Robot * LT(0) is undefined. For i>=n, return null. 48*16467b97STreehugger Robot * Return null for LT(0) and any index that results in an absolute address 49*16467b97STreehugger Robot * that is negative. 50*16467b97STreehugger Robot * 51*16467b97STreehugger Robot * This is analogus to the LT() method of the TokenStream, but this 52*16467b97STreehugger Robot * returns a tree node instead of a token. Makes code gen identical 53*16467b97STreehugger Robot * for both parser and tree grammars. :) 54*16467b97STreehugger Robot */ 55*16467b97STreehugger Robot - (id) LT:(NSInteger)k; 56*16467b97STreehugger Robot /** Where is this stream pulling nodes from? This is not the name, but 57*16467b97STreehugger Robot * the object that provides node objects. 58*16467b97STreehugger Robot */ 59*16467b97STreehugger Robot - (id) getTreeSource; 60*16467b97STreehugger Robot /** If the tree associated with this stream was created from a TokenStream, 61*16467b97STreehugger Robot * you can specify it here. Used to do rule $text attribute in tree 62*16467b97STreehugger Robot * parser. Optional unless you use tree parser rule text attribute 63*16467b97STreehugger Robot * or output=template and rewrite=true options. 64*16467b97STreehugger Robot */ 65*16467b97STreehugger Robot - (id<TokenStream>) getTokenStream; 66*16467b97STreehugger Robot /** What adaptor can tell me how to interpret/navigate nodes and 67*16467b97STreehugger Robot * trees. E.g., get text of a node. 68*16467b97STreehugger Robot */ 69*16467b97STreehugger Robot - (id<TreeAdaptor>) getTreeAdaptor; 70*16467b97STreehugger Robot /** As we flatten the tree, we use UP, DOWN nodes to represent 71*16467b97STreehugger Robot * the tree structure. When debugging we need unique nodes 72*16467b97STreehugger Robot * so we have to instantiate new ones. When doing normal tree 73*16467b97STreehugger Robot * parsing, it's slow and a waste of memory to create unique 74*16467b97STreehugger Robot * navigation nodes. Default should be false; 75*16467b97STreehugger Robot */ 76*16467b97STreehugger Robot - (void) setUniqueNavigationNodes:(BOOL)flag; 77*16467b97STreehugger Robot /** Reset the tree node stream in such a way that it acts like 78*16467b97STreehugger Robot * a freshly constructed stream. 79*16467b97STreehugger Robot */ 80*16467b97STreehugger Robot - (void) reset; 81*16467b97STreehugger Robot 82*16467b97STreehugger Robot /** Return the text of all nodes from start to stop, inclusive. 83*16467b97STreehugger Robot * If the stream does not buffer all the nodes then it can still 84*16467b97STreehugger Robot * walk recursively from start until stop. You can always return 85*16467b97STreehugger Robot * null or "" too, but users should not access $ruleLabel.text in 86*16467b97STreehugger Robot * an action of course in that case. 87*16467b97STreehugger Robot */ 88*16467b97STreehugger Robot - (NSString *) toStringFromNode:(id)startNode ToNode:(id)stopNode; 89*16467b97STreehugger Robot 90*16467b97STreehugger Robot /** Replace from start to stop child index of parent with t, which might 91*16467b97STreehugger Robot * be a list. Number of children may be different 92*16467b97STreehugger Robot * after this call. The stream is notified because it is walking the 93*16467b97STreehugger Robot * tree and might need to know you are monkeying with the underlying 94*16467b97STreehugger Robot * tree. Also, it might be able to modify the node stream to avoid 95*16467b97STreehugger Robot * restreaming for future phases. 96*16467b97STreehugger Robot * 97*16467b97STreehugger Robot * If parent is null, don't do anything; must be at root of overall tree. 98*16467b97STreehugger Robot * Can't replace whatever points to the parent externally. Do nothing. 99*16467b97STreehugger Robot */ 100*16467b97STreehugger Robot - (void) replaceChildren:(id)parent From:(NSInteger)startChildIndex To:(NSInteger)stopChildIndex With:(id) t; 101*16467b97STreehugger Robot 102*16467b97STreehugger Robot 103*16467b97STreehugger Robot @end 104