]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2014 Joel de Guzman | |
3 | ||
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 | |
9 | ||
10 | #include <boost/spirit/home/x3/support/unused.hpp> | |
11 | #include <boost/spirit/home/x3/core/parser.hpp> | |
12 | ||
13 | namespace boost { namespace spirit { namespace x3 | |
14 | { | |
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> | |
21 | { | |
22 | typedef unary_parser<Subject, Derived> base_type; | |
23 | mutable T val; | |
24 | with_value_holder(Subject const& subject, T const& val) | |
25 | : base_type(subject) | |
26 | , val(val) {} | |
27 | }; | |
28 | ||
29 | template <typename Subject, typename Derived, typename T> | |
30 | struct with_value_holder<Subject, Derived, T const> | |
31 | : unary_parser<Subject, Derived> | |
32 | { | |
33 | typedef unary_parser<Subject, Derived> base_type; | |
34 | T val; | |
35 | with_value_holder(Subject const& subject, T const& val) | |
36 | : base_type(subject) | |
37 | , val(val) {} | |
38 | }; | |
39 | ||
40 | template <typename Subject, typename ID, typename T> | |
41 | struct with_directive | |
42 | : with_value_holder<Subject, with_directive<Subject, ID, T>, T> | |
43 | { | |
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; | |
47 | ||
48 | typedef Subject subject_type; | |
49 | ||
50 | with_directive(Subject const& subject, T const& val) | |
51 | : base_type(subject, val) {} | |
52 | ||
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 | |
57 | { | |
58 | return this->subject.parse( | |
59 | first, last | |
60 | , make_context<ID>(this->val, context) | |
61 | , rcontext | |
62 | , attr); | |
63 | } | |
64 | }; | |
65 | ||
66 | template <typename ID, typename T, typename NextContext = unused_type> | |
67 | struct with_context | |
68 | { | |
69 | typedef context<ID, T, NextContext> type; | |
70 | }; | |
71 | ||
72 | template <typename ID, typename T> | |
73 | struct with_context<ID, T, unused_type> | |
74 | { | |
75 | typedef context<ID, T> type; | |
76 | }; | |
77 | ||
78 | template <typename ID, typename T> | |
79 | struct with_gen | |
80 | { | |
81 | T& val; | |
82 | ||
83 | with_gen(T& val) | |
84 | : val(val) {} | |
85 | ||
86 | template <typename Subject> | |
87 | with_directive<typename extension::as_parser<Subject>::value_type, ID, T> | |
88 | operator[](Subject const& subject) const | |
89 | { | |
90 | return { as_parser(subject), val }; | |
91 | } | |
92 | }; | |
93 | ||
94 | template <typename ID, typename T> | |
95 | inline with_gen<ID, T> with(T& val) | |
96 | { | |
97 | return { val }; | |
98 | } | |
99 | ||
100 | template <typename ID, typename T> | |
101 | inline with_gen<ID, T const> with(T const& val) | |
102 | { | |
103 | return { val }; | |
104 | } | |
105 | }}} | |
106 | ||
107 | #endif |