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 "ANTLRRecognitionException.h" 28 #import "ANTLRTokenStream.h" 29 #import "ANTLRTreeNodeStream.h" 30 31 @implementation ANTLRRecognitionException 32 33 @synthesize input; 34 @synthesize token; 35 @synthesize node; 36 @synthesize line; 37 @synthesize charPositionInLine; 38 39 + (id) newException 40 { 41 return [[ANTLRRecognitionException alloc] init]; 42 } 43 44 + (id) newException:(id<ANTLRIntStream>) anInputStream 45 { 46 return [[ANTLRRecognitionException alloc] initWithStream:anInputStream]; 47 } 48 49 + (id) newException:(id<ANTLRIntStream>) anInputStream reason:(NSString *)aReason 50 { 51 return [[ANTLRRecognitionException alloc] initWithStream:anInputStream reason:aReason]; 52 } 53 54 - (id) init 55 { 56 self = [super initWithName:@"Recognition Exception" reason:@"Recognition Exception" userInfo:nil]; 57 if ( self != nil ) { 58 } 59 return self; 60 } 61 62 - (id) initWithStream:(id<ANTLRIntStream>)anInputStream reason:(NSString *)aReason 63 { 64 self = [super initWithName:NSStringFromClass([self class]) reason:aReason userInfo:nil]; 65 if ( self != nil ) { 66 [self setStream:anInputStream]; 67 index = input.index; 68 69 Class inputClass = [input class]; 70 if ([inputClass conformsToProtocol:@protocol(ANTLRTokenStream)]) { 71 [self setToken:[(id<ANTLRTokenStream>)input LT:1]]; 72 line = token.line; 73 charPositionInLine = token.charPositionInLine; 74 } else if ([inputClass conformsToProtocol:@protocol(ANTLRCharStream)]) { 75 c = (unichar)[input LA:1]; 76 line = ((id<ANTLRCharStream>)input).line; 77 charPositionInLine = ((id<ANTLRCharStream>)input).charPositionInLine; 78 } else if ([inputClass conformsToProtocol:@protocol(ANTLRTreeNodeStream)]) { 79 [self setNode:[(id<ANTLRTreeNodeStream>)input LT:1]]; 80 line = [node line]; 81 charPositionInLine = [node charPositionInLine]; 82 } else { 83 c = (unichar)[input LA:1]; 84 } 85 } 86 return self; 87 } 88 89 - (id) initWithStream:(id<ANTLRIntStream>)anInputStream 90 { 91 self = [super initWithName:NSStringFromClass([self class]) reason:@"Runtime Exception" userInfo:nil]; 92 if ( self != nil ) { 93 } 94 return self; 95 } 96 97 - (id) initWithName:(NSString *)aName reason:(NSString *)aReason userInfo:(NSDictionary *)aUserInfo 98 { 99 self = [super initWithName:aName reason:aReason userInfo:aUserInfo]; 100 if ( self != nil ) { 101 } 102 return self; 103 } 104 105 - (void) dealloc 106 { 107 #ifdef DEBUG_DEALLOC 108 NSLog( @"called dealloc in ANTLRRecognitionException" ); 109 #endif 110 if ( input ) [input release]; 111 if ( token ) [token release]; 112 if ( node ) [node release]; 113 [super dealloc]; 114 } 115 116 - (NSInteger) unexpectedType 117 { 118 if (token) { 119 return token.type; 120 } else if (node) { 121 return [node type]; 122 } else { 123 return c; 124 } 125 } 126 127 - (id<ANTLRToken>)getUnexpectedToken 128 { 129 return token; 130 } 131 132 - (NSString *) description 133 { 134 //NSMutableString *desc = [[NSMutableString alloc] initWithString:NSStringFromClass([self class])]; 135 NSMutableString *desc = [NSMutableString stringWithString:[self className]]; 136 if (token) { 137 [desc appendFormat:@" token:%@", token]; 138 } else if (node) { 139 [desc appendFormat:@" node:%@", node]; 140 } else { 141 [desc appendFormat:@" char:%c", c]; 142 } 143 [desc appendFormat:@" line:%d position:%d", line, charPositionInLine]; 144 return desc; 145 } 146 147 //---------------------------------------------------------- 148 // input 149 //---------------------------------------------------------- 150 - (id<ANTLRIntStream>) getStream 151 { 152 return input; 153 } 154 155 - (void) setStream: (id<ANTLRIntStream>) aStream 156 { 157 if ( input != aStream ) { 158 if ( input ) [input release]; 159 if ( aStream ) [aStream retain]; 160 input = aStream; 161 } 162 } 163 164 //---------------------------------------------------------- 165 // token 166 //---------------------------------------------------------- 167 - (id<ANTLRToken>) getToken 168 { 169 return token; 170 } 171 172 - (void) setToken: (id<ANTLRToken>) aToken 173 { 174 if (token != aToken) { 175 if ( token ) [token release]; 176 if ( aToken ) [aToken retain]; 177 token = aToken; 178 } 179 } 180 181 //---------------------------------------------------------- 182 // node 183 //---------------------------------------------------------- 184 - (id<ANTLRBaseTree>) getNode 185 { 186 return node; 187 } 188 189 - (void) setNode: (id<ANTLRBaseTree>) aNode 190 { 191 if (node != aNode) { 192 if ( node ) [node release]; 193 if ( aNode ) [aNode retain]; 194 node = aNode; 195 } 196 } 197 198 - (NSString *)getMessage 199 { 200 return @"Fix getMessage in ANTLRRecognitionException"; 201 } 202 203 - (NSUInteger)charPositionInLine 204 { 205 return charPositionInLine; 206 } 207 208 - (void)setCharPositionInLine:(NSUInteger)aPos 209 { 210 charPositionInLine = aPos; 211 } 212 213 @synthesize index; 214 @synthesize c; 215 @end 216