Node.java revision 1350:3cb11f4d617e
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
27@SuppressWarnings("javadoc")
28public 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(final Node tgt) {
38        //empty, default definition
39    }
40    protected Node getChild() {
41        return null; // default definition
42        }
43
44    public void swap(final Node with) {
45        Node tmp;
46
47        //if (getChild() != null) getChild().parent = with;
48        //if (with.getChild() != null) with.getChild().parent = this;
49
50        //tmp = getChild();
51        //setChild(with.getChild());
52        //with.setChild(tmp);
53
54        if (parent != null) {
55            parent.setChild(with);
56        }
57
58        if (with.parent != null) {
59            with.parent.setChild(this);
60        }
61
62        tmp = parent;
63        parent = with.parent;
64        with.parent = tmp;
65    }
66
67    // overridden by ConsAltNode and CallNode
68    public void verifyTree(final Set<Node> set, final WarnCallback warnings) {
69        if (!set.contains(this) && getChild() != null) {
70            set.add(this);
71            if (getChild().parent != this) {
72                warnings.warn("broken link to child: " + this.getAddressName() + " -> " + getChild().getAddressName());
73            }
74            getChild().verifyTree(set, warnings);
75        }
76    }
77
78    public abstract String getName();
79    protected abstract String toString(int level);
80
81    public String getAddressName() {
82        return getName() + ":0x" + Integer.toHexString(System.identityHashCode(this));
83    }
84
85    @Override
86    public final String toString() {
87        final StringBuilder s = new StringBuilder();
88        s.append("<").append(getAddressName()).append(" (").append(parent == null ? "NULL" : parent.getAddressName()).append(")>");
89        return s + toString(0);
90    }
91
92    protected static String pad(final Object value, final int level) {
93        if (value == null) {
94            return "NULL";
95        }
96
97        final StringBuilder pad = new StringBuilder("  ");
98        for (int i=0; i<level; i++) {
99            pad.append(pad);
100        }
101
102        return value.toString().replace("\n",  "\n" + pad);
103    }
104
105    public final boolean isInvalidQuantifier() {
106        if (!Config.VANILLA) {
107            return false;
108        }
109
110        ConsAltNode node;
111
112        switch(getType()) {
113
114        case ANCHOR:
115            return true;
116
117        case ENCLOSE:
118            /* allow enclosed elements */
119            /* return is_invalid_quantifier_target(NENCLOSE(node)->target); */
120            break;
121
122        case LIST:
123            node = (ConsAltNode)this;
124            do {
125                if (!node.car.isInvalidQuantifier()) {
126                    return false;
127                }
128            } while ((node = node.cdr) != null);
129            return false;
130
131        case ALT:
132            node = (ConsAltNode)this;
133            do {
134                if (node.car.isInvalidQuantifier()) {
135                    return true;
136                }
137            } while ((node = node.cdr) != null);
138            break;
139
140        default:
141            break;
142        }
143
144        return false;
145    }
146
147    public final boolean isAllowedInLookBehind() {
148        return (getType2Bit() & ALLOWED_IN_LB) != 0;
149    }
150
151    public final boolean isSimple() {
152        return (getType2Bit() & SIMPLE) != 0;
153    }
154}
155