antlr3commontree.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /** Interface for an ANTLR3 common tree which is what gets
  2. * passed around by the AST producing parser.
  3. */
  4. #ifndef _ANTLR3_COMMON_TREE_HPP
  5. #define _ANTLR3_COMMON_TREE_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<class ImplTraits>
  34. class CommonTree : public ImplTraits::AllocPolicyType
  35. {
  36. public:
  37. typedef typename ImplTraits::AllocPolicyType AllocPolicyType;
  38. typedef typename ImplTraits::StringType StringType;
  39. typedef typename ImplTraits::CommonTokenType CommonTokenType;
  40. typedef typename ImplTraits::TreeType TreeType;
  41. typedef typename ImplTraits::TreeTypePtr TreeTypePtr;
  42. //typedef CommonTree TokenType;
  43. typedef typename AllocPolicyType::template VectorType<TreeTypePtr> ChildrenType;
  44. typedef typename AllocPolicyType::template ListType<TreeTypePtr> ChildListType;
  45. typedef typename ImplTraits::TreeUserDataType UserDataType;
  46. protected:
  47. /// The list of all the children that belong to this node. They are not part of the node
  48. /// as they belong to the common tree node that implements this.
  49. ///
  50. ChildrenType m_children;
  51. /// Start token index that encases this tree
  52. ///
  53. ANTLR_MARKER m_startIndex;
  54. /// End token that encases this tree
  55. ///
  56. ANTLR_MARKER m_stopIndex;
  57. /// A single token, this is the payload for the tree
  58. ///
  59. const CommonTokenType* m_token;
  60. /// Points to the node that has this node as a child.
  61. /// If this is NULL, then this is the root node.
  62. ///
  63. CommonTree* m_parent;
  64. /// What index is this particular node in the child list it
  65. /// belongs to?
  66. ///
  67. ANTLR_INT32 m_childIndex;
  68. public:
  69. CommonTree();
  70. CommonTree( const CommonTokenType* token );
  71. CommonTree( const CommonTree* token );
  72. CommonTree( const CommonTree& ctree );
  73. ~CommonTree();
  74. const CommonTokenType* get_token() const;
  75. void set_token(CommonTokenType const*);
  76. ChildrenType& get_children();
  77. const ChildrenType& get_children() const;
  78. ANTLR_INT32 get_childIndex() const;
  79. TreeType* get_parent() const;
  80. ANTLR_MARKER get_startIndex() const;
  81. void set_startIndex(ANTLR_MARKER index);
  82. ANTLR_MARKER get_stopIndex() const;
  83. void set_stopIndex(ANTLR_MARKER index);
  84. void set_parent( TreeType* parent);
  85. void set_childIndex( ANTLR_INT32 );
  86. void addChild(TreeTypePtr& child);
  87. /// Add all elements of the supplied list as children of this node
  88. ///
  89. void addChildren(const ChildListType& kids);
  90. TreeTypePtr deleteChild(ANTLR_UINT32 i);
  91. /// Delete children from start to stop and replace with t even if t is
  92. /// a list (nil-root tree). Num of children can increase or decrease.
  93. /// For huge child lists, inserting children can force walking rest of
  94. /// children to set their child index; could be slow.
  95. ///
  96. void replaceChildren(ANTLR_INT32 startChildIndex, ANTLR_INT32 stopChildIndex, TreeTypePtr t);
  97. // clone itself
  98. CommonTree* dupNode() const;
  99. // clone itself in pre-allocated storage
  100. CommonTree* dupNode(void *) const;
  101. ANTLR_UINT32 get_charPositionInLine() const;
  102. ANTLR_UINT32 get_line() const;
  103. TreeTypePtr& getChild(ANTLR_UINT32 i);
  104. ANTLR_UINT32 getChildCount() const;
  105. ANTLR_UINT32 getType();
  106. TreeTypePtr& getFirstChildWithType(ANTLR_UINT32 type);
  107. StringType getText();
  108. bool isNilNode();
  109. void setChild(ANTLR_UINT32 i, TreeTypePtr child);
  110. StringType toStringTree();
  111. StringType toString();
  112. void freshenParentAndChildIndexes();
  113. void freshenParentAndChildIndexes(ANTLR_UINT32 offset);
  114. void freshenParentAndChildIndexesDeeply();
  115. void freshenParentAndChildIndexesDeeply(ANTLR_UINT32 offset);
  116. // Prepare tree node to be re-used
  117. void reuse();
  118. UserDataType UserData;
  119. };
  120. }
  121. #include "antlr3commontree.inl"
  122. #endif