ConsAltNode.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.WarnCallback; 24import jdk.nashorn.internal.runtime.regexp.joni.exception.ErrorMessages; 25import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException; 26 27public final class ConsAltNode extends Node { 28 public Node car; 29 public ConsAltNode cdr; 30 private int type; // List or Alt 31 32 private ConsAltNode(final Node car, final ConsAltNode cdr, final int type) { 33 this.car = car; 34 if (car != null) car.parent = this; 35 this.cdr = cdr; 36 if (cdr != null) cdr.parent = this; 37 38 this.type = type; 39 } 40 41 public static ConsAltNode newAltNode(final Node left, final ConsAltNode right) { 42 return new ConsAltNode(left, right, ALT); 43 } 44 45 public static ConsAltNode newListNode(final Node left, final ConsAltNode right) { 46 return new ConsAltNode(left, right, LIST); 47 } 48 49 public static ConsAltNode listAdd(ConsAltNode list, final Node x) { 50 final ConsAltNode n = newListNode(x, null); 51 52 if (list != null) { 53 while (list.cdr != null) { 54 list = list.cdr; 55 } 56 list.setCdr(n); 57 } 58 return n; 59 } 60 61 public void toListNode() { 62 type = LIST; 63 } 64 65 public void toAltNode() { 66 type = ALT; 67 } 68 69 @Override 70 public int getType() { 71 return type; 72 } 73 74 @Override 75 protected void setChild(final Node newChild) { 76 car = newChild; 77 } 78 79 @Override 80 protected Node getChild() { 81 return car; 82 } 83 84 @Override 85 public void swap(final Node with) { 86 if (cdr != null) { 87 cdr.parent = with; 88 if (with instanceof ConsAltNode) { 89 final ConsAltNode withCan = (ConsAltNode)with; 90 withCan.cdr.parent = this; 91 final ConsAltNode tmp = cdr; 92 cdr = withCan.cdr; 93 withCan.cdr = tmp; 94 } 95 } 96 super.swap(with); 97 } 98 99 @Override 100 public void verifyTree(final Set<Node> set, final WarnCallback warnings) { 101 if (!set.contains(this)) { 102 set.add(this); 103 if (car != null) { 104 if (car.parent != this) { 105 warnings.warn("broken list car: " + this.getAddressName() + " -> " + car.getAddressName()); 106 } 107 car.verifyTree(set,warnings); 108 } 109 if (cdr != null) { 110 if (cdr.parent != this) { 111 warnings.warn("broken list cdr: " + this.getAddressName() + " -> " + cdr.getAddressName()); 112 } 113 cdr.verifyTree(set,warnings); 114 } 115 } 116 } 117 118 public Node setCar(final Node ca) { 119 car = ca; 120 ca.parent = this; 121 return car; 122 } 123 124 public ConsAltNode setCdr(final ConsAltNode cd) { 125 cdr = cd; 126 cd.parent = this; 127 return cdr; 128 } 129 130 @Override 131 public String getName() { 132 switch (type) { 133 case ALT: 134 return "Alt"; 135 case LIST: 136 return "List"; 137 default: 138 throw new InternalException(ErrorMessages.ERR_PARSER_BUG); 139 } 140 } 141 142 @Override 143 public String toString(final int level) { 144 final StringBuilder value = new StringBuilder(); 145 value.append("\n car: " + pad(car, level + 1)); 146 value.append("\n cdr: " + (cdr == null ? "NULL" : cdr.toString())); 147 148 return value.toString(); 149 } 150 151} 152