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 */ 20 package jdk.nashorn.internal.runtime.regexp.joni.ast; 21 22 import java.util.Set; 23 24 import jdk.nashorn.internal.runtime.regexp.joni.Config; 25 import jdk.nashorn.internal.runtime.regexp.joni.WarnCallback; 26 import jdk.nashorn.internal.runtime.regexp.joni.constants.NodeType; 27 28 public abstract class Node implements NodeType { 29 public Node parent; 30 31 public abstract int getType(); 32 33 public final int getType2Bit() { 34 return 1 << getType(); 35 } 36 37 protected void setChild(Node tgt){} // default definition 38 protected Node getChild(){return null;}; // default definition 39 40 public void swap(Node with) { 41 Node tmp; 42 43 //if (getChild() != null) getChild().parent = with; 44 //if (with.getChild() != null) with.getChild().parent = this; 45 46 //tmp = getChild(); 47 //setChild(with.getChild()); 48 //with.setChild(tmp); 49 50 if (parent != null) parent.setChild(with); 51 52 if (with.parent != null) with.parent.setChild(this); 53 54 tmp = parent; 55 parent = with.parent; 56 with.parent = tmp; 57 } 58 59 // overridden by ConsAltNode and CallNode 60 public void verifyTree(Set<Node> set, WarnCallback warnings) { 61 if (!set.contains(this) && getChild() != null) { 62 set.add(this); 63 if (getChild().parent != this) { 64 warnings.warn("broken link to child: " + this.getAddressName() + " -> " + getChild().getAddressName()); 65 } 66 getChild().verifyTree(set, warnings); 67 } 68 } 69 70 public abstract String getName(); 71 protected abstract String toString(int level); 72 73 public String getAddressName() { 74 return getName() + ":0x" + Integer.toHexString(System.identityHashCode(this)); 75 } 76 77 public final String toString() { 78 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(Object value, int level) { 84 if (value == null) return "NULL"; 85 86 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 }