1 // [The "BSD licence"]
2 // Copyright (c) 2006-2007 Kay Roepke 2010 Alan Condit
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 #import <Foundation/Foundation.h>
28 #import "CommonTree.h"
29 #import "CommonTreeNodeStream.h"
30 #import "LookaheadStream.h"
31 #import "TreeNodeStream.h"
32 #import "TreeIterator.h"
33 #import "IntArray.h"
34 
35 @interface CommonTreeNodeStream : LookaheadStream <TreeNodeStream> {
36 #define DEFAULT_INITIAL_BUFFER_SIZE 100
37 #define INITIAL_CALL_STACK_SIZE 10
38 
39 /** Pull nodes from which tree? */
40 __strong id root;
41 
42 /** If this tree (root) was created from a token stream, track it. */
43 __strong id <TokenStream> tokens;
44 
45 	/** What tree adaptor was used to build these trees */
46 __strong CommonTreeAdaptor *adaptor;
47 
48 /** The tree iterator we using */
49 __strong TreeIterator *it;
50 
51 /** Stack of indexes used for push/pop calls */
52 __strong IntArray *calls;
53 
54 /** Tree (nil A B C) trees like flat A B C streams */
55 BOOL hasNilRoot;
56 
57 /** Tracks tree depth.  Level=0 means we're at root node level. */
58 NSInteger level;
59 }
60 @property (retain, getter=getRoot, setter=setRoot:) CommonTree *root;
61 @property (retain, getter=getTokens,setter=setTokens:) id<TokenStream> tokens;
62 @property (retain, getter=getTreeAdaptor, setter=setTreeAdaptor:) CommonTreeAdaptor *adaptor;
63 @property (assign, getter=getLevel, setter=setLevel:) NSInteger level;
64 
65 + (CommonTreeNodeStream *) newCommonTreeNodeStream:(CommonTree *)theTree;
66 + (CommonTreeNodeStream *) newCommonTreeNodeStream:(id<TreeAdaptor>)anAdaptor Tree:(CommonTree *)theTree;
67 
68 - (id) initWithTree:(CommonTree *)theTree;
69 
70 - (id) initWithTreeAdaptor:(id<TreeAdaptor>)adaptor Tree:(CommonTree *)theTree;
71 
72 - (void) reset;
73 
74     /** Pull elements from tree iterator.  Track tree level 0..max_level.
75      *  If nil rooted tree, don't give initial nil and DOWN nor final UP.
76      */
77 - (id) nextElement;
78 
79 - (BOOL) isEOF:(id<BaseTree>) obj;
80 - (void) setUniqueNavigationNodes:(BOOL) uniqueNavigationNodes;
81 
82 - (id) getTreeSource;
83 
84 - (NSString *) getSourceName;
85 
86 - (id<TokenStream>) getTokenStream;
87 
88 - (void) setTokenStream:(id<TokenStream>) tokens;
89 
90 - (CommonTreeAdaptor *) getTreeAdaptor;
91 
92 - (void) setTreeAdaptor:(CommonTreeAdaptor *) adaptor;
93 
94 - (CommonTree *)get:(NSInteger) i;
95 
96 - (NSInteger) LA:(NSInteger) i;
97 
98     /** Make stream jump to a new location, saving old location.
99      *  Switch back with pop().
100      */
101 - (void) push:(NSInteger) index;
102 
103     /** Seek back to previous index saved during last push() call.
104      *  Return top of stack (return index).
105      */
106 - (NSInteger) pop;
107 
108 // TREE REWRITE INTERFACE
109 
110 - (void) replaceChildren:(id)parent From:(NSInteger)startChildIndex To:(NSInteger)stopChildIndex With:(id) t;
111 
112 - (NSString *) toStringFromNode:(id<BaseTree>)startNode ToNode:(id<BaseTree>)stopNode;
113 
114 /** For debugging; destructive: moves tree iterator to end. */
115 - (NSString *) toTokenTypeString;
116 
117 @property (retain) TreeIterator *it;
118 @property (retain) IntArray *calls;
119 @property BOOL hasNilRoot;
120 @end
121