Chris@16
|
1 // Copyright (c) 2001-2011 Hartmut Kaiser
|
Chris@16
|
2 //
|
Chris@16
|
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6 #if !defined(SPIRIT_LEX_ACTION_NOV_18_2007_0743PM)
|
Chris@16
|
7 #define SPIRIT_LEX_ACTION_NOV_18_2007_0743PM
|
Chris@16
|
8
|
Chris@16
|
9 #if defined(_MSC_VER)
|
Chris@16
|
10 #pragma once
|
Chris@16
|
11 #endif
|
Chris@16
|
12
|
Chris@16
|
13 #include <boost/spirit/home/lex/meta_compiler.hpp>
|
Chris@16
|
14 #include <boost/spirit/home/lex/lexer_type.hpp>
|
Chris@16
|
15 #include <boost/spirit/home/lex/argument.hpp>
|
Chris@16
|
16 #include <boost/spirit/home/lex/lexer/support_functions.hpp>
|
Chris@16
|
17 #include <boost/mpl/if.hpp>
|
Chris@16
|
18 #include <boost/type_traits/remove_const.hpp>
|
Chris@16
|
19 #include <boost/type_traits/is_same.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
22 namespace boost { namespace spirit { namespace lex
|
Chris@16
|
23 {
|
Chris@16
|
24 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
25 template <typename Subject, typename Action>
|
Chris@16
|
26 struct action : unary_lexer<action<Subject, Action> >
|
Chris@16
|
27 {
|
Chris@16
|
28 action(Subject const& subject, Action f)
|
Chris@16
|
29 : subject(subject), f(f) {}
|
Chris@16
|
30
|
Chris@16
|
31 template <typename LexerDef, typename String>
|
Chris@16
|
32 void collect(LexerDef& lexdef, String const& state
|
Chris@16
|
33 , String const& targetstate) const
|
Chris@16
|
34 {
|
Chris@16
|
35 // collect the token definition information for the token_def
|
Chris@16
|
36 // this action is attached to
|
Chris@16
|
37 subject.collect(lexdef, state, targetstate);
|
Chris@16
|
38 }
|
Chris@16
|
39
|
Chris@16
|
40 template <typename LexerDef>
|
Chris@16
|
41 void add_actions(LexerDef& lexdef) const
|
Chris@16
|
42 {
|
Chris@16
|
43 // call to add all actions attached further down the hierarchy
|
Chris@16
|
44 subject.add_actions(lexdef);
|
Chris@16
|
45
|
Chris@16
|
46 // retrieve the id of the associated token_def and register the
|
Chris@16
|
47 // given semantic action with the lexer instance
|
Chris@16
|
48 lexdef.add_action(subject.unique_id(), subject.state(), f);
|
Chris@16
|
49 }
|
Chris@16
|
50
|
Chris@16
|
51 Subject subject;
|
Chris@16
|
52 Action f;
|
Chris@16
|
53
|
Chris@16
|
54 private:
|
Chris@16
|
55 // silence MSVC warning C4512: assignment operator could not be generated
|
Chris@16
|
56 action& operator= (action const&);
|
Chris@16
|
57 };
|
Chris@16
|
58
|
Chris@16
|
59 }}}
|
Chris@16
|
60
|
Chris@16
|
61 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
62 namespace boost { namespace spirit
|
Chris@16
|
63 {
|
Chris@16
|
64 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
65 // Karma action meta-compiler
|
Chris@16
|
66 template <>
|
Chris@16
|
67 struct make_component<lex::domain, tag::action>
|
Chris@16
|
68 {
|
Chris@16
|
69 template <typename Sig>
|
Chris@16
|
70 struct result;
|
Chris@16
|
71
|
Chris@16
|
72 template <typename This, typename Elements, typename Modifiers>
|
Chris@16
|
73 struct result<This(Elements, Modifiers)>
|
Chris@16
|
74 {
|
Chris@16
|
75 typedef typename
|
Chris@16
|
76 remove_const<typename Elements::car_type>::type
|
Chris@16
|
77 subject_type;
|
Chris@16
|
78
|
Chris@16
|
79 typedef typename
|
Chris@16
|
80 remove_const<typename Elements::cdr_type::car_type>::type
|
Chris@16
|
81 action_type;
|
Chris@16
|
82
|
Chris@16
|
83 typedef lex::action<subject_type, action_type> type;
|
Chris@16
|
84 };
|
Chris@16
|
85
|
Chris@16
|
86 template <typename Elements>
|
Chris@16
|
87 typename result<make_component(Elements, unused_type)>::type
|
Chris@16
|
88 operator()(Elements const& elements, unused_type) const
|
Chris@16
|
89 {
|
Chris@16
|
90 typename result<make_component(Elements, unused_type)>::type
|
Chris@16
|
91 result(elements.car, elements.cdr.car);
|
Chris@16
|
92 return result;
|
Chris@16
|
93 }
|
Chris@16
|
94 };
|
Chris@16
|
95 }}
|
Chris@16
|
96
|
Chris@16
|
97 #endif
|