1 // Copyright (c) 2001-2011 Hartmut Kaiser
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)
6 #if !defined(BOOST_SPIRIT_KARMA_DELIMIT_MAR_02_2007_0217PM)
7 #define BOOST_SPIRIT_KARMA_DELIMIT_MAR_02_2007_0217PM
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/support/unused.hpp>
20 #include <boost/spirit/home/support/common_terminals.hpp>
21 #include <boost/spirit/home/support/has_semantic_action.hpp>
22 #include <boost/spirit/home/support/handles_container.hpp>
23 #include <boost/spirit/home/karma/detail/attributes.hpp>
24 #include <boost/spirit/home/support/info.hpp>
25 #include <boost/fusion/include/at.hpp>
26 #include <boost/fusion/include/vector.hpp>
28 namespace boost { namespace spirit
30 ///////////////////////////////////////////////////////////////////////////
32 ///////////////////////////////////////////////////////////////////////////
34 struct use_directive<karma::domain, tag::delimit> // enables delimit[]
37 // enables delimit(d)[g], where d is a generator
39 struct use_directive<karma::domain
40 , terminal_ex<tag::delimit, fusion::vector1<T> > >
41 : boost::spirit::traits::matches<karma::domain, T> {};
43 // enables *lazy* delimit(d)[g]
45 struct use_lazy_directive<karma::domain, tag::delimit, 1>
50 namespace boost { namespace spirit { namespace karma
52 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
53 using spirit::delimit;
55 using spirit::delimit_type;
57 ///////////////////////////////////////////////////////////////////////////
58 // The redelimit_generator generator is used for delimit[...] directives.
59 ///////////////////////////////////////////////////////////////////////////
60 template <typename Subject>
61 struct redelimit_generator : unary_generator<redelimit_generator<Subject> >
63 typedef Subject subject_type;
65 typedef typename subject_type::properties properties;
67 template <typename Context, typename Iterator>
69 : traits::attribute_of<subject_type, Context, Iterator>
72 redelimit_generator(Subject const& subject)
75 template <typename OutputIterator, typename Context, typename Delimiter
77 bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d
78 , Attribute const& attr) const
80 // The delimit_space generator simply dispatches to the embedded
81 // generator while supplying either the delimiter which has been
82 // used before a surrounding verbatim[] directive or a single
83 // space as the new delimiter to use (if no surrounding verbatim[]
85 return subject.generate(sink, ctx
86 , detail::get_delimiter(d, compile<karma::domain>(' ')), attr);
89 template <typename Context>
90 info what(Context& context) const
92 return info("delimit", subject.what(context));
98 ///////////////////////////////////////////////////////////////////////////
99 // The delimit_generator is used for delimit(d)[...] directives.
100 ///////////////////////////////////////////////////////////////////////////
101 template <typename Subject, typename Delimiter>
102 struct delimit_generator
103 : unary_generator<delimit_generator<Subject, Delimiter> >
105 typedef Subject subject_type;
106 typedef Delimiter delimiter_type;
108 typedef typename subject_type::properties properties;
110 template <typename Context, typename Iterator>
112 : traits::attribute_of<subject_type, Context, Iterator>
115 delimit_generator(Subject const& subject, Delimiter const& delimiter)
116 : subject(subject), delimiter(delimiter) {}
118 template <typename OutputIterator, typename Context
119 , typename Delimiter_, typename Attribute>
120 bool generate(OutputIterator& sink, Context& ctx, Delimiter_ const&
121 , Attribute const& attr) const
123 // the delimit generator simply dispatches to the embedded
124 // generator while supplying it's argument as the new delimiter
126 return subject.generate(sink, ctx, delimiter, attr);
129 template <typename Context>
130 info what(Context& context) const
132 return info("delimit", subject.what(context));
139 ///////////////////////////////////////////////////////////////////////////
140 // Generator generators: make_xxx function (objects)
141 ///////////////////////////////////////////////////////////////////////////
142 template <typename Subject, typename Modifiers>
143 struct make_directive<tag::delimit, Subject, Modifiers>
145 typedef redelimit_generator<Subject> result_type;
146 result_type operator()(unused_type, Subject const& subject
149 return result_type(subject);
153 template <typename Delimiter, typename Subject, typename Modifiers>
154 struct make_directive<
155 terminal_ex<tag::delimit, fusion::vector1<Delimiter> >
156 , Subject, Modifiers>
159 result_of::compile<karma::domain, Delimiter, Modifiers>::type
162 typedef delimit_generator<Subject, delimiter_type> result_type;
164 template <typename Terminal>
165 result_type operator()(Terminal const& term, Subject const& subject
168 return result_type(subject
169 , compile<karma::domain>(fusion::at_c<0>(term.args)));
175 namespace boost { namespace spirit { namespace traits
177 ///////////////////////////////////////////////////////////////////////////
178 template <typename Subject>
179 struct has_semantic_action<karma::redelimit_generator<Subject> >
180 : unary_has_semantic_action<Subject> {};
182 template <typename Subject, typename Delimiter>
183 struct has_semantic_action<karma::delimit_generator<Subject, Delimiter> >
184 : unary_has_semantic_action<Subject> {};
186 ///////////////////////////////////////////////////////////////////////////
187 template <typename Subject, typename Attribute
188 , typename Context, typename Iterator>
189 struct handles_container<karma::redelimit_generator<Subject>, Attribute
191 : unary_handles_container<Subject, Attribute, Context, Iterator> {};
193 template <typename Subject, typename Delimiter, typename Attribute
194 , typename Context, typename Iterator>
195 struct handles_container<karma::delimit_generator<Subject, Delimiter>
196 , Attribute, Context, Iterator>
197 : unary_handles_container<Subject, Attribute, Context, Iterator> {};