ANTLR Support Libraries 2.7.1+
Loading...
Searching...
No Matches
BaseAST.hpp
Go to the documentation of this file.
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 <iostream>
15
16#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
17namespace antlr {
18#endif
19
20class ANTLR_API BaseAST;
22
23class ANTLR_API BaseAST : public AST {
24public:
26 {
27 }
28 BaseAST(const BaseAST& other)
29 : AST(other)
30 {
31 }
32 virtual ~BaseAST()
33 {
34 }
35
37 virtual const char* typeName( void ) const = 0;
38
40 virtual RefAST clone( void ) const = 0;
41
43 virtual bool equals(RefAST t) const;
44
48 virtual bool equalsList(RefAST t) const;
49
52 virtual bool equalsListPartial(RefAST t) const;
53
57 virtual bool equalsTree(RefAST t) const;
58
62 virtual bool equalsTreePartial(RefAST t) const;
63
68 virtual ANTLR_USE_NAMESPACE(std)vector<RefAST> findAll(RefAST t);
69
74 virtual ANTLR_USE_NAMESPACE(std)vector<RefAST> findAllPartial(RefAST t);
75
77 virtual void addChild(RefAST c)
78 {
79 if( !c )
80 return;
81
82 RefBaseAST tmp = down;
83
84 if (tmp)
85 {
86 while (tmp->right)
87 tmp = tmp->right;
88 tmp->right = c;
89 }
90 else
91 down = c;
92 }
93
97 virtual size_t getNumberOfChildren() const;
98
100 virtual RefAST getFirstChild() const
101 {
102 return RefAST(down);
103 }
105 virtual RefAST getNextSibling() const
106 {
107 return RefAST(right);
108 }
109
111 virtual ANTLR_USE_NAMESPACE(std)string getText() const
112 {
113 return "";
114 }
116 virtual int getType() const
117 {
118 return 0;
119 }
120
122 virtual void removeChildren()
123 {
124 down = static_cast<BaseAST*>(static_cast<AST*>(nullAST));
125 }
126
128 virtual void setFirstChild(RefAST c)
129 {
130 down = static_cast<BaseAST*>(static_cast<AST*>(c));
131 }
132
134 virtual void setNextSibling(RefAST n)
135 {
136 right = static_cast<BaseAST*>(static_cast<AST*>(n));
137 }
138
140 virtual void setText(const ANTLR_USE_NAMESPACE(std)string& txt)
141 {
142 }
143
145 virtual void setType(int type)
146 {
147 }
148
149#ifdef ANTLR_SUPPORT_XML
154 virtual bool attributesToStream( ANTLR_USE_NAMESPACE(std)ostream& out ) const;
159 virtual void toStream( ANTLR_USE_NAMESPACE(std)ostream &out ) const;
160#endif
161
163 virtual ANTLR_USE_NAMESPACE(std)string toString() const
164 {
165 return getText();
166 }
167
169 virtual ANTLR_USE_NAMESPACE(std)string toStringList() const;
170 virtual ANTLR_USE_NAMESPACE(std)string toStringTree() const;
171protected:
174private:
175 void doWorkForFindAll(ANTLR_USE_NAMESPACE(std)vector<RefAST>& v,
176 RefAST target,
177 bool partialMatch);
178};
179
182inline bool BaseAST::equals(RefAST t) const
183{
184 if (!t)
185 return false;
186 return ((getType() == t->getType()) && (getText() == t->getText()));
187}
188
189#ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
190}
191#endif
192
193#endif //INC_BaseAST_hpp__
ASTRefCount< AST > RefAST
Definition: ASTRefCount.hpp:92
RefAST nullAST
Definition: BaseAST.cpp:271
ASTRefCount< BaseAST > RefBaseAST
Definition: BaseAST.hpp:21
Definition: ASTRefCount.hpp:44
Definition: AST.hpp:23
virtual int getType() const =0
Get the token type for this node.
virtual std::string getText() const =0
Get the token text for this node.
Definition: BaseAST.hpp:23
virtual void setNextSibling(RefAST n)
Set the next sibling after this one.
Definition: BaseAST.hpp:134
virtual RefAST clone(void) const =0
Clone this AST node.
virtual void removeChildren()
Remove all children.
Definition: BaseAST.hpp:122
virtual void addChild(RefAST c)
Add a node to the end of the child list for this node.
Definition: BaseAST.hpp:77
virtual void setFirstChild(RefAST c)
Set the first child of a node.
Definition: BaseAST.hpp:128
virtual std::string getText() const
Get the token text for this node.
Definition: BaseAST.hpp:111
RefBaseAST down
Definition: BaseAST.hpp:172
virtual ~BaseAST()
Definition: BaseAST.hpp:32
RefBaseAST right
Definition: BaseAST.hpp:173
virtual std::string toString() const
Return string representation for the AST.
Definition: BaseAST.hpp:163
virtual int getType() const
Get the token type for this node.
Definition: BaseAST.hpp:116
virtual RefAST getNextSibling() const
Get the next sibling in line after this one.
Definition: BaseAST.hpp:105
virtual void setText(const std ::string &txt)
Set the token text for this node.
Definition: BaseAST.hpp:140
BaseAST(const BaseAST &other)
Definition: BaseAST.hpp:28
virtual const char * typeName(void) const =0
Return the class name.
virtual void setType(int type)
Set the token type for this node.
Definition: BaseAST.hpp:145
BaseAST()
Definition: BaseAST.hpp:25
virtual RefAST getFirstChild() const
Get the first child of this node; null if no children.
Definition: BaseAST.hpp:100
virtual bool equals(RefAST t) const
Is node t equal to this in terms of token type and text?
Definition: BaseAST.hpp:182
#define ANTLR_USE_NAMESPACE(_x_)
Definition: config.hpp:18
#define ANTLR_API
Definition: config.hpp:22
Definition: ANTLRException.hpp:15