Home | History | Annotate | Download | only in Framework
      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 "ANTLRDebugTokenStream.h"
     28 
     29 
     30 @implementation ANTLRDebugTokenStream
     31 
     32 
     33 - (id) initWithTokenStream:(id<ANTLRTokenStream>)theStream debugListener:(id<ANTLRDebugEventListener>)debugger
     34 {
     35 	self = [super init];
     36 	if (self) {
     37 		[self setDebugListener:debugger];
     38 		[self setInput:theStream];
     39 		[self.input LT:1];	// force reading first on-channel token
     40 		initialStreamState = YES;
     41 	}
     42 	return self;
     43 }
     44 
     45 - (void) dealloc
     46 {
     47     [self setDebugListener:nil];
     48     self.input = nil;
     49     [super dealloc];
     50 }
     51 
     52 
     53 - (id<ANTLRDebugEventListener>) debugListener
     54 {
     55     return debugListener; 
     56 }
     57 
     58 - (void) setDebugListener: (id<ANTLRDebugEventListener>) aDebugListener
     59 {
     60     if (debugListener != aDebugListener) {
     61         [(id<ANTLRDebugEventListener,NSObject>)aDebugListener retain];
     62         [(id<ANTLRDebugEventListener,NSObject>)debugListener release];
     63         debugListener = aDebugListener;
     64     }
     65 }
     66 
     67 - (id<ANTLRTokenStream>) input
     68 {
     69     return input; 
     70 }
     71 
     72 - (void) setInput: (id<ANTLRTokenStream>) aTokenStream
     73 {
     74     if (input != aTokenStream) {
     75         if ( input ) [input release];
     76         input = aTokenStream;
     77         [input retain];
     78     }
     79 }
     80 
     81 - (void) consumeInitialHiddenTokens
     82 {
     83 	int firstIdx = input.index;
     84 	for (int i = 0; i<firstIdx; i++)
     85 		[debugListener consumeHiddenToken:[input getToken:i]];
     86 	initialStreamState = NO;
     87 }
     88 
     89 #pragma mark -
     90 #pragma mark Proxy implementation
     91 
     92 // anything else that hasn't some debugger event assicioated with it, is simply
     93 // forwarded to the actual token stream
     94 - (void) forwardInvocation:(NSInvocation *)anInvocation
     95 {
     96 	[anInvocation invokeWithTarget:self.input];
     97 }
     98 
     99 - (void) consume
    100 {
    101 	if ( initialStreamState )
    102 		[self consumeInitialHiddenTokens];
    103 	int a = input.index;
    104 	id<ANTLRToken> token = [input LT:1];
    105 	[input consume];
    106 	int b = input.index;
    107 	[debugListener consumeToken:token];
    108 	if (b > a+1) // must have consumed hidden tokens
    109 		for (int i = a+1; i < b; i++)
    110 			[debugListener consumeHiddenToken:[input getToken:i]];
    111 }
    112 
    113 - (NSInteger) mark
    114 {
    115 	lastMarker = [input mark];
    116 	[debugListener mark:lastMarker];
    117 	return lastMarker;
    118 }
    119 
    120 - (void) rewind
    121 {
    122 	[debugListener rewind];
    123 	[input rewind];
    124 }
    125 
    126 - (void) rewind:(NSInteger)marker
    127 {
    128 	[debugListener rewind:marker];
    129 	[input rewind:marker];
    130 }
    131 
    132 - (id<ANTLRToken>) LT:(NSInteger)k
    133 {
    134 	if ( initialStreamState )
    135 		[self consumeInitialHiddenTokens];
    136 	[debugListener LT:k foundToken:[input LT:k]];
    137 	return [input LT:k];
    138 }
    139 
    140 - (NSInteger) LA:(NSInteger)k
    141 {
    142 	if ( initialStreamState )
    143 		[self consumeInitialHiddenTokens];
    144 	[debugListener LT:k foundToken:[input LT:k]];
    145 	return [input LA:k];
    146 }
    147 
    148 - (id<ANTLRToken>) getToken:(NSInteger)i
    149 {
    150     return [input getToken:i];
    151 }
    152 
    153 - (NSInteger) getIndex
    154 {
    155     return input.index;
    156 }
    157 
    158 - (void) release:(NSInteger) marker
    159 {
    160 }
    161 
    162 - (void) seek:(NSInteger)index
    163 {
    164     // TODO: implement seek in dbg interface
    165     // db.seek(index);
    166     [input seek:index];
    167 }
    168 
    169 - (NSInteger) size
    170 {
    171     return [input size];
    172 }
    173 
    174 - (id<ANTLRTokenSource>) getTokenSource
    175 {
    176     return [input getTokenSource];
    177 }
    178 
    179 - (NSString *) getSourceName
    180 {
    181     return [[input getTokenSource] getSourceName];
    182 }
    183 
    184 - (NSString *) description
    185 {
    186     return [input toString];
    187 }
    188 
    189 - (NSString *) toString
    190 {
    191     return [input toString];
    192 }
    193 
    194 - (NSString *) toStringFromStart:(NSInteger)startIndex ToEnd:(NSInteger)stopIndex
    195 {
    196     return [input toStringFromStart:startIndex ToEnd:stopIndex];
    197 }
    198 
    199 - (NSString *) toStringFromToken:(id<ANTLRToken>)startToken ToToken:(id<ANTLRToken>)stopToken
    200 {
    201     return [input toStringFromStart:[startToken getStart] ToEnd:[stopToken getStopToken]];
    202 }
    203 
    204 @end
    205