source: trunk/yao/share/antlr-2.7.7/lib/cpp/antlr/BaseAST.hpp @ 1

Last change on this file since 1 was 1, checked in by lnalod, 15 years ago

Initial import of YAO sources

File size: 4.5 KB
Line 
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;
21typedef ASTRefCount<BaseAST> RefBaseAST;
22
23class ANTLR_API BaseAST : public AST {
24public:
25        BaseAST() : AST()
26        {
27        }
28        BaseAST(const BaseAST& other)
29        : AST(other)
30        {
31        }
32        virtual ~BaseAST()
33        {
34        }
35
36        /// Return the class name
37        virtual const char* typeName( void ) const = 0;
38
39        /// Clone this AST node.
40        virtual RefAST clone( void ) const = 0;
41
42   /// Is node t equal to this in terms of token type and text?
43        virtual bool equals(RefAST t) const;
44
45   /** Is t an exact structural and equals() match of this tree. The
46         * 'this' reference is considered the start of a sibling list.
47         */
48        virtual bool equalsList(RefAST t) const;
49
50   /** Is 't' a subtree of this list? The siblings of the root are NOT ignored.
51    */
52        virtual bool equalsListPartial(RefAST t) const;
53
54        /** Is tree rooted at 'this' equal to 't'?  The siblings of 'this' are
55         * ignored.
56         */
57        virtual bool equalsTree(RefAST t) const;
58
59        /** Is 't' a subtree of the tree rooted at 'this'? The siblings of
60         * 'this' are ignored.
61         */
62        virtual bool equalsTreePartial(RefAST t) const;
63
64        /** Walk the tree looking for all exact subtree matches.  Return
65         *  an ASTEnumerator that lets the caller walk the list
66         *  of subtree roots found herein.
67         */
68        virtual ANTLR_USE_NAMESPACE(std)vector<RefAST> findAll(RefAST t);
69
70   /** Walk the tree looking for all subtrees.  Return
71    *  an ASTEnumerator that lets the caller walk the list
72    *  of subtree roots found herein.
73    */
74        virtual ANTLR_USE_NAMESPACE(std)vector<RefAST> findAllPartial(RefAST t);
75
76   /// Add a node to the end of the child list for this node
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
94        /** Get the number of child nodes of this node (shallow e.g. not of the
95         * whole tree it spans).
96         */
97        virtual size_t getNumberOfChildren() const;
98
99        /// Get the first child of this node; null if no children
100        virtual RefAST getFirstChild() const
101        {
102                return RefAST(down);
103        }
104        /// Get  the next sibling in line after this one
105        virtual RefAST getNextSibling() const
106        {
107                return RefAST(right);
108        }
109
110        /// Get the token text for this node
111        virtual ANTLR_USE_NAMESPACE(std)string getText() const
112        {
113                return "";
114        }
115        /// Get the token type for this node
116        virtual int getType() const
117        {
118                return 0;
119        }
120
121        /// Remove all children
122        virtual void removeChildren()
123        {
124                down = static_cast<BaseAST*>(static_cast<AST*>(nullAST));
125        }
126
127        /// Set the first child of a node.
128        virtual void setFirstChild(RefAST c)
129        {
130                down = static_cast<BaseAST*>(static_cast<AST*>(c));
131        }
132
133        /// Set the next sibling after this one.
134        virtual void setNextSibling(RefAST n)
135        {
136                right = static_cast<BaseAST*>(static_cast<AST*>(n));
137        }
138
139        /// Set the token text for this node
140        virtual void setText(const ANTLR_USE_NAMESPACE(std)string& txt)
141        {
142        }
143
144        /// Set the token type for this node
145        virtual void setType(int type)
146        {
147        }
148
149#ifdef ANTLR_SUPPORT_XML
150        /** print attributes of this node to 'out'. Override to customize XML
151         * output.
152         * @param out the stream to write the AST attributes to.
153         */
154        virtual bool attributesToStream( ANTLR_USE_NAMESPACE(std)ostream& out ) const;
155        /** Write this subtree to a stream. Overload this one to customize the XML
156         * output for AST derived AST-types
157         * @param output stream
158         */
159        virtual void toStream( ANTLR_USE_NAMESPACE(std)ostream &out ) const;
160#endif
161
162        /// Return string representation for the AST
163        virtual ANTLR_USE_NAMESPACE(std)string toString() const
164        {
165                return getText();
166        }
167
168        /// Print out a child sibling tree in LISP notation
169        virtual ANTLR_USE_NAMESPACE(std)string toStringList() const;
170        virtual ANTLR_USE_NAMESPACE(std)string toStringTree() const;
171protected:
172        RefBaseAST down;
173        RefBaseAST right;
174private:
175        void doWorkForFindAll(ANTLR_USE_NAMESPACE(std)vector<RefAST>& v,
176                                                                 RefAST target,
177                                                                 bool partialMatch);
178};
179
180/** Is node t equal to this in terms of token type and text?
181 */
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__
Note: See TracBrowser for help on using the repository browser.