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