1#ifndef INC_BaseAST_hpp__
2#define INC_BaseAST_hpp__
3
4/* ANTLR Translator Generator
5 * Project led by Terence Parr at http://www.jGuru.com
6 * Software rights: http://www.antlr.org/license.html
7 *
8 * $Id: //depot/code/org.antlr/release/antlr-2.7.7/lib/cpp/antlr/BaseAST.hpp#2 $
9 */
10
11#include <antlr/config.hpp>
12#include <antlr/AST.hpp>
13
14#include <vector>
15#include <string>
16#include <ostream>
17
18#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
19namespace antlr {
20#endif
21
22class ANTLR_API BaseAST;
23typedef ASTRefCount<BaseAST> RefBaseAST;
24
25class ANTLR_API BaseAST : public AST {
26public:
27	BaseAST() : AST()
28	{
29	}
30	BaseAST(const BaseAST& other)
31	: AST(other)
32	{
33	}
34	virtual ~BaseAST()
35	{
36	}
37
38	/// Return the class name
39	virtual const char* typeName( void ) const = 0;
40
41	/// Clone this AST node.
42	virtual RefAST clone( void ) const = 0;
43
44   /// Is node t equal to this in terms of token type and text?
45	virtual bool equals(RefAST t) const;
46
47   /** Is t an exact structural and equals() match of this tree. The
48	 * 'this' reference is considered the start of a sibling list.
49	 */
50	virtual bool equalsList(RefAST t) const;
51
52   /** Is 't' a subtree of this list? The siblings of the root are NOT ignored.
53    */
54	virtual bool equalsListPartial(RefAST t) const;
55
56	/** Is tree rooted at 'this' equal to 't'?  The siblings of 'this' are
57	 * ignored.
58	 */
59	virtual bool equalsTree(RefAST t) const;
60
61	/** Is 't' a subtree of the tree rooted at 'this'? The siblings of
62	 * 'this' are ignored.
63	 */
64	virtual bool equalsTreePartial(RefAST t) const;
65
66	/** Walk the tree looking for all exact subtree matches.  Return
67	 *  an ASTEnumerator that lets the caller walk the list
68	 *  of subtree roots found herein.
69	 */
70	virtual ANTLR_USE_NAMESPACE(std)vector<RefAST> findAll(RefAST t);
71
72   /** Walk the tree looking for all subtrees.  Return
73    *  an ASTEnumerator that lets the caller walk the list
74    *  of subtree roots found herein.
75    */
76	virtual ANTLR_USE_NAMESPACE(std)vector<RefAST> findAllPartial(RefAST t);
77
78   /// Add a node to the end of the child list for this node
79	virtual void addChild(RefAST c)
80	{
81		if( !c )
82			return;
83
84		RefBaseAST tmp = down;
85
86		if (tmp)
87		{
88			while (tmp->right)
89				tmp = tmp->right;
90			tmp->right = c;
91		}
92		else
93			down = c;
94	}
95
96	/** Get the number of child nodes of this node (shallow e.g. not of the
97	 * whole tree it spans).
98	 */
99	virtual size_t getNumberOfChildren() const;
100
101	/// Get the first child of this node; null if no children
102	virtual RefAST getFirstChild() const
103	{
104		return RefAST(down);
105	}
106	/// Get  the next sibling in line after this one
107	virtual RefAST getNextSibling() const
108	{
109		return RefAST(right);
110	}
111
112	/// Get the token text for this node
113	virtual ANTLR_USE_NAMESPACE(std)string getText() const
114	{
115		return "";
116	}
117	/// Get the token type for this node
118	virtual int getType() const
119	{
120		return 0;
121	}
122
123	/// Remove all children
124	virtual void removeChildren()
125	{
126		down = static_cast<BaseAST*>(static_cast<AST*>(nullAST));
127	}
128
129	/// Set the first child of a node.
130	virtual void setFirstChild(RefAST c)
131	{
132		down = static_cast<BaseAST*>(static_cast<AST*>(c));
133	}
134
135	/// Set the next sibling after this one.
136	virtual void setNextSibling(RefAST n)
137	{
138		right = static_cast<BaseAST*>(static_cast<AST*>(n));
139	}
140
141	/// Set the token text for this node
142	virtual void setText(const ANTLR_USE_NAMESPACE(std)string& txt)
143	{
144	}
145
146	/// Set the token type for this node
147	virtual void setType(int type)
148	{
149	}
150
151#ifdef ANTLR_SUPPORT_XML
152	/** print attributes of this node to 'out'. Override to customize XML
153	 * output.
154	 * @param out the stream to write the AST attributes to.
155	 */
156	virtual bool attributesToStream( ANTLR_USE_NAMESPACE(std)ostream& out ) const;
157	/** Write this subtree to a stream. Overload this one to customize the XML
158	 * output for AST derived AST-types
159	 * @param output stream
160	 */
161	virtual void toStream( ANTLR_USE_NAMESPACE(std)ostream &out ) const;
162#endif
163
164	/// Return string representation for the AST
165	virtual ANTLR_USE_NAMESPACE(std)string toString() const
166	{
167		return getText();
168	}
169
170	/// Print out a child sibling tree in LISP notation
171	virtual ANTLR_USE_NAMESPACE(std)string toStringList() const;
172	virtual ANTLR_USE_NAMESPACE(std)string toStringTree() const;
173protected:
174	RefBaseAST down;
175	RefBaseAST right;
176private:
177	void doWorkForFindAll(ANTLR_USE_NAMESPACE(std)vector<RefAST>& v,
178								 RefAST target,
179								 bool partialMatch);
180};
181
182/** Is node t equal to this in terms of token type and text?
183 */
184inline bool BaseAST::equals(RefAST t) const
185{
186	if (!t)
187		return false;
188	return ((getType() == t->getType()) && (getText() == t->getText()));
189}
190
191#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
192}
193#endif
194
195#endif //INC_BaseAST_hpp__
196