001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.jexl2.parser;
018
019/**
020 * A class originally generated by JJTree with the following JavaCCOptions:
021 * MULTI=true,NODE_USES_PARSER=true,VISITOR=true,TRACK_TOKENS=false,NODE_PREFIX=AST,NODE_EXTENDS=,NODE_FACTORY=
022 *
023 * Works around issue https://javacc.dev.java.net/issues/show_bug.cgi?id=227
024 * As soon as this issue if fixed and the maven plugin uses the correct version of Javacc, this
025 * class can go away.
026 *
027 * The technical goal is to ensure every reference made in the parser was to a JexlNode; unfortunately,
028 * as in javacc 4.1, it still uses a SimpleNode reference in the generated ParserVisitor.
029 * Besides, there is no need to keep the parser around in the node.
030 *
031 * The functional goal is to a allow a <em>volatile</em> value in the node
032 * so it can serve as a last evaluation cache even in multi-threaded executions.
033 */
034public class SimpleNode implements Node {
035    /** The parent node. */
036    protected JexlNode parent;
037    /** The array of children nodes. */
038    protected JexlNode[] children;
039    /** The node type id. */
040    protected final int id;
041    /** volatile value so it can be used as a last evaluation cache. */
042    protected volatile Object value;
043
044    /**
045     * Creates a SimpleNode instance.
046     * @param i the node type identifier
047     */
048    public SimpleNode(int i) {
049        id = i;
050    }
051
052    /**
053     * Creates a SimpleNode instance.
054     * @param p the parser instance
055     * @param i the node type identifier
056     */
057    public SimpleNode(Parser p, int i) {
058        this(i);
059    }
060
061    /** {@inheritDoc} */
062    public void jjtOpen() {
063    }
064
065    /** {@inheritDoc} */
066    public void jjtClose() {
067    }
068
069    /**
070     * Sets this node's parent.
071     * @param n the parent
072     */
073    public void jjtSetParent(Node n) {
074        parent = (JexlNode) n;
075    }
076
077    /**
078     * Gets this node's parent.
079     * @return the parent node
080     */
081    public JexlNode jjtGetParent() {
082        return parent;
083    }
084
085    /** Adds a child node.
086     * @param n the child node
087     * @param i the child offset
088     */
089    public void jjtAddChild(Node n, int i) {
090        if (children == null) {
091            children = new JexlNode[i + 1];
092        } else if (i >= children.length) {
093            JexlNode[] c = new JexlNode[i + 1];
094            System.arraycopy(children, 0, c, 0, children.length);
095            children = c;
096        }
097        children[i] = (JexlNode) n;
098    }
099
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    public int getId() { return id; }
185}
186
187/* JavaCC - OriginalChecksum=7dff880883d088a37c1e3197e4b455a0 (do not edit this line) */