antlr3rewriteruletokenstream.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. namespace antlr3 {
  30. /// This is an implementation of a token stream, which is basically an element
  31. /// stream that deals with tokens only.
  32. ///
  33. template<class ImplTraits>
  34. class RewriteRuleTokenStream
  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::ParserType ComponentType;
  41. typedef typename ComponentType::StreamType StreamType;
  42. typedef typename ImplTraits::CommonTokenType TokenType;
  43. typedef typename ImplTraits::TreeType TreeType;
  44. typedef typename ImplTraits::TreeTypePtr TreeTypePtr;
  45. typedef typename AllocPolicyType::template VectorType<const TokenType* > ElementsType;
  46. typedef typename ImplTraits::template RecognizerType< StreamType > RecognizerType;
  47. typedef typename ImplTraits::CommonTokenType ElementType;
  48. public:
  49. RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* description);
  50. RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* description, const TokenType* oneElement);
  51. RewriteRuleTokenStream(TreeAdaptorType* adaptor, const char* description, const ElementsType& elements);
  52. ~RewriteRuleTokenStream();
  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. const TokenType* nextToken();
  59. /// TODO copied from RewriteRuleElementStreamType
  60. /// Add a new pANTLR3_BASE_TREE to this stream
  61. ///
  62. void add(const ElementType* el);
  63. /// When constructing trees, sometimes we need to dup a token or AST
  64. /// subtree. Dup'ing a token means just creating another AST node
  65. /// around it. For trees, you must call the adaptor.dupTree().
  66. ///
  67. ElementType* dup( ElementType* el );
  68. /// Ensure stream emits trees; tokens must be converted to AST nodes.
  69. /// AST nodes can be passed through unmolested.
  70. ///
  71. TreeTypePtr toTree(const ElementType* el);
  72. /// Pointer to the tree adaptor in use for this stream
  73. ///
  74. TreeAdaptorType* m_adaptor;
  75. ElementType nextTree();
  76. typename ElementsType::iterator _next();
  77. /// Returns true if there is a next element available
  78. ///
  79. bool hasNext();
  80. /// Number of elements available in the stream
  81. ///
  82. ANTLR_UINT32 size();
  83. /// Returns the description string if there is one available (check for NULL).
  84. ///
  85. StringType getDescription();
  86. private:
  87. ElementType* dupImpl(typename ImplTraits::CommonTokenType* el);
  88. ElementType* dupImpl(typename ImplTraits::TreeTypePtr el);
  89. /// Cursor 0..n-1. If singleElement!=NULL, cursor is 0 until you next(),
  90. /// which bumps it to 1 meaning no more elements.
  91. ///
  92. typename ElementsType::iterator m_cursor;
  93. /// The element or stream description; usually has name of the token or
  94. /// rule reference that this list tracks. Can include rulename too, but
  95. /// the exception would track that info.
  96. ///
  97. StringType m_elementDescription;
  98. /// The list of tokens or subtrees we are tracking
  99. ///
  100. ElementsType m_elements;
  101. /// Once a node / subtree has been used in a stream, it must be dup'ed
  102. /// from then on. Streams are reset after sub rules so that the streams
  103. /// can be reused in future sub rules. So, reset must set a dirty bit.
  104. /// If dirty, then next() always returns a dup.
  105. ///
  106. bool m_dirty;
  107. };
  108. }
  109. #include "antlr3rewriteruletokenstream.inl"
  110. #endif