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__strong id root;
41
42/** If this tree (root) was created from a token stream, track it. */
43__strong id <ANTLRTokenStream> tokens;
44
45	/** What tree adaptor was used to build these trees */
46__strong ANTLRCommonTreeAdaptor *adaptor;
47
48/** The tree iterator we using */
49__strong ANTLRTreeIterator *it;
50
51/** Stack of indexes used for push/pop calls */
52__strong ANTLRIntArray *calls;
53
54/** Tree (nil A B C) trees like flat A B C streams */
55BOOL hasNilRoot;
56
57/** Tracks tree depth.  Level=0 means we're at root node level. */
58NSInteger 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@property (assign, getter=getLevel, setter=setLevel:) NSInteger level;
64
65+ (ANTLRCommonTreeNodeStream *) newANTLRCommonTreeNodeStream:(ANTLRCommonTree *)theTree;
66+ (ANTLRCommonTreeNodeStream *) newANTLRCommonTreeNodeStream:(id<ANTLRTreeAdaptor>)anAdaptor Tree:(ANTLRCommonTree *)theTree;
67
68- (id) initWithTree:(ANTLRCommonTree *)theTree;
69
70- (id) initWithTreeAdaptor:(id<ANTLRTreeAdaptor>)adaptor Tree:(ANTLRCommonTree *)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<ANTLRBaseTree>) obj;
80- (void) setUniqueNavigationNodes:(BOOL) uniqueNavigationNodes;
81
82- (id) getTreeSource;
83
84- (NSString *) getSourceName;
85
86- (id<ANTLRTokenStream>) getTokenStream;
87
88- (void) setTokenStream:(id<ANTLRTokenStream>) tokens;
89
90- (ANTLRCommonTreeAdaptor *) getTreeAdaptor;
91
92- (void) setTreeAdaptor:(ANTLRCommonTreeAdaptor *) adaptor;
93
94- (NSInteger) LA:(NSInteger) i;
95
96    /** Make stream jump to a new location, saving old location.
97     *  Switch back with pop().
98     */
99- (ANTLRCommonTree *)getNode:(NSInteger) i;
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<ANTLRBaseTree>)startNode ToNode:(id<ANTLRBaseTree>)stopNode;
113
114/** For debugging; destructive: moves tree iterator to end. */
115- (NSString *) toTokenTypeString;
116
117@property (retain) ANTLRTreeIterator *it;
118@property (retain) ANTLRIntArray *calls;
119@property BOOL hasNilRoot;
120@end
121