1 // 2 // TreeWizard.h 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 <Foundation/Foundation.h> 33 #import "CommonTreeAdaptor.h" 34 #import "CommonTree.h" 35 #import "MapElement.h" 36 #import "Map.h" 37 #import "AMutableArray.h" 38 39 @class ANTLRVisitor; 40 41 @protocol ANTLRContextVisitor <NSObject> 42 // TODO: should this be called visit or something else? 43 - (void) visit:(CommonTree *)t Parent:(CommonTree *)parent ChildIndex:(NSInteger)childIndex Map:(Map *)labels; 44 45 @end 46 47 @interface ANTLRVisitor : NSObject <ANTLRContextVisitor> { 48 NSInteger action; 49 id actor; 50 id object1; 51 id object2; 52 } 53 + (ANTLRVisitor *)newANTLRVisitor:(NSInteger)anAction Actor:(id)anActor Object:(id)anObject1 Object:(id)anObject2; 54 - (id) initWithAction:(NSInteger)anAction Actor:(id)anActor Object:(id)anObject1 Object:(id)anObject2; 55 56 - (void) visit:(CommonTree *)t; 57 - (void) visit:(CommonTree *)t Parent:(CommonTree *)parent ChildIndex:(NSInteger)childIndex Map:(Map *)labels; 58 59 @property NSInteger action; 60 @property (retain) id actor; 61 @property (retain) id object1; 62 @property (retain) id object2; 63 @end 64 65 /** When using %label:TOKENNAME in a tree for parse(), we must 66 * track the label. 67 */ 68 @interface TreePattern : CommonTree { 69 NSString *label; 70 BOOL hasTextArg; 71 } 72 @property (retain, getter=getLabel, setter=setLabel:) NSString *label; 73 @property (assign, getter=getHasTextArg, setter=setHasTextArg:) BOOL hasTextArg; 74 75 + (CommonTree *)newTreePattern:(id<Token>)payload; 76 77 - (id) initWithToken:(id<Token>)payload; 78 - (NSString *)toString; 79 @end 80 81 @interface ANTLRWildcardTreePattern : TreePattern { 82 } 83 84 + (ANTLRWildcardTreePattern *)newANTLRWildcardTreePattern:(id<Token>)payload; 85 - (id) initWithToken:(id<Token>)payload; 86 @end 87 88 /** This adaptor creates TreePattern objects for use during scan() */ 89 @interface TreePatternTreeAdaptor : CommonTreeAdaptor { 90 } 91 + (TreePatternTreeAdaptor *)newTreeAdaptor; 92 - (id) init; 93 - (CommonTree *)createTreePattern:(id<Token>)payload; 94 95 @end 96 97 @interface TreeWizard : NSObject { 98 id<TreeAdaptor> adaptor; 99 Map *tokenNameToTypeMap; 100 } 101 + (TreeWizard *) newTreeWizard:(id<TreeAdaptor>)anAdaptor; 102 + (TreeWizard *)newTreeWizard:(id<TreeAdaptor>)adaptor Map:(Map *)aTokenNameToTypeMap; 103 + (TreeWizard *)newTreeWizard:(id<TreeAdaptor>)adaptor TokenNames:(NSArray *)theTokNams; 104 + (TreeWizard *)newTreeWizardWithTokenNames:(NSArray *)theTokNams; 105 - (id) init; 106 - (id) initWithAdaptor:(id<TreeAdaptor>)adaptor; 107 - (id) initWithAdaptor:(id<TreeAdaptor>)adaptor Map:(Map *)tokenNameToTypeMap; 108 - (id) initWithTokenNames:(NSArray *)theTokNams; 109 - (id) initWithTokenNames:(id<TreeAdaptor>)anAdaptor TokenNames:(NSArray *)theTokNams; 110 - (void) dealloc; 111 - (Map *)computeTokenTypes:(NSArray *)theTokNams; 112 - (NSInteger)getTokenType:(NSString *)tokenName; 113 - (Map *)index:(CommonTree *)t; 114 - (void) _index:(CommonTree *)t Map:(Map *)m; 115 - (AMutableArray *)find:(CommonTree *) t Pattern:(NSString *)pattern; 116 - (TreeWizard *)findFirst:(CommonTree *) t Type:(NSInteger)ttype; 117 - (TreeWizard *)findFirst:(CommonTree *) t Pattern:(NSString *)pattern; 118 - (void) visit:(CommonTree *)t Type:(NSInteger)ttype Visitor:(ANTLRVisitor *)visitor; 119 - (void) _visit:(CommonTree *)t 120 Parent:(CommonTree *)parent 121 ChildIndex:(NSInteger)childIndex 122 Type:(NSInteger)ttype 123 Visitor:(ANTLRVisitor *)visitor; 124 - (void)visit:(CommonTree *)t Pattern:(NSString *)pattern Visitor:(ANTLRVisitor *)visitor; 125 - (BOOL)parse:(CommonTree *)t Pattern:(NSString *)pattern Map:(Map *)labels; 126 - (BOOL) parse:(CommonTree *) t Pattern:(NSString *)pattern; 127 - (BOOL) _parse:(CommonTree *)t1 Pattern:(CommonTree *)tpattern Map:(Map *)labels; 128 - (CommonTree *) createTree:(NSString *)pattern; 129 - (BOOL)equals:(id)t1 O2:(id)t2 Adaptor:(id<TreeAdaptor>)anAdaptor; 130 - (BOOL)equals:(id)t1 O2:(id)t2; 131 - (BOOL) _equals:(id)t1 O2:(id)t2 Adaptor:(id<TreeAdaptor>)anAdaptor; 132 133 @property (retain) id<TreeAdaptor> adaptor; 134 @property (retain) Map *tokenNameToTypeMap; 135 @end 136 137