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 "ANTLRParser.h" 28 29 30@implementation ANTLRParser 31 32+ (ANTLRParser *)newANTLRParser:(id<ANTLRTokenStream>)anInput 33{ 34 return [[ANTLRParser alloc] initWithTokenStream:anInput]; 35} 36 37+ (ANTLRParser *)newANTLRParser:(id<ANTLRTokenStream>)anInput State:(ANTLRRecognizerSharedState *)aState 38{ 39 return [[ANTLRParser alloc] initWithTokenStream:anInput State:aState]; 40} 41 42- (id) initWithTokenStream:(id<ANTLRTokenStream>)theStream 43{ 44 if ((self = [super init]) != nil) { 45 input = theStream; 46 } 47 return self; 48} 49 50- (id) initWithTokenStream:(id<ANTLRTokenStream>)theStream State:(ANTLRRecognizerSharedState *)aState 51{ 52 if ((self = [super initWithState:aState]) != nil) { 53 input = theStream; 54 } 55 return self; 56} 57 58- (void) reset 59{ 60 [super reset]; // reset all recognizer state variables 61 if ( input!=nil ) { 62 [input seek:0]; // rewind the input 63 } 64} 65 66- (void) dealloc 67{ 68#ifdef DEBUG_DEALLOC 69 NSLog( @"called dealloc in ANTLRParser" ); 70#endif 71 [self setInput:nil]; 72 [super dealloc]; 73} 74 75//---------------------------------------------------------- 76// input 77//---------------------------------------------------------- 78- (id<ANTLRTokenStream>) input 79{ 80 return input; 81} 82 83- (void) setInput: (id<ANTLRTokenStream>) anInput 84{ 85 if (input != anInput) { 86 if ( input ) [input release]; 87 [anInput retain]; 88 } 89 input = anInput; 90} 91 92- (id) getCurrentInputSymbol:(id<ANTLRTokenStream>)anInput 93{ 94 state.token = [input LT:1]; 95 return state.token; 96} 97 98- (ANTLRCommonToken *)getMissingSymbol:(id<ANTLRTokenStream>)anInput 99 Exception:(ANTLRRecognitionException *)e 100 TType:(NSInteger)expectedTokenType 101 BitSet:(ANTLRBitSet *)follow 102{ 103 NSString *tokenText = nil; 104 if ( expectedTokenType == ANTLRTokenTypeEOF ) 105 tokenText = @"<missing EOF>"; 106 else 107 tokenText = [NSString stringWithFormat:@"<missing %@>\n",[[ANTLRBaseRecognizer getTokenNames] objectAtIndex:expectedTokenType]]; 108 ANTLRCommonToken *t = [[ANTLRCommonToken newToken:expectedTokenType Text:tokenText] retain]; 109 ANTLRCommonToken *current = [anInput LT:1]; 110 if ( current.type == ANTLRTokenTypeEOF ) { 111 current = [anInput LT:-1]; 112 } 113 t.line = current.line; 114 t.charPositionInLine = current.charPositionInLine; 115 t.channel = ANTLRTokenChannelDefault; 116 return t; 117} 118 119/** Set the token stream and reset the parser */ 120- (void) setTokenStream:(id<ANTLRTokenStream>)anInput 121{ 122 input = nil; 123 [self reset]; 124 input = anInput; 125} 126 127- (id<ANTLRTokenStream>)getTokenStream 128{ 129 return input; 130} 131 132- (NSString *)getSourceName 133{ 134 return [input getSourceName]; 135} 136 137- (void) traceIn:(NSString *)ruleName Index:(int)ruleIndex 138{ 139 [super traceIn:ruleName Index:ruleIndex Object:[input LT:1]]; 140} 141 142- (void) traceOut:(NSString *)ruleName Index:(NSInteger) ruleIndex 143{ 144 [super traceOut:ruleName Index:ruleIndex Object:[input LT:1]]; 145} 146 147@end 148