AST.hpp
  1  #ifndef INC_AST_hpp__
  2  #define INC_AST_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/AST.hpp#2 $
  9   */
 10  
 11  #include <antlr/config.hpp>
 12  #include <antlr/ASTRefCount.hpp>
 13  #include <antlr/Token.hpp>
 14  #include <vector>
 15  #include <string>
 16  
 17  #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
 18  namespace antlr {
 19  #endif
 20  
 21  struct ASTRef;
 22  
 23  class ANTLR_API AST {
 24  public:
 25  	AST() : ref(0) {}
 26  	AST(const AST&) : ref(0) {}
 27  	virtual ~AST() {}
 28  
 29  	/// Return the type name for this AST node. (for XML output)
 30  	virtual const char* typeName( void ) const = 0;
 31  	/// Clone this AST node.
 32  	virtual RefAST clone( void ) const = 0;
 33     /// Is node t equal to this in terms of token type and text?
 34  	virtual bool equals(RefAST t) const = 0;
 35     /** Is t an exact structural and equals() match of this tree. The
 36  	 * 'this' reference is considered the start of a sibling list.
 37  	 */
 38  	virtual bool equalsList(RefAST t) const = 0;
 39  
 40  	/** Is 't' a subtree of this list? The siblings of the root are NOT ignored.
 41      */
 42  	virtual bool equalsListPartial(RefAST t) const = 0;
 43  	/** Is tree rooted at 'this' equal to 't'?  The siblings of 'this' are
 44  	 * ignored.
 45  	 */
 46  	virtual bool equalsTree(RefAST t) const = 0;
 47  	/** Is 't' a subtree of the tree rooted at 'this'? The siblings of
 48  	 * 'this' are ignored.
 49  	 */
 50  	virtual bool equalsTreePartial(RefAST t) const = 0;
 51  
 52  	/** Walk the tree looking for all exact subtree matches.  Return
 53  	 *  a vector of RefAST that lets the caller walk the list
 54  	 *  of subtree roots found herein.
 55  	 */
 56  	virtual ANTLR_USE_NAMESPACE(std)vector<RefAST> findAll(RefAST t) = 0;
 57  
 58     /** Walk the tree looking for all subtrees.  Return
 59      *  a vector of RefAST that lets the caller walk the list
 60      *  of subtree roots found herein.
 61      */
 62  	virtual ANTLR_USE_NAMESPACE(std)vector<RefAST> findAllPartial(RefAST t) = 0;
 63  
 64     /// Add a node to the end of the child list for this node
 65  	virtual void addChild(RefAST c) = 0;
 66  	/// Get the number of children. Returns 0 if the node is a leaf
 67  	virtual size_t getNumberOfChildren() const = 0;
 68  
 69  	/// Get the first child of this node; null if no children
 70  	virtual RefAST getFirstChild() const = 0;
 71  	/// Get  the next sibling in line after this one
 72  	virtual RefAST getNextSibling() const = 0;
 73  
 74  	/// Get the token text for this node
 75  	virtual ANTLR_USE_NAMESPACE(std)string getText() const = 0;
 76  	/// Get the token type for this node
 77  	virtual int getType() const = 0;
 78  
 79  	/** Various initialization routines. Used by several factories to initialize
 80  	 * an AST element.
 81  	 */
 82  	virtual void initialize(int t, const ANTLR_USE_NAMESPACE(std)string& txt) = 0;
 83  	virtual void initialize(RefAST t) = 0;
 84  	virtual void initialize(RefToken t) = 0;
 85  
 86  #ifdef ANTLR_SUPPORT_XML
 87  	/** initialize this node from the contents of a stream.
 88  	 * @param in the stream to read the AST attributes from.
 89  	 */
 90  	virtual void initialize( ANTLR_USE_NAMESPACE(std)istream& in ) = 0;
 91  #endif
 92  
 93  	/// Set the first child of a node.
 94  	virtual void setFirstChild(RefAST c) = 0;
 95  	/// Set the next sibling after this one.
 96  	virtual void setNextSibling(RefAST n) = 0;
 97  
 98  	/// Set the token text for this node
 99  	virtual void setText(const ANTLR_USE_NAMESPACE(std)string& txt) = 0;
100  	/// Set the token type for this node
101  	virtual void setType(int type) = 0;
102  
103  	/// Return this AST node as a string
104  	virtual ANTLR_USE_NAMESPACE(std)string toString() const = 0;
105  
106  	/// Print out a child-sibling tree in LISP notation
107  	virtual ANTLR_USE_NAMESPACE(std)string toStringList() const = 0;
108  	virtual ANTLR_USE_NAMESPACE(std)string toStringTree() const = 0;
109  
110  #ifdef ANTLR_SUPPORT_XML
111  	/** get attributes of this node to 'out'. Override to customize XML
112  	 * output.
113  	 * @param out the stream to write the AST attributes to.
114  	 * @returns if a explicit closetag should be written
115  	 */
116  	virtual bool attributesToStream( ANTLR_USE_NAMESPACE(std)ostream& out ) const = 0;
117  
118  	/** Print a symbol over ostream. Overload this one to customize the XML
119  	 * output for AST derived AST-types
120  	 * @param output stream
121  	 */
122  	virtual void toStream( ANTLR_USE_NAMESPACE(std)ostream &out ) const = 0;
123  
124  	/** Dump AST contents in XML format to output stream.
125  	 * Works in conjunction with to_stream method. Overload that one is
126  	 * derived classes to customize behaviour.
127  	 * @param output stream to write to string to put the stuff in.
128  	 * @param ast RefAST object to write.
129  	 */
130  	friend ANTLR_USE_NAMESPACE(std)ostream& operator<<( ANTLR_USE_NAMESPACE(std)ostream& output, const RefAST& ast );
131  #endif
132  
133  private:
134  	friend struct ASTRef;
135  	ASTRef* ref;
136  
137  	AST(RefAST other);
138  	AST& operator=(const AST& other);
139  	AST& operator=(RefAST other);
140  };
141  
142  #ifdef ANTLR_SUPPORT_XML
143  inline ANTLR_USE_NAMESPACE(std)ostream& operator<<( ANTLR_USE_NAMESPACE(std)ostream& output, const RefAST& ast )
144  {
145  	ast->toStream(output);
146  	return output;
147  }
148  #endif
149  
150  extern ANTLR_API RefAST nullAST;
151  extern ANTLR_API AST* const nullASTptr;
152  
153  #ifdef NEEDS_OPERATOR_LESS_THAN
154  // RK: apparently needed by MSVC and a SUN CC, up to and including
155  // 2.7.2 this was undefined ?
156  inline bool operator<( RefAST l, RefAST r )
157  {
158  	return nullAST == l ? ( nullAST == r ? false : true ) : l->getType() < r->getType();
159  }
160  #endif
161  
162  #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
163  }
164  #endif
165  
166  #endif //INC_AST_hpp__