2 // Copyright (c) 2007-2009 Ben Hanson (http://www.benhanson.net/)
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
9 #include <boost/assert.hpp>
10 #include "../../containers/ptr_vector.hpp"
11 #include "../../runtime_error.hpp"
12 #include "../../size_t.hpp"
25 enum type {LEAF, SEQUENCE, SELECTION, ITERATION, END};
27 typedef std::stack<bool> bool_stack;
28 typedef std::stack<node *> node_stack;
29 // stack and vector not owner of node pointers
30 typedef std::stack<const node *> const_node_stack;
31 typedef std::vector<node *> node_vector;
32 typedef ptr_vector<node> node_ptr_vector;
39 node (const bool nullable_) :
48 bool nullable () const
53 void append_firstpos (node_vector &firstpos_) const
55 firstpos_.insert (firstpos_.end (),
56 _firstpos.begin (), _firstpos.end ());
59 void append_lastpos (node_vector &lastpos_) const
61 lastpos_.insert (lastpos_.end (),
62 _lastpos.begin (), _lastpos.end ());
65 virtual void append_followpos (const node_vector &/*followpos_*/)
67 throw runtime_error ("Internal error node::append_followpos()");
70 node *copy (node_ptr_vector &node_ptr_vector_) const
73 const_node_stack node_stack_;
74 bool_stack perform_op_stack_;
76 node_stack new_node_stack_;
78 node_stack_.push (this);
80 while (!node_stack_.empty ())
84 down_ = node_stack_.top ()->traverse (node_stack_,
88 while (!down_ && !node_stack_.empty ())
90 const node *top_ = node_stack_.top ();
92 top_->copy_node (node_ptr_vector_, new_node_stack_,
93 perform_op_stack_, down_);
95 if (!down_) node_stack_.pop ();
99 BOOST_ASSERT(new_node_stack_.size () == 1);
100 new_root_ = new_node_stack_.top ();
101 new_node_stack_.pop ();
105 virtual type what_type () const = 0;
107 virtual bool traverse (const_node_stack &node_stack_,
108 bool_stack &perform_op_stack_) const = 0;
110 node_vector &firstpos ()
115 const node_vector &firstpos () const
120 // _lastpos modified externally, so not const &
121 node_vector &lastpos ()
126 virtual bool end_state () const
131 virtual std::size_t id () const
133 throw runtime_error ("Internal error node::id()");
136 virtual std::size_t unique_id () const
138 throw runtime_error ("Internal error node::unique_id()");
141 virtual std::size_t lexer_state () const
143 throw runtime_error ("Internal error node::state()");
146 virtual std::size_t token () const
148 throw runtime_error ("Internal error node::token()");
151 virtual void greedy (const bool /*greedy_*/)
153 throw runtime_error ("Internal error node::token(bool)");
156 virtual bool greedy () const
158 throw runtime_error ("Internal error node::token()");
161 virtual const node_vector &followpos () const
163 throw runtime_error ("Internal error node::followpos()");
166 virtual node_vector &followpos ()
168 throw runtime_error ("Internal error node::followpos()");
172 const bool _nullable;
173 node_vector _firstpos;
174 node_vector _lastpos;
176 virtual void copy_node (node_ptr_vector &node_ptr_vector_,
177 node_stack &new_node_stack_, bool_stack &perform_op_stack_,
178 bool &down_) const = 0;
181 node (node const &); // No copy construction.
182 node &operator = (node const &); // No assignment.