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 <Cocoa/Cocoa.h>
28 #import "ANTLRCommonTree.h"
29 #import "ANTLRCommonTreeNodeStream.h"
30 #import "ANTLRLookaheadStream.h"
31 #import "ANTLRTreeNodeStream.h"
32 #import "ANTLRTreeIterator.h"
33 #import "ANTLRIntArray.h"
34 
35 @interface ANTLRCommonTreeNodeStream : ANTLRLookaheadStream <ANTLRTreeNodeStream> {
36 #define DEFAULT_INITIAL_BUFFER_SIZE 100
37 #define INITIAL_CALL_STACK_SIZE 10
38 
39 /** Pull nodes from which tree? */
40 id root;
41 
42 /** If this tree (root) was created from a token stream, track it. */
43 id <ANTLRTokenStream> tokens;
44 
45 	/** What tree adaptor was used to build these trees */
46 ANTLRCommonTreeAdaptor *adaptor;
47 
48 /** The tree iterator we using */
49 ANTLRTreeIterator *it;
50 
51 /** Stack of indexes used for push/pop calls */
52 ANTLRIntArray *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:) ANTLRCommonTree *root;
61 @property (retain, getter=getTokens,setter=setTokens:) id<ANTLRTokenStream> tokens;
62 @property (retain, getter=getTreeAdaptor, setter=setTreeAdaptor:) ANTLRCommonTreeAdaptor *adaptor;
63 
64 + (ANTLRCommonTreeNodeStream *) newANTLRCommonTreeNodeStream:(ANTLRCommonTree *)theTree;
65 + (ANTLRCommonTreeNodeStream *) newANTLRCommonTreeNodeStream:(id<ANTLRTreeAdaptor>)anAdaptor Tree:(ANTLRCommonTree *)theTree;
66 
67 - (id) initWithTree:(ANTLRCommonTree *)theTree;
68 
69 - (id) initWithTreeAdaptor:(id<ANTLRTreeAdaptor>)adaptor Tree:(ANTLRCommonTree *)theTree;
70 
71 - (void) reset;
72 
73     /** Pull elements from tree iterator.  Track tree level 0..max_level.
74      *  If nil rooted tree, don't give initial nil and DOWN nor final UP.
75      */
76 - (id) nextElement;
77 
78 - (BOOL) isEOF:(id<ANTLRTree>) o;
79 - (void) setUniqueNavigationNodes:(BOOL) uniqueNavigationNodes;
80 
81 - (id) getTreeSource;
82 
83 - (NSString *) getSourceName;
84 
85 - (id<ANTLRTokenStream>) getTokenStream;
86 
87 - (void) setTokenStream:(id<ANTLRTokenStream>) tokens;
88 
89 - (ANTLRCommonTreeAdaptor *) getTreeAdaptor;
90 
91 - (void) setTreeAdaptor:(ANTLRCommonTreeAdaptor *) adaptor;
92 
93 - (NSInteger) LA:(NSInteger) i;
94 
95     /** Make stream jump to a new location, saving old location.
96      *  Switch back with pop().
97      */
98 - (ANTLRCommonTree *)getNode:(NSInteger) i;
99 
100 - (void) push:(NSInteger) index;
101 
102     /** Seek back to previous index saved during last push() call.
103      *  Return top of stack (return index).
104      */
105 - (NSInteger) pop;
106 
107 // TREE REWRITE INTERFACE
108 
109 - (void) replaceChildren:(id)parent From:(NSInteger)startChildIndex To:(NSInteger)stopChildIndex With:(id) t;
110 
111 - (NSString *) toStringFromNode:(id<ANTLRTree>)startNode ToNode:(id<ANTLRTree>)stopNode;
112 
113 /** For debugging; destructive: moves tree iterator to end. */
114 - (NSString *) toTokenTypeString;
115 
116 @end
117