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 #ifndef BOOST_SPIRIT_LEX_LEXER_ACTION_HPP
7 #define BOOST_SPIRIT_LEX_LEXER_ACTION_HPP
13 #include <boost/spirit/home/lex/meta_compiler.hpp>
14 #include <boost/spirit/home/lex/lexer_type.hpp>
15 #include <boost/spirit/home/lex/argument.hpp>
16 #include <boost/spirit/home/lex/lexer/support_functions.hpp>
17 #include <boost/mpl/if.hpp>
18 #include <boost/type_traits/remove_const.hpp>
19 #include <boost/type_traits/is_same.hpp>
21 ///////////////////////////////////////////////////////////////////////////////
22 namespace boost { namespace spirit { namespace lex
24 ///////////////////////////////////////////////////////////////////////////
25 template <typename Subject, typename Action>
26 struct action : unary_lexer<action<Subject, Action> >
28 action(Subject const& subject, Action f)
29 : subject(subject), f(f) {}
31 template <typename LexerDef, typename String>
32 void collect(LexerDef& lexdef, String const& state
33 , String const& targetstate) const
35 // collect the token definition information for the token_def
36 // this action is attached to
37 subject.collect(lexdef, state, targetstate);
40 template <typename LexerDef>
41 void add_actions(LexerDef& lexdef) const
43 // call to add all actions attached further down the hierarchy
44 subject.add_actions(lexdef);
46 // retrieve the id of the associated token_def and register the
47 // given semantic action with the lexer instance
48 lexdef.add_action(subject.unique_id(), subject.state(), f);
54 // silence MSVC warning C4512: assignment operator could not be generated
55 BOOST_DELETED_FUNCTION(action& operator= (action const&))
60 ///////////////////////////////////////////////////////////////////////////////
61 namespace boost { namespace spirit
63 ///////////////////////////////////////////////////////////////////////////
64 // Karma action meta-compiler
66 struct make_component<lex::domain, tag::action>
68 template <typename Sig>
71 template <typename This, typename Elements, typename Modifiers>
72 struct result<This(Elements, Modifiers)>
75 remove_const<typename Elements::car_type>::type
79 remove_const<typename Elements::cdr_type::car_type>::type
82 typedef lex::action<subject_type, action_type> type;
85 template <typename Elements>
86 typename result<make_component(Elements, unused_type)>::type
87 operator()(Elements const& elements, unused_type) const
89 typename result<make_component(Elements, unused_type)>::type
90 result(elements.car, elements.cdr.car);