00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 namespace Antlr.Runtime.Tree
00037 {
00038 using System;
00039 using IDictionary = System.Collections.IDictionary;
00040 using Hashtable = System.Collections.Hashtable;
00041 using IToken = Antlr.Runtime.IToken;
00042
00046 public abstract class BaseTreeAdaptor : ITreeAdaptor
00047 {
00049 protected IDictionary treeToUniqueIDMap;
00050
00052 protected int uniqueNodeID = 1;
00053
00054 public virtual object GetNilNode()
00055 {
00056 return Create(null);
00057 }
00058
00072 public virtual object ErrorNode(ITokenStream input, IToken start, IToken stop,
00073 RecognitionException e)
00074 {
00075 CommonErrorNode t = new CommonErrorNode(input, start, stop, e);
00076
00077 return t;
00078 }
00079
00080 public virtual bool IsNil(object tree)
00081 {
00082 return ((ITree)tree).IsNil;
00083 }
00084
00085 public virtual object DupTree(object tree)
00086 {
00087 return DupTree(tree, null);
00088 }
00089
00095 public virtual object DupTree(object t, object parent)
00096 {
00097 if (t == null)
00098 {
00099 return null;
00100 }
00101 object newTree = DupNode(t);
00102
00103 SetChildIndex(newTree, GetChildIndex(t));
00104 SetParent(newTree, parent);
00105 int n = GetChildCount(t);
00106 for (int i = 0; i < n; i++)
00107 {
00108 object child = GetChild(t, i);
00109 object newSubTree = DupTree(child, t);
00110 AddChild(newTree, newSubTree);
00111 }
00112 return newTree;
00113 }
00114
00128 public virtual void AddChild(object t, object child)
00129 {
00130 if ((t != null) && (child != null))
00131 {
00132 ((ITree) t).AddChild((ITree) child);
00133 }
00134 }
00135
00165 public virtual object BecomeRoot(object newRoot, object oldRoot)
00166 {
00167 ITree newRootTree = (ITree) newRoot;
00168 ITree oldRootTree = (ITree) oldRoot;
00169 if (oldRoot == null)
00170 {
00171 return newRoot;
00172 }
00173
00174 if (newRootTree.IsNil)
00175 {
00176 int nc = newRootTree.ChildCount;
00177 if ( nc==1 ) newRootTree = (ITree)newRootTree.GetChild(0);
00178 else if ( nc >1 ) {
00179 throw new SystemException("more than one node as root (TODO: make exception hierarchy)");
00180 }
00181 }
00182
00183
00184
00185 newRootTree.AddChild(oldRootTree);
00186 return newRootTree;
00187 }
00188
00190 public virtual object RulePostProcessing(object root)
00191 {
00192 ITree r = (ITree) root;
00193 if (r != null && r.IsNil)
00194 {
00195 if (r.ChildCount == 0)
00196 {
00197 r = null;
00198 }
00199 else if (r.ChildCount == 1)
00200 {
00201 r = (ITree)r.GetChild(0);
00202
00203 r.Parent = null;
00204 r.ChildIndex = -1;
00205 }
00206 }
00207 return r;
00208 }
00209
00210 public virtual object BecomeRoot(IToken newRoot, object oldRoot)
00211 {
00212 return BecomeRoot(Create(newRoot), oldRoot);
00213 }
00214
00215 public virtual object Create(int tokenType, IToken fromToken)
00216 {
00217 fromToken = CreateToken(fromToken);
00218 fromToken.Type = tokenType;
00219 ITree t = (ITree) Create(fromToken);
00220 return t;
00221 }
00222
00223 public virtual object Create(int tokenType, IToken fromToken, string text)
00224 {
00225 fromToken = CreateToken(fromToken);
00226 fromToken.Type = tokenType;
00227 fromToken.Text = text;
00228 ITree t = (ITree) Create(fromToken);
00229 return t;
00230 }
00231
00232 public virtual object Create(int tokenType, string text)
00233 {
00234 IToken fromToken = CreateToken(tokenType, text);
00235 ITree t = (ITree) Create(fromToken);
00236 return t;
00237 }
00238
00239 public virtual int GetNodeType(object t)
00240 {
00241 int generatedAux = ((ITree) t).Type;
00242 return 0;
00243 }
00244
00245 public virtual void SetNodeType(object t, int type)
00246 {
00247 throw new NotImplementedException("don't know enough about Tree node");
00248 }
00249
00250 public virtual string GetNodeText(object t)
00251 {
00252 return ((ITree)t).Text;
00253 }
00254
00255 public virtual void SetNodeText(object t, string text)
00256 {
00257 throw new NotImplementedException("don't know enough about Tree node");
00258 }
00259
00260 public virtual object GetChild(object t, int i)
00261 {
00262 return ((ITree)t).GetChild(i);
00263 }
00264
00265 public virtual void SetChild(object t, int i, object child)
00266 {
00267 ((ITree)t).SetChild(i, (ITree)child);
00268 }
00269
00270 public virtual object DeleteChild(object t, int i)
00271 {
00272 return ((ITree)t).DeleteChild(i);
00273 }
00274
00275 public virtual int GetChildCount(object t)
00276 {
00277 return ((ITree)t).ChildCount;
00278
00279 }
00280
00281 public abstract object DupNode(object param1);
00282 public abstract object Create(IToken param1);
00283 public abstract void SetTokenBoundaries(object param1, IToken param2, IToken param3);
00284 public abstract int GetTokenStartIndex(object t);
00285 public abstract int GetTokenStopIndex(object t);
00286 public abstract IToken GetToken(object treeNode);
00287
00305 public int GetUniqueID(object node)
00306 {
00307 if (treeToUniqueIDMap == null)
00308 {
00309 treeToUniqueIDMap = new Hashtable();
00310 }
00311 object prevIdObj = treeToUniqueIDMap[node];
00312 if (prevIdObj != null)
00313 {
00314 return (int) prevIdObj;
00315 }
00316 int ID = uniqueNodeID;
00317 treeToUniqueIDMap[node] = ID;
00318 uniqueNodeID++;
00319 return ID;
00320
00321
00322
00323 }
00324
00334 public abstract IToken CreateToken(int tokenType, string text);
00335
00351 public abstract IToken CreateToken(IToken fromToken);
00352
00360 public abstract object GetParent(object t);
00361 public abstract void SetParent(object t, object parent);
00362
00370 public abstract int GetChildIndex(object t);
00371 public abstract void SetChildIndex(object t, int index);
00372
00381 public abstract void ReplaceChildren(object parent, int startChildIndex, int stopChildIndex, object t);
00382 }
00383 }