| 1 | /*============================================================================= |
| 2 | Copyright (c) 2001-2014 Joel de Guzman |
| 3 | Copyright (c) 2001-2011 Hartmut Kaiser |
| 4 | |
| 5 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 6 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 7 | =============================================================================*/ |
| 8 | #if !defined(BOOST_SPIRIT_X3_OPTIONAL_MARCH_23_2007_1117PM) |
| 9 | #define BOOST_SPIRIT_X3_OPTIONAL_MARCH_23_2007_1117PM |
| 10 | |
| 11 | #include <boost/spirit/home/x3/core/proxy.hpp> |
| 12 | #include <boost/spirit/home/x3/core/detail/parse_into_container.hpp> |
| 13 | #include <boost/spirit/home/x3/support/traits/attribute_of.hpp> |
| 14 | #include <boost/spirit/home/x3/support/traits/move_to.hpp> |
| 15 | #include <boost/spirit/home/x3/support/traits/optional_traits.hpp> |
| 16 | #include <boost/spirit/home/x3/support/traits/attribute_category.hpp> |
| 17 | |
| 18 | namespace boost { namespace spirit { namespace x3 |
| 19 | { |
| 20 | template <typename Subject> |
| 21 | struct optional : proxy<Subject, optional<Subject>> |
| 22 | { |
| 23 | typedef proxy<Subject, optional<Subject>> base_type; |
| 24 | static bool const is_pass_through_unary = false; |
| 25 | static bool const handles_container = true; |
| 26 | |
| 27 | constexpr optional(Subject const& subject) |
| 28 | : base_type(subject) {} |
| 29 | |
| 30 | using base_type::parse_subject; |
| 31 | |
| 32 | // Attribute is a container |
| 33 | template <typename Iterator, typename Context |
| 34 | , typename RContext, typename Attribute> |
| 35 | bool parse_subject(Iterator& first, Iterator const& last |
| 36 | , Context const& context, RContext& rcontext, Attribute& attr |
| 37 | , traits::container_attribute) const |
| 38 | { |
| 39 | detail::parse_into_container( |
| 40 | this->subject, first, last, context, rcontext, attr); |
| 41 | return true; |
| 42 | } |
| 43 | |
| 44 | // Attribute is an optional |
| 45 | template <typename Iterator, typename Context |
| 46 | , typename RContext, typename Attribute> |
| 47 | bool parse_subject(Iterator& first, Iterator const& last |
| 48 | , Context const& context, RContext& rcontext, Attribute& attr |
| 49 | , traits::optional_attribute) const |
| 50 | { |
| 51 | typedef typename |
| 52 | x3::traits::optional_value<Attribute>::type |
| 53 | value_type; |
| 54 | |
| 55 | // create a local value |
| 56 | value_type val{}; |
| 57 | |
| 58 | if (this->subject.parse(first, last, context, rcontext, val)) |
| 59 | { |
| 60 | // assign the parsed value into our attribute |
| 61 | x3::traits::move_to(val, attr); |
| 62 | } |
| 63 | return true; |
| 64 | } |
| 65 | }; |
| 66 | |
| 67 | template <typename Subject> |
| 68 | constexpr optional<typename extension::as_parser<Subject>::value_type> |
| 69 | operator-(Subject const& subject) |
| 70 | { |
| 71 | return { as_parser(subject) }; |
| 72 | } |
| 73 | }}} |
| 74 | |
| 75 | namespace boost { namespace spirit { namespace x3 { namespace traits |
| 76 | { |
| 77 | template <typename Subject, typename Context> |
| 78 | struct attribute_of<x3::optional<Subject>, Context> |
| 79 | : build_optional< |
| 80 | typename attribute_of<Subject, Context>::type> {}; |
| 81 | }}}} |
| 82 | |
| 83 | #endif |
| 84 | |