antlr3rewriteruletokenstream.inl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. namespace antlr3 {
  2. template<class ImplTraits>
  3. RewriteRuleTokenStream<ImplTraits>::RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* /*description*/)
  4. : m_adaptor(adaptor)
  5. , m_elements()
  6. , m_dirty(false)
  7. {
  8. m_cursor = m_elements.begin();
  9. }
  10. template<class ImplTraits>
  11. RewriteRuleTokenStream<ImplTraits>::RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* /*description*/,
  12. const TokenType* oneElement
  13. )
  14. : m_adaptor(adaptor)
  15. , m_elements()
  16. , m_dirty(false)
  17. {
  18. if( oneElement != NULL )
  19. this->add( oneElement );
  20. m_cursor = m_elements.begin();
  21. }
  22. template<class ImplTraits>
  23. RewriteRuleTokenStream<ImplTraits>::RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* /*description*/,
  24. const ElementsType& elements
  25. )
  26. : m_adaptor(adaptor)
  27. , m_elements(elements)
  28. , m_dirty(false)
  29. {
  30. m_cursor = m_elements.begin();
  31. }
  32. template<class ImplTraits>
  33. void RewriteRuleTokenStream<ImplTraits>::reset()
  34. {
  35. m_cursor = m_elements.begin();
  36. m_dirty = true;
  37. }
  38. template<class ImplTraits>
  39. void RewriteRuleTokenStream<ImplTraits>::add(const ElementType* el)
  40. {
  41. if ( el == NULL)
  42. return;
  43. m_elements.push_back(el);
  44. m_cursor = m_elements.begin();
  45. }
  46. template<class ImplTraits>
  47. typename RewriteRuleTokenStream<ImplTraits>::ElementsType::iterator
  48. RewriteRuleTokenStream<ImplTraits>::_next()
  49. {
  50. if (m_elements.empty())
  51. {
  52. // This means that the stream is empty
  53. // Caller must cope with this (TODO throw RewriteEmptyStreamException)
  54. return m_elements.end();
  55. }
  56. if (m_dirty || m_cursor == m_elements.end())
  57. {
  58. if( m_elements.size() == 1)
  59. {
  60. // Special case when size is single element, it will just dup a lot
  61. //return this->toTree(m_singleElement);
  62. return m_elements.begin();
  63. }
  64. // Out of elements and the size is not 1, so we cannot assume
  65. // that we just duplicate the entry n times (such as ID ent+ -> ^(ID ent)+)
  66. // This means we ran out of elements earlier than was expected.
  67. //
  68. return m_elements.end(); // Caller must cope with this (TODO throw RewriteEmptyStreamException)
  69. }
  70. // More than just a single element so we extract it from the
  71. // vector.
  72. return m_cursor++;
  73. }
  74. template<class ImplTraits>
  75. typename RewriteRuleTokenStream<ImplTraits>::ElementType
  76. RewriteRuleTokenStream<ImplTraits>::nextTree()
  77. {
  78. ANTLR_UINT32 n = this->size();
  79. if ( m_dirty || ( (m_cursor >=n) && (n==1)) )
  80. {
  81. // if out of elements and size is 1, dup
  82. typename ElementsType::iterator el = this->_next();
  83. return this->dup(*el);
  84. }
  85. // test size above then fetch
  86. typename ElementsType::iterator el = this->_next();
  87. return *el;
  88. }
  89. /*
  90. template<class ImplTraits, class SuperType>
  91. typename RewriteRuleTokenStream<ImplTraits, SuperType>::TokenType*
  92. RewriteRuleTokenStream<ImplTraits, SuperType>::nextToken()
  93. {
  94. return this->_next();
  95. }
  96. template<class ImplTraits, class SuperType>
  97. typename RewriteRuleTokenStream<ImplTraits, SuperType>::TokenType*
  98. RewriteRuleTokenStream<ImplTraits, SuperType>::next()
  99. {
  100. ANTLR_UINT32 s;
  101. s = this->size();
  102. if ( (m_cursor >= s) && (s == 1) )
  103. {
  104. TreeTypePtr el;
  105. el = this->_next();
  106. return this->dup(el);
  107. }
  108. return this->_next();
  109. }
  110. */
  111. template<class ImplTraits>
  112. typename RewriteRuleTokenStream<ImplTraits>::ElementType*
  113. RewriteRuleTokenStream<ImplTraits>::dup( ElementType* element)
  114. {
  115. return dupImpl(element);
  116. }
  117. template<class ImplTraits>
  118. typename RewriteRuleTokenStream<ImplTraits>::ElementType*
  119. RewriteRuleTokenStream<ImplTraits>::dupImpl( typename ImplTraits::CommonTokenType* /*element*/)
  120. {
  121. return NULL; // TODO throw here
  122. }
  123. template<class ImplTraits>
  124. typename RewriteRuleTokenStream<ImplTraits>::ElementType*
  125. RewriteRuleTokenStream<ImplTraits>::dupImpl( typename ImplTraits::TreeTypePtr element)
  126. {
  127. return m_adaptor->dupTree(element);
  128. }
  129. template<class ImplTraits>
  130. typename RewriteRuleTokenStream<ImplTraits>::TreeTypePtr
  131. RewriteRuleTokenStream<ImplTraits>::toTree(const ElementType* element)
  132. {
  133. return m_adaptor->create(element);
  134. }
  135. template<class ImplTraits>
  136. bool
  137. RewriteRuleTokenStream<ImplTraits>::hasNext()
  138. {
  139. return m_cursor != m_elements.end();
  140. }
  141. template<class ImplTraits >
  142. typename RewriteRuleTokenStream<ImplTraits>::TreeTypePtr
  143. RewriteRuleTokenStream<ImplTraits>::nextNode()
  144. {
  145. const TokenType *Token = this->nextToken();
  146. return m_adaptor->create(Token);
  147. }
  148. /// Number of elements available in the stream
  149. ///
  150. template<class ImplTraits>
  151. ANTLR_UINT32 RewriteRuleTokenStream<ImplTraits>::size()
  152. {
  153. return (ANTLR_UINT32)(m_elements.size());
  154. }
  155. template<class ImplTraits>
  156. typename RewriteRuleTokenStream<ImplTraits>::StringType
  157. RewriteRuleTokenStream<ImplTraits>::getDescription()
  158. {
  159. if ( m_elementDescription.empty() )
  160. {
  161. m_elementDescription = "<unknown source>";
  162. }
  163. return m_elementDescription;
  164. }
  165. template<class ImplTraits>
  166. RewriteRuleTokenStream<ImplTraits>::~RewriteRuleTokenStream()
  167. {
  168. // Before placing the stream back in the pool, we
  169. // need to clear any vector it has. This is so any
  170. // free pointers that are associated with the
  171. // entries are called. However, if this particular function is called
  172. // then we know that the entries in the stream are definitely
  173. // tree nodes. Hence we check to see if any of them were nilNodes as
  174. // if they were, we can reuse them.
  175. //
  176. // We have some elements to traverse
  177. //
  178. for (ANTLR_UINT32 i = 0; i < m_elements.size(); i++)
  179. {
  180. const ElementType *tree = m_elements.at(i);
  181. //if ( (tree != NULL) && tree->isNilNode() )
  182. {
  183. // Had to remove this for now, check is not comprehensive enough
  184. // tree->reuse(tree);
  185. }
  186. }
  187. m_elements.clear();
  188. }
  189. template<class ImplTraits>
  190. const typename RewriteRuleTokenStream<ImplTraits>::TokenType*
  191. RewriteRuleTokenStream<ImplTraits>::nextToken()
  192. {
  193. auto retval = this->_next();
  194. if (retval == m_elements.end())
  195. return NULL;
  196. else
  197. return *retval;
  198. }
  199. }