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