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