1 // Copyright (c) 2001-2011 Hartmut Kaiser
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #if !defined(BOOST_SPIRIT_LEX_LEXER_SEMANTIC_ACTION_DATA_JUN_10_2009_0417PM)
7 #define BOOST_SPIRIT_LEX_LEXER_SEMANTIC_ACTION_DATA_JUN_10_2009_0417PM
13 #include <boost/spirit/home/lex/lexer/pass_flags.hpp>
14 #include <boost/mpl/bool.hpp>
15 #include <boost/function.hpp>
18 namespace boost { namespace spirit { namespace lex { namespace lexertl
22 ///////////////////////////////////////////////////////////////////////
23 template <typename Iterator, typename SupportsState, typename Data>
24 struct semantic_actions;
26 // This specialization of semantic_actions will be used if the token
27 // type (lexer definition) does not support states, which simplifies
28 // the data structures used to store the semantic action function
30 template <typename Iterator, typename Data>
31 struct semantic_actions<Iterator, mpl::false_, Data>
33 typedef void functor_type(Iterator&, Iterator&
34 , BOOST_SCOPED_ENUM(pass_flags)&, std::size_t&, Data&);
35 typedef boost::function<functor_type> functor_wrapper_type;
37 // add a semantic action function object
39 void add_action(std::size_t unique_id, std::size_t, F act)
41 if (actions_.size() <= unique_id)
42 actions_.resize(unique_id + 1);
44 actions_[unique_id] = act;
47 // try to invoke a semantic action for the given token (unique_id)
48 BOOST_SCOPED_ENUM(pass_flags) invoke_actions(std::size_t /*state*/
49 , std::size_t& id, std::size_t unique_id, Iterator& end
52 // if there is nothing to invoke, continue with 'match'
53 if (unique_id >= actions_.size() || !actions_[unique_id])
54 return pass_flags::pass_normal;
56 // Note: all arguments might be changed by the invoked semantic
58 BOOST_SCOPED_ENUM(pass_flags) match = pass_flags::pass_normal;
59 actions_[unique_id](data.get_first(), end, match, id, data);
63 std::vector<functor_wrapper_type> actions_;
66 // This specialization of semantic_actions will be used if the token
67 // type (lexer definition) needs to support states, resulting in a more
68 // complex data structure needed for storing the semantic action
70 template <typename Iterator, typename Data>
71 struct semantic_actions<Iterator, mpl::true_, Data>
73 typedef void functor_type(Iterator&, Iterator&
74 , BOOST_SCOPED_ENUM(pass_flags)&, std::size_t&, Data&);
75 typedef boost::function<functor_type> functor_wrapper_type;
77 // add a semantic action function object
79 void add_action(std::size_t unique_id, std::size_t state, F act)
81 if (actions_.size() <= state)
82 actions_.resize(state + 1);
84 std::vector<functor_wrapper_type>& actions (actions_[state]);
85 if (actions.size() <= unique_id)
86 actions.resize(unique_id + 1);
88 actions[unique_id] = act;
91 // try to invoke a semantic action for the given token (unique_id)
92 BOOST_SCOPED_ENUM(pass_flags) invoke_actions(std::size_t state
93 , std::size_t& id, std::size_t unique_id, Iterator& end
96 // if there is no action defined for this state, return match
97 if (state >= actions_.size())
98 return pass_flags::pass_normal;
100 // if there is nothing to invoke, continue with 'match'
101 std::vector<functor_wrapper_type> const& actions = actions_[state];
102 if (unique_id >= actions.size() || !actions[unique_id])
103 return pass_flags::pass_normal;
108 // Note: all arguments might be changed by the invoked semantic
110 BOOST_SCOPED_ENUM(pass_flags) match = pass_flags::pass_normal;
111 actions[unique_id](data.get_first(), end, match, id, data);
115 std::vector<std::vector<functor_wrapper_type> > actions_;