| 1 | tim | 770 | #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: BaseAST.hpp,v 1.1 2005-12-02 15:38:02 tim Exp $ | 
| 9 |  |  | */ | 
| 10 |  |  |  | 
| 11 |  |  | #include <iostream> | 
| 12 |  |  |  | 
| 13 |  |  | #include <antlr/config.hpp> | 
| 14 |  |  | #include <antlr/AST.hpp> | 
| 15 |  |  |  | 
| 16 |  |  | #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE | 
| 17 |  |  | namespace antlr { | 
| 18 |  |  | #endif | 
| 19 |  |  |  | 
| 20 |  |  | class ANTLR_API BaseAST; | 
| 21 |  |  | typedef ASTRefCount<BaseAST> RefBaseAST; | 
| 22 |  |  |  | 
| 23 |  |  | class ANTLR_API BaseAST : public AST { | 
| 24 |  |  | public: | 
| 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 |  |  | const char* typeName( void ) const | 
| 38 |  |  | { | 
| 39 |  |  | return BaseAST::TYPE_NAME; | 
| 40 |  |  | } | 
| 41 |  |  |  | 
| 42 |  |  | /// Clone this AST node. | 
| 43 |  |  | RefAST clone( void ) const | 
| 44 |  |  | { | 
| 45 |  |  | ANTLR_USE_NAMESPACE(std)cerr << "BaseAST::clone()" << ANTLR_USE_NAMESPACE(std)endl; | 
| 46 |  |  | return nullAST; | 
| 47 |  |  | } | 
| 48 |  |  |  | 
| 49 |  |  | /// Is node t equal to this in terms of token type and text? | 
| 50 |  |  | virtual bool equals(RefAST t) const; | 
| 51 |  |  |  | 
| 52 |  |  | /** Is t an exact structural and equals() match of this tree. The | 
| 53 |  |  | * 'this' reference is considered the start of a sibling list. | 
| 54 |  |  | */ | 
| 55 |  |  | virtual bool equalsList(RefAST t) const; | 
| 56 |  |  |  | 
| 57 |  |  | /** Is 't' a subtree of this list? The siblings of the root are NOT ignored. | 
| 58 |  |  | */ | 
| 59 |  |  | virtual bool equalsListPartial(RefAST t) const; | 
| 60 |  |  |  | 
| 61 |  |  | /** Is tree rooted at 'this' equal to 't'?  The siblings of 'this' are | 
| 62 |  |  | * ignored. | 
| 63 |  |  | */ | 
| 64 |  |  | virtual bool equalsTree(RefAST t) const; | 
| 65 |  |  |  | 
| 66 |  |  | /** Is 't' a subtree of the tree rooted at 'this'? The siblings of | 
| 67 |  |  | * 'this' are ignored. | 
| 68 |  |  | */ | 
| 69 |  |  | virtual bool equalsTreePartial(RefAST t) const; | 
| 70 |  |  |  | 
| 71 |  |  | /** Walk the tree looking for all exact subtree matches.  Return | 
| 72 |  |  | *  an ASTEnumerator that lets the caller walk the list | 
| 73 |  |  | *  of subtree roots found herein. | 
| 74 |  |  | */ | 
| 75 |  |  | virtual ANTLR_USE_NAMESPACE(std)vector<RefAST> findAll(RefAST t); | 
| 76 |  |  |  | 
| 77 |  |  | /** Walk the tree looking for all subtrees.  Return | 
| 78 |  |  | *  an ASTEnumerator that lets the caller walk the list | 
| 79 |  |  | *  of subtree roots found herein. | 
| 80 |  |  | */ | 
| 81 |  |  | virtual ANTLR_USE_NAMESPACE(std)vector<RefAST> findAllPartial(RefAST t); | 
| 82 |  |  |  | 
| 83 |  |  | /// Add a node to the end of the child list for this node | 
| 84 |  |  | virtual void addChild(RefAST c) | 
| 85 |  |  | { | 
| 86 |  |  | if( !c ) | 
| 87 |  |  | return; | 
| 88 |  |  |  | 
| 89 |  |  | RefBaseAST tmp = down; | 
| 90 |  |  |  | 
| 91 |  |  | if (tmp) | 
| 92 |  |  | { | 
| 93 |  |  | while (tmp->right) | 
| 94 |  |  | tmp = tmp->right; | 
| 95 |  |  | tmp->right = c; | 
| 96 |  |  | } | 
| 97 |  |  | else | 
| 98 |  |  | down = c; | 
| 99 |  |  | } | 
| 100 |  |  |  | 
| 101 |  |  | /** Get the number of child nodes of this node (shallow e.g. not of the | 
| 102 |  |  | * whole tree it spans). | 
| 103 |  |  | */ | 
| 104 |  |  | virtual size_t getNumberOfChildren() const; | 
| 105 |  |  |  | 
| 106 |  |  | /// Get the first child of this node; null if no children | 
| 107 |  |  | virtual RefAST getFirstChild() const | 
| 108 |  |  | { | 
| 109 |  |  | return RefAST(down); | 
| 110 |  |  | } | 
| 111 |  |  | /// Get  the next sibling in line after this one | 
| 112 |  |  | virtual RefAST getNextSibling() const | 
| 113 |  |  | { | 
| 114 |  |  | return RefAST(right); | 
| 115 |  |  | } | 
| 116 |  |  |  | 
| 117 |  |  | /// Get the token text for this node | 
| 118 |  |  | virtual ANTLR_USE_NAMESPACE(std)string getText() const | 
| 119 |  |  | { | 
| 120 |  |  | return ""; | 
| 121 |  |  | } | 
| 122 |  |  | /// Get the token type for this node | 
| 123 |  |  | virtual int getType() const | 
| 124 |  |  | { | 
| 125 |  |  | return 0; | 
| 126 |  |  | } | 
| 127 |  |  |  | 
| 128 |  |  | /// Remove all children | 
| 129 |  |  | virtual void removeChildren() | 
| 130 |  |  | { | 
| 131 |  |  | down = static_cast<BaseAST*>(static_cast<AST*>(nullAST)); | 
| 132 |  |  | } | 
| 133 |  |  |  | 
| 134 |  |  | /// Set the first child of a node. | 
| 135 |  |  | virtual void setFirstChild(RefAST c) | 
| 136 |  |  | { | 
| 137 |  |  | down = static_cast<BaseAST*>(static_cast<AST*>(c)); | 
| 138 |  |  | } | 
| 139 |  |  |  | 
| 140 |  |  | /// Set the next sibling after this one. | 
| 141 |  |  | void setNextSibling(RefAST n) | 
| 142 |  |  | { | 
| 143 |  |  | right = static_cast<BaseAST*>(static_cast<AST*>(n)); | 
| 144 |  |  | } | 
| 145 |  |  |  | 
| 146 |  |  | /// Set the token text for this node | 
| 147 |  |  | virtual void setText(const ANTLR_USE_NAMESPACE(std)string& txt) | 
| 148 |  |  | { | 
| 149 |  |  | } | 
| 150 |  |  |  | 
| 151 |  |  | /// Set the token type for this node | 
| 152 |  |  | virtual void setType(int type) | 
| 153 |  |  | { | 
| 154 |  |  | } | 
| 155 |  |  |  | 
| 156 |  |  | #ifdef ANTLR_SUPPORT_XML | 
| 157 |  |  | /** print attributes of this node to 'out'. Override to customize XML | 
| 158 |  |  | * output. | 
| 159 |  |  | * @param out the stream to write the AST attributes to. | 
| 160 |  |  | */ | 
| 161 |  |  | virtual bool attributesToStream( ANTLR_USE_NAMESPACE(std)ostream& out ) const; | 
| 162 |  |  | /** Write this subtree to a stream. Overload this one to customize the XML | 
| 163 |  |  | * output for AST derived AST-types | 
| 164 |  |  | * @param output stream | 
| 165 |  |  | */ | 
| 166 |  |  | virtual void toStream( ANTLR_USE_NAMESPACE(std)ostream &out ) const; | 
| 167 |  |  | #endif | 
| 168 |  |  |  | 
| 169 |  |  | /// Return string representation for the AST | 
| 170 |  |  | virtual ANTLR_USE_NAMESPACE(std)string toString() const | 
| 171 |  |  | { | 
| 172 |  |  | return getText(); | 
| 173 |  |  | } | 
| 174 |  |  |  | 
| 175 |  |  | /// Print out a child sibling tree in LISP notation | 
| 176 |  |  | virtual ANTLR_USE_NAMESPACE(std)string toStringList() const; | 
| 177 |  |  | virtual ANTLR_USE_NAMESPACE(std)string toStringTree() const; | 
| 178 |  |  |  | 
| 179 |  |  | static const char* const TYPE_NAME; | 
| 180 |  |  | protected: | 
| 181 |  |  | RefBaseAST down; | 
| 182 |  |  | RefBaseAST right; | 
| 183 |  |  | private: | 
| 184 |  |  | void doWorkForFindAll(ANTLR_USE_NAMESPACE(std)vector<RefAST>& v, | 
| 185 |  |  | RefAST target, | 
| 186 |  |  | bool partialMatch); | 
| 187 |  |  | }; | 
| 188 |  |  |  | 
| 189 |  |  | /** Is node t equal to this in terms of token type and text? | 
| 190 |  |  | */ | 
| 191 |  |  | inline bool BaseAST::equals(RefAST t) const | 
| 192 |  |  | { | 
| 193 |  |  | if (!t) | 
| 194 |  |  | return false; | 
| 195 |  |  | return ((getType() == t->getType()) && (getText() == t->getText())); | 
| 196 |  |  | } | 
| 197 |  |  |  | 
| 198 |  |  | #ifdef ANTLR_CXX_SUPPORTS_NAMESPACE | 
| 199 |  |  | } | 
| 200 |  |  | #endif | 
| 201 |  |  |  | 
| 202 |  |  | #endif //INC_BaseAST_hpp__ |