]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (c) 2001-2011 Hartmut Kaiser |
2 | // Copyright (c) 2001-2011 Joel de Guzman | |
3 | // | |
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(SPIRIT_KARMA_ALTERNATIVE_MAR_01_2007_1117AM) | |
8 | #define SPIRIT_KARMA_ALTERNATIVE_MAR_01_2007_1117AM | |
9 | ||
10 | #if defined(_MSC_VER) | |
11 | #pragma once | |
12 | #endif | |
13 | ||
14 | #include <boost/spirit/home/karma/detail/alternative_function.hpp> | |
15 | #include <boost/spirit/home/karma/detail/get_stricttag.hpp> | |
16 | #include <boost/spirit/home/karma/domain.hpp> | |
17 | #include <boost/spirit/home/karma/generator.hpp> | |
18 | #include <boost/spirit/home/karma/meta_compiler.hpp> | |
19 | #include <boost/spirit/home/support/info.hpp> | |
20 | #include <boost/spirit/home/support/unused.hpp> | |
21 | #include <boost/spirit/home/support/has_semantic_action.hpp> | |
22 | #include <boost/spirit/home/support/handles_container.hpp> | |
23 | #include <boost/spirit/home/support/detail/what_function.hpp> | |
24 | #include <boost/fusion/include/any.hpp> | |
25 | #include <boost/fusion/include/mpl.hpp> | |
26 | #include <boost/fusion/include/for_each.hpp> | |
27 | #include <boost/mpl/accumulate.hpp> | |
28 | #include <boost/mpl/bitor.hpp> | |
29 | #include <boost/config.hpp> | |
30 | ||
31 | namespace boost { namespace spirit | |
32 | { | |
33 | /////////////////////////////////////////////////////////////////////////// | |
34 | // Enablers | |
35 | /////////////////////////////////////////////////////////////////////////// | |
36 | template <> | |
37 | struct use_operator<karma::domain, proto::tag::bitwise_or> // enables | | |
38 | : mpl::true_ {}; | |
39 | ||
40 | template <> | |
41 | struct flatten_tree<karma::domain, proto::tag::bitwise_or> // flattens | | |
42 | : mpl::true_ {}; | |
43 | ||
44 | }} | |
45 | ||
46 | /////////////////////////////////////////////////////////////////////////////// | |
47 | namespace boost { namespace spirit { namespace traits | |
48 | { | |
49 | // specialization for sequences | |
50 | template <typename Elements> | |
51 | struct alternative_properties | |
52 | { | |
53 | struct element_properties | |
54 | { | |
55 | template <typename T> | |
56 | struct result; | |
57 | ||
58 | template <typename F, typename Element> | |
59 | struct result<F(Element)> | |
60 | { | |
61 | typedef properties_of<Element> type; | |
62 | }; | |
63 | ||
64 | // never called, but needed for decltype-based result_of (C++0x) | |
65 | #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES | |
66 | template <typename Element> | |
67 | typename result<element_properties(Element)>::type | |
68 | operator()(Element&&) const; | |
69 | #endif | |
70 | }; | |
71 | ||
72 | typedef typename mpl::accumulate< | |
73 | typename fusion::result_of::transform< | |
74 | Elements, element_properties>::type | |
75 | , mpl::int_<karma::generator_properties::countingbuffer> | |
76 | , mpl::bitor_<mpl::_2, mpl::_1> | |
77 | >::type type; | |
78 | }; | |
79 | ||
80 | }}} | |
81 | ||
82 | namespace boost { namespace spirit { namespace karma | |
83 | { | |
84 | template <typename Elements, typename Strict, typename Derived> | |
85 | struct base_alternative : nary_generator<Derived> | |
86 | { | |
87 | typedef typename traits::alternative_properties<Elements>::type | |
88 | properties; | |
89 | ||
90 | template <typename Context, typename Iterator = unused_type> | |
91 | struct attribute | |
92 | { | |
93 | // Put all the element attributes in a tuple | |
94 | typedef typename traits::build_attribute_sequence< | |
95 | Elements, Context, traits::alternative_attribute_transform | |
96 | , Iterator, karma::domain | |
97 | >::type all_attributes; | |
98 | ||
99 | // Ok, now make a variant over the attribute sequence. Note that | |
100 | // build_variant makes sure that 1) all attributes in the variant | |
101 | // are unique 2) puts the unused attribute, if there is any, to | |
102 | // the front and 3) collapses single element variants, variant<T> | |
103 | // to T. | |
104 | typedef typename traits::build_variant<all_attributes>::type type; | |
105 | }; | |
106 | ||
107 | base_alternative(Elements const& elements) | |
108 | : elements(elements) {} | |
109 | ||
110 | template < | |
111 | typename OutputIterator, typename Context, typename Delimiter | |
112 | , typename Attribute> | |
113 | bool generate(OutputIterator& sink, Context& ctx | |
114 | , Delimiter const& d, Attribute const& attr) const | |
115 | { | |
116 | typedef detail::alternative_generate_function< | |
117 | OutputIterator, Context, Delimiter, Attribute, Strict | |
118 | > functor; | |
119 | ||
120 | // f return true if *any* of the parser succeeds | |
121 | functor f (sink, ctx, d, attr); | |
122 | return fusion::any(elements, f); | |
123 | } | |
124 | ||
125 | template <typename Context> | |
126 | info what(Context& context) const | |
127 | { | |
128 | info result("alternative"); | |
129 | fusion::for_each(elements, | |
130 | spirit::detail::what_function<Context>(result, context)); | |
131 | return result; | |
132 | } | |
133 | ||
134 | Elements elements; | |
135 | }; | |
136 | ||
137 | template <typename Elements> | |
138 | struct alternative | |
139 | : base_alternative<Elements, mpl::false_, alternative<Elements> > | |
140 | { | |
141 | typedef base_alternative<Elements, mpl::false_, alternative> | |
142 | base_alternative_; | |
143 | ||
144 | alternative(Elements const& elements) | |
145 | : base_alternative_(elements) {} | |
146 | }; | |
147 | ||
148 | template <typename Elements> | |
149 | struct strict_alternative | |
150 | : base_alternative<Elements, mpl::true_, strict_alternative<Elements> > | |
151 | { | |
152 | typedef base_alternative<Elements, mpl::true_, strict_alternative> | |
153 | base_alternative_; | |
154 | ||
155 | strict_alternative(Elements const& elements) | |
156 | : base_alternative_(elements) {} | |
157 | }; | |
158 | ||
159 | /////////////////////////////////////////////////////////////////////////// | |
160 | // Generator generators: make_xxx function (objects) | |
161 | /////////////////////////////////////////////////////////////////////////// | |
162 | namespace detail | |
163 | { | |
164 | template <typename Elements, bool strict_mode = false> | |
165 | struct make_alternative | |
166 | : make_nary_composite<Elements, alternative> | |
167 | {}; | |
168 | ||
169 | template <typename Elements> | |
170 | struct make_alternative<Elements, true> | |
171 | : make_nary_composite<Elements, strict_alternative> | |
172 | {}; | |
173 | } | |
174 | ||
175 | template <typename Elements, typename Modifiers> | |
176 | struct make_composite<proto::tag::bitwise_or, Elements, Modifiers> | |
177 | : detail::make_alternative<Elements | |
178 | , detail::get_stricttag<Modifiers>::value> | |
179 | {}; | |
180 | ||
181 | }}} | |
182 | ||
183 | namespace boost { namespace spirit { namespace traits | |
184 | { | |
185 | /////////////////////////////////////////////////////////////////////////// | |
186 | template <typename Elements> | |
187 | struct has_semantic_action<karma::alternative<Elements> > | |
188 | : nary_has_semantic_action<Elements> {}; | |
189 | ||
190 | template <typename Elements> | |
191 | struct has_semantic_action<karma::strict_alternative<Elements> > | |
192 | : nary_has_semantic_action<Elements> {}; | |
193 | ||
194 | /////////////////////////////////////////////////////////////////////////// | |
195 | template <typename Elements, typename Attribute, typename Context | |
196 | , typename Iterator> | |
197 | struct handles_container<karma::alternative<Elements> | |
198 | , Attribute, Context, Iterator> | |
199 | : nary_handles_container<Elements, Attribute, Context, Iterator> {}; | |
200 | ||
201 | template <typename Elements, typename Attribute, typename Context | |
202 | , typename Iterator> | |
203 | struct handles_container<karma::strict_alternative<Elements> | |
204 | , Attribute, Context, Iterator> | |
205 | : nary_handles_container<Elements, Attribute, Context, Iterator> {}; | |
206 | }}} | |
207 | ||
208 | #endif |