antlr3rewriterulesubtreestream.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #ifndef ANTLR3REWRITERULESUBTREESTREAM_HPP
  2. #define ANTLR3REWRITERULESUBTREESTREAM_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. namespace antlr3 {
  30. /// This is an implementation of a subtree stream which is a set of trees
  31. /// modeled as an element stream.
  32. ///
  33. template<class ImplTraits>
  34. class RewriteRuleSubtreeStream
  35. {
  36. public:
  37. typedef typename ImplTraits::StringType StringType;
  38. typedef typename ImplTraits::AllocPolicyType AllocPolicyType;
  39. typedef typename ImplTraits::TreeAdaptorType TreeAdaptorType;
  40. typedef typename ImplTraits::TreeParserType ComponentType;
  41. typedef typename ComponentType::StreamType StreamType;
  42. typedef typename ImplTraits::TreeType TreeType;
  43. typedef typename ImplTraits::TreeTypePtr TreeTypePtr;
  44. typedef TreeType TokenType;
  45. typedef typename ImplTraits::template RecognizerType< StreamType > RecognizerType;
  46. typedef typename AllocPolicyType::template VectorType< TreeTypePtr > ElementsType;
  47. typedef typename ImplTraits::TreeType ElementType;
  48. RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description);
  49. RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description, TreeType* oneElement);
  50. RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description, TreeTypePtr& oneElement);
  51. RewriteRuleSubtreeStream(TreeAdaptorType* adaptor, const char* description, const ElementsType& elements);
  52. ~RewriteRuleSubtreeStream();
  53. /// Reset the condition of this stream so that it appears we have
  54. /// not consumed any of its elements. Elements themselves are untouched.
  55. ///
  56. void reset();
  57. TreeTypePtr nextNode();
  58. /// TODO copied from RewriteRuleElementStreamType
  59. /// Add a new pANTLR3_BASE_TREE to this stream
  60. ///
  61. void add(TreeTypePtr& el);
  62. bool hasNext();
  63. TreeTypePtr nextTree();
  64. typename ElementsType::iterator _next();
  65. ElementType* toTree(ElementType* el);
  66. /// Number of elements available in the stream
  67. ///
  68. ANTLR_UINT32 size();
  69. /// Returns the description string if there is one available (check for NULL).
  70. ///
  71. StringType getDescription();
  72. protected:
  73. TreeTypePtr dup(const TreeTypePtr& el );
  74. TreeTypePtr dup(const TreeType* el );
  75. TreeTypePtr& leftestNode(TreeTypePtr& node) const;
  76. private:
  77. /// Pointer to the tree adaptor in use for this stream
  78. ///
  79. TreeAdaptorType* m_adaptor;
  80. /// Cursor 0..n-1. If singleElement!=NULL, cursor is 0 until you next(),
  81. /// which bumps it to 1 meaning no more elements.
  82. ///
  83. typename ElementsType::iterator m_cursor;
  84. /// The element or stream description; usually has name of the token or
  85. /// rule reference that this list tracks. Can include rulename too, but
  86. /// the exception would track that info.
  87. ///
  88. StringType m_elementDescription;
  89. /// The list of tokens or subtrees we are tracking
  90. ///
  91. ElementsType m_elements;
  92. TreeTypePtr dupTree(const TreeTypePtr& el );
  93. TreeTypePtr dupTree(const TreeType* el );
  94. /// Once a node / subtree has been used in a stream, it must be dup'ed
  95. /// from then on. Streams are reset after sub rules so that the streams
  96. /// can be reused in future sub rules. So, reset must set a dirty bit.
  97. /// If dirty, then next() always returns a dup.
  98. ///
  99. bool m_dirty;
  100. };
  101. }
  102. #include "antlr3rewriterulesubtreestream.inl"
  103. #endif