1 // selection_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_SPIRIT_SUPPORT_DETAIL_LEXER_PARSER_TREE_SELECTION_NODE_HPP
7 #define BOOST_SPIRIT_SUPPORT_DETAIL_LEXER_PARSER_TREE_SELECTION_NODE_HPP
8 
9 #include "node.hpp"
10 
11 namespace boost
12 {
13 namespace lexer
14 {
15 namespace detail
16 {
17 class selection_node : public node
18 {
19 public:
selection_node(node * left_,node * right_)20     selection_node (node *left_, node *right_) :
21         node (left_->nullable () || right_->nullable ()),
22         _left (left_),
23         _right (right_)
24     {
25         _left->append_firstpos (_firstpos);
26         _right->append_firstpos (_firstpos);
27         _left->append_lastpos (_lastpos);
28         _right->append_lastpos (_lastpos);
29     }
30 
~selection_node()31     virtual ~selection_node ()
32     {
33     }
34 
what_type() const35     virtual type what_type () const
36     {
37         return SELECTION;
38     }
39 
traverse(const_node_stack & node_stack_,bool_stack & perform_op_stack_) const40     virtual bool traverse (const_node_stack &node_stack_,
41         bool_stack &perform_op_stack_) const
42     {
43         perform_op_stack_.push (true);
44 
45         switch (_right->what_type ())
46         {
47         case SEQUENCE:
48         case SELECTION:
49         case ITERATION:
50             perform_op_stack_.push (false);
51             break;
52         default:
53             break;
54         }
55 
56         node_stack_.push (_right);
57         node_stack_.push (_left);
58         return true;
59     }
60 
61 private:
62     // Not owner of these pointers...
63     node *_left;
64     node *_right;
65 
copy_node(node_ptr_vector & node_ptr_vector_,node_stack & new_node_stack_,bool_stack & perform_op_stack_,bool & down_) const66     virtual void copy_node (node_ptr_vector &node_ptr_vector_,
67         node_stack &new_node_stack_, bool_stack &perform_op_stack_,
68         bool &down_) const
69     {
70         if (perform_op_stack_.top ())
71         {
72             node *rhs_ = new_node_stack_.top ();
73 
74             new_node_stack_.pop ();
75 
76             node *lhs_ = new_node_stack_.top ();
77 
78             node_ptr_vector_->push_back (static_cast<selection_node *>(0));
79             node_ptr_vector_->back () = new selection_node (lhs_, rhs_);
80             new_node_stack_.top () = node_ptr_vector_->back ();
81         }
82         else
83         {
84             down_ = true;
85         }
86 
87         perform_op_stack_.pop ();
88     }
89 };
90 }
91 }
92 }
93 
94 #endif
95