liuxiaolong
2021-07-20 58d904a328c0d849769b483e901a0be9426b8209
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
///////////////////////////////////////////////////////////////////////////////
// as_independent.hpp
//
//  Copyright 2008 Eric Niebler. Distributed under the Boost
//  Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 
#ifndef BOOST_XPRESSIVE_DETAIL_STATIC_TRANSFORMS_AS_INDEPENDENT_HPP_EAN_04_05_2007
#define BOOST_XPRESSIVE_DETAIL_STATIC_TRANSFORMS_AS_INDEPENDENT_HPP_EAN_04_05_2007
 
// MS compatible compilers support #pragma once
#if defined(_MSC_VER)
# pragma once
#endif
 
#include <boost/mpl/sizeof.hpp>
#include <boost/xpressive/detail/detail_fwd.hpp>
#include <boost/xpressive/detail/static/static.hpp>
#include <boost/proto/core.hpp>
#include <boost/proto/transform/arg.hpp>
#include <boost/proto/transform/when.hpp>
#include <boost/proto/transform/fold.hpp>
#include <boost/proto/transform/fold_tree.hpp>
 
namespace boost { namespace xpressive { namespace detail
{
    struct keeper_tag
    {};
 
    struct lookahead_tag
    {};
 
    struct lookbehind_tag
    {};
}}}
 
namespace boost { namespace xpressive { namespace grammar_detail
{
    // A grammar that only accepts static regexes that
    // don't have semantic actions.
    struct NotHasAction
      : proto::switch_<struct NotHasActionCases>
    {};
 
    struct NotHasActionCases
    {
        template<typename Tag, int Dummy = 0>
        struct case_
          : proto::nary_expr<Tag, proto::vararg<NotHasAction> >
        {};
 
        template<int Dummy>
        struct case_<proto::tag::terminal, Dummy>
          : not_< or_<
                proto::terminal<detail::tracking_ptr<detail::regex_impl<_> > >,
                proto::terminal<reference_wrapper<_> >
            > >
        {};
 
        template<int Dummy>
        struct case_<proto::tag::comma, Dummy>
          : proto::_    // because (set='a','b') can't contain an action
        {};
 
        template<int Dummy>
        struct case_<proto::tag::complement, Dummy>
          : proto::_    // because in ~X, X can't contain an unscoped action
        {};
 
        template<int Dummy>
        struct case_<detail::lookahead_tag, Dummy>
          : proto::_    // because actions in lookaheads are scoped
        {};
 
        template<int Dummy>
        struct case_<detail::lookbehind_tag, Dummy>
          : proto::_    // because actions in lookbehinds are scoped
        {};
 
        template<int Dummy>
        struct case_<detail::keeper_tag, Dummy>
          : proto::_    // because actions in keepers are scoped
        {};
 
        template<int Dummy>
        struct case_<proto::tag::subscript, Dummy>
          : proto::subscript<detail::set_initializer_type, _>
        {}; // only accept set[...], not actions!
    };
 
    struct IndependentEndXpression
      : or_<
            when<NotHasAction, detail::true_xpression()>
          , otherwise<detail::independent_end_xpression()>
        >
    {};
 
    template<typename Grammar, typename Callable = proto::callable>
    struct as_lookahead : proto::transform<as_lookahead<Grammar, Callable> >
    {
        template<typename Expr, typename State, typename Data>
        struct impl : proto::transform_impl<Expr, State, Data>
        {
            typedef typename proto::result_of::child<Expr>::type arg_type;
 
            typedef
                typename IndependentEndXpression::impl<arg_type, int, int>::result_type
            end_xpr_type;
 
            typedef
                typename Grammar::template impl<arg_type, end_xpr_type, Data>::result_type
            xpr_type;
 
            typedef
                detail::lookahead_matcher<xpr_type>
            result_type;
 
            result_type operator ()(
                typename impl::expr_param expr
              , typename impl::state_param
              , typename impl::data_param data
            ) const
            {
                int i = 0;
                return result_type(
                    typename Grammar::template impl<arg_type, end_xpr_type, Data>()(
                        proto::child(expr)
                      , IndependentEndXpression::impl<arg_type, int, int>()(proto::child(expr), i, i)
                      , data
                    )
                  , false
                );
            }
        };
    };
 
    template<typename Grammar, typename Callable = proto::callable>
    struct as_lookbehind : proto::transform<as_lookbehind<Grammar, Callable> >
    {
        template<typename Expr, typename State, typename Data>
        struct impl : proto::transform_impl<Expr, State, Data>
        {
            typedef typename proto::result_of::child<Expr>::type arg_type;
 
            typedef
                typename IndependentEndXpression::impl<arg_type, int, int>::result_type
            end_xpr_type;
 
            typedef
                typename Grammar::template impl<arg_type, end_xpr_type, Data>::result_type
            xpr_type;
 
            typedef
                detail::lookbehind_matcher<xpr_type>
            result_type;
 
            result_type operator ()(
                typename impl::expr_param expr
              , typename impl::state_param
              , typename impl::data_param data
            ) const
            {
                int i = 0;
                xpr_type expr2 = typename Grammar::template impl<arg_type, end_xpr_type, Data>()(
                    proto::child(expr)
                  , IndependentEndXpression::impl<arg_type, int, int>()(proto::child(expr), i, i)
                  , data
                );
                std::size_t width = expr2.get_width().value();
                return result_type(expr2, width, false);
            }
        };
    };
 
    template<typename Grammar, typename Callable = proto::callable>
    struct as_keeper : proto::transform<as_keeper<Grammar, Callable> >
    {
        template<typename Expr, typename State, typename Data>
        struct impl : proto::transform_impl<Expr, State, Data>
        {
            typedef typename proto::result_of::child<Expr>::type arg_type;
 
            typedef
                typename IndependentEndXpression::impl<arg_type, int, int>::result_type
            end_xpr_type;
 
            typedef
                typename Grammar::template impl<arg_type, end_xpr_type, Data>::result_type
            xpr_type;
 
            typedef
                detail::keeper_matcher<xpr_type>
            result_type;
 
            result_type operator ()(
                typename impl::expr_param expr
              , typename impl::state_param
              , typename impl::data_param data
            ) const
            {
                int i = 0;
                return result_type(
                    typename Grammar::template impl<arg_type, end_xpr_type, Data>()(
                        proto::child(expr)
                      , IndependentEndXpression::impl<arg_type, int, int>()(proto::child(expr), i, i)
                      , data
                    )
                );
            }
        };
    };
 
}}}
 
#endif