]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/metaparse/example/meta_metaparse/main.cpp
1 // Copyright Abel Sinkovics (abel@sinkovics.hu) 2012.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 #define BOOST_MPL_LIMIT_STRING_SIZE 64
7 #define BOOST_METAPARSE_LIMIT_STRING_SIZE BOOST_MPL_LIMIT_STRING_SIZE
9 #include <boost/metaparse/grammar.hpp>
11 #include <boost/metaparse/string.hpp>
12 #include <boost/metaparse/build_parser.hpp>
13 #include <boost/metaparse/token.hpp>
14 #include <boost/metaparse/entire_input.hpp>
15 #include <boost/metaparse/int_.hpp>
16 #include <boost/metaparse/transform.hpp>
18 #include <boost/mpl/apply_wrap.hpp>
19 #include <boost/mpl/front.hpp>
20 #include <boost/mpl/back.hpp>
21 #include <boost/mpl/if.hpp>
22 #include <boost/mpl/bool.hpp>
23 #include <boost/mpl/fold.hpp>
25 using boost::mpl::apply_wrap1
;
26 using boost::mpl::front
;
27 using boost::mpl::back
;
28 using boost::mpl::if_
;
29 using boost::mpl::bool_
;
31 using boost::metaparse::build_parser
;
32 using boost::metaparse::token
;
33 using boost::metaparse::entire_input
;
34 using boost::metaparse::int_
;
35 using boost::metaparse::grammar
;
36 using boost::metaparse::transform
;
38 #if BOOST_METAPARSE_STD < 2011
41 std::cout
<< "Please use a compiler that supports constexpr" << std::endl
;
46 # error _STR already defined
48 #define _STR BOOST_METAPARSE_STRING
50 template <class T
, char C
>
51 struct is_c
: bool_
<T::type::value
== C
> {};
53 struct build_plus_impl
55 template <class A
, class B
>
62 T
operator()(T t
) const
64 return _left(t
) + _right(t
);
67 typename
A::type _left
;
68 typename
B::type _right
;
71 template <class A
, class B
>
78 T
operator()(T t
) const
80 return _left(t
) - _right(t
);
83 typename
A::type _left
;
84 typename
B::type _right
;
87 template <class State
, class C
>
90 typename is_c
<front
<C
>, '+'>::type
,
91 _plus
<State
, typename back
<C
>::type
>,
92 _minus
<State
, typename back
<C
>::type
>
99 typedef build_plus type
;
104 typename back
<Seq
>::type
,
105 typename front
<Seq
>::type
,
111 struct build_mult_impl
113 template <class A
, class B
>
120 T
operator()(T t
) const
122 return _left(t
) * _right(t
);
125 typename
A::type _left
;
126 typename
B::type _right
;
129 template <class A
, class B
>
136 T
operator()(T t
) const
138 return _left(t
) / _right(t
);
141 typename
A::type _left
;
142 typename
B::type _right
;
145 template <class State
, class C
>
148 typename is_c
<front
<C
>, '*'>::type
,
149 _mult
<State
, typename back
<C
>::type
>,
150 _div
<State
, typename back
<C
>::type
>
157 typedef build_mult type
;
162 typename back
<Seq
>::type
,
163 typename front
<Seq
>::type
,
171 typedef build_value type
;
179 int operator()(T
) const
181 return V::type::value
;
188 typedef build_arg type
;
196 T
operator()(T t
) const
205 typedef keep_front type
;
208 struct apply
: front
<Seq
> {};
212 grammar
<_STR("plus_exp")>
213 ::import
<_STR("int_token"), token
<transform
<int_
, build_value
>>>::type
215 ::rule
<_STR("ws ::= (' ' | '\n' | '\r' | '\t')*")>::type
216 ::rule
<_STR("plus_token ::= '+' ws"), keep_front
>::type
217 ::rule
<_STR("minus_token ::= '-' ws"), keep_front
>::type
218 ::rule
<_STR("mult_token ::= '*' ws"), keep_front
>::type
219 ::rule
<_STR("div_token ::= '/' ws"), keep_front
>::type
220 ::rule
<_STR("arg_token ::= '_' ws"), keep_front
>::type
222 ::rule
<_STR("plus_exp ::= prod_exp ((plus_token | minus_token) prod_exp)*"), build_plus
>::type
223 ::rule
<_STR("prod_exp ::= value_exp ((mult_token | div_token) value_exp)*"), build_mult
>::type
224 ::rule
<_STR("value_exp ::= int_token | arg_exp")>::type
225 ::rule
<_STR("arg_exp ::= arg_token"), build_arg
>::type
228 typedef build_parser
<entire_input
<g
>> function_parser
;
231 #error LAMBDA already defined
233 #define LAMBDA(exp) apply_wrap1<function_parser, _STR(#exp)>::type
238 LAMBDA(1+ 2*4-6/2) f4
;