Node.java revision 953:221a84ef44c0
1/* 2 * Permission is hereby granted, free of charge, to any person obtaining a copy of 3 * this software and associated documentation files (the "Software"), to deal in 4 * the Software without restriction, including without limitation the rights to 5 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 6 * of the Software, and to permit persons to whom the Software is furnished to do 7 * so, subject to the following conditions: 8 * 9 * The above copyright notice and this permission notice shall be included in all 10 * copies or substantial portions of the Software. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 15 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 * SOFTWARE. 19 */ 20package jdk.nashorn.internal.runtime.regexp.joni.ast; 21 22import java.util.Set; 23import jdk.nashorn.internal.runtime.regexp.joni.Config; 24import jdk.nashorn.internal.runtime.regexp.joni.WarnCallback; 25import jdk.nashorn.internal.runtime.regexp.joni.constants.NodeType; 26 27public abstract class Node implements NodeType { 28 public Node parent; 29 30 public abstract int getType(); 31 32 public final int getType2Bit() { 33 return 1 << getType(); 34 } 35 36 protected void setChild(final Node tgt){} // default definition 37 protected Node getChild(){return null;} // default definition 38 39 public void swap(final Node with) { 40 Node tmp; 41 42 //if (getChild() != null) getChild().parent = with; 43 //if (with.getChild() != null) with.getChild().parent = this; 44 45 //tmp = getChild(); 46 //setChild(with.getChild()); 47 //with.setChild(tmp); 48 49 if (parent != null) parent.setChild(with); 50 51 if (with.parent != null) with.parent.setChild(this); 52 53 tmp = parent; 54 parent = with.parent; 55 with.parent = tmp; 56 } 57 58 // overridden by ConsAltNode and CallNode 59 public void verifyTree(final Set<Node> set, final WarnCallback warnings) { 60 if (!set.contains(this) && getChild() != null) { 61 set.add(this); 62 if (getChild().parent != this) { 63 warnings.warn("broken link to child: " + this.getAddressName() + " -> " + getChild().getAddressName()); 64 } 65 getChild().verifyTree(set, warnings); 66 } 67 } 68 69 public abstract String getName(); 70 protected abstract String toString(int level); 71 72 public String getAddressName() { 73 return getName() + ":0x" + Integer.toHexString(System.identityHashCode(this)); 74 } 75 76 @Override 77 public final String toString() { 78 final StringBuilder s = new StringBuilder(); 79 s.append("<" + getAddressName() + " (" + (parent == null ? "NULL" : parent.getAddressName()) + ")>"); 80 return s + toString(0); 81 } 82 83 protected static String pad(final Object value, final int level) { 84 if (value == null) return "NULL"; 85 86 final StringBuilder pad = new StringBuilder(" "); 87 for (int i=0; i<level; i++) pad.append(pad); 88 89 return value.toString().replace("\n", "\n" + pad); 90 } 91 92 public final boolean isInvalidQuantifier() { 93 if (!Config.VANILLA) return false; 94 95 ConsAltNode node; 96 97 switch(getType()) { 98 99 case ANCHOR: 100 return true; 101 102 case ENCLOSE: 103 /* allow enclosed elements */ 104 /* return is_invalid_quantifier_target(NENCLOSE(node)->target); */ 105 break; 106 107 case LIST: 108 node = (ConsAltNode)this; 109 do { 110 if (!node.car.isInvalidQuantifier()) return false; 111 } while ((node = node.cdr) != null); 112 return false; 113 114 case ALT: 115 node = (ConsAltNode)this; 116 do { 117 if (node.car.isInvalidQuantifier()) return true; 118 } while ((node = node.cdr) != null); 119 break; 120 121 default: 122 break; 123 } 124 125 return false; 126 } 127 128 public final boolean isAllowedInLookBehind() { 129 return (getType2Bit() & ALLOWED_IN_LB) != 0; 130 } 131 132 public final boolean isSimple() { 133 return (getType2Bit() & SIMPLE) != 0; 134 } 135} 136