antlr3rewritestreams.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #ifndef ANTLR3REWRITESTREAM_HPP
  2. #define ANTLR3REWRITESTREAM_HPP
  3. // [The "BSD licence"]
  4. // Copyright (c) 2005-2009 Gokulakannan Somasundaram, ElectronDB
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions
  10. // are met:
  11. // 1. Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // 2. Redistributions in binary form must reproduce the above copyright
  14. // notice, this list of conditions and the following disclaimer in the
  15. // documentation and/or other materials provided with the distribution.
  16. // 3. The name of the author may not be used to endorse or promote products
  17. // derived from this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  20. // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. /// A generic list of elements tracked in an alternative to be used in
  30. /// a -> rewrite rule.
  31. ///
  32. /// In the C implementation, all tree oriented streams return a pointer to
  33. /// the same type: pANTLR3_BASE_TREE. Anything that has subclassed from this
  34. /// still passes this type, within which there is a super pointer, which points
  35. /// to it's own data and methods. Hence we do not need to implement this as
  36. /// the equivalent of an abstract class, but just fill in the appropriate interface
  37. /// as usual with this model.
  38. ///
  39. /// Once you start next()ing, do not try to add more elements. It will
  40. /// break the cursor tracking I believe.
  41. ///
  42. ///
  43. /// \see #pANTLR3_REWRITE_RULE_NODE_STREAM
  44. /// \see #pANTLR3_REWRITE_RULE_ELEMENT_STREAM
  45. /// \see #pANTLR3_REWRITE_RULE_SUBTREE_STREAM
  46. ///
  47. /// TODO: add mechanism to detect/puke on modification after reading from stream
  48. ///
  49. namespace antlr3 {
  50. template<class ImplTraits, class ElementType>
  51. //template<class ImplTraits>
  52. class RewriteRuleElementStream : public ImplTraits::AllocPolicyType
  53. {
  54. public:
  55. //typedef typename ElementTypePtr::element_type ElementType; unique_ptr
  56. //typedef typename ImplTraits::TreeType TreeType;
  57. typedef typename ImplTraits::AllocPolicyType AllocPolicyType;
  58. typedef typename ImplTraits::TreeAdaptorType TreeAdaptorType;
  59. //typedef typename ImplTraits::template RecognizerType< typename SuperType::StreamType > RecognizerType;
  60. typedef typename ImplTraits::StringType StringType;
  61. typedef typename AllocPolicyType::template VectorType< ElementType* > ElementsType;
  62. protected:
  63. /// The list of tokens or subtrees we are tracking
  64. ///
  65. ElementsType m_elements;
  66. /// The element or stream description; usually has name of the token or
  67. /// rule reference that this list tracks. Can include rulename too, but
  68. /// the exception would track that info.
  69. ///
  70. StringType m_elementDescription;
  71. private:
  72. ElementType* dupImpl(typename ImplTraits::CommonTokenType* el);
  73. ElementType* dupImpl(typename ImplTraits::TreeTypePtr el);
  74. /// Pointer to the tree adaptor in use for this stream
  75. ///
  76. TreeAdaptorType* m_adaptor;
  77. /// Cursor 0..n-1. If singleElement!=NULL, cursor is 0 until you next(),
  78. /// which bumps it to 1 meaning no more elements.
  79. ///
  80. ANTLR_UINT32 m_cursor;
  81. /// Once a node / subtree has been used in a stream, it must be dup'ed
  82. /// from then on. Streams are reset after sub rules so that the streams
  83. /// can be reused in future sub rules. So, reset must set a dirty bit.
  84. /// If dirty, then next() always returns a dup.
  85. ///
  86. bool m_dirty;
  87. public:
  88. RewriteRuleElementStream(TreeAdaptorType* adaptor, const char* description);
  89. RewriteRuleElementStream(TreeAdaptorType* adaptor, const char* description, const ElementType* oneElement);
  90. RewriteRuleElementStream(TreeAdaptorType* adaptor, const char* description, const ElementsType& elements);
  91. ~RewriteRuleElementStream();
  92. // Methods
  93. /// Reset the condition of this stream so that it appears we have
  94. /// not consumed any of its elements. Elements themselves are untouched.
  95. ///
  96. void reset();
  97. /// Add a new pANTLR3_BASE_TREE to this stream
  98. ///
  99. void add(ElementType* el);
  100. /// Return the next element in the stream. If out of elements, throw
  101. /// an exception unless size()==1. If size is 1, then return elements[0].
  102. ///
  103. //TokenType* next();
  104. ElementType nextTree();
  105. //TokenType* nextToken();
  106. ElementType* _next();
  107. /// When constructing trees, sometimes we need to dup a token or AST
  108. /// subtree. Dup'ing a token means just creating another AST node
  109. /// around it. For trees, you must call the adaptor.dupTree().
  110. ///
  111. ElementType* dup( ElementType* el );
  112. /// Ensure stream emits trees; tokens must be converted to AST nodes.
  113. /// AST nodes can be passed through unmolested.
  114. ///
  115. ElementType* toTree(ElementType* el);
  116. /// Returns true if there is a next element available
  117. ///
  118. bool hasNext();
  119. /// Treat next element as a single node even if it's a subtree.
  120. /// This is used instead of next() when the result has to be a
  121. /// tree root node. Also prevents us from duplicating recently-added
  122. /// children; e.g., ^(type ID)+ adds ID to type and then 2nd iteration
  123. /// must dup the type node, but ID has been added.
  124. ///
  125. /// Referencing to a rule result twice is ok; dup entire tree as
  126. /// we can't be adding trees; e.g., expr expr.
  127. ///
  128. //TreeTypePtr nextNode();
  129. /// Number of elements available in the stream
  130. ///
  131. ANTLR_UINT32 size();
  132. /// Returns the description string if there is one available (check for NULL).
  133. ///
  134. StringType getDescription();
  135. protected:
  136. void init(TreeAdaptorType* adaptor, const char* description);
  137. };
  138. /// This is an implementation of a token stream, which is basically an element
  139. /// stream that deals with tokens only.
  140. ///
  141. template<class ImplTraits>
  142. //class RewriteRuleTokenStream : public ImplTraits::template RewriteRuleElementStreamType< typename ImplTraits::ParserType>
  143. class RewriteRuleTokenStream
  144. //: public ImplTraits::template RewriteStreamType< const typename ImplTraits::CommonTokenType >
  145. {
  146. public:
  147. typedef typename ImplTraits::AllocPolicyType AllocPolicyType;
  148. typedef typename ImplTraits::TreeAdaptorType TreeAdaptorType;
  149. typedef typename ImplTraits::ParserType ComponentType;
  150. typedef typename ComponentType::StreamType StreamType;
  151. typedef typename ImplTraits::CommonTokenType TokenType;
  152. typedef typename ImplTraits::TreeType TreeType;
  153. typedef typename ImplTraits::TreeTypePtr TreeTypePtr;
  154. typedef typename AllocPolicyType::template VectorType< TokenType* > ElementsType;
  155. typedef typename ImplTraits::template RecognizerType< StreamType > RecognizerType;
  156. typedef typename ImplTraits::template RewriteStreamType< const typename ImplTraits::CommonTokenType > BaseType;
  157. public:
  158. RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* description);
  159. RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* description, const TokenType* oneElement);
  160. RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* description, const ElementsType& elements);
  161. TreeTypePtr nextNode();
  162. TokenType* nextToken();
  163. /// TODO copied from RewriteRuleElementStreamType
  164. /// Add a new pANTLR3_BASE_TREE to this stream
  165. ///
  166. typedef typename ImplTraits::CommonTokenType ElementType;
  167. void add(const ElementType* el);
  168. /// Pointer to the tree adaptor in use for this stream
  169. ///
  170. TreeAdaptorType* m_adaptor;
  171. ElementType* _next();
  172. private:
  173. //TreeTypePtr nextNodeToken();
  174. };
  175. /// This is an implementation of a subtree stream which is a set of trees
  176. /// modeled as an element stream.
  177. ///
  178. template<class ImplTraits>
  179. //class RewriteRuleSubtreeStream : public ImplTraits::template RewriteStreamType< typename ImplTraits::TreeParserType>
  180. class RewriteRuleSubtreeStream
  181. //: public ImplTraits::template RewriteStreamType< typename ImplTraits::TreeType >
  182. {
  183. public:
  184. typedef typename ImplTraits::AllocPolicyType AllocPolicyType;
  185. typedef typename ImplTraits::TreeAdaptorType TreeAdaptorType;
  186. typedef typename ImplTraits::TreeParserType ComponentType;
  187. typedef typename ComponentType::StreamType StreamType;
  188. typedef typename ImplTraits::TreeType TreeType;
  189. typedef typename ImplTraits::TreeTypePtr TreeTypePtr;
  190. typedef TreeType TokenType;
  191. typedef typename ImplTraits::template RecognizerType< StreamType > RecognizerType;
  192. typedef typename AllocPolicyType::template VectorType< TokenType* > ElementsType;
  193. typedef typename ImplTraits::template RewriteStreamType< typename ImplTraits::TreeType > BaseType;
  194. RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description);
  195. RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description, TreeTypePtr& oneElement);
  196. RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description, const ElementsType& elements);
  197. TreeTypePtr nextNode(TreeTypePtr);
  198. /// TODO copied from RewriteRuleElementStreamType
  199. /// Add a new pANTLR3_BASE_TREE to this stream
  200. ///
  201. void add(TreeTypePtr& el);
  202. bool hasNext();
  203. TreeTypePtr& nextTree();
  204. void reset();
  205. protected:
  206. TreeTypePtr dup( TreeTypePtr el );
  207. private:
  208. TreeTypePtr dupTree( TreeTypePtr el );
  209. };
  210. /* TODO This class is probably used in TreeParser only
  211. * Notes about Java target
  212. * - these classes reimplement only dup and toTree methods:
  213. * base ElementStr
  214. * abstract dup
  215. * toTree(Object e) { return e; }
  216. * TokenStr
  217. * dup { throw }
  218. * toTree(Object e) { return e; }
  219. * SubTreeStr
  220. * dup(Object e) { return adaptor.dupTree }
  221. * NodeStr
  222. * dup { throw }
  223. * toTree(Object e) { return adaptor.dupNode }
  224. * See: RewriteRuleElementStream::dup, RewriteRuleElementStream::dupImpl
  225. *
  226. * There should 3 types of specializations for RewriteRuleElementStreamType (which is not defined yet)
  227. * ATM: RewriteRuleElementStreamType is replaced with ImplTraits::template RewriteStreamType
  228. *
  229. /// This is an implementation of a node stream, which is basically an element
  230. /// stream that deals with tree nodes only.
  231. ///
  232. template<class ImplTraits>
  233. //class RewriteRuleNodeStream : public ImplTraits::template RewriteStreamType< typename ImplTraits::TreeParserType>
  234. class RewriteRuleNodeStream : public ImplTraits::template RewriteStreamType< typename ImplTraits::TreeType >
  235. {
  236. public:
  237. typedef typename ImplTraits::AllocPolicyType AllocPolicyType;
  238. typedef typename ImplTraits::TreeAdaptorType TreeAdaptorType;
  239. typedef typename ImplTraits::TreeParserType ComponentType;
  240. typedef typename ComponentType::StreamType StreamType;
  241. typedef typename ImplTraits::TreeType TreeType;
  242. typedef TreeType TokenType;
  243. typedef typename ImplTraits::template RecognizerType< StreamType > RecognizerType;
  244. typedef typename AllocPolicyType::template VectorType< TokenType* > ElementsType;
  245. typedef typename ImplTraits::template RewriteRuleElementStreamType< typename ImplTraits::TreeType > BaseType;
  246. public:
  247. RewriteRuleNodeStream(TreeAdaptorType* adaptor, const char* description);
  248. RewriteRuleNodeStream(TreeAdaptorType* adaptor, const char* description, TokenType* oneElement);
  249. RewriteRuleNodeStream(TreeAdaptorType* adaptor, const char* description, const ElementsType& elements);
  250. protected:
  251. TreeTypePtr toTree(TreeTypePtr element);
  252. private:
  253. TreeTypePtr toTreeNode(TreeTypePtr element);
  254. };
  255. */
  256. }
  257. #include "antlr3rewritestreams.inl"
  258. #endif