Chris@102
|
1 /*=============================================================================
|
Chris@102
|
2 Copyright (c) 2014 Joel de Guzman
|
Chris@102
|
3
|
Chris@102
|
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@102
|
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
6 =============================================================================*/
|
Chris@102
|
7 #if !defined(SPIRIT_X3_WITH_MAY_02_2014_0749AM)
|
Chris@102
|
8 #define SPIRIT_X3_WITH_MAY_02_2014_0749AM
|
Chris@102
|
9
|
Chris@102
|
10 #include <boost/spirit/home/x3/support/unused.hpp>
|
Chris@102
|
11 #include <boost/spirit/home/x3/core/parser.hpp>
|
Chris@102
|
12
|
Chris@102
|
13 namespace boost { namespace spirit { namespace x3
|
Chris@102
|
14 {
|
Chris@102
|
15 ///////////////////////////////////////////////////////////////////////////
|
Chris@102
|
16 // with directive injects a value into the context prior to parsing.
|
Chris@102
|
17 ///////////////////////////////////////////////////////////////////////////
|
Chris@102
|
18 template <typename Subject, typename Derived, typename T>
|
Chris@102
|
19 struct with_value_holder
|
Chris@102
|
20 : unary_parser<Subject, Derived>
|
Chris@102
|
21 {
|
Chris@102
|
22 typedef unary_parser<Subject, Derived> base_type;
|
Chris@102
|
23 mutable T val;
|
Chris@102
|
24 with_value_holder(Subject const& subject, T const& val)
|
Chris@102
|
25 : base_type(subject)
|
Chris@102
|
26 , val(val) {}
|
Chris@102
|
27 };
|
Chris@102
|
28
|
Chris@102
|
29 template <typename Subject, typename Derived, typename T>
|
Chris@102
|
30 struct with_value_holder<Subject, Derived, T const>
|
Chris@102
|
31 : unary_parser<Subject, Derived>
|
Chris@102
|
32 {
|
Chris@102
|
33 typedef unary_parser<Subject, Derived> base_type;
|
Chris@102
|
34 T val;
|
Chris@102
|
35 with_value_holder(Subject const& subject, T const& val)
|
Chris@102
|
36 : base_type(subject)
|
Chris@102
|
37 , val(val) {}
|
Chris@102
|
38 };
|
Chris@102
|
39
|
Chris@102
|
40 template <typename Subject, typename ID, typename T>
|
Chris@102
|
41 struct with_directive
|
Chris@102
|
42 : with_value_holder<Subject, with_directive<Subject, ID, T>, T>
|
Chris@102
|
43 {
|
Chris@102
|
44 typedef with_value_holder<Subject, with_directive<Subject, ID, T>, T> base_type;
|
Chris@102
|
45 static bool const is_pass_through_unary = true;
|
Chris@102
|
46 static bool const handles_container = Subject::handles_container;
|
Chris@102
|
47
|
Chris@102
|
48 typedef Subject subject_type;
|
Chris@102
|
49
|
Chris@102
|
50 with_directive(Subject const& subject, T const& val)
|
Chris@102
|
51 : base_type(subject, val) {}
|
Chris@102
|
52
|
Chris@102
|
53 template <typename Iterator, typename Context
|
Chris@102
|
54 , typename RContext, typename Attribute>
|
Chris@102
|
55 bool parse(Iterator& first, Iterator const& last
|
Chris@102
|
56 , Context const& context, RContext& rcontext, Attribute& attr) const
|
Chris@102
|
57 {
|
Chris@102
|
58 return this->subject.parse(
|
Chris@102
|
59 first, last
|
Chris@102
|
60 , make_context<ID>(this->val, context)
|
Chris@102
|
61 , rcontext
|
Chris@102
|
62 , attr);
|
Chris@102
|
63 }
|
Chris@102
|
64 };
|
Chris@102
|
65
|
Chris@102
|
66 template <typename ID, typename T, typename NextContext = unused_type>
|
Chris@102
|
67 struct with_context
|
Chris@102
|
68 {
|
Chris@102
|
69 typedef context<ID, T, NextContext> type;
|
Chris@102
|
70 };
|
Chris@102
|
71
|
Chris@102
|
72 template <typename ID, typename T>
|
Chris@102
|
73 struct with_context<ID, T, unused_type>
|
Chris@102
|
74 {
|
Chris@102
|
75 typedef context<ID, T> const type;
|
Chris@102
|
76 };
|
Chris@102
|
77
|
Chris@102
|
78 template <typename ID, typename T>
|
Chris@102
|
79 struct with_gen
|
Chris@102
|
80 {
|
Chris@102
|
81 T& val;
|
Chris@102
|
82
|
Chris@102
|
83 with_gen(T& val)
|
Chris@102
|
84 : val(val) {}
|
Chris@102
|
85
|
Chris@102
|
86 template <typename Subject>
|
Chris@102
|
87 with_directive<typename extension::as_parser<Subject>::value_type, ID, T>
|
Chris@102
|
88 operator[](Subject const& subject) const
|
Chris@102
|
89 {
|
Chris@102
|
90 return {as_parser(subject), val};
|
Chris@102
|
91 }
|
Chris@102
|
92 };
|
Chris@102
|
93
|
Chris@102
|
94 template <typename ID, typename T>
|
Chris@102
|
95 inline with_gen<ID, T> with(T& val)
|
Chris@102
|
96 {
|
Chris@102
|
97 return with_gen<ID, T>{val};
|
Chris@102
|
98 }
|
Chris@102
|
99
|
Chris@102
|
100 template <typename ID, typename T>
|
Chris@102
|
101 inline with_gen<ID, T const> with(T const& val)
|
Chris@102
|
102 {
|
Chris@102
|
103 return with_gen<ID, T const>{val};
|
Chris@102
|
104 }
|
Chris@102
|
105 }}}
|
Chris@102
|
106
|
Chris@102
|
107 #endif
|