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/AST.hpp#2 $
11 #include <antlr/config.hpp>
12 #include <antlr/ASTRefCount.hpp>
13 #include <antlr/Token.hpp>
17 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
26 AST(const AST&) : ref(0) {}
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.
38 virtual bool equalsList(RefAST t) const = 0;
40 /** Is 't' a subtree of this list? The siblings of the root are NOT ignored.
42 virtual bool equalsListPartial(RefAST t) const = 0;
43 /** Is tree rooted at 'this' equal to 't'? The siblings of 'this' are
46 virtual bool equalsTree(RefAST t) const = 0;
47 /** Is 't' a subtree of the tree rooted at 'this'? The siblings of
50 virtual bool equalsTreePartial(RefAST t) const = 0;
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.
56 virtual ANTLR_USE_NAMESPACE(std)vector<RefAST> findAll(RefAST t) = 0;
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.
62 virtual ANTLR_USE_NAMESPACE(std)vector<RefAST> findAllPartial(RefAST t) = 0;
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;
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;
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;
79 /** Various initialization routines. Used by several factories to initialize
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;
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.
90 virtual void initialize( ANTLR_USE_NAMESPACE(std)istream& in ) = 0;
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;
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;
103 /// Return this AST node as a string
104 virtual ANTLR_USE_NAMESPACE(std)string toString() const = 0;
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;
110 #ifdef ANTLR_SUPPORT_XML
111 /** get attributes of this node to 'out'. Override to customize XML
113 * @param out the stream to write the AST attributes to.
114 * @returns if a explicit closetag should be written
116 virtual bool attributesToStream( ANTLR_USE_NAMESPACE(std)ostream& out ) const = 0;
118 /** Print a symbol over ostream. Overload this one to customize the XML
119 * output for AST derived AST-types
120 * @param output stream
122 virtual void toStream( ANTLR_USE_NAMESPACE(std)ostream &out ) const = 0;
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.
130 friend ANTLR_USE_NAMESPACE(std)ostream& operator<<( ANTLR_USE_NAMESPACE(std)ostream& output, const RefAST& ast );
134 friend struct ASTRef;
138 AST& operator=(const AST& other);
139 AST& operator=(RefAST other);
142 #ifdef ANTLR_SUPPORT_XML
143 inline ANTLR_USE_NAMESPACE(std)ostream& operator<<( ANTLR_USE_NAMESPACE(std)ostream& output, const RefAST& ast )
145 ast->toStream(output);
150 extern ANTLR_API RefAST nullAST;
151 extern ANTLR_API AST* const nullASTptr;
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 )
158 return nullAST == l ? ( nullAST == r ? false : true ) : l->getType() < r->getType();
162 #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE
166 #endif //INC_AST_hpp__