1// 2// TreeVisitorAction.m 3// ANTLR 4// 5// Created by Alan Condit on 6/18/10. 6// [The "BSD licence"] 7// Copyright (c) 2010 Alan Condit 8// All rights reserved. 9// 10// Redistribution and use in source and binary forms, with or without 11// modification, are permitted provided that the following conditions 12// are met: 13// 1. Redistributions of source code must retain the above copyright 14// notice, this list of conditions and the following disclaimer. 15// 2. Redistributions in binary form must reproduce the above copyright 16// notice, this list of conditions and the following disclaimer in the 17// documentation and/or other materials provided with the distribution. 18// 3. The name of the author may not be used to endorse or promote products 19// derived from this software without specific prior written permission. 20// 21// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 32#import "TreeVisitorAction.h" 33 34 35@implementation TreeVisitorAction 36 37+ (TreeVisitorAction *)newTreeVisitorAction 38{ 39 return [[TreeVisitorAction alloc] init]; 40} 41 42- (id) init 43{ 44 if ((self = [super init]) != nil ) { 45 preAction = nil; 46 postAction = nil; 47 } 48 return self; 49} 50 51- (void)setPreAction:(SEL)anAction 52{ 53 preAction = anAction; 54} 55 56- (void)setPostAction:(SEL)anAction 57{ 58 postAction = anAction; 59} 60 61/** Execute an action before visiting children of t. Return t or 62 * a rewritten t. It is up to the visitor to decide what to do 63 * with the return value. Children of returned value will be 64 * visited if using TreeVisitor.visit(). 65 */ 66- (id<BaseTree>)pre:(id<BaseTree>) t 67{ 68 if ( (preAction != nil ) && ( [self respondsToSelector:preAction] )) { 69 [self performSelector:preAction]; 70 return t; 71 } 72 return nil; 73} 74 75/** Execute an action after visiting children of t. Return t or 76 * a rewritten t. It is up to the visitor to decide what to do 77 * with the return value. 78 */ 79- (id<BaseTree>)post:(id<BaseTree>) t 80{ 81 if ( (postAction != nil ) && ( [self respondsToSelector:postAction] )) { 82 [self performSelector:postAction]; 83 return t; 84 } 85 return nil; 86} 87 88@synthesize preAction; 89@synthesize postAction; 90 91@end 92 93@implementation TreeVisitorActionFiltered 94 95+ (TreeVisitorAction *)newTreeVisitorActionFiltered:(TreeFilter *)aFilter 96 RuleD:(fptr *)aTDRule 97 RuleU:(fptr *)aBURule 98{ 99 return [[TreeVisitorActionFiltered alloc] initWithFilter:aFilter RuleD:aTDRule RuleU:aBURule]; 100} 101 102- (id) initWithFilter:(TreeFilter *)aFilter 103 RuleD:(fptr *)aTDRule 104 RuleU:(fptr *)aBURule 105{ 106 if (( self = [super init] ) != nil ) { 107 aTFilter = aFilter; 108 TDRule = aTDRule; 109 BURule = aBURule; 110 } 111 return self; 112} 113 114/** Execute an action before visiting children of t. Return t or 115 * a rewritten t. It is up to the visitor to decide what to do 116 * with the return value. Children of returned value will be 117 * visited if using TreeVisitor.visit(). 118 */ 119- (id<BaseTree>)pre:(id<BaseTree>) t 120{ 121 [aTFilter applyOnce:t rule:(fptr *)TDRule]; 122 return t; 123} 124 125/** Execute an action after visiting children of t. Return t or 126 * a rewritten t. It is up to the visitor to decide what to do 127 * with the return value. 128 */ 129- (id<BaseTree>)post:(id<BaseTree>) t 130{ 131 [aTFilter applyOnce:t rule:(fptr *)BURule]; 132 return t; 133} 134 135 136 137@synthesize aTFilter; 138 139@end 140 141