1 /** Interface for an ANTLR3 common tree which is what gets 2 * passed around by the AST producing parser. 3 */ 4 5 #ifndef _ANTLR3_COMMON_TREE_HPP 6 #define _ANTLR3_COMMON_TREE_HPP 7 8 // [The "BSD licence"] 9 // Copyright (c) 2005-2009 Gokulakannan Somasundaram, ElectronDB 10 11 // 12 // All rights reserved. 13 // 14 // Redistribution and use in source and binary forms, with or without 15 // modification, are permitted provided that the following conditions 16 // are met: 17 // 1. Redistributions of source code must retain the above copyright 18 // notice, this list of conditions and the following disclaimer. 19 // 2. Redistributions in binary form must reproduce the above copyright 20 // notice, this list of conditions and the following disclaimer in the 21 // documentation and/or other materials provided with the distribution. 22 // 3. The name of the author may not be used to endorse or promote products 23 // derived from this software without specific prior written permission. 24 // 25 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 26 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 27 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 28 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 29 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 30 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 36 #include "antlr3defs.hpp" 37 38 ANTLR_BEGIN_NAMESPACE() 39 40 template<class ImplTraits> 41 class CommonTree : public ImplTraits::AllocPolicyType 42 { 43 public: 44 typedef typename ImplTraits::AllocPolicyType AllocPolicyType; 45 typedef typename ImplTraits::StringType StringType; 46 typedef typename ImplTraits::CommonTokenType CommonTokenType; 47 typedef typename ImplTraits::TreeType TreeType; 48 typedef CommonTree TokenType; 49 typedef typename AllocPolicyType::template VectorType<TreeType*> ChildrenType; 50 typedef typename AllocPolicyType::template ListType<TreeType*> ChildListType; 51 52 private: 53 /// The list of all the children that belong to this node. They are not part of the node 54 /// as they belong to the common tree node that implements this. 55 /// 56 ChildrenType m_children; 57 58 /// This is used to store the current child index position while descending 59 /// and ascending trees as the tree walk progresses. 60 /// 61 ANTLR_MARKER m_savedIndex; 62 63 /// Start token index that encases this tree 64 /// 65 ANTLR_MARKER m_startIndex; 66 67 /// End token that encases this tree 68 /// 69 ANTLR_MARKER m_stopIndex; 70 71 /// A single token, this is the payload for the tree 72 /// 73 CommonTokenType* m_token; 74 75 /// Points to the node that has this node as a child. 76 /// If this is NULL, then this is the root node. 77 /// 78 CommonTree* m_parent; 79 80 /// What index is this particular node in the child list it 81 /// belongs to? 82 /// 83 ANTLR_INT32 m_childIndex; 84 85 public: 86 CommonTree(); 87 CommonTree( CommonTokenType* token ); 88 CommonTree( CommonTree* token ); 89 CommonTree( const CommonTree& ctree ); 90 91 TokenType* get_token() const; 92 ChildrenType& get_children(); 93 const ChildrenType& get_children() const; 94 ChildrenType* get_children_p(); 95 ANTLR_INT32 get_childIndex() const; 96 TreeType* get_parent() const; 97 98 void set_parent( TreeType* parent); 99 void set_childIndex( ANTLR_INT32 ); 100 101 void addChild(TreeType* child); 102 /// Add all elements of the supplied list as children of this node 103 /// 104 void addChildren(const ChildListType& kids); 105 void createChildrenList(); 106 TreeType* deleteChild(ANTLR_UINT32 i); 107 /// Delete children from start to stop and replace with t even if t is 108 /// a list (nil-root tree). Num of children can increase or decrease. 109 /// For huge child lists, inserting children can force walking rest of 110 /// children to set their child index; could be slow. 111 /// 112 void replaceChildren(ANTLR_INT32 startChildIndex, ANTLR_INT32 stopChildIndex, TreeType* t); 113 CommonTree* dupNode() const; 114 TreeType* dupTree(); 115 ANTLR_UINT32 getCharPositionInLine(); 116 TreeType* getChild(ANTLR_UINT32 i); 117 118 ANTLR_UINT32 getChildCount() const; 119 ANTLR_UINT32 getType(); 120 TreeType* getFirstChildWithType(ANTLR_UINT32 type); 121 ANTLR_UINT32 getLine(); 122 StringType getText(); 123 bool isNilNode(); 124 void setChild(ANTLR_UINT32 i, TreeType* child); 125 StringType toStringTree(); 126 StringType toString(); 127 void freshenPACIndexesAll(); 128 void freshenPACIndexes(ANTLR_UINT32 offset); 129 void reuse(); 130 ~CommonTree(); 131 }; 132 133 ANTLR_END_NAMESPACE() 134 135 #include "antlr3commontree.inl" 136 137 #endif 138 139 140