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 #ifndef BOOST_SPIRIT_QI_OPERATOR_SEQUENTIAL_OR_HPP 8 #define BOOST_SPIRIT_QI_OPERATOR_SEQUENTIAL_OR_HPP 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/pass_function.hpp> 16 #include <boost/spirit/home/qi/detail/attributes.hpp> 17 #include <boost/spirit/home/support/detail/what_function.hpp> 18 #include <boost/spirit/home/support/algorithm/any_if_ns_so.hpp> 19 #include <boost/spirit/home/support/handles_container.hpp> 20 #include <boost/fusion/include/as_vector.hpp> 21 #include <boost/fusion/include/for_each.hpp> 22 #include <boost/proto/operators.hpp> 23 #include <boost/proto/tags.hpp> 24 25 namespace boost { namespace spirit 26 { 27 /////////////////////////////////////////////////////////////////////////// 28 // Enablers 29 /////////////////////////////////////////////////////////////////////////// 30 template <> 31 struct use_operator<qi::domain, proto::tag::logical_or> // enables || 32 : mpl::true_ {}; 33 34 template <> 35 struct flatten_tree<qi::domain, proto::tag::logical_or> // flattens || 36 : mpl::true_ {}; 37 }} 38 39 namespace boost { namespace spirit { namespace qi 40 { 41 template <typename Elements> 42 struct sequential_or : nary_parser<sequential_or<Elements> > 43 { 44 template <typename Context, typename Iterator> 45 struct attribute 46 { 47 // Put all the element attributes in a tuple, 48 // wrapping each element in a boost::optional 49 typedef typename traits::build_attribute_sequence< 50 Elements, Context, traits::sequential_or_attribute_transform 51 , Iterator, qi::domain 52 >::type all_attributes; 53 54 // Now, build a fusion vector over the attributes. Note 55 // that build_fusion_vector 1) removes all unused attributes 56 // and 2) may return unused_type if all elements have 57 // unused_type(s). 58 typedef typename 59 traits::build_fusion_vector<all_attributes>::type 60 type; 61 }; 62 sequential_orboost::spirit::qi::sequential_or63 sequential_or(Elements const& elements_) 64 : elements(elements_) {} 65 66 template <typename Iterator, typename Context 67 , typename Skipper, typename Attribute> parseboost::spirit::qi::sequential_or68 bool parse(Iterator& first, Iterator const& last 69 , Context& context, Skipper const& skipper 70 , Attribute& attr_) const 71 { 72 typedef traits::attribute_not_unused<Context, Iterator> predicate; 73 detail::pass_function<Iterator, Context, Skipper> 74 f(first, last, context, skipper); 75 76 // wrap the attribute in a tuple if it is not a tuple 77 typename traits::wrap_if_not_tuple<Attribute>::type attr_local(attr_); 78 79 // return true if *any* of the parsers succeed 80 // (we use the non-short-circuiting and strict order version: 81 // any_if_ns_so to force all the elements to be tested and 82 // in the defined order: first is first, last is last) 83 return spirit::any_if_ns_so(elements, attr_local, f, predicate()); 84 } 85 86 template <typename Context> whatboost::spirit::qi::sequential_or87 info what(Context& context) const 88 { 89 info result("sequential-or"); 90 fusion::for_each(elements, 91 spirit::detail::what_function<Context>(result, context)); 92 return result; 93 } 94 95 Elements elements; 96 }; 97 98 /////////////////////////////////////////////////////////////////////////// 99 // Parser generators: make_xxx function (objects) 100 /////////////////////////////////////////////////////////////////////////// 101 template <typename Elements, typename Modifiers> 102 struct make_composite<proto::tag::logical_or, Elements, Modifiers> 103 : make_nary_composite<Elements, sequential_or> 104 {}; 105 }}} 106 107 namespace boost { namespace spirit { namespace traits 108 { 109 /////////////////////////////////////////////////////////////////////////// 110 // We specialize this for sequential_or (see support/attributes.hpp). 111 // For sequential_or, we only wrap the attribute in a tuple IFF 112 // it is not already a fusion tuple. 113 template <typename Elements, typename Attribute> 114 struct pass_attribute<qi::sequential_or<Elements>, Attribute> 115 : wrap_if_not_tuple<Attribute> {}; 116 117 /////////////////////////////////////////////////////////////////////////// 118 template <typename Elements> 119 struct has_semantic_action<qi::sequential_or<Elements> > 120 : nary_has_semantic_action<Elements> {}; 121 122 /////////////////////////////////////////////////////////////////////////// 123 template <typename Elements, typename Attribute, typename Context 124 , typename Iterator> 125 struct handles_container<qi::sequential_or<Elements>, Attribute, Context 126 , Iterator> 127 : nary_handles_container<Elements, Attribute, Context, Iterator> {}; 128 }}} 129 130 #endif 131