1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.jexl2.parser;
18
19 /**
20 * A class originally generated by JJTree with the following JavaCCOptions:
21 * MULTI=true,NODE_USES_PARSER=true,VISITOR=true,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=
22 *
23 * Works around issue https://javacc.dev.java.net/issues/show_bug.cgi?id=227
24 * As soon as this issue if fixed and the maven plugin uses the correct version of Javacc, this
25 * class can go away.
26 *
27 * The technical goal is to ensure every reference made in the parser was to a JexlNode; unfortunately,
28 * as in javacc 4.1, it still uses a SimpleNode reference in the generated ParserVisitor.
29 * Besides, there is no need to keep the parser around in the node.
30 *
31 * The functional goal is to a allow a <em>volatile</em> value in the node
32 * so it can serve as a last evaluation cache even in multi-threaded executions.
33 */
34 public class SimpleNode implements Node {
35 /** The parent node. */
36 protected JexlNode parent;
37 /** The array of children nodes. */
38 protected JexlNode[] children;
39 /** The node type id. */
40 protected final int id;
41 /** volatile value so it can be used as a last evaluation cache. */
42 protected volatile Object value;
43
44 /**
45 * Creates a SimpleNode instance.
46 * @param i the node type identifier
47 */
48 public SimpleNode(int i) {
49 id = i;
50 }
51
52 /**
53 * Creates a SimpleNode instance.
54 * @param p the parser instance
55 * @param i the node type identifier
56 */
57 public SimpleNode(Parser p, int i) {
58 this(i);
59 }
60
61 /** {@inheritDoc} */
62 public void jjtOpen() {
63 }
64
65 /** {@inheritDoc} */
66 public void jjtClose() {
67 }
68
69 /**
70 * Sets this node's parent.
71 * @param n the parent
72 */
73 public void jjtSetParent(Node n) {
74 parent = (JexlNode) n;
75 }
76
77 /**
78 * Gets this node's parent.
79 * @return the parent node
80 */
81 public JexlNode jjtGetParent() {
82 return parent;
83 }
84
85 /** Adds a child node.
86 * @param n the child node
87 * @param i the child offset
88 */
89 public void jjtAddChild(Node n, int i) {
90 if (children == null) {
91 children = new JexlNode[i + 1];
92 } else if (i >= children.length) {
93 JexlNode[] c = new JexlNode[i + 1];
94 System.arraycopy(children, 0, c, 0, children.length);
95 children = c;
96 }
97 children[i] = (JexlNode) n;
98 }
99
100 /**
101 * Gets a child of this node.
102 * @param i the child offset
103 * @return the child node
104 */
105 public JexlNode jjtGetChild(int i) {
106 return children[i];
107 }
108
109 /**
110 * Gets this node number of children.
111 * @return the number of children
112 */
113 public int jjtGetNumChildren() {
114 return (children == null) ? 0 : children.length;
115 }
116
117 /** Sets this node value.
118 * @param value
119 */
120 public void jjtSetValue(Object value) {
121 this.value = value;
122 }
123
124 /** Gets this node value.
125 * @return value
126 */
127 public Object jjtGetValue() {
128 return value;
129 }
130
131 /**
132 * Accept the visitor.
133 * @param visitor the visitor
134 * @param data contextual data
135 * @return result of visit
136 **/
137 public Object jjtAccept(ParserVisitor visitor, Object data) {
138 return visitor.visit(this, data);
139 }
140
141 /**
142 * Accept the visitor on all this node's children.
143 * @param visitor the visitor
144 * @param data contextual data
145 * @return result of visit
146 **/
147 public Object childrenAccept(ParserVisitor visitor, Object data) {
148 if (children != null) {
149 for (int i = 0; i < children.length; ++i) {
150 children[i].jjtAccept(visitor, data);
151 }
152 }
153 return data;
154 }
155
156 /* You can override these two methods in subclasses of SimpleNode to
157 customize the way the JexlNode appears when the tree is dumped. If
158 your output uses more than one line you should override
159 toString(String), otherwise overriding toString() is probably all
160 you need to do. */
161 @Override
162 public String toString() {
163 return ParserTreeConstants.jjtNodeName[id];
164 }
165
166 public String toString(String prefix) {
167 return prefix + toString();
168 }
169
170 /* Override this method if you want to customize how the JexlNode dumps
171 out its children. */
172 public void dump(String prefix) {
173 System.out.println(toString(prefix));
174 if (children != null) {
175 for (int i = 0; i < children.length; ++i) {
176 SimpleNode n = children[i];
177 if (n != null) {
178 n.dump(prefix + " ");
179 }
180 }
181 }
182 }
183 }
184
185 /* JavaCC - OriginalChecksum=7dff880883d088a37c1e3197e4b455a0 (do not edit this line) */