1 /*=============================================================================
2 Copyright (c) 2014 Joel de Guzman
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_X3_WITH_MAY_02_2014_0749AM)
8 #define SPIRIT_X3_WITH_MAY_02_2014_0749AM
10 #include <boost/spirit/home/x3/support/unused.hpp>
11 #include <boost/spirit/home/x3/core/parser.hpp>
13 namespace boost { namespace spirit { namespace x3
15 ///////////////////////////////////////////////////////////////////////////
16 // with directive injects a value into the context prior to parsing.
17 ///////////////////////////////////////////////////////////////////////////
18 template <typename Subject, typename Derived, typename T>
19 struct with_value_holder
20 : unary_parser<Subject, Derived>
22 typedef unary_parser<Subject, Derived> base_type;
24 with_value_holder(Subject const& subject, T const& val)
29 template <typename Subject, typename Derived, typename T>
30 struct with_value_holder<Subject, Derived, T const>
31 : unary_parser<Subject, Derived>
33 typedef unary_parser<Subject, Derived> base_type;
35 with_value_holder(Subject const& subject, T const& val)
40 template <typename Subject, typename ID, typename T>
42 : with_value_holder<Subject, with_directive<Subject, ID, T>, T>
44 typedef with_value_holder<Subject, with_directive<Subject, ID, T>, T> base_type;
45 static bool const is_pass_through_unary = true;
46 static bool const handles_container = Subject::handles_container;
48 typedef Subject subject_type;
50 with_directive(Subject const& subject, T const& val)
51 : base_type(subject, val) {}
53 template <typename Iterator, typename Context
54 , typename RContext, typename Attribute>
55 bool parse(Iterator& first, Iterator const& last
56 , Context const& context, RContext& rcontext, Attribute& attr) const
58 return this->subject.parse(
60 , make_context<ID>(this->val, context)
66 template <typename ID, typename T, typename NextContext = unused_type>
69 typedef context<ID, T, NextContext> type;
72 template <typename ID, typename T>
73 struct with_context<ID, T, unused_type>
75 typedef context<ID, T> type;
78 template <typename ID, typename T>
86 template <typename Subject>
87 with_directive<typename extension::as_parser<Subject>::value_type, ID, T>
88 operator[](Subject const& subject) const
90 return { as_parser(subject), val };
94 template <typename ID, typename T>
95 inline with_gen<ID, T> with(T& val)
100 template <typename ID, typename T>
101 inline with_gen<ID, T const> with(T const& val)