1// Copyright (c) 2001-2011 Hartmut Kaiser
2//
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)
5
6#ifndef BOOST_SPIRIT_LEX_LEXER_ACTION_HPP
7#define BOOST_SPIRIT_LEX_LEXER_ACTION_HPP
8
9#if defined(_MSC_VER)
10#pragma once
11#endif
12
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>
20
21///////////////////////////////////////////////////////////////////////////////
22namespace boost { namespace spirit { namespace lex
23{
24 ///////////////////////////////////////////////////////////////////////////
25 template <typename Subject, typename Action>
26 struct action : unary_lexer<action<Subject, Action> >
27 {
28 action(Subject const& subject, Action f)
29 : subject(subject), f(f) {}
30
31 template <typename LexerDef, typename String>
32 void collect(LexerDef& lexdef, String const& state
33 , String const& targetstate) const
34 {
35 // collect the token definition information for the token_def
36 // this action is attached to
37 subject.collect(lexdef, state, targetstate);
38 }
39
40 template <typename LexerDef>
41 void add_actions(LexerDef& lexdef) const
42 {
43 // call to add all actions attached further down the hierarchy
44 subject.add_actions(lexdef);
45
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);
49 }
50
51 Subject subject;
52 Action f;
53 };
54
55}}}
56
57///////////////////////////////////////////////////////////////////////////////
58namespace boost { namespace spirit
59{
60 ///////////////////////////////////////////////////////////////////////////
61 // Karma action meta-compiler
62 template <>
63 struct make_component<lex::domain, tag::action>
64 {
65 template <typename Sig>
66 struct result;
67
68 template <typename This, typename Elements, typename Modifiers>
69 struct result<This(Elements, Modifiers)>
70 {
71 typedef typename
72 remove_const<typename Elements::car_type>::type
73 subject_type;
74
75 typedef typename
76 remove_const<typename Elements::cdr_type::car_type>::type
77 action_type;
78
79 typedef lex::action<subject_type, action_type> type;
80 };
81
82 template <typename Elements>
83 typename result<make_component(Elements, unused_type)>::type
84 operator()(Elements const& elements, unused_type) const
85 {
86 typename result<make_component(Elements, unused_type)>::type
87 result(elements.car, elements.cdr.car);
88 return result;
89 }
90 };
91}}
92
93#endif
94

source code of boost/libs/spirit/include/boost/spirit/home/lex/lexer/action.hpp