);
}
final AstNode oldParent = _parent;
final AstNode oldSuccessor = _nextSibling;
final Role oldRole = this.getRole();
remove();
final AstNode replacement = replaceFunction.apply(this);
if (oldSuccessor != null && oldSuccessor._parent != oldParent) {
throw new IllegalStateException("Replace function changed next sibling of node being replaced.");
}
if (replacement != null && !replacement.isNull()) {
if (replacement._parent != null) {
throw new IllegalStateException("replace function must return the root of a tree");
}
if (!oldRole.isValid(replacement)) {
throw new IllegalStateException(
String.format(
"The new node '%s' is not valid in the role %s.",
replacement.getClass().getSimpleName(),
oldRole