]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
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_PERMUTATION_OR_MARCH_13_2007_1145PM) | |
8 | #define SPIRIT_PERMUTATION_OR_MARCH_13_2007_1145PM | |
9 | ||
10 | #if defined(_MSC_VER) | |
11 | #pragma once | |
12 | #endif | |
13 | ||
14 | #include <boost/spirit/home/qi/meta_compiler.hpp> | |
15 | #include <boost/spirit/home/qi/detail/permute_function.hpp> | |
16 | #include <boost/spirit/home/qi/detail/attributes.hpp> | |
17 | #include <boost/spirit/home/support/algorithm/any_if_ns.hpp> | |
18 | #include <boost/spirit/home/support/detail/what_function.hpp> | |
19 | #include <boost/spirit/home/support/has_semantic_action.hpp> | |
20 | #include <boost/spirit/home/support/handles_container.hpp> | |
21 | #include <boost/spirit/home/support/info.hpp> | |
22 | #include <boost/fusion/include/size.hpp> | |
23 | #include <boost/optional.hpp> | |
24 | #include <boost/foreach.hpp> | |
25 | #include <boost/array.hpp> | |
26 | ||
27 | namespace boost { namespace spirit | |
28 | { | |
29 | /////////////////////////////////////////////////////////////////////////// | |
30 | // Enablers | |
31 | /////////////////////////////////////////////////////////////////////////// | |
32 | template <> | |
33 | struct use_operator<qi::domain, proto::tag::bitwise_xor> // enables ^ | |
34 | : mpl::true_ {}; | |
35 | ||
36 | template <> | |
37 | struct flatten_tree<qi::domain, proto::tag::bitwise_xor> // flattens ^ | |
38 | : mpl::true_ {}; | |
39 | }} | |
40 | ||
41 | namespace boost { namespace spirit { namespace qi | |
42 | { | |
43 | template <typename Elements> | |
44 | struct permutation : nary_parser<permutation<Elements> > | |
45 | { | |
46 | template <typename Context, typename Iterator> | |
47 | struct attribute | |
48 | { | |
49 | // Put all the element attributes in a tuple, | |
50 | // wrapping each element in a boost::optional | |
51 | typedef typename traits::build_attribute_sequence< | |
52 | Elements, Context, traits::permutation_attribute_transform | |
53 | , Iterator, qi::domain | |
54 | >::type all_attributes; | |
55 | ||
56 | // Now, build a fusion vector over the attributes. Note | |
57 | // that build_fusion_vector 1) removes all unused attributes | |
58 | // and 2) may return unused_type if all elements have | |
59 | // unused_type(s). | |
60 | typedef typename | |
61 | traits::build_fusion_vector<all_attributes>::type | |
62 | type; | |
63 | }; | |
64 | ||
65 | permutation(Elements const& elements_) | |
66 | : elements(elements_) {} | |
67 | ||
68 | template <typename Iterator, typename Context | |
69 | , typename Skipper, typename Attribute> | |
70 | bool parse(Iterator& first, Iterator const& last | |
71 | , Context& context, Skipper const& skipper | |
72 | , Attribute& attr_) const | |
73 | { | |
74 | typedef traits::attribute_not_unused<Context, Iterator> predicate; | |
75 | detail::permute_function<Iterator, Context, Skipper> | |
76 | f(first, last, context, skipper); | |
77 | ||
78 | boost::array<bool, fusion::result_of::size<Elements>::value> flags; | |
79 | BOOST_FOREACH(bool& taken, flags) | |
80 | { | |
81 | taken = false; | |
82 | } | |
83 | ||
84 | // wrap the attribute in a tuple if it is not a tuple | |
85 | typename traits::wrap_if_not_tuple<Attribute>::type attr_local(attr_); | |
86 | ||
87 | // We have a bool array 'flags' with one flag for each parser. | |
88 | // permute_function sets the slot to true when the corresponding | |
89 | // parser successful matches. We loop until there are no more | |
90 | // successful parsers. | |
91 | ||
92 | bool result = false; | |
93 | f.taken = flags.begin(); | |
94 | while (spirit::any_if_ns(elements, attr_local, f, predicate())) | |
95 | { | |
96 | f.taken = flags.begin(); | |
97 | result = true; | |
98 | } | |
99 | return result; | |
100 | } | |
101 | ||
102 | template <typename Context> | |
103 | info what(Context& context) const | |
104 | { | |
105 | info result("permutation"); | |
106 | fusion::for_each(elements, | |
107 | spirit::detail::what_function<Context>(result, context)); | |
108 | return result; | |
109 | } | |
110 | ||
111 | Elements elements; | |
112 | }; | |
113 | ||
114 | /////////////////////////////////////////////////////////////////////////// | |
115 | // Parser generators: make_xxx function (objects) | |
116 | /////////////////////////////////////////////////////////////////////////// | |
117 | template <typename Elements, typename Modifiers> | |
118 | struct make_composite<proto::tag::bitwise_xor, Elements, Modifiers> | |
119 | : make_nary_composite<Elements, permutation> | |
120 | {}; | |
121 | }}} | |
122 | ||
123 | namespace boost { namespace spirit { namespace traits | |
124 | { | |
125 | /////////////////////////////////////////////////////////////////////////// | |
126 | // We specialize this for permutation (see support/attributes.hpp). | |
127 | // For permutation, we only wrap the attribute in a tuple IFF | |
128 | // it is not already a fusion tuple. | |
129 | template <typename Elements, typename Attribute> | |
130 | struct pass_attribute<qi::permutation<Elements>, Attribute> | |
131 | : wrap_if_not_tuple<Attribute> {}; | |
132 | ||
133 | /////////////////////////////////////////////////////////////////////////// | |
134 | template <typename Elements> | |
135 | struct has_semantic_action<qi::permutation<Elements> > | |
136 | : nary_has_semantic_action<Elements> {}; | |
137 | ||
138 | /////////////////////////////////////////////////////////////////////////// | |
139 | template <typename Elements, typename Attribute, typename Context | |
140 | , typename Iterator> | |
141 | struct handles_container<qi::permutation<Elements>, Attribute, Context | |
142 | , Iterator> | |
143 | : nary_handles_container<Elements, Attribute, Context, Iterator> {}; | |
144 | }}} | |
145 | ||
146 | #endif |