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 "DebugTreeAdaptor.h"
28
29
30@implementation DebugTreeAdaptor
31
32
33- (id) initWithTreeAdaptor:(CommonTreeAdaptor *)aTreeAdaptor debugListener:(id<DebugEventListener>)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<DebugEventListener>) debugListener
51{
52    return debugListener;
53}
54
55- (void) setDebugListener: (id<DebugEventListener>) aDebugListener
56{
57    if (debugListener != aDebugListener) {
58        [(id<TreeAdaptor,NSObject>)aDebugListener retain];
59        [(id<TreeAdaptor,NSObject>)debugListener release];
60        debugListener = aDebugListener;
61    }
62}
63
64- (CommonTreeAdaptor *) getTreeAdaptor
65{
66    return treeAdaptor;
67}
68
69- (void) setTreeAdaptor: (CommonTreeAdaptor *) 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<BaseTree>) newTreeWithToken:(id<Token>) payload
93{
94	id<BaseTree> newTree = [CommonTree newTreeWithToken:payload];
95	[debugListener createNode:[treeAdaptor uniqueIdForTree:newTree] fromTokenAtIndex:[payload getTokenIndex]];
96	return newTree;
97}
98
99- (id<BaseTree>) emptyTree
100{
101	id<BaseTree> 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<BaseTree>)aNode
108{
109}
110- (id) copyTree:(id<BaseTree>)aTree
111{
112}
113*/
114
115- (void) addChild:(id<BaseTree>)child toTree:(id<BaseTree>)aTree
116{
117	[treeAdaptor addChild:child toTree:aTree];
118	[debugListener addChild:[treeAdaptor uniqueIdForTree:child] toTree:[self uniqueIdForTree:aTree]];
119}
120
121- (id<BaseTree>) becomeRoot:(id<BaseTree>)newRoot old:(id<BaseTree>)oldRoot
122{
123	id<BaseTree> 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<BaseTree>)aNode
130{
131}
132*/
133
134#pragma mark Rewrite Rules
135
136 - (void) addTokenAsChild:(id<Token>)child toTree:(id<BaseTree>)aTree
137{
138	id<BaseTree> newChild = [self newTreeWithToken:child];
139	[self addChild:newChild toTree:aTree];
140}
141
142- (id<BaseTree>) makeToken:(id<Token>)newRoot parentOf:(id<BaseTree>)oldRoot
143{
144	id<BaseTree> newNode = [self newTreeWithToken:newRoot];
145	return [self becomeRoot:newNode old:oldRoot];
146}
147
148- (id<BaseTree>) newTreeWithTokenType:(NSInteger)tokenType
149{
150	id<BaseTree> newTree = [treeAdaptor newTreeWithTokenType:tokenType];
151	[debugListener createNode:[treeAdaptor uniqueIdForTree:newTree] text:nil type:tokenType];
152	return newTree;
153}
154
155- (id<BaseTree>) newTreeWithTokenType:(NSInteger)tokenType text:(NSString *)tokenText
156{
157	id<BaseTree> newTree = [treeAdaptor newTreeWithTokenType:tokenType text:tokenText];
158	[debugListener createNode:[treeAdaptor uniqueIdForTree:newTree] text:tokenText type:tokenType];
159	return newTree;
160}
161- (id<BaseTree>) newTreeWithToken:(id<Token>)fromToken tokenType:(NSInteger)tokenType
162{
163	id<BaseTree> newTree = [treeAdaptor newTreeWithToken:fromToken tokenType:tokenType];
164	[debugListener createNode:[treeAdaptor uniqueIdForTree:newTree] text:fromToken.text type:tokenType];
165	return newTree;
166}
167
168- (id<BaseTree>) newTreeWithToken:(id<Token>)fromToken tokenType:(NSInteger)tokenType text:(NSString *)tokenText
169{
170	id<BaseTree> newTree = [treeAdaptor newTreeWithToken:fromToken tokenType:tokenType text:tokenText];
171	[debugListener createNode:[treeAdaptor uniqueIdForTree:newTree] text:tokenText type:tokenType];
172	return newTree;
173}
174
175- (id<BaseTree>) newTreeWithToken:(id<Token>)fromToken text:(NSString *)tokenText
176{
177	id<BaseTree> newTree = [treeAdaptor newTreeWithToken: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<BaseTree>)aNode
186{
187}
188
189- (void) setTokenType:(NSInteger)tokenType forNode:(id)aNode
190{
191}
192
193- (NSString *) textForNode:(id<BaseTree>)aNode
194{
195}
196
197- (void) setText:(NSString *)tokenText forNode:(id<BaseTree>)aNode
198{
199}
200*/
201- (void) setBoundariesForTree:(id<BaseTree>)aTree fromToken:(id<Token>)startToken toToken:(id<Token>)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<BaseTree>)aTree
210{
211}
212
213- (NSInteger) tokenStopIndexForTree:(id<BaseTree>)aTree
214{
215}
216*/
217
218#pragma mark Navigation / Tree Parsing
219/* handled by forwardInvocation:
220- (id<BaseTree>) childForNode:(id<BaseTree>) aNode atIndex:(NSInteger) i
221{
222}
223
224- (NSInteger) childCountForTree:(id<BaseTree>) aTree
225{
226}
227*/
228
229@end
230