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 "ANTLRDebugTreeNodeStream.h" 28 29 30 @implementation ANTLRDebugTreeNodeStream 31 32 - (id) initWithTreeNodeStream:(id<ANTLRTreeNodeStream>)theStream debugListener:(id<ANTLRDebugEventListener>)debugger 33 { 34 self = [super init]; 35 if (self) { 36 [self setDebugListener:debugger]; 37 [self setTreeAdaptor:[theStream treeAdaptor]]; 38 [self setInput:theStream]; 39 } 40 return self; 41 } 42 43 - (void) dealloc 44 { 45 [self setDebugListener: nil]; 46 [self setTreeAdaptor: nil]; 47 input = nil; 48 [super dealloc]; 49 } 50 51 - (id<ANTLRDebugEventListener>) debugListener 52 { 53 return debugListener; 54 } 55 56 - (void) setDebugListener: (id<ANTLRDebugEventListener>) aDebugListener 57 { 58 if (debugListener != aDebugListener) { 59 [(id<ANTLRDebugEventListener,NSObject>)aDebugListener retain]; 60 [(id<ANTLRDebugEventListener,NSObject>)debugListener release]; 61 debugListener = aDebugListener; 62 } 63 } 64 65 66 - (id<ANTLRTreeAdaptor>) getTreeAdaptor 67 { 68 return treeAdaptor; 69 } 70 71 - (void) setTreeAdaptor: (id<ANTLRTreeAdaptor>) aTreeAdaptor 72 { 73 if (treeAdaptor != aTreeAdaptor) { 74 [(id<ANTLRTreeAdaptor,NSObject>)aTreeAdaptor retain]; 75 [(id<ANTLRTreeAdaptor,NSObject>)treeAdaptor release]; 76 treeAdaptor = aTreeAdaptor; 77 } 78 } 79 80 81 - (id<ANTLRTreeNodeStream>) input 82 { 83 return input; 84 } 85 86 - (void) setInput:(id<ANTLRTreeNodeStream>) aTreeNodeStream 87 { 88 if (input != aTreeNodeStream) { 89 [input release]; 90 [(id<ANTLRTreeNodeStream,NSObject>)aTreeNodeStream retain]; 91 } 92 input = aTreeNodeStream; 93 } 94 95 96 #pragma mark ANTLRTreeNodeStream conformance 97 98 - (id) LT:(NSInteger)k 99 { 100 id node = [input LT:k]; 101 unsigned hash = [treeAdaptor uniqueIdForTree:node]; 102 NSString *text = [treeAdaptor textForNode:node]; 103 int type = [treeAdaptor tokenTypeForNode:node]; 104 [debugListener LT:k foundNode:hash ofType:type text:text]; 105 return node; 106 } 107 108 - (void) setUniqueNavigationNodes:(BOOL)flag 109 { 110 [input setUniqueNavigationNodes:flag]; 111 } 112 113 #pragma mark ANTLRIntStream conformance 114 - (void) consume 115 { 116 id node = [input LT:1]; 117 [input consume]; 118 unsigned hash = [treeAdaptor uniqueIdForTree:node]; 119 NSString *theText = [treeAdaptor textForNode:node]; 120 int aType = [treeAdaptor tokenTypeForNode:node]; 121 [debugListener consumeNode:hash ofType:aType text:theText]; 122 } 123 124 - (NSInteger) LA:(NSUInteger) i 125 { 126 id<ANTLRBaseTree> node = [self LT:1]; 127 return node.type; 128 } 129 130 - (NSUInteger) mark 131 { 132 unsigned lastMarker = [input mark]; 133 [debugListener mark:lastMarker]; 134 return lastMarker; 135 } 136 137 - (NSUInteger) getIndex 138 { 139 return input.index; 140 } 141 142 - (void) rewind:(NSUInteger) marker 143 { 144 [input rewind:marker]; 145 [debugListener rewind:marker]; 146 } 147 148 - (void) rewind 149 { 150 [input rewind]; 151 [debugListener rewind]; 152 } 153 154 - (void) release:(NSUInteger) marker 155 { 156 [input release:marker]; 157 } 158 159 - (void) seek:(NSUInteger) index 160 { 161 [input seek:index]; 162 // todo: seek missing in debug protocol 163 } 164 165 - (NSUInteger) size 166 { 167 return [input size]; 168 } 169 170 - (NSString *) toStringFromToken:(id)startNode ToToken:(id)stopNode 171 { 172 return [input toStringFromToken:(id<ANTLRToken>)startNode ToToken:(id<ANTLRToken>)stopNode]; 173 } 174 175 @end 176