// [The "BSD licence"] // Copyright (c) 2006-2007 Kay Roepke 2010 Alan Condit // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions // are met: // 1. Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // 2. Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // 3. The name of the author may not be used to endorse or promote products // derived from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #import "CommonTreeAdaptor.h" @implementation CommonTreeAdaptor + (CommonTree *) newEmptyTree; { return [CommonTree newTree]; } + (CommonTreeAdaptor *)newTreeAdaptor { return[[CommonTreeAdaptor alloc] init]; } - (id) init { self = [super init]; if (self) { } return self; } /** Duplicate a node. This is part of the factory; * override if you want another kind of node to be built. * * I could use reflection to prevent having to override this * but reflection is slow. */ - (id) dupNode:(id)t { if ( t==nil ) return nil; return [CommonTree newTree:t]; } /** Tell me how to create a token for use with imaginary token nodes. * For example, there is probably no input symbol associated with imaginary * token DECL, but you need to create it as a payload or whatever for * the DECL node as in ^(DECL type ID). * * This is a variant of createToken where the new token is derived from * an actual real input token. Typically this is for converting '{' * tokens to BLOCK etc... You'll see * * r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ; * * If you care what the token payload objects' type is, you should * override this method and any other createToken variant. */ - (CommonTree *) create:(CommonToken *)aToken { return [CommonTree newTreeWithToken:aToken]; } /** Tell me how to create a token for use with imaginary token nodes. * For example, there is probably no input symbol associated with imaginary * token DECL, but you need to create it as a payload or whatever for * the DECL node as in ^(DECL type ID). * * If you care what the token payload objects' type is, you should * override this method and any other createToken variant. */ - (CommonTree *)createTree:(NSInteger)tokenType Text:(NSString *)text { return [CommonTree newTreeWithTokenType:tokenType Text:text]; } - (id)createToken:(NSInteger)tokenType Text:(NSString *)text { id fromToken = [CommonToken newToken:tokenType Text:text]; return fromToken; } - (id)createToken:(id)fromToken { return [CommonToken newTokenWithToken:(CommonToken *)fromToken]; } /** Track start/stop token for subtree root created for a rule. * Only works with Tree nodes. For rules that match nothing, * seems like this will yield start=i and stop=i-1 in a nil node. * Might be useful info so I'll not force to be i..i. */ - (void) setTokenBoundaries:(id)aTree From:(id)startToken To:(id)stopToken { if ( aTree == nil ) return; int startTokIdx = 0; int stopTokIdx = 0; if ( startToken != nil ) startTokIdx = [startToken getTokenIndex]; if ( stopToken != nil ) stopTokIdx = [stopToken getTokenIndex]; [(id)aTree setTokenStartIndex:startTokIdx]; [(id)aTree setTokenStopIndex:stopTokIdx]; } - (NSInteger)getTokenStartIndex:(id) t { if ( t == nil ) return -1; return [(id)t getTokenStartIndex]; } - (NSInteger)getTokenStopIndex:(id) t { if ( t == nil ) return -1; return [(id)t getTokenStopIndex]; } - (NSString *)getText:(CommonTree *)t { if ( t == nil ) return nil; return t.token.text; } - (void)setText:(id)t Text:(NSString *)text { if ( t == nil ) return; } - (NSInteger)getType:(CommonTree *)t { if ( t==nil ) return TokenTypeInvalid; return t.token.type; } - (void) setType:(id)t Type:(NSInteger)tokenType { if ( t==nil ) return; } /** What is the Token associated with this node? If * you are not using CommonTree, then you must * override this in your own adaptor. */ - (id) getToken:(CommonTree *) t { if ( [t isKindOfClass:[CommonTree class]] ) { return t.token; } return nil; // no idea what to do } - (id) getChild:(id)t At:(NSInteger)i { if ( t == nil ) return nil; return [(id)t getChild:i]; } - (void) setChild:(id)t At:(NSInteger)i Child:(id)child { if ( t == nil ) return; [(id)t setChild:i With:child]; } - (id) deleteChild:(id)t Index:(NSInteger)anIndex { return [t deleteChild:anIndex]; } - (NSInteger) getChildCount:(id) t { if ( t == nil ) return 0; return [(id) t getChildCount]; } - (id) getParent:(id) t { if ( t == nil ) return nil; return (id)[t getParent]; } - (void) setParent:(id)t With:(id) parent { if ( t != nil ) [(id) t setParent:(id)parent]; } - (NSInteger) getChildIndex:(id) t { if ( t == nil ) return 0; return [(id) t getChildIndex]; } - (void) setChildIndex:(id)t With:(NSInteger)anIndex { if ( t!=nil ) [(id)t setChildIndex:anIndex]; } - (void) replaceChildren:(id)parent From:(NSInteger)startChildIndex To:(NSInteger)stopChildIndex With:(id)t { if ( parent != nil ) { [(id)parent replaceChildrenFrom:startChildIndex To:stopChildIndex With:t]; } } - (id) copyWithZone:(NSZone *)aZone { return [[[self class] allocWithZone:aZone] init]; } @end