1 #ifndef INC_BaseAST_hpp__
2 #define INC_BaseAST_hpp__
4 /* ANTLR Translator Generator
5 * Project led by Terence Parr at http://www.jGuru.com
6 * Software rights: http://www.antlr.org/license.html
8 * $Id: //depot/code/org.antlr/release/antlr-2.7.7/lib/cpp/antlr/BaseAST.hpp#2 $
11 #include <antlr/config.hpp>
12 #include <antlr/AST.hpp>
18 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
22 class ANTLR_API BaseAST;
23 typedef ASTRefCount<BaseAST> RefBaseAST;
25 class ANTLR_API BaseAST : public AST {
30 BaseAST(const BaseAST& other)
38 /// Return the class name
39 virtual const char* typeName( void ) const = 0;
41 /// Clone this AST node.
42 virtual RefAST clone( void ) const = 0;
44 /// Is node t equal to this in terms of token type and text?
45 virtual bool equals(RefAST t) const;
47 /** Is t an exact structural and equals() match of this tree. The
48 * 'this' reference is considered the start of a sibling list.
50 virtual bool equalsList(RefAST t) const;
52 /** Is 't' a subtree of this list? The siblings of the root are NOT ignored.
54 virtual bool equalsListPartial(RefAST t) const;
56 /** Is tree rooted at 'this' equal to 't'? The siblings of 'this' are
59 virtual bool equalsTree(RefAST t) const;
61 /** Is 't' a subtree of the tree rooted at 'this'? The siblings of
64 virtual bool equalsTreePartial(RefAST t) const;
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.
70 virtual ANTLR_USE_NAMESPACE(std)vector<RefAST> findAll(RefAST t);
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.
76 virtual ANTLR_USE_NAMESPACE(std)vector<RefAST> findAllPartial(RefAST t);
78 /// Add a node to the end of the child list for this node
79 virtual void addChild(RefAST c)
84 RefBaseAST tmp = down;
96 /** Get the number of child nodes of this node (shallow e.g. not of the
97 * whole tree it spans).
99 virtual size_t getNumberOfChildren() const;
101 /// Get the first child of this node; null if no children
102 virtual RefAST getFirstChild() const
106 /// Get the next sibling in line after this one
107 virtual RefAST getNextSibling() const
109 return RefAST(right);
112 /// Get the token text for this node
113 virtual ANTLR_USE_NAMESPACE(std)string getText() const
117 /// Get the token type for this node
118 virtual int getType() const
123 /// Remove all children
124 virtual void removeChildren()
126 down = static_cast<BaseAST*>(static_cast<AST*>(nullAST));
129 /// Set the first child of a node.
130 virtual void setFirstChild(RefAST c)
132 down = static_cast<BaseAST*>(static_cast<AST*>(c));
135 /// Set the next sibling after this one.
136 virtual void setNextSibling(RefAST n)
138 right = static_cast<BaseAST*>(static_cast<AST*>(n));
141 /// Set the token text for this node
142 virtual void setText(const ANTLR_USE_NAMESPACE(std)string& txt)
146 /// Set the token type for this node
147 virtual void setType(int type)
151 #ifdef ANTLR_SUPPORT_XML
152 /** print attributes of this node to 'out'. Override to customize XML
154 * @param out the stream to write the AST attributes to.
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
161 virtual void toStream( ANTLR_USE_NAMESPACE(std)ostream &out ) const;
164 /// Return string representation for the AST
165 virtual ANTLR_USE_NAMESPACE(std)string toString() const
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;
177 void doWorkForFindAll(ANTLR_USE_NAMESPACE(std)vector<RefAST>& v,
182 /** Is node t equal to this in terms of token type and text?
184 inline bool BaseAST::equals(RefAST t) const
188 return ((getType() == t->getType()) && (getText() == t->getText()));
191 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
195 #endif //INC_BaseAST_hpp__