package jdk.nashorn.internal.runtime.regexp.joni.ast;
import java.util.Set;
import jdk.nashorn.internal.runtime.regexp.joni.Config;
import jdk.nashorn.internal.runtime.regexp.joni.WarnCallback;
import jdk.nashorn.internal.runtime.regexp.joni.constants.NodeType;
@SuppressWarnings("javadoc")
public abstract class Node implements NodeType {
public Node parent;
public abstract int getType();
public final int getType2Bit() {
return 1 << getType();
}
protected void setChild(final Node tgt) {
}
protected Node getChild() {
return null;
}
public void swap(final Node with) {
Node tmp;
if (parent != null) {
parent.setChild(with);
}
if (with.parent != null) {
with.parent.setChild(this);
}
tmp = parent;
parent = with.parent;
with.parent = tmp;
}
public void verifyTree(final Set<Node> set, final WarnCallback warnings) {
if (!set.contains(this) && getChild() != null) {
set.add(this);
if (getChild().parent != this) {
warnings.warn("broken link to child: " + this.getAddressName() + " -> " + getChild().getAddressName());
}
getChild().verifyTree(set, warnings);
}
}
public abstract String getName();
protected abstract String toString(int level);
public String getAddressName() {
return getName() + ":0x" + Integer.toHexString(System.identityHashCode(this));
}
@Override
public final String toString() {
final StringBuilder s = new StringBuilder();
s.append("<").append(getAddressName()).append(" (").append(parent == null ? "NULL" : parent.getAddressName()).append(")>");
return s + toString(0);
}
protected static String pad(final Object value, final int level) {
if (value == null) {
return "NULL";
}
final StringBuilder pad = new StringBuilder(" ");
for (int i=0; i<level; i++) {
pad.append(pad);
}
return value.toString().replace("\n", "\n" + pad);
}
public final boolean isInvalidQuantifier() {
if (!Config.VANILLA) {
return false;
}
ConsAltNode node;
switch(getType()) {
case ANCHOR:
return true;
case ENCLOSE:
break;
case LIST:
node = (ConsAltNode)this;
do {
if (!node.car.isInvalidQuantifier()) {
return false;
}
} while ((node = node.cdr) != null);
return false;
case ALT:
node = (ConsAltNode)this;
do {
if (node.car.isInvalidQuantifier()) {
return true;
}
} while ((node = node.cdr) != null);
break;
default:
break;
}
return false;
}
public final boolean isAllowedInLookBehind() {
return (getType2Bit() & ALLOWED_IN_LB) != 0;
}
public final boolean isSimple() {
return (getType2Bit() & SIMPLE) != 0;
}
}