antlr3commontreeadaptor.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /** \file
  2. * Definition of the ANTLR3 common tree adaptor.
  3. */
  4. #ifndef _ANTLR3_COMMON_TREE_ADAPTOR_HPP
  5. #define _ANTLR3_COMMON_TREE_ADAPTOR_HPP
  6. // [The "BSD licence"]
  7. // Copyright (c) 2005-2009 Gokulakannan Somasundaram, ElectronDB
  8. //
  9. // All rights reserved.
  10. //
  11. // Redistribution and use in source and binary forms, with or without
  12. // modification, are permitted provided that the following conditions
  13. // are met:
  14. // 1. Redistributions of source code must retain the above copyright
  15. // notice, this list of conditions and the following disclaimer.
  16. // 2. Redistributions in binary form must reproduce the above copyright
  17. // notice, this list of conditions and the following disclaimer in the
  18. // documentation and/or other materials provided with the distribution.
  19. // 3. The name of the author may not be used to endorse or promote products
  20. // derived from this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  23. // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  24. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25. // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  27. // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  31. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. namespace antlr3 {
  33. template <typename ImplTraits> class CommonTreeStore;
  34. /** Helper class for unique_ptr. Implements deleter for instances of unique_ptr
  35. While building AST tree dangling pointers are automatically put back into pool
  36. */
  37. template <typename ImplTraits>
  38. class CommonResourcePoolManager
  39. {
  40. public:
  41. typedef typename ImplTraits::TreeType TreeType;
  42. CommonResourcePoolManager(CommonTreeStore<ImplTraits> * pool);
  43. CommonResourcePoolManager();
  44. ~CommonResourcePoolManager();
  45. void operator()(TreeType* releasedResource) const;
  46. private:
  47. CommonTreeStore<ImplTraits> * m_pool;
  48. };
  49. template <class ImplTraits>
  50. class CommonTreeStore
  51. {
  52. public:
  53. typedef typename ImplTraits::TreeType TreeType;
  54. typedef typename ImplTraits::CommonTokenType CommonTokenType;
  55. typedef CommonResourcePoolManager<ImplTraits> ResourcePoolManagerType;
  56. typedef std::unique_ptr<TreeType, CommonResourcePoolManager<ImplTraits> > TreeTypePtr;
  57. //typedef typename ImplTraits::TreeTypePtr TreeTypePtr;
  58. CommonTreeStore();
  59. TreeTypePtr create();
  60. CommonTokenType* createToken();
  61. CommonTokenType* createToken(const CommonTokenType* fromToken);
  62. // Return special kind of NULL pointer wrapped into TreeTypePtr
  63. TreeTypePtr
  64. null()
  65. {
  66. return TreeTypePtr(NULL, m_manager);
  67. }
  68. std::size_t size() const
  69. {
  70. return m_treeStore.size();
  71. }
  72. template <class T>
  73. bool contains(const std::vector<T> &vec, const T &value)
  74. {
  75. return std::find(vec.begin(), vec.end(), value) != vec.end();
  76. }
  77. protected:
  78. template<typename> friend class CommonResourcePoolManager;
  79. template<typename> friend class CommonTreeAdaptor;
  80. void reuse(TreeType* releasedResource);
  81. std::vector<TreeType *> m_recycleBin;
  82. std::vector<std::unique_ptr<TreeType> > m_treeStore;
  83. std::vector<std::unique_ptr<CommonTokenType> > m_tokenStore;
  84. ResourcePoolManagerType m_manager;
  85. };
  86. template<class ImplTraits>
  87. class CommonTreeAdaptor
  88. : public ImplTraits::AllocPolicyType
  89. , public CommonTreeStore<ImplTraits>
  90. {
  91. public:
  92. typedef typename ImplTraits::StringType StringType;
  93. typedef typename ImplTraits::TreeType TreeType;
  94. typedef typename ImplTraits::TreeTypePtr TreeTypePtr;
  95. typedef typename TreeType::ChildrenType ChildrenType;
  96. typedef TreeType TokenType;
  97. typedef typename ImplTraits::CommonTokenType CommonTokenType;
  98. typedef typename ImplTraits::DebugEventListenerType DebuggerType;
  99. typedef typename ImplTraits::TokenStreamType TokenStreamType;
  100. typedef CommonTreeStore<ImplTraits> TreeStoreType;
  101. public:
  102. //The parameter is there only to provide uniform constructor interface
  103. CommonTreeAdaptor(DebuggerType* dbg = nullptr);
  104. TreeTypePtr nilNode();
  105. TreeTypePtr dupTree( const TreeTypePtr& tree);
  106. TreeTypePtr dupTree( const TreeType* tree);
  107. TreeTypePtr dupNode(const TreeTypePtr& treeNode);
  108. TreeTypePtr dupNode(const TreeType* treeNode);
  109. void addChild( TreeTypePtr& t, TreeTypePtr& child);
  110. void addChild( TreeTypePtr& t, TreeTypePtr&& child);
  111. void addChildToken( TreeTypePtr& t, CommonTokenType* child);
  112. void setParent( TreeTypePtr& child, TreeType* parent);
  113. TreeType* getParent( TreeTypePtr& child);
  114. TreeTypePtr errorNode( CommonTokenType* tnstream, const CommonTokenType* startToken, const CommonTokenType* stopToken);
  115. bool isNilNode( TreeTypePtr& t);
  116. TreeTypePtr becomeRoot( TreeTypePtr& newRoot, TreeTypePtr& oldRoot);
  117. TreeTypePtr becomeRoot( TreeTypePtr&& newRoot, TreeTypePtr& oldRoot);
  118. TreeTypePtr becomeRootToken(CommonTokenType* newRoot, TreeTypePtr& oldRoot);
  119. TreeTypePtr rulePostProcessing( TreeTypePtr& root);
  120. TreeTypePtr create( CommonTokenType const* payload);
  121. TreeTypePtr create( ANTLR_UINT32 tokenType, const CommonTokenType* fromToken);
  122. TreeTypePtr create( ANTLR_UINT32 tokenType, const CommonTokenType* fromToken, const char* text);
  123. TreeTypePtr create( ANTLR_UINT32 tokenType, const CommonTokenType* fromToken, StringType const& text);
  124. TreeTypePtr create( ANTLR_UINT32 tokenType, const char* text);
  125. TreeTypePtr create( ANTLR_UINT32 tokenType, StringType const& text);
  126. CommonTokenType* createToken( ANTLR_UINT32 tokenType, const char* text);
  127. CommonTokenType* createToken( ANTLR_UINT32 tokenType, StringType const& text);
  128. CommonTokenType* createToken( const CommonTokenType* fromToken);
  129. ANTLR_UINT32 getType( TreeTypePtr& t);
  130. StringType getText( TreeTypePtr& t);
  131. TreeTypePtr& getChild( TreeTypePtr& t, ANTLR_UINT32 i);
  132. void setChild( TreeTypePtr& t, ANTLR_UINT32 i, TreeTypePtr& child);
  133. void deleteChild( TreeTypePtr& t, ANTLR_UINT32 i);
  134. void setChildIndex( TreeTypePtr& t, ANTLR_INT32 i);
  135. ANTLR_INT32 getChildIndex( TreeTypePtr& t);
  136. ANTLR_UINT32 getChildCount( TreeTypePtr&);
  137. ANTLR_UINT64 getUniqueID( TreeTypePtr&);
  138. CommonTokenType* getToken( TreeTypePtr& t);
  139. void setTokenBoundaries( TreeTypePtr& t, const CommonTokenType* startToken, const CommonTokenType* stopToken);
  140. ANTLR_MARKER getTokenStartIndex( TreeTypePtr& t);
  141. ANTLR_MARKER getTokenStopIndex( TreeTypePtr& t);
  142. /// Produce a DOT (see graphviz freeware suite) from a base tree
  143. ///
  144. StringType makeDot( TreeTypePtr& theTree);
  145. /// Replace from start to stop child index of parent with t, which might
  146. /// be a list. Number of children may be different
  147. /// after this call.
  148. ///
  149. /// If parent is null, don't do anything; must be at root of overall tree.
  150. /// Can't replace whatever points to the parent externally. Do nothing.
  151. ///
  152. void replaceChildren( TreeTypePtr parent, ANTLR_INT32 startChildIndex,
  153. ANTLR_INT32 stopChildIndex, TreeTypePtr t);
  154. ~CommonTreeAdaptor();
  155. protected:
  156. TreeTypePtr dupTreeImpl( const TreeType* root, TreeType* parent);
  157. void defineDotNodes(TreeTypePtr t, const StringType& dotSpec);
  158. void defineDotEdges(TreeTypePtr t, const StringType& dotSpec);
  159. };
  160. //If someone can override the CommonTreeAdaptor at the compile time, that will be
  161. //inherited here. Still you can choose to override the DebugTreeAdaptor, if you wish to
  162. //change the DebugTreeAdaptor
  163. template<class ImplTraits>
  164. class DebugTreeAdaptor : public ImplTraits::CommonTreeAdaptorType
  165. {
  166. public:
  167. //DebugEventListener implements functionality through virtual functions
  168. //the template parameter is required for pointing back at the adaptor
  169. typedef typename ImplTraits::DebugEventListener DebuggerType;
  170. typedef typename ImplTraits::TreeType TreeType;
  171. typedef typename ImplTraits::TreeTypePtr TreeTypePtr;
  172. typedef typename ImplTraits::CommonTokenType CommonTokenType;
  173. typedef typename ImplTraits::CommonTreeAdaptorType super;
  174. private:
  175. /// If set to something other than NULL, then this structure is
  176. /// points to an instance of the debugger interface. In general, the
  177. /// debugger is only referenced internally in recovery/error operations
  178. /// so that it does not cause overhead by having to check this pointer
  179. /// in every function/method
  180. ///
  181. DebuggerType* m_debugger;
  182. public:
  183. DebugTreeAdaptor( DebuggerType* debugger );
  184. void setDebugEventListener( DebuggerType* debugger);
  185. TreeTypePtr nilNode();
  186. void addChild(TreeTypePtr& t, TreeTypePtr& child);
  187. void addChildToken(TreeTypePtr& t, CommonTokenType* child);
  188. TreeTypePtr becomeRoot( TreeTypePtr& newRootTree, TreeTypePtr& oldRootTree );
  189. TreeTypePtr becomeRootToken( CommonTokenType* newRoot, TreeTypePtr& oldRoot);
  190. TreeTypePtr createTypeToken(ANTLR_UINT32 tokenType, CommonTokenType* fromToken);
  191. TreeTypePtr createTypeTokenText(ANTLR_UINT32 tokenType, CommonTokenType* fromToken, ANTLR_UINT8* text);
  192. TreeTypePtr createTypeText( ANTLR_UINT32 tokenType, ANTLR_UINT8* text);
  193. TreeTypePtr dupTree( const TreeTypePtr& tree);
  194. TreeTypePtr dupTree( const TreeType* tree);
  195. /// Sends the required debugging events for duplicating a tree
  196. /// to the debugger.
  197. ///
  198. void simulateTreeConstruction(TreeTypePtr& tree);
  199. };
  200. }
  201. #include "antlr3commontreeadaptor.inl"
  202. #endif