1 // [The "BSD licence"] 2 // Copyright (c) 2006-2007 Kay Roepke 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 "ANTLRToken.h" 28 #import "ANTLRBaseTree.h" 29 #import "ANTLRTokenStream.h" 30 31 #pragma warning tree/node diction is broken. 32 33 @protocol ANTLRTreeAdaptor <NSObject, NSCopying> 34 35 #pragma mark Construction 36 37 + (id<ANTLRTree>) newEmptyTree; 38 39 - (id<ANTLRTree>) createTree:(id<ANTLRToken>)payload; 40 41 #pragma mark ANTLRTreeAdaptor implementation 42 - (id<ANTLRTree>)dupNode:(id<ANTLRTree>)aNode; // copies just the node 43 - (id<ANTLRTree>)dupTree:(id<ANTLRTree>)aTree; // copies the entire subtree, recursively 44 45 /** Return a nil node (an empty but non-null node) that can hold 46 * a list of element as the children. If you want a flat tree (a list) 47 * use "t=adaptor.nil(); t.addChild(x); t.addChild(y);" 48 */ 49 - (id) emptyNode; 50 51 /** Return a tree node representing an error. This node records the 52 * tokens consumed during error recovery. The start token indicates the 53 * input symbol at which the error was detected. The stop token indicates 54 * the last symbol consumed during recovery. 55 * 56 * You must specify the input stream so that the erroneous text can 57 * be packaged up in the error node. The exception could be useful 58 * to some applications; default implementation stores ptr to it in 59 * the CommonErrorNode. 60 * 61 * This only makes sense during token parsing, not tree parsing. 62 * Tree parsing should happen only when parsing and tree construction 63 * succeed. 64 */ 65 - (id) errorNode:(id<ANTLRTokenStream>)anInput 66 From:(id<ANTLRToken>)aStartToken 67 To:(id<ANTLRToken>)aStopToken 68 Exception:(NSException *) e; 69 70 /** Is tree considered a nil node used to make lists of child nodes? */ 71 - (BOOL) isNil:(id<ANTLRTree>)aTree; 72 73 74 - (void) addChild:(id<ANTLRTree>)child toTree:(id<ANTLRTree>)aTree; 75 76 /** If oldRoot is a nil root, just copy or move the children to newRoot. 77 * If not a nil root, make oldRoot a child of newRoot. 78 * 79 * old=^(nil a b c), new=r yields ^(r a b c) 80 * old=^(a b c), new=r yields ^(r ^(a b c)) 81 * 82 * If newRoot is a nil-rooted single child tree, use the single 83 * child as the new root node. 84 * 85 * old=^(nil a b c), new=^(nil r) yields ^(r a b c) 86 * old=^(a b c), new=^(nil r) yields ^(r ^(a b c)) 87 * 88 * If oldRoot was null, it's ok, just return newRoot (even if isNil). 89 * 90 * old=null, new=r yields r 91 * old=null, new=^(nil r) yields ^(nil r) 92 * 93 * Return newRoot. Throw an exception if newRoot is not a 94 * simple node or nil root with a single child node--it must be a root 95 * node. If newRoot is ^(nil x) return x as newRoot. 96 * 97 * Be advised that it's ok for newRoot to point at oldRoot's 98 * children; i.e., you don't have to copy the list. We are 99 * constructing these nodes so we should have this control for 100 * efficiency. 101 */ 102 - (id) becomeRoot:(id<ANTLRTree>)newRoot old:(id<ANTLRTree>)oldRoot; 103 104 - (id) rulePostProcessing:(id<ANTLRTree>)root; 105 106 #pragma mark Rewrite Rules 107 108 - (NSUInteger) getUniqueID:(id<ANTLRTree>)aNode; 109 110 - (id<ANTLRTree>) createTree:(NSInteger)tokenType FromToken:(id<ANTLRToken>)fromToken; 111 - (id<ANTLRTree>) createTree:(NSInteger)tokenType FromToken:(id<ANTLRToken>)fromToken Text:(NSString *)text; 112 - (id<ANTLRTree>) createTree:(NSInteger)tokenType Text:(NSString *)text; 113 114 #pragma mark Content 115 116 - (id<ANTLRTree>)dupNode:(id<ANTLRTree>)aNode; 117 - (id<ANTLRTree>)dupTree:(id<ANTLRTree>)aTree; 118 119 - (NSInteger) getType:(id<ANTLRTree>)aNode; 120 - (void) setType:(id<ANTLRTree>)aNode Type:(NSInteger)tokenType; 121 122 - (NSString *) getText:(id<ANTLRTree>)aNode; 123 - (void) setText:(id<ANTLRTree>)aNode Text:(NSString *)tokenText; 124 125 - (id<ANTLRToken>) getToken:(id<ANTLRTree>)t; 126 127 - (void) setTokenBoundaries:(id<ANTLRTree>)aTree From:(id<ANTLRToken>)startToken To:(id<ANTLRToken>)stopToken; 128 - (NSInteger) getTokenStartIndex:(id<ANTLRTree>)aTree; 129 - (NSInteger) getTokenStopIndex:(id<ANTLRTree>)aTree; 130 131 #pragma mark Navigation / Tree Parsing 132 133 /** Get a child 0..n-1 node */ 134 - (id<ANTLRTree>) getChild:(id<ANTLRTree>)aNode At:(NSInteger) i; 135 /** Set ith child (0..n-1) to t; t must be non-null and non-nil node */ 136 - (void) setChild:(id<ANTLRTree>)aTree At:(NSInteger)index Child:(id<ANTLRTree>)child; 137 /** Remove ith child and shift children down from right. */ 138 - (id<ANTLRTree>) deleteChild:(id<ANTLRTree>)t Index:(NSInteger)index; 139 140 /** How many children? If 0, then this is a leaf node */ 141 - (NSInteger) getChildCount:(id<ANTLRTree>) aTree; 142 143 /** Who is the parent node of this node; if null, implies node is root. 144 * If your node type doesn't handle this, it's ok but the tree rewrites 145 * in tree parsers need this functionality. 146 */ 147 - (id<ANTLRTree>)getParent:(id<ANTLRTree>)t; 148 - (void) setParent:(id<ANTLRTree>)t With:(id<ANTLRTree>)parent; 149 150 /** What index is this node in the child list? Range: 0..n-1 151 * If your node type doesn't handle this, it's ok but the tree rewrites 152 * in tree parsers need this functionality. 153 */ 154 - (NSInteger) getChildIndex:(id<ANTLRTree>)t; 155 - (void) setChildIndex:(id<ANTLRTree>)t With:(NSInteger)index; 156 157 - (void) replaceChildren:(id<ANTLRTree>)parent From:(NSInteger)startChildIndex To:(NSInteger)stopChildIndex With:(id<ANTLRTree>)t; 158 159 @end 160