]>
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 | =============================================================================*/ | |
f67539c2 TL |
7 | #ifndef BOOST_SPIRIT_X3_DIRECTIVE_WITH_HPP |
8 | #define BOOST_SPIRIT_X3_DIRECTIVE_WITH_HPP | |
7c673cae FG |
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; | |
f67539c2 | 24 | constexpr with_value_holder(Subject const& subject, T&& val) |
7c673cae | 25 | : base_type(subject) |
11fdf7f2 | 26 | , val(std::forward<T>(val)) {} |
7c673cae FG |
27 | }; |
28 | ||
29 | template <typename Subject, typename Derived, typename T> | |
11fdf7f2 | 30 | struct with_value_holder<Subject, Derived, T&> |
7c673cae FG |
31 | : unary_parser<Subject, Derived> |
32 | { | |
33 | typedef unary_parser<Subject, Derived> base_type; | |
11fdf7f2 | 34 | T& val; |
f67539c2 | 35 | constexpr with_value_holder(Subject const& subject, T& val) |
7c673cae FG |
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 | ||
f67539c2 | 50 | constexpr with_directive(Subject const& subject, T&& val) |
11fdf7f2 | 51 | : base_type(subject, std::forward<T>(val)) {} |
7c673cae FG |
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 | ||
7c673cae FG |
66 | template <typename ID, typename T> |
67 | struct with_gen | |
68 | { | |
11fdf7f2 | 69 | T&& val; |
7c673cae FG |
70 | |
71 | template <typename Subject> | |
f67539c2 | 72 | constexpr with_directive<typename extension::as_parser<Subject>::value_type, ID, T> |
7c673cae FG |
73 | operator[](Subject const& subject) const |
74 | { | |
11fdf7f2 | 75 | return { as_parser(subject), std::forward<T>(val) }; |
7c673cae FG |
76 | } |
77 | }; | |
78 | ||
79 | template <typename ID, typename T> | |
f67539c2 | 80 | constexpr with_gen<ID, T> with(T&& val) |
7c673cae | 81 | { |
11fdf7f2 | 82 | return { std::forward<T>(val) }; |
7c673cae FG |
83 | } |
84 | }}} | |
85 | ||
86 | #endif |