1*16467b97STreehugger Robot// [The "BSD licence"] 2*16467b97STreehugger Robot// Copyright (c) 2006-2007 Kay Roepke 2010 Alan Condit 3*16467b97STreehugger Robot// All rights reserved. 4*16467b97STreehugger Robot// 5*16467b97STreehugger Robot// Redistribution and use in source and binary forms, with or without 6*16467b97STreehugger Robot// modification, are permitted provided that the following conditions 7*16467b97STreehugger Robot// are met: 8*16467b97STreehugger Robot// 1. Redistributions of source code must retain the above copyright 9*16467b97STreehugger Robot// notice, this list of conditions and the following disclaimer. 10*16467b97STreehugger Robot// 2. Redistributions in binary form must reproduce the above copyright 11*16467b97STreehugger Robot// notice, this list of conditions and the following disclaimer in the 12*16467b97STreehugger Robot// documentation and/or other materials provided with the distribution. 13*16467b97STreehugger Robot// 3. The name of the author may not be used to endorse or promote products 14*16467b97STreehugger Robot// derived from this software without specific prior written permission. 15*16467b97STreehugger Robot// 16*16467b97STreehugger Robot// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17*16467b97STreehugger Robot// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18*16467b97STreehugger Robot// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19*16467b97STreehugger Robot// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20*16467b97STreehugger Robot// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21*16467b97STreehugger Robot// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22*16467b97STreehugger Robot// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23*16467b97STreehugger Robot// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24*16467b97STreehugger Robot// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25*16467b97STreehugger Robot// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26*16467b97STreehugger Robot 27*16467b97STreehugger Robot#import "TreeParser.h" 28*16467b97STreehugger Robot 29*16467b97STreehugger Robot@implementation TreeParser 30*16467b97STreehugger Robot 31*16467b97STreehugger Robot@synthesize input; 32*16467b97STreehugger Robot 33*16467b97STreehugger Robot+ (id) newTreeParser:(id<TreeNodeStream>)anInput 34*16467b97STreehugger Robot{ 35*16467b97STreehugger Robot return [[TreeParser alloc] initWithStream:anInput]; 36*16467b97STreehugger Robot} 37*16467b97STreehugger Robot 38*16467b97STreehugger Robot+ (id) newTreeParser:(id<TreeNodeStream>)anInput State:(RecognizerSharedState *)theState 39*16467b97STreehugger Robot{ 40*16467b97STreehugger Robot return [[TreeParser alloc] initWithStream:anInput State:theState]; 41*16467b97STreehugger Robot} 42*16467b97STreehugger Robot 43*16467b97STreehugger Robot- (id) initWithStream:(id<TreeNodeStream>)theInput 44*16467b97STreehugger Robot{ 45*16467b97STreehugger Robot if ((self = [super init]) != nil) { 46*16467b97STreehugger Robot [self setInput:theInput]; 47*16467b97STreehugger Robot } 48*16467b97STreehugger Robot return self; 49*16467b97STreehugger Robot} 50*16467b97STreehugger Robot 51*16467b97STreehugger Robot- (id) initWithStream:(id<TreeNodeStream>)theInput State:(RecognizerSharedState *)theState 52*16467b97STreehugger Robot{ 53*16467b97STreehugger Robot if ((self = [super init]) != nil) { 54*16467b97STreehugger Robot [self setInput:theInput]; 55*16467b97STreehugger Robot state = theState; 56*16467b97STreehugger Robot } 57*16467b97STreehugger Robot return self; 58*16467b97STreehugger Robot} 59*16467b97STreehugger Robot 60*16467b97STreehugger Robot- (void) dealloc 61*16467b97STreehugger Robot{ 62*16467b97STreehugger Robot#ifdef DEBUG_DEALLOC 63*16467b97STreehugger Robot NSLog( @"called dealloc in TreeParser" ); 64*16467b97STreehugger Robot#endif 65*16467b97STreehugger Robot if ( input ) [input release]; 66*16467b97STreehugger Robot [super dealloc]; 67*16467b97STreehugger Robot} 68*16467b97STreehugger Robot 69*16467b97STreehugger Robot- (void) reset 70*16467b97STreehugger Robot{ 71*16467b97STreehugger Robot [super reset]; // reset all recognizer state variables 72*16467b97STreehugger Robot if ( input != nil ) { 73*16467b97STreehugger Robot [input seek:0]; // rewind the input 74*16467b97STreehugger Robot } 75*16467b97STreehugger Robot} 76*16467b97STreehugger Robot 77*16467b97STreehugger Robot- (void) mismatch:(id<IntStream>)aStream tokenType:(TokenType)aTType follow:(ANTLRBitSet *)aBitset 78*16467b97STreehugger Robot{ 79*16467b97STreehugger Robot MismatchedTreeNodeException *mte = [MismatchedTreeNodeException newException:aTType Stream:aStream]; 80*16467b97STreehugger Robot [mte setNode:[((id<TreeNodeStream>)aStream) LT:1]]; 81*16467b97STreehugger Robot [self recoverFromMismatchedToken:aStream Type:aTType Follow:aBitset]; 82*16467b97STreehugger Robot} 83*16467b97STreehugger Robot 84*16467b97STreehugger Robot- (void) setTreeNodeStream:(id<TreeNodeStream>) anInput 85*16467b97STreehugger Robot{ 86*16467b97STreehugger Robot input = anInput; 87*16467b97STreehugger Robot} 88*16467b97STreehugger Robot 89*16467b97STreehugger Robot- (id<TreeNodeStream>) getTreeNodeStream 90*16467b97STreehugger Robot{ 91*16467b97STreehugger Robot return input; 92*16467b97STreehugger Robot} 93*16467b97STreehugger Robot 94*16467b97STreehugger Robot- (NSString *)getSourceName 95*16467b97STreehugger Robot{ 96*16467b97STreehugger Robot return [input getSourceName]; 97*16467b97STreehugger Robot} 98*16467b97STreehugger Robot 99*16467b97STreehugger Robot- (id) getCurrentInputSymbol:(id<IntStream>) anInput 100*16467b97STreehugger Robot{ 101*16467b97STreehugger Robot return [(id<TreeNodeStream>)anInput LT:1]; 102*16467b97STreehugger Robot} 103*16467b97STreehugger Robot 104*16467b97STreehugger Robot- (id) getMissingSymbol:(id<IntStream>)anInput 105*16467b97STreehugger Robot Exception:(RecognitionException *)e 106*16467b97STreehugger Robot ExpectedToken:(NSInteger)expectedTokenType 107*16467b97STreehugger Robot BitSet:(ANTLRBitSet *)follow 108*16467b97STreehugger Robot{ 109*16467b97STreehugger Robot NSString *tokenText =[NSString stringWithFormat:@"<missing %@ %d>", [self getTokenNames], expectedTokenType]; 110*16467b97STreehugger Robot //id<TreeAdaptor> anAdaptor = (id<TreeAdaptor>)[((id<TreeNodeStream>)e.input) getTreeAdaptor]; 111*16467b97STreehugger Robot //return [anAdaptor createToken:expectedTokenType Text:tokenText]; 112*16467b97STreehugger Robot return [CommonToken newToken:expectedTokenType Text:tokenText]; 113*16467b97STreehugger Robot} 114*16467b97STreehugger Robot 115*16467b97STreehugger Robot/** Match '.' in tree parser has special meaning. Skip node or 116*16467b97STreehugger Robot * entire tree if node has children. If children, scan until 117*16467b97STreehugger Robot * corresponding UP node. 118*16467b97STreehugger Robot */ 119*16467b97STreehugger Robot- (void) matchAny:(id<IntStream>)ignore 120*16467b97STreehugger Robot{ // ignore stream, copy of input 121*16467b97STreehugger Robot state.errorRecovery = NO; 122*16467b97STreehugger Robot state.failed = NO; 123*16467b97STreehugger Robot id look = [input LT:1]; 124*16467b97STreehugger Robot if ( [((CommonTreeAdaptor *)[input getTreeAdaptor]) getChildCount:look] == 0) { 125*16467b97STreehugger Robot [input consume]; // not subtree, consume 1 node and return 126*16467b97STreehugger Robot return; 127*16467b97STreehugger Robot } 128*16467b97STreehugger Robot // current node is a subtree, skip to corresponding UP. 129*16467b97STreehugger Robot // must count nesting level to get right UP 130*16467b97STreehugger Robot int level=0; 131*16467b97STreehugger Robot int tokenType = [((id<TreeAdaptor>)[input getTreeAdaptor]) getType:look]; 132*16467b97STreehugger Robot while ( tokenType != TokenTypeEOF && !( tokenType == TokenTypeUP && level == 0) ) { 133*16467b97STreehugger Robot [input consume]; 134*16467b97STreehugger Robot look = [input LT:1]; 135*16467b97STreehugger Robot tokenType = [((id<TreeAdaptor>)[input getTreeAdaptor]) getType:look]; 136*16467b97STreehugger Robot if ( tokenType == TokenTypeDOWN ) { 137*16467b97STreehugger Robot level++; 138*16467b97STreehugger Robot } 139*16467b97STreehugger Robot else if ( tokenType == TokenTypeUP ) { 140*16467b97STreehugger Robot level--; 141*16467b97STreehugger Robot } 142*16467b97STreehugger Robot } 143*16467b97STreehugger Robot [input consume]; // consume UP 144*16467b97STreehugger Robot} 145*16467b97STreehugger Robot 146*16467b97STreehugger Robot/** We have DOWN/UP nodes in the stream that have no line info; override. 147*16467b97STreehugger Robot * plus we want to alter the exception type. Don't try to recover 148*16467b97STreehugger Robot * from tree parser errors inline... 149*16467b97STreehugger Robot */ 150*16467b97STreehugger Robot- (id) recoverFromMismatchedToken:(id<IntStream>)anInput Type:(NSInteger)ttype Follow:(ANTLRBitSet *)follow 151*16467b97STreehugger Robot{ 152*16467b97STreehugger Robot @throw [MismatchedTreeNodeException newException:ttype Stream:anInput]; 153*16467b97STreehugger Robot} 154*16467b97STreehugger Robot 155*16467b97STreehugger Robot/** Prefix error message with the grammar name because message is 156*16467b97STreehugger Robot * always intended for the programmer because the parser built 157*16467b97STreehugger Robot * the input tree not the user. 158*16467b97STreehugger Robot */ 159*16467b97STreehugger Robot- (NSString *)getErrorHeader:(RecognitionException *)e 160*16467b97STreehugger Robot{ 161*16467b97STreehugger Robot return [NSString stringWithFormat:@"%@: node after line %@:%@", 162*16467b97STreehugger Robot [self getGrammarFileName], e.line, e.charPositionInLine]; 163*16467b97STreehugger Robot} 164*16467b97STreehugger Robot 165*16467b97STreehugger Robot/** Tree parsers parse nodes they usually have a token object as 166*16467b97STreehugger Robot * payload. Set the exception token and do the default behavior. 167*16467b97STreehugger Robot */ 168*16467b97STreehugger Robot- (NSString *)getErrorMessage:(RecognitionException *)e TokenNames:(AMutableArray *) theTokNams 169*16467b97STreehugger Robot{ 170*16467b97STreehugger Robot if ( [self isKindOfClass:[TreeParser class]] ) { 171*16467b97STreehugger Robot CommonTreeAdaptor *adaptor = (CommonTreeAdaptor *)[((id<TreeNodeStream>)e.input) getTreeAdaptor]; 172*16467b97STreehugger Robot e.token = [adaptor getToken:((CommonTree *)e.node)]; 173*16467b97STreehugger Robot if ( e.token == nil ) { // could be an UP/DOWN node 174*16467b97STreehugger Robot e.token = [CommonToken newToken:[adaptor getType:(CommonTree *)e.node] 175*16467b97STreehugger Robot Text:[adaptor getText:(CommonTree *)e.node]]; 176*16467b97STreehugger Robot } 177*16467b97STreehugger Robot } 178*16467b97STreehugger Robot return [super getErrorMessage:e TokenNames:theTokNams]; 179*16467b97STreehugger Robot} 180*16467b97STreehugger Robot 181*16467b97STreehugger Robot- (void) traceIn:(NSString *)ruleName Index:(NSInteger)ruleIndex 182*16467b97STreehugger Robot{ 183*16467b97STreehugger Robot [super traceIn:ruleName Index:ruleIndex Object:[input LT:1]]; 184*16467b97STreehugger Robot} 185*16467b97STreehugger Robot 186*16467b97STreehugger Robot- (void) traceOut:(NSString *)ruleName Index:(NSInteger)ruleIndex 187*16467b97STreehugger Robot{ 188*16467b97STreehugger Robot [super traceOut:ruleName Index:ruleIndex Object:[input LT:1]]; 189*16467b97STreehugger Robot} 190*16467b97STreehugger Robot 191*16467b97STreehugger Robot 192*16467b97STreehugger Robot@end 193