1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #if !defined(BOOST_SPIRIT_X3_RULE_JAN_08_2012_0326PM)
8 #define BOOST_SPIRIT_X3_RULE_JAN_08_2012_0326PM
10 #include <boost/spirit/home/x3/nonterminal/detail/rule.hpp>
11 #include <boost/type_traits/is_same.hpp>
12 #include <boost/spirit/home/x3/support/context.hpp>
13 #include <boost/preprocessor/variadic/to_seq.hpp>
14 #include <boost/preprocessor/variadic/elem.hpp>
15 #include <boost/preprocessor/seq/for_each.hpp>
17 #if !defined(BOOST_SPIRIT_X3_NO_RTTI)
21 namespace boost { namespace spirit { namespace x3
23 template <typename ID>
26 // default parse_rule implementation
27 template <typename ID, typename Attribute, typename Iterator
28 , typename Context, typename ActualAttribute>
29 inline detail::default_parse_rule_result
31 rule<ID, Attribute> rule_
32 , Iterator& first, Iterator const& last
33 , Context const& context, ActualAttribute& attr)
35 static_assert(!is_same<decltype(get<ID>(context)), unused_type>::value,
36 "BOOST_SPIRIT_DEFINE undefined for this rule.");
37 return get<ID>(context).parse(first, last, context, unused, attr);
40 template <typename ID, typename RHS, typename Attribute, bool force_attribute_>
41 struct rule_definition : parser<rule_definition<ID, RHS, Attribute, force_attribute_>>
43 typedef rule_definition<ID, RHS, Attribute, force_attribute_> this_type;
46 typedef rule<ID, Attribute> lhs_type;
47 typedef Attribute attribute_type;
49 static bool const has_attribute =
50 !is_same<Attribute, unused_type>::value;
51 static bool const handles_container =
52 traits::is_container<Attribute>::value;
53 static bool const force_attribute =
56 rule_definition(RHS const& rhs, char const* name)
57 : rhs(rhs), name(name) {}
59 template <typename Iterator, typename Context, typename Attribute_>
60 bool parse(Iterator& first, Iterator const& last
61 , Context const& context, unused_type, Attribute_& attr) const
63 return detail::rule_parser<attribute_type, ID>
64 ::call_rule_definition(
65 rhs, name, first, last
68 , mpl::bool_<force_attribute>());
75 template <typename ID, typename Attribute, bool force_attribute_>
76 struct rule : parser<rule<ID, Attribute>>
79 typedef Attribute attribute_type;
80 static bool const has_attribute =
81 !is_same<Attribute, unused_type>::value;
82 static bool const handles_container =
83 traits::is_container<Attribute>::value;
84 static bool const force_attribute = force_attribute_;
86 #if !defined(BOOST_SPIRIT_X3_NO_RTTI)
87 rule() : name(typeid(rule).name()) {}
89 rule() : name("unnamed") {}
92 rule(char const* name)
95 template <typename RHS>
97 ID, typename extension::as_parser<RHS>::value_type, Attribute, force_attribute_>
98 operator=(RHS const& rhs) const
100 return { as_parser(rhs), name };
103 template <typename RHS>
105 ID, typename extension::as_parser<RHS>::value_type, Attribute, true>
106 operator%=(RHS const& rhs) const
108 return { as_parser(rhs), name };
112 template <typename Iterator, typename Context, typename Attribute_>
113 bool parse(Iterator& first, Iterator const& last
114 , Context const& context, unused_type, Attribute_& attr) const
116 return parse_rule(*this, first, last, context, attr);
124 template <typename T, typename Enable = void>
125 struct is_rule : mpl::false_ {};
127 template <typename ID, typename Attribute>
128 struct is_rule<rule<ID, Attribute>> : mpl::true_ {};
130 template <typename ID, typename Attribute, typename RHS, bool force_attribute>
131 struct is_rule<rule_definition<ID, RHS, Attribute, force_attribute>> : mpl::true_ {};
134 template <typename T>
135 struct get_info<T, typename enable_if<traits::is_rule<T>>::type>
137 typedef std::string result_type;
138 std::string operator()(T const& r) const
144 #define BOOST_SPIRIT_DECLARE_(r, data, rule_type) \
145 template <typename Iterator, typename Context, typename Attribute> \
148 , Iterator& first, Iterator const& last \
149 , Context const& context, Attribute& attr); \
152 #define BOOST_SPIRIT_DECLARE(...) BOOST_PP_SEQ_FOR_EACH( \
153 BOOST_SPIRIT_DECLARE_, _, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) \
156 #define BOOST_SPIRIT_DEFINE_(r, data, rule_name) \
157 template <typename Iterator, typename Context, typename Attribute> \
158 inline bool parse_rule( \
159 decltype(rule_name) rule_ \
160 , Iterator& first, Iterator const& last \
161 , Context const& context, Attribute& attr) \
163 using boost::spirit::x3::unused; \
164 static auto const def_ = (rule_name = BOOST_PP_CAT(rule_name, _def)); \
165 return def_.parse(first, last, context, unused, attr); \
169 #define BOOST_SPIRIT_DEFINE(...) BOOST_PP_SEQ_FOR_EACH( \
170 BOOST_SPIRIT_DEFINE_, _, BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) \
173 #define BOOST_SPIRIT_INSTANTIATE(rule_type, Iterator, Context) \
174 template bool parse_rule<Iterator, Context, rule_type::attribute_type>( \
176 , Iterator& first, Iterator const& last \
177 , Context const& context, rule_type::attribute_type& attr); \