| 1 | // Copyright (c) 2001-2011 Hartmut Kaiser |
| 2 | // |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #if !defined(BOOST_SPIRIT_KARMA_DELIMIT_MAR_02_2007_0217PM) |
| 7 | #define BOOST_SPIRIT_KARMA_DELIMIT_MAR_02_2007_0217PM |
| 8 | |
| 9 | #if defined(_MSC_VER) |
| 10 | #pragma once |
| 11 | #endif |
| 12 | |
| 13 | #include <boost/spirit/home/karma/meta_compiler.hpp> |
| 14 | #include <boost/spirit/home/karma/generator.hpp> |
| 15 | #include <boost/spirit/home/karma/domain.hpp> |
| 16 | #include <boost/spirit/home/karma/detail/unused_delimiter.hpp> |
| 17 | #include <boost/spirit/home/karma/delimit_out.hpp> |
| 18 | #include <boost/spirit/home/karma/auxiliary/lazy.hpp> |
| 19 | #include <boost/spirit/home/karma/char/char.hpp> |
| 20 | #include <boost/spirit/home/support/unused.hpp> |
| 21 | #include <boost/spirit/home/support/common_terminals.hpp> |
| 22 | #include <boost/spirit/home/support/has_semantic_action.hpp> |
| 23 | #include <boost/spirit/home/support/handles_container.hpp> |
| 24 | #include <boost/spirit/home/karma/detail/attributes.hpp> |
| 25 | #include <boost/spirit/home/support/info.hpp> |
| 26 | #include <boost/fusion/include/at.hpp> |
| 27 | #include <boost/fusion/include/vector.hpp> |
| 28 | |
| 29 | namespace boost { namespace spirit |
| 30 | { |
| 31 | /////////////////////////////////////////////////////////////////////////// |
| 32 | // Enablers |
| 33 | /////////////////////////////////////////////////////////////////////////// |
| 34 | template <> |
| 35 | struct use_directive<karma::domain, tag::delimit> // enables delimit[] |
| 36 | : mpl::true_ {}; |
| 37 | |
| 38 | // enables delimit(d)[g], where d is a generator |
| 39 | template <typename T> |
| 40 | struct use_directive<karma::domain |
| 41 | , terminal_ex<tag::delimit, fusion::vector1<T> > > |
| 42 | : boost::spirit::traits::matches<karma::domain, T> {}; |
| 43 | |
| 44 | // enables *lazy* delimit(d)[g] |
| 45 | template <> |
| 46 | struct use_lazy_directive<karma::domain, tag::delimit, 1> |
| 47 | : mpl::true_ {}; |
| 48 | |
| 49 | }} |
| 50 | |
| 51 | namespace boost { namespace spirit { namespace karma |
| 52 | { |
| 53 | #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS |
| 54 | using spirit::delimit; |
| 55 | #endif |
| 56 | using spirit::delimit_type; |
| 57 | |
| 58 | /////////////////////////////////////////////////////////////////////////// |
| 59 | // The redelimit_generator generator is used for delimit[...] directives. |
| 60 | /////////////////////////////////////////////////////////////////////////// |
| 61 | template <typename Subject> |
| 62 | struct redelimit_generator : unary_generator<redelimit_generator<Subject> > |
| 63 | { |
| 64 | typedef Subject subject_type; |
| 65 | |
| 66 | typedef typename subject_type::properties properties; |
| 67 | |
| 68 | template <typename Context, typename Iterator> |
| 69 | struct attribute |
| 70 | : traits::attribute_of<subject_type, Context, Iterator> |
| 71 | {}; |
| 72 | |
| 73 | redelimit_generator(Subject const& subject) |
| 74 | : subject(subject) {} |
| 75 | |
| 76 | template <typename OutputIterator, typename Context, typename Delimiter |
| 77 | , typename Attribute> |
| 78 | bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d |
| 79 | , Attribute const& attr) const |
| 80 | { |
| 81 | // The delimit_space generator simply dispatches to the embedded |
| 82 | // generator while supplying either the delimiter which has been |
| 83 | // used before a surrounding verbatim[] directive or a single |
| 84 | // space as the new delimiter to use (if no surrounding verbatim[] |
| 85 | // was specified). |
| 86 | return subject.generate(sink, ctx |
| 87 | , detail::get_delimiter(d, compile<karma::domain>(expr: ' ')), attr); |
| 88 | } |
| 89 | |
| 90 | template <typename Context> |
| 91 | info what(Context& context) const |
| 92 | { |
| 93 | return info("delimit" , subject.what(context)); |
| 94 | } |
| 95 | |
| 96 | Subject subject; |
| 97 | }; |
| 98 | |
| 99 | /////////////////////////////////////////////////////////////////////////// |
| 100 | // The delimit_generator is used for delimit(d)[...] directives. |
| 101 | /////////////////////////////////////////////////////////////////////////// |
| 102 | template <typename Subject, typename Delimiter> |
| 103 | struct delimit_generator |
| 104 | : unary_generator<delimit_generator<Subject, Delimiter> > |
| 105 | { |
| 106 | typedef Subject subject_type; |
| 107 | typedef Delimiter delimiter_type; |
| 108 | |
| 109 | typedef typename subject_type::properties properties; |
| 110 | |
| 111 | template <typename Context, typename Iterator> |
| 112 | struct attribute |
| 113 | : traits::attribute_of<subject_type, Context, Iterator> |
| 114 | {}; |
| 115 | |
| 116 | delimit_generator(Subject const& subject, Delimiter const& delimiter) |
| 117 | : subject(subject), delimiter(delimiter) {} |
| 118 | |
| 119 | template <typename OutputIterator, typename Context |
| 120 | , typename Delimiter_, typename Attribute> |
| 121 | bool generate(OutputIterator& sink, Context& ctx, Delimiter_ const& |
| 122 | , Attribute const& attr) const |
| 123 | { |
| 124 | // the delimit generator simply dispatches to the embedded |
| 125 | // generator while supplying it's argument as the new delimiter |
| 126 | // to use |
| 127 | return subject.generate(sink, ctx, delimiter, attr); |
| 128 | } |
| 129 | |
| 130 | template <typename Context> |
| 131 | info what(Context& context) const |
| 132 | { |
| 133 | return info("delimit" , subject.what(context)); |
| 134 | } |
| 135 | |
| 136 | Subject subject; |
| 137 | Delimiter delimiter; |
| 138 | }; |
| 139 | |
| 140 | /////////////////////////////////////////////////////////////////////////// |
| 141 | // Generator generators: make_xxx function (objects) |
| 142 | /////////////////////////////////////////////////////////////////////////// |
| 143 | template <typename Subject, typename Modifiers> |
| 144 | struct make_directive<tag::delimit, Subject, Modifiers> |
| 145 | { |
| 146 | typedef redelimit_generator<Subject> result_type; |
| 147 | result_type operator()(unused_type, Subject const& subject |
| 148 | , unused_type) const |
| 149 | { |
| 150 | return result_type(subject); |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | template <typename Delimiter, typename Subject, typename Modifiers> |
| 155 | struct make_directive< |
| 156 | terminal_ex<tag::delimit, fusion::vector1<Delimiter> > |
| 157 | , Subject, Modifiers> |
| 158 | { |
| 159 | typedef typename |
| 160 | result_of::compile<karma::domain, Delimiter, Modifiers>::type |
| 161 | delimiter_type; |
| 162 | |
| 163 | typedef delimit_generator<Subject, delimiter_type> result_type; |
| 164 | |
| 165 | template <typename Terminal> |
| 166 | result_type operator()(Terminal const& term, Subject const& subject |
| 167 | , unused_type) const |
| 168 | { |
| 169 | return result_type(subject |
| 170 | , compile<karma::domain>(fusion::at_c<0>(term.args))); |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | }}} |
| 175 | |
| 176 | namespace boost { namespace spirit { namespace traits |
| 177 | { |
| 178 | /////////////////////////////////////////////////////////////////////////// |
| 179 | template <typename Subject> |
| 180 | struct has_semantic_action<karma::redelimit_generator<Subject> > |
| 181 | : unary_has_semantic_action<Subject> {}; |
| 182 | |
| 183 | template <typename Subject, typename Delimiter> |
| 184 | struct has_semantic_action<karma::delimit_generator<Subject, Delimiter> > |
| 185 | : unary_has_semantic_action<Subject> {}; |
| 186 | |
| 187 | /////////////////////////////////////////////////////////////////////////// |
| 188 | template <typename Subject, typename Attribute |
| 189 | , typename Context, typename Iterator> |
| 190 | struct handles_container<karma::redelimit_generator<Subject>, Attribute |
| 191 | , Context, Iterator> |
| 192 | : unary_handles_container<Subject, Attribute, Context, Iterator> {}; |
| 193 | |
| 194 | template <typename Subject, typename Delimiter, typename Attribute |
| 195 | , typename Context, typename Iterator> |
| 196 | struct handles_container<karma::delimit_generator<Subject, Delimiter> |
| 197 | , Attribute, Context, Iterator> |
| 198 | : unary_handles_container<Subject, Attribute, Context, Iterator> {}; |
| 199 | }}} |
| 200 | |
| 201 | #endif |
| 202 | |