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#import "ANTLRDebugTreeAdaptor.h"
28
29
30@implementation ANTLRDebugTreeAdaptor
31
32
33- (id) initWithTreeAdaptor:(ANTLRCommonTreeAdaptor *)aTreeAdaptor debugListener:(id<ANTLRDebugEventListener>)aDebugListener
34{
35	self = [super init];
36	if (self) {
37		[self setDebugListener:aDebugListener];
38		[self setTreeAdaptor:aTreeAdaptor];
39	}
40	return self;
41}
42
43- (void) dealloc
44{
45    [self setDebugListener: nil];
46    [self setTreeAdaptor: nil];
47    [super dealloc];
48}
49
50- (id<ANTLRDebugEventListener>) debugListener
51{
52    return debugListener; 
53}
54
55- (void) setDebugListener: (id<ANTLRDebugEventListener>) aDebugListener
56{
57    if (debugListener != aDebugListener) {
58        [(id<ANTLRTreeAdaptor,NSObject>)aDebugListener retain];
59        [(id<ANTLRTreeAdaptor,NSObject>)debugListener release];
60        debugListener = aDebugListener;
61    }
62}
63
64- (ANTLRCommonTreeAdaptor *) getTreeAdaptor
65{
66    return treeAdaptor; 
67}
68
69- (void) setTreeAdaptor: (ANTLRCommonTreeAdaptor *) aTreeAdaptor
70{
71    if (treeAdaptor != aTreeAdaptor) {
72        [aTreeAdaptor retain];
73        [treeAdaptor release];
74        treeAdaptor = aTreeAdaptor;
75    }
76}
77
78#pragma mark -
79#pragma mark Proxy implementation
80
81// anything else that hasn't some debugger event assicioated with it, is simply
82// forwarded to the actual token stream
83- (void) forwardInvocation:(NSInvocation *)anInvocation
84{
85	[anInvocation invokeWithTarget:[self getTreeAdaptor]];
86}
87
88#pragma mark -
89
90#pragma mark Construction
91
92- (id<ANTLRBaseTree>) newANTLRTreeWithToken:(id<ANTLRToken>) payload
93{
94	id<ANTLRBaseTree> newTree = [ANTLRCommonTree newTreeWithToken:payload];
95	[debugListener createNode:[treeAdaptor uniqueIdForTree:newTree] fromTokenAtIndex:[payload getTokenIndex]];
96	return newTree;
97}
98
99- (id<ANTLRBaseTree>) emptyTree
100{
101	id<ANTLRBaseTree> newTree = [treeAdaptor newEmptyTree];
102	[debugListener createNilNode:[treeAdaptor uniqueIdForTree:newTree]];
103	return newTree;
104}
105
106/*	We don't have debug events for those:
107 - (id) copyNode:(id<ANTLRBaseTree>)aNode
108{
109}
110- (id) copyTree:(id<ANTLRBaseTree>)aTree
111{
112}
113*/
114
115- (void) addChild:(id<ANTLRBaseTree>)child toTree:(id<ANTLRBaseTree>)aTree
116{
117	[treeAdaptor addChild:child toTree:aTree];
118	[debugListener addChild:[treeAdaptor uniqueIdForTree:child] toTree:[self uniqueIdForTree:aTree]];
119}
120
121- (id<ANTLRBaseTree>) becomeRoot:(id<ANTLRBaseTree>)newRoot old:(id<ANTLRBaseTree>)oldRoot
122{
123	id<ANTLRBaseTree> newTree = [treeAdaptor becomeRoot:newRoot old:oldRoot];
124	[debugListener becomeRoot:[treeAdaptor uniqueIdForTree:newTree] old:[self uniqueIdForTree:oldRoot]];
125	return newTree;
126}
127
128/* handle by forwardInvocation: 
129- (NSUInteger) uniqueIdForTree:(id<ANTLRBaseTree>)aNode
130{
131}
132*/
133
134#pragma mark Rewrite Rules
135
136 - (void) addTokenAsChild:(id<ANTLRToken>)child toTree:(id<ANTLRBaseTree>)aTree
137{
138	id<ANTLRBaseTree> newChild = [self newANTLRTreeWithToken:child];
139	[self addChild:newChild toTree:aTree];
140}
141
142- (id<ANTLRBaseTree>) makeToken:(id<ANTLRToken>)newRoot parentOf:(id<ANTLRBaseTree>)oldRoot
143{
144	id<ANTLRBaseTree> newNode = [self newANTLRTreeWithToken:newRoot];
145	return [self becomeRoot:newNode old:oldRoot];
146}
147
148- (id<ANTLRBaseTree>) newANTLRTreeWithTokenType:(NSInteger)tokenType
149{
150	id<ANTLRBaseTree> newTree = [treeAdaptor newANTLRTreeWithTokenType:tokenType];
151	[debugListener createNode:[treeAdaptor uniqueIdForTree:newTree] text:nil type:tokenType];
152	return newTree;
153}
154
155- (id<ANTLRBaseTree>) newANTLRTreeWithTokenType:(NSInteger)tokenType text:(NSString *)tokenText
156{
157	id<ANTLRBaseTree> newTree = [treeAdaptor newANTLRTreeWithTokenType:tokenType text:tokenText];
158	[debugListener createNode:[treeAdaptor uniqueIdForTree:newTree] text:tokenText type:tokenType];
159	return newTree;
160}
161- (id<ANTLRBaseTree>) newANTLRTreeWithToken:(id<ANTLRToken>)fromToken tokenType:(NSInteger)tokenType
162{
163	id<ANTLRBaseTree> newTree = [treeAdaptor newANTLRTreeWithToken:fromToken tokenType:tokenType];
164	[debugListener createNode:[treeAdaptor uniqueIdForTree:newTree] text:fromToken.text type:tokenType];
165	return newTree;
166}
167
168- (id<ANTLRBaseTree>) newANTLRTreeWithToken:(id<ANTLRToken>)fromToken tokenType:(NSInteger)tokenType text:(NSString *)tokenText
169{
170	id<ANTLRBaseTree> newTree = [treeAdaptor newANTLRTreeWithToken:fromToken tokenType:tokenType text:tokenText];
171	[debugListener createNode:[treeAdaptor uniqueIdForTree:newTree] text:tokenText type:tokenType];
172	return newTree;
173}
174
175- (id<ANTLRBaseTree>) newANTLRTreeWithToken:(id<ANTLRToken>)fromToken text:(NSString *)tokenText
176{
177	id<ANTLRBaseTree> newTree = [treeAdaptor newANTLRTreeWithToken:fromToken text:tokenText];
178	[debugListener createNode:[treeAdaptor uniqueIdForTree:newTree] text:tokenText type:fromToken.type];
179	return newTree;
180}
181
182#pragma mark Content
183
184/* handled by forwardInvocation:
185- (NSInteger) tokenTypeForNode:(id<ANTLRBaseTree>)aNode
186{
187}
188 
189- (void) setTokenType:(NSInteger)tokenType forNode:(id)aNode
190{
191}
192
193- (NSString *) textForNode:(id<ANTLRBaseTree>)aNode
194{
195}
196 
197- (void) setText:(NSString *)tokenText forNode:(id<ANTLRBaseTree>)aNode
198{
199}
200*/
201- (void) setBoundariesForTree:(id<ANTLRBaseTree>)aTree fromToken:(id<ANTLRToken>)startToken toToken:(id<ANTLRToken>)stopToken
202{
203	[treeAdaptor setBoundariesForTree:aTree fromToken:startToken toToken:stopToken];
204	if (aTree && startToken && stopToken) {
205		[debugListener setTokenBoundariesForTree:[aTree hash] From:[startToken getTokenIndex] To:[stopToken getTokenIndex]];
206	}
207}
208/* handled by forwardInvocation:
209- (NSInteger) tokenStartIndexForTree:(id<ANTLRBaseTree>)aTree
210{
211}
212 
213- (NSInteger) tokenStopIndexForTree:(id<ANTLRBaseTree>)aTree
214{
215}
216*/
217
218#pragma mark Navigation / Tree Parsing
219/* handled by forwardInvocation:
220- (id<ANTLRBaseTree>) childForNode:(id<ANTLRBaseTree>) aNode atIndex:(NSInteger) i
221{
222}
223 
224- (NSInteger) childCountForTree:(id<ANTLRBaseTree>) aTree
225{
226}
227*/
228
229@end
230