xref: /aosp_15_r20/external/antlr/runtime/Cpp/include/antlr3commontree.hpp (revision 16467b971bd3e2009fad32dd79016f2c7e421deb)
1*16467b97STreehugger Robot /** Interface for an ANTLR3 common tree which is what gets
2*16467b97STreehugger Robot  *  passed around by the AST producing parser.
3*16467b97STreehugger Robot  */
4*16467b97STreehugger Robot 
5*16467b97STreehugger Robot #ifndef	_ANTLR3_COMMON_TREE_HPP
6*16467b97STreehugger Robot #define	_ANTLR3_COMMON_TREE_HPP
7*16467b97STreehugger Robot 
8*16467b97STreehugger Robot // [The "BSD licence"]
9*16467b97STreehugger Robot // Copyright (c) 2005-2009 Gokulakannan Somasundaram, ElectronDB
10*16467b97STreehugger Robot 
11*16467b97STreehugger Robot //
12*16467b97STreehugger Robot // All rights reserved.
13*16467b97STreehugger Robot //
14*16467b97STreehugger Robot // Redistribution and use in source and binary forms, with or without
15*16467b97STreehugger Robot // modification, are permitted provided that the following conditions
16*16467b97STreehugger Robot // are met:
17*16467b97STreehugger Robot // 1. Redistributions of source code must retain the above copyright
18*16467b97STreehugger Robot //    notice, this list of conditions and the following disclaimer.
19*16467b97STreehugger Robot // 2. Redistributions in binary form must reproduce the above copyright
20*16467b97STreehugger Robot //    notice, this list of conditions and the following disclaimer in the
21*16467b97STreehugger Robot //    documentation and/or other materials provided with the distribution.
22*16467b97STreehugger Robot // 3. The name of the author may not be used to endorse or promote products
23*16467b97STreehugger Robot //    derived from this software without specific prior written permission.
24*16467b97STreehugger Robot //
25*16467b97STreehugger Robot // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26*16467b97STreehugger Robot // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27*16467b97STreehugger Robot // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28*16467b97STreehugger Robot // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29*16467b97STreehugger Robot // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30*16467b97STreehugger Robot // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31*16467b97STreehugger Robot // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32*16467b97STreehugger Robot // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33*16467b97STreehugger Robot // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34*16467b97STreehugger Robot // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35*16467b97STreehugger Robot 
36*16467b97STreehugger Robot #include    "antlr3defs.hpp"
37*16467b97STreehugger Robot 
38*16467b97STreehugger Robot ANTLR_BEGIN_NAMESPACE()
39*16467b97STreehugger Robot 
40*16467b97STreehugger Robot template<class ImplTraits>
41*16467b97STreehugger Robot class CommonTree : public ImplTraits::AllocPolicyType
42*16467b97STreehugger Robot {
43*16467b97STreehugger Robot public:
44*16467b97STreehugger Robot 	typedef typename ImplTraits::AllocPolicyType AllocPolicyType;
45*16467b97STreehugger Robot 	typedef typename ImplTraits::StringType	StringType;
46*16467b97STreehugger Robot 	typedef typename ImplTraits::CommonTokenType CommonTokenType;
47*16467b97STreehugger Robot 	typedef typename ImplTraits::TreeType TreeType;
48*16467b97STreehugger Robot 	typedef CommonTree TokenType;
49*16467b97STreehugger Robot 	typedef typename AllocPolicyType::template VectorType<TreeType*> ChildrenType;
50*16467b97STreehugger Robot 	typedef typename AllocPolicyType::template ListType<TreeType*>	ChildListType;
51*16467b97STreehugger Robot 
52*16467b97STreehugger Robot private:
53*16467b97STreehugger Robot 	/// The list of all the children that belong to this node. They are not part of the node
54*16467b97STreehugger Robot     /// as they belong to the common tree node that implements this.
55*16467b97STreehugger Robot     ///
56*16467b97STreehugger Robot     ChildrenType		m_children;
57*16467b97STreehugger Robot 
58*16467b97STreehugger Robot     /// This is used to store the current child index position while descending
59*16467b97STreehugger Robot     /// and ascending trees as the tree walk progresses.
60*16467b97STreehugger Robot     ///
61*16467b97STreehugger Robot     ANTLR_MARKER		m_savedIndex;
62*16467b97STreehugger Robot 
63*16467b97STreehugger Robot     /// Start token index that encases this tree
64*16467b97STreehugger Robot     ///
65*16467b97STreehugger Robot     ANTLR_MARKER		m_startIndex;
66*16467b97STreehugger Robot 
67*16467b97STreehugger Robot     /// End token that encases this tree
68*16467b97STreehugger Robot     ///
69*16467b97STreehugger Robot     ANTLR_MARKER		m_stopIndex;
70*16467b97STreehugger Robot 
71*16467b97STreehugger Robot     /// A single token, this is the payload for the tree
72*16467b97STreehugger Robot     ///
73*16467b97STreehugger Robot     CommonTokenType*    m_token;
74*16467b97STreehugger Robot 
75*16467b97STreehugger Robot 	/// Points to the node that has this node as a child.
76*16467b97STreehugger Robot 	/// If this is NULL, then this is the root node.
77*16467b97STreehugger Robot 	///
78*16467b97STreehugger Robot 	CommonTree*			m_parent;
79*16467b97STreehugger Robot 
80*16467b97STreehugger Robot 	/// What index is this particular node in the child list it
81*16467b97STreehugger Robot 	/// belongs to?
82*16467b97STreehugger Robot 	///
83*16467b97STreehugger Robot 	ANTLR_INT32			m_childIndex;
84*16467b97STreehugger Robot 
85*16467b97STreehugger Robot public:
86*16467b97STreehugger Robot 	CommonTree();
87*16467b97STreehugger Robot 	CommonTree( CommonTokenType* token );
88*16467b97STreehugger Robot 	CommonTree( CommonTree* token );
89*16467b97STreehugger Robot 	CommonTree( const CommonTree& ctree );
90*16467b97STreehugger Robot 
91*16467b97STreehugger Robot 	TokenType*   get_token() const;
92*16467b97STreehugger Robot 	ChildrenType& get_children();
93*16467b97STreehugger Robot 	const ChildrenType& get_children() const;
94*16467b97STreehugger Robot 	ChildrenType* get_children_p();
95*16467b97STreehugger Robot 	ANTLR_INT32	get_childIndex() const;
96*16467b97STreehugger Robot 	TreeType* get_parent() const;
97*16467b97STreehugger Robot 
98*16467b97STreehugger Robot 	void    set_parent( TreeType* parent);
99*16467b97STreehugger Robot 	void    set_childIndex( ANTLR_INT32 );
100*16467b97STreehugger Robot 
101*16467b97STreehugger Robot 	void	addChild(TreeType* child);
102*16467b97STreehugger Robot 	/// Add all elements of the supplied list as children of this node
103*16467b97STreehugger Robot 	///
104*16467b97STreehugger Robot 	void	addChildren(const ChildListType& kids);
105*16467b97STreehugger Robot 	void    createChildrenList();
106*16467b97STreehugger Robot 	TreeType*	deleteChild(ANTLR_UINT32 i);
107*16467b97STreehugger Robot 	/// Delete children from start to stop and replace with t even if t is
108*16467b97STreehugger Robot 	/// a list (nil-root tree). Num of children can increase or decrease.
109*16467b97STreehugger Robot 	/// For huge child lists, inserting children can force walking rest of
110*16467b97STreehugger Robot 	/// children to set their child index; could be slow.
111*16467b97STreehugger Robot 	///
112*16467b97STreehugger Robot 	void	replaceChildren(ANTLR_INT32 startChildIndex, ANTLR_INT32 stopChildIndex, TreeType* t);
113*16467b97STreehugger Robot 	CommonTree*	dupNode() const;
114*16467b97STreehugger Robot 	TreeType*	dupTree();
115*16467b97STreehugger Robot 	ANTLR_UINT32	getCharPositionInLine();
116*16467b97STreehugger Robot 	TreeType*	getChild(ANTLR_UINT32 i);
117*16467b97STreehugger Robot 
118*16467b97STreehugger Robot 	ANTLR_UINT32	getChildCount() const;
119*16467b97STreehugger Robot 	ANTLR_UINT32	getType();
120*16467b97STreehugger Robot 	TreeType*	getFirstChildWithType(ANTLR_UINT32 type);
121*16467b97STreehugger Robot 	ANTLR_UINT32	getLine();
122*16467b97STreehugger Robot 	StringType	getText();
123*16467b97STreehugger Robot 	bool	isNilNode();
124*16467b97STreehugger Robot 	void	setChild(ANTLR_UINT32 i, TreeType* child);
125*16467b97STreehugger Robot 	StringType	toStringTree();
126*16467b97STreehugger Robot 	StringType	toString();
127*16467b97STreehugger Robot 	void	freshenPACIndexesAll();
128*16467b97STreehugger Robot 	void	freshenPACIndexes(ANTLR_UINT32 offset);
129*16467b97STreehugger Robot 	void    reuse();
130*16467b97STreehugger Robot 	~CommonTree();
131*16467b97STreehugger Robot };
132*16467b97STreehugger Robot 
133*16467b97STreehugger Robot ANTLR_END_NAMESPACE()
134*16467b97STreehugger Robot 
135*16467b97STreehugger Robot #include "antlr3commontree.inl"
136*16467b97STreehugger Robot 
137*16467b97STreehugger Robot #endif
138*16467b97STreehugger Robot 
139*16467b97STreehugger Robot 
140