1 // [The "BSD licence"] 2 // Copyright (c) 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 <Foundation/Foundation.h> 28 #import "TreeAdaptor.h" 29 #import "CommonErrorNode.h" 30 #import "UniqueIDMap.h" 31 32 @interface BaseTreeAdaptor : NSObject <TreeAdaptor, NSCopying> { 33 UniqueIDMap *treeToUniqueIDMap; 34 NSInteger uniqueNodeID; 35 } 36 37 - (id) init; 38 39 - (id) copyWithZone:(NSZone *)aZone; 40 41 - (id) emptyNode; 42 43 - (id) createNil; 44 45 /** create tree node that holds the start and stop tokens associated 46 * with an error. 47 * 48 * If you specify your own kind of tree nodes, you will likely have to 49 * override this method. CommonTree returns Token.INVALID_TOKEN_TYPE 50 * if no token payload but you might have to set token type for diff 51 * node type. 52 * 53 * You don't have to subclass CommonErrorNode; you will likely need to 54 * subclass your own tree node class to avoid class cast exception. 55 */ 56 - (id) errorNode:(id<TokenStream>)anInput 57 From:(id<Token>)startToken 58 To:(id<Token>)stopToken 59 Exception:(NSException *) e; 60 61 - (BOOL) isNil:(id<BaseTree>) aTree; 62 63 - (id<BaseTree>)dupTree:(id<BaseTree>)aTree; 64 65 /** This is generic in the sense that it will work with any kind of 66 * tree (not just Tree interface). It invokes the adaptor routines 67 * not the tree node routines to do the construction. 68 */ 69 - (id<BaseTree>)dupTree:(id<BaseTree>)aTree Parent:(id<BaseTree>)parent; 70 - (id<BaseTree>)dupNode:(id<BaseTree>)aNode; 71 /** Add a child to the tree t. If child is a flat tree (a list), make all 72 * in list children of t. Warning: if t has no children, but child does 73 * and child isNil then you can decide it is ok to move children to t via 74 * t.children = child.children; i.e., without copying the array. Just 75 * make sure that this is consistent with have the user will build 76 * ASTs. 77 */ 78 - (void) addChild:(id<BaseTree>)aChild toTree:(id<BaseTree>)aTree; 79 80 /** If oldRoot is a nil root, just copy or move the children to newRoot. 81 * If not a nil root, make oldRoot a child of newRoot. 82 * 83 * old=^(nil a b c), new=r yields ^(r a b c) 84 * old=^(a b c), new=r yields ^(r ^(a b c)) 85 * 86 * If newRoot is a nil-rooted single child tree, use the single 87 * child as the new root node. 88 * 89 * old=^(nil a b c), new=^(nil r) yields ^(r a b c) 90 * old=^(a b c), new=^(nil r) yields ^(r ^(a b c)) 91 * 92 * If oldRoot was null, it's ok, just return newRoot (even if isNil). 93 * 94 * old=null, new=r yields r 95 * old=null, new=^(nil r) yields ^(nil r) 96 * 97 * Return newRoot. Throw an exception if newRoot is not a 98 * simple node or nil root with a single child node--it must be a root 99 * node. If newRoot is ^(nil x) return x as newRoot. 100 * 101 * Be advised that it's ok for newRoot to point at oldRoot's 102 * children; i.e., you don't have to copy the list. We are 103 * constructing these nodes so we should have this control for 104 * efficiency. 105 */ 106 - (id<BaseTree>)becomeRoot:(id<BaseTree>)aNewRoot old:(id<BaseTree>)oldRoot; 107 108 /** Transform ^(nil x) to x and nil to null */ 109 - (id<BaseTree>)rulePostProcessing:(id<BaseTree>)aRoot; 110 111 - (id<BaseTree>)becomeRootfromToken:(id<Token>)aNewRoot old:(id<BaseTree>)oldRoot; 112 113 - (id<BaseTree>) create:(id<Token>)payload; 114 - (id<BaseTree>) createTree:(NSInteger)aTType FromToken:(id<Token>)aFromToken; 115 - (id<BaseTree>) createTree:(NSInteger)aTType FromToken:(id<Token>)aFromToken Text:(NSString *)theText; 116 - (id<BaseTree>) createTree:(NSInteger)aTType Text:(NSString *)theText; 117 118 - (NSInteger) getType:(id<BaseTree>)aTree; 119 120 - (void) setType:(id<BaseTree>)aTree Type:(NSInteger)type; 121 122 - (id<Token>)getToken:(CommonTree *)t; 123 124 - (NSString *)getText:(CommonTree *)aTree; 125 126 - (void) setText:(id<BaseTree>)aTree Text:(NSString *)theText; 127 128 - (id<BaseTree>) getChild:(id<BaseTree>)aTree At:(NSInteger)i; 129 130 - (void) setChild:(id<BaseTree>)aTree At:(NSInteger)index Child:(id<BaseTree>)aChild; 131 132 - (id<BaseTree>) deleteChild:(id<BaseTree>)aTree Index:(NSInteger)index; 133 134 - (NSInteger) getChildCount:(id<BaseTree>)aTree; 135 136 - (id<BaseTree>) getParent:(id<BaseTree>) t; 137 138 - (void) setParent:(id<BaseTree>)t With:(id<BaseTree>) parent; 139 140 /** What index is this node in the child list? Range: 0..n-1 141 * If your node type doesn't handle this, it's ok but the tree rewrites 142 * in tree parsers need this functionality. 143 */ 144 - (NSInteger) getChildIndex:(id)t; 145 - (void) setChildIndex:(id)t With:(NSInteger)index; 146 147 - (void) replaceChildren:(id)parent From:(NSInteger)startChildIndex To:(NSInteger)stopChildIndex With:(id)t; 148 149 - (NSInteger) getUniqueID:(id<BaseTree>)node; 150 151 #ifdef DONTUSENOMO 152 - (NSInteger) getUniqueID; 153 154 - (void) setUniqueNodeID:(NSInteger)aUniqueNodeID; 155 156 - (UniqueIDMap *)getTreeToUniqueIDMap; 157 158 - (void) setTreeToUniqueIDMap:(UniqueIDMap *)aMapNode; 159 #endif 160 161 /** Tell me how to create a token for use with imaginary token nodes. 162 * For example, there is probably no input symbol associated with imaginary 163 * token DECL, but you need to create it as a payload or whatever for 164 * the DECL node as in ^(DECL type ID). 165 * 166 * This is a variant of createToken where the new token is derived from 167 * an actual real input token. Typically this is for converting '{' 168 * tokens to BLOCK etc... You'll see 169 * 170 * r : lc='{' ID+ '}' -> ^(BLOCK[$lc] ID+) ; 171 * 172 * If you care what the token payload objects' type is, you should 173 * override this method and any other createToken variant. 174 */ 175 - (id<Token>)createToken:(NSInteger)aTType Text:(NSString *)theText; 176 177 - (id<Token>)createToken:(id<Token>)aFromToken; 178 179 @property (retain) UniqueIDMap *treeToUniqueIDMap; 180 @property (assign) NSInteger uniqueNodeID; 181 182 @end 183