Chris@16
|
1 /*=============================================================================
|
Chris@16
|
2 Copyright (c) 2001-2011 Joel de Guzman
|
Chris@16
|
3 Copyright (c) 2001-2011 Hartmut Kaiser
|
Chris@16
|
4
|
Chris@16
|
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 =============================================================================*/
|
Chris@16
|
8 #if !defined(SPIRIT_OPTIONAL_MARCH_23_2007_1117PM)
|
Chris@16
|
9 #define SPIRIT_OPTIONAL_MARCH_23_2007_1117PM
|
Chris@16
|
10
|
Chris@16
|
11 #if defined(_MSC_VER)
|
Chris@16
|
12 #pragma once
|
Chris@16
|
13 #endif
|
Chris@16
|
14
|
Chris@16
|
15 #include <boost/spirit/home/support/unused.hpp>
|
Chris@16
|
16 #include <boost/spirit/home/qi/detail/attributes.hpp>
|
Chris@16
|
17 #include <boost/spirit/home/support/has_semantic_action.hpp>
|
Chris@16
|
18 #include <boost/spirit/home/support/handles_container.hpp>
|
Chris@16
|
19 #include <boost/spirit/home/support/info.hpp>
|
Chris@16
|
20 #include <boost/spirit/home/support/container.hpp>
|
Chris@16
|
21 #include <boost/spirit/home/qi/parser.hpp>
|
Chris@16
|
22 #include <boost/spirit/home/qi/meta_compiler.hpp>
|
Chris@16
|
23 #include <boost/spirit/home/qi/detail/assign_to.hpp>
|
Chris@16
|
24 #include <boost/optional.hpp>
|
Chris@16
|
25 #include <vector>
|
Chris@16
|
26
|
Chris@16
|
27 namespace boost { namespace spirit
|
Chris@16
|
28 {
|
Chris@16
|
29 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
30 // Enablers
|
Chris@16
|
31 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
32 template <>
|
Chris@16
|
33 struct use_operator<qi::domain, proto::tag::negate> // enables -p
|
Chris@16
|
34 : mpl::true_ {};
|
Chris@16
|
35 }}
|
Chris@16
|
36
|
Chris@16
|
37 namespace boost { namespace spirit { namespace qi
|
Chris@16
|
38 {
|
Chris@16
|
39 template <typename Subject>
|
Chris@16
|
40 struct optional : unary_parser<optional<Subject> >
|
Chris@16
|
41 {
|
Chris@16
|
42 typedef Subject subject_type;
|
Chris@16
|
43
|
Chris@16
|
44 template <typename Context, typename Iterator>
|
Chris@16
|
45 struct attribute
|
Chris@16
|
46 {
|
Chris@16
|
47 // Build a boost::optional from the subject's attribute. Note
|
Chris@16
|
48 // that boost::optional may return unused_type if the
|
Chris@16
|
49 // subject's attribute is an unused_type.
|
Chris@16
|
50 typedef typename
|
Chris@16
|
51 traits::build_optional<
|
Chris@16
|
52 typename traits::
|
Chris@16
|
53 attribute_of<Subject, Context, Iterator>::type
|
Chris@16
|
54 >::type
|
Chris@16
|
55 type;
|
Chris@16
|
56 };
|
Chris@16
|
57
|
Chris@16
|
58 optional(Subject const& subject_)
|
Chris@16
|
59 : subject(subject_) {}
|
Chris@16
|
60
|
Chris@16
|
61 template <typename Iterator, typename Context
|
Chris@16
|
62 , typename Skipper, typename Attribute>
|
Chris@16
|
63 bool parse_impl(Iterator& first, Iterator const& last
|
Chris@16
|
64 , Context& context, Skipper const& skipper
|
Chris@16
|
65 , Attribute& attr_, mpl::false_) const
|
Chris@16
|
66 {
|
Chris@16
|
67 // create a local value if Attribute is not unused_type
|
Chris@101
|
68 typename spirit::result_of::optional_value<Attribute>::type val =
|
Chris@16
|
69 typename spirit::result_of::optional_value<Attribute>::type();
|
Chris@16
|
70
|
Chris@16
|
71 if (subject.parse(first, last, context, skipper, val))
|
Chris@16
|
72 {
|
Chris@16
|
73 // assign the parsed value into our attribute
|
Chris@16
|
74 spirit::traits::assign_to(val, attr_);
|
Chris@16
|
75 }
|
Chris@16
|
76 return true;
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 template <typename Iterator, typename Context
|
Chris@16
|
80 , typename Skipper, typename Attribute>
|
Chris@16
|
81 bool parse_impl(Iterator& first, Iterator const& last
|
Chris@16
|
82 , Context& context, Skipper const& skipper
|
Chris@16
|
83 , Attribute& attr_, mpl::true_) const
|
Chris@16
|
84 {
|
Chris@16
|
85 subject.parse(first, last, context, skipper, attr_);
|
Chris@16
|
86 return true;
|
Chris@16
|
87 }
|
Chris@16
|
88
|
Chris@16
|
89 template <typename Iterator, typename Context
|
Chris@16
|
90 , typename Skipper, typename Attribute>
|
Chris@16
|
91 bool parse(Iterator& first, Iterator const& last
|
Chris@16
|
92 , Context& context, Skipper const& skipper
|
Chris@16
|
93 , Attribute& attr_) const
|
Chris@16
|
94 {
|
Chris@101
|
95 typedef typename spirit::result_of::optional_value<Attribute>::type
|
Chris@16
|
96 attribute_type;
|
Chris@16
|
97
|
Chris@16
|
98 return parse_impl(first, last, context, skipper, attr_
|
Chris@16
|
99 , traits::is_container<attribute_type>());
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 template <typename Context>
|
Chris@16
|
103 info what(Context& context) const
|
Chris@16
|
104 {
|
Chris@16
|
105 return info("optional", subject.what(context));
|
Chris@16
|
106 }
|
Chris@16
|
107
|
Chris@16
|
108 Subject subject;
|
Chris@16
|
109 };
|
Chris@16
|
110
|
Chris@16
|
111 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
112 // Parser generators: make_xxx function (objects)
|
Chris@16
|
113 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
114 template <typename Elements, typename Modifiers>
|
Chris@16
|
115 struct make_composite<proto::tag::negate, Elements, Modifiers>
|
Chris@16
|
116 : make_unary_composite<Elements, optional>
|
Chris@16
|
117 {};
|
Chris@16
|
118 }}}
|
Chris@16
|
119
|
Chris@16
|
120 namespace boost { namespace spirit { namespace traits
|
Chris@16
|
121 {
|
Chris@16
|
122 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
123 template <typename Subject>
|
Chris@16
|
124 struct has_semantic_action<qi::optional<Subject> >
|
Chris@16
|
125 : unary_has_semantic_action<Subject> {};
|
Chris@16
|
126
|
Chris@16
|
127 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
128 template <typename Subject, typename Attribute, typename Context
|
Chris@16
|
129 , typename Iterator>
|
Chris@16
|
130 struct handles_container<qi::optional<Subject>, Attribute
|
Chris@16
|
131 , Context, Iterator>
|
Chris@16
|
132 : mpl::true_ {};
|
Chris@16
|
133 }}}
|
Chris@16
|
134
|
Chris@16
|
135 #endif
|