/Src/Dependencies/Boost/boost/spirit/home/support/detail/lexer/parser/tree/node.hpp

http://hadesmem.googlecode.com/ · C++ Header · 188 lines · 146 code · 35 blank · 7 comment · 6 complexity · 5ee085f6caa62935ff6ebbb625a9625c MD5 · raw file

  1. // node.hpp
  2. // Copyright (c) 2007-2009 Ben Hanson (http://www.benhanson.net/)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file licence_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_LEXER_NODE_HPP
  7. #define BOOST_LEXER_NODE_HPP
  8. #include <boost/assert.hpp>
  9. #include "../../containers/ptr_vector.hpp"
  10. #include "../../runtime_error.hpp"
  11. #include "../../size_t.hpp"
  12. #include <stack>
  13. #include <vector>
  14. namespace boost
  15. {
  16. namespace lexer
  17. {
  18. namespace detail
  19. {
  20. class node
  21. {
  22. public:
  23. enum type {LEAF, SEQUENCE, SELECTION, ITERATION, END};
  24. typedef std::stack<bool> bool_stack;
  25. typedef std::stack<node *> node_stack;
  26. // stack and vector not owner of node pointers
  27. typedef std::stack<const node *> const_node_stack;
  28. typedef std::vector<node *> node_vector;
  29. typedef ptr_vector<node> node_ptr_vector;
  30. node () :
  31. _nullable (false)
  32. {
  33. }
  34. node (const bool nullable_) :
  35. _nullable (nullable_)
  36. {
  37. }
  38. virtual ~node ()
  39. {
  40. }
  41. bool nullable () const
  42. {
  43. return _nullable;
  44. }
  45. void append_firstpos (node_vector &firstpos_) const
  46. {
  47. firstpos_.insert (firstpos_.end (),
  48. _firstpos.begin (), _firstpos.end ());
  49. }
  50. void append_lastpos (node_vector &lastpos_) const
  51. {
  52. lastpos_.insert (lastpos_.end (),
  53. _lastpos.begin (), _lastpos.end ());
  54. }
  55. virtual void append_followpos (const node_vector &/*followpos_*/)
  56. {
  57. throw runtime_error ("Internal error node::append_followpos()");
  58. }
  59. node *copy (node_ptr_vector &node_ptr_vector_) const
  60. {
  61. node *new_root_ = 0;
  62. const_node_stack node_stack_;
  63. bool_stack perform_op_stack_;
  64. bool down_ = true;
  65. node_stack new_node_stack_;
  66. node_stack_.push (this);
  67. while (!node_stack_.empty ())
  68. {
  69. while (down_)
  70. {
  71. down_ = node_stack_.top ()->traverse (node_stack_,
  72. perform_op_stack_);
  73. }
  74. while (!down_ && !node_stack_.empty ())
  75. {
  76. const node *top_ = node_stack_.top ();
  77. top_->copy_node (node_ptr_vector_, new_node_stack_,
  78. perform_op_stack_, down_);
  79. if (!down_) node_stack_.pop ();
  80. }
  81. }
  82. BOOST_ASSERT(new_node_stack_.size () == 1);
  83. new_root_ = new_node_stack_.top ();
  84. new_node_stack_.pop ();
  85. return new_root_;
  86. }
  87. virtual type what_type () const = 0;
  88. virtual bool traverse (const_node_stack &node_stack_,
  89. bool_stack &perform_op_stack_) const = 0;
  90. node_vector &firstpos ()
  91. {
  92. return _firstpos;
  93. }
  94. const node_vector &firstpos () const
  95. {
  96. return _firstpos;
  97. }
  98. // _lastpos modified externally, so not const &
  99. node_vector &lastpos ()
  100. {
  101. return _lastpos;
  102. }
  103. virtual bool end_state () const
  104. {
  105. return false;
  106. }
  107. virtual std::size_t id () const
  108. {
  109. throw runtime_error ("Internal error node::id()");
  110. }
  111. virtual std::size_t unique_id () const
  112. {
  113. throw runtime_error ("Internal error node::unique_id()");
  114. }
  115. virtual std::size_t lexer_state () const
  116. {
  117. throw runtime_error ("Internal error node::state()");
  118. }
  119. virtual std::size_t token () const
  120. {
  121. throw runtime_error ("Internal error node::token()");
  122. }
  123. virtual void greedy (const bool /*greedy_*/)
  124. {
  125. throw runtime_error ("Internal error node::token(bool)");
  126. }
  127. virtual bool greedy () const
  128. {
  129. throw runtime_error ("Internal error node::token()");
  130. }
  131. virtual const node_vector &followpos () const
  132. {
  133. throw runtime_error ("Internal error node::followpos()");
  134. }
  135. virtual node_vector &followpos ()
  136. {
  137. throw runtime_error ("Internal error node::followpos()");
  138. }
  139. protected:
  140. const bool _nullable;
  141. node_vector _firstpos;
  142. node_vector _lastpos;
  143. virtual void copy_node (node_ptr_vector &node_ptr_vector_,
  144. node_stack &new_node_stack_, bool_stack &perform_op_stack_,
  145. bool &down_) const = 0;
  146. private:
  147. node (node const &); // No copy construction.
  148. node &operator = (node const &); // No assignment.
  149. };
  150. }
  151. }
  152. }
  153. #endif