/// <summary> /// Adds a child without performing any safety checks. /// </summary> internal void AddChildUnsafe(AstNode child, Role role) { child.parent = this; child.SetRole(role); if (firstChild == null) { lastChild = firstChild = child; } else { lastChild.nextSibling = child; child.prevSibling = lastChild; lastChild = child; } }
void InsertChildBeforeUnsafe(AstNode nextSibling, AstNode child, Role role) { child.parent = this; child.SetRole(role); child.nextSibling = nextSibling; child.prevSibling = nextSibling.prevSibling; if (nextSibling.prevSibling != null) { Debug.Assert(nextSibling.prevSibling.nextSibling == nextSibling); nextSibling.prevSibling.nextSibling = child; } else { Debug.Assert(firstChild == nextSibling); firstChild = child; } nextSibling.prevSibling = child; }
/// <summary> /// Replaces this node with the new node. /// </summary> public void ReplaceWith(AstNode newNode) { if (newNode == null || newNode.IsNull) { Remove(); return; } if (newNode == this) { return; // nothing to do... } if (parent == null) { throw new InvalidOperationException(this.IsNull ? "Cannot replace the null nodes" : "Cannot replace the root node"); } ThrowIfFrozen(); // Because this method doesn't statically check the new node's type with the role, // we perform a runtime test: if (!this.Role.IsValid(newNode)) { throw new ArgumentException(string.Format("The new node '{0}' is not valid in the role {1}", newNode.GetType().Name, this.Role.ToString()), "newNode"); } if (newNode.parent != null) { // newNode is used within this tree? if (newNode.Ancestors.Contains(this)) { // e.g. "parenthesizedExpr.ReplaceWith(parenthesizedExpr.Expression);" // enable automatic removal newNode.Remove(); } else { throw new ArgumentException("Node is already used in another tree.", "newNode"); } } if (newNode.IsFrozen) { throw new ArgumentException("Cannot add a frozen node.", "newNode"); } newNode.parent = parent; newNode.SetRole(this.Role); newNode.prevSibling = prevSibling; newNode.nextSibling = nextSibling; if (prevSibling != null) { Debug.Assert(prevSibling.nextSibling == this); prevSibling.nextSibling = newNode; } else { Debug.Assert(parent.firstChild == this); parent.firstChild = newNode; } if (nextSibling != null) { Debug.Assert(nextSibling.prevSibling == this); nextSibling.prevSibling = newNode; } else { Debug.Assert(parent.lastChild == this); parent.lastChild = newNode; } parent = null; prevSibling = null; nextSibling = null; }