1 [/==============================================================================
2 Copyright (C) 2001-2011 Hartmut Kaiser
3 Copyright (C) 2001-2011 Joel de Guzman
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 ===============================================================================/]
9 [section:auxiliary Auxiliary Generators]
11 This module includes different auxiliary generators not fitting into any of the
12 other categories. It includes the `attr_cast`, `eol`, `eps`, and `lazy`
15 [heading Module Header]
17 // forwards to <boost/spirit/home/karma/auxiliary.hpp>
18 #include <boost/spirit/include/karma_auxiliary.hpp>
20 Also, see __include_structure__.
22 [/////////////////////////////////////////////////////////////////////////////]
23 [section:attr_cast Attribute Transformation Pseudo Generator (`attr_cast`)]
27 The `attr_cast<Exposed, Transformed>()` component invokes the embedded generator
28 while supplying an attribute of type `Transformed`. The supplied attribute gets created
29 from the original attribute (of type `Exposed`) passed to this component using the
30 customization point __customize_transform_attribute__.
35 // forwards to <boost/spirit/home/karma/auxiliary/attr_cast.hpp>
36 #include <boost/spirit/include/karma_attr_cast.hpp>
38 Also, see __include_structure__.
44 [[`boost::spirit::attr_cast // alias: boost::spirit::karma::attr_cast`]]
49 template <Exposed, Transformed>
50 <unspecified> attr_cast(<unspecified>);
52 [heading Template parameters]
55 [[Parameter] [Description] [Default]]
56 [[`Exposed`] [The type of the attribute supplied to the `attr_cast`.] [__unused_type__]]
57 [[`Transformed`][The type of the attribute expected by the embedded
58 generator `g`.] [__unused_type__]]
61 The `attr_cast` is a function template. It is possible to invoke it using the
66 attr_cast<Exposed, Transformed>(g)
68 depending on which of the attribute types can be deduced properly if not
73 [:__unary_generator_concept__]
75 [variablelist Notation
76 [[`g`] [A generator object.]]
79 [heading Expression Semantics]
81 Semantics of an expression is defined only where it differs from, or is
82 not defined in __unary_generator_concept__.
85 [[Expression] [Semantics]]
86 [[`attr_cast(g)`] [Create a component invoking the
87 generator `g` while passing an attribute of the type
88 as normally expected by `g`. The type of the supplied
89 attribute will be transformed to the type
90 `g` exposes as its attribute type (by using the
91 attribute customization point __customize_transform_attribute__).
92 This generator does not fail unless `g` fails.]]
93 [[`attr_cast<Exposed>(g)`] [Create a component invoking the
94 generator `g` while passing an attribute of the type
95 as normally expected by `g`. The supplied attribute
96 is expected to be of the type `Exposed`, it will be
97 transformed to the type `g` exposes as its attribute type
98 (using the attribute customization point
99 __customize_transform_attribute__).
100 This generator does not fail unless `g` fails.]]
101 [[`attr_cast<Exposed, Transformed>(g)`] [Create a component invoking the
102 generator `g` while passing an attribute of type
103 `Transformed`. The supplied attribute is expected
104 to be of the type `Exposed`, it will be transformed
105 to the type `Transformed` (using the attribute
106 customization point __customize_transform_attribute__).
107 This generator does not fail unless `g` fails.]]
113 [[Expression] [Attribute]]
114 [[`attr_cast(g)`] [`g: A --> attr_cast(g): A`]]
115 [[`attr_cast<Exposed>(g)`] [`g: A --> attr_cast<Exposed>(g): Exposed`]]
116 [[`attr_cast<Exposed, Transformed>(g)`]
117 [`g: A --> attr_cast<Exposed, Transformed>(g): Exposed`]]
122 [:The complexity of this component is fully defined by the complexity of the
123 embedded generator `g`.]
127 [note The test harness for the example(s) below is presented in the
128 __karma_basics_examples__ section.]
132 [reference_karma_includes]
134 Some using declarations:
136 [reference_karma_using_declarations_attr_cast]
138 The example references data structure `int_data` which needs a specialization of
139 the customization point __customize_transform_attribute__:
141 [reference_karma_auxiliary_attr_cast_data1]
143 Now we use the `attr_cast` pseudo generator to invoke the attribute
146 [reference_karma_attr_cast1]
150 [/////////////////////////////////////////////////////////////////////////////]
151 [section:eol End of Line Generator (`eol`)]
153 [heading Description]
155 The `eol` component generates a single newline character. It is equivalent
156 to `lit('\n')` or simply '\\n' (please see the [karma_char `char_`] generator
157 module for more details).
161 // forwards to <boost/spirit/home/karma/auxiliary/eol.hpp>
162 #include <boost/spirit/include/karma_eol.hpp>
164 Also, see __include_structure__.
170 [[`boost::spirit::eol // alias: boost::spirit::karma::eol`]]
175 [:__primitive_generator_concept__]
177 [heading Expression Semantics]
179 Semantics of an expression is defined only where it differs from, or is
180 not defined in __primitive_generator_concept__.
183 [[Expression] [Semantics]]
184 [[`eol`] [Create a component generating a single end of line
185 character in the output. This generator never fails
186 (unless the underlying output stream reports an
193 [[Expression] [Attribute]]
194 [[`eol`] [__unused__]]
201 The complexity is constant as a single character is generated in the output.
205 [note The test harness for the example(s) below is presented in the
206 __karma_basics_examples__ section.]
210 [reference_karma_includes]
212 Some using declarations:
214 [reference_karma_using_declarations_eol]
216 Basic usage of the `eol` generator:
218 [reference_karma_eol]
222 [/////////////////////////////////////////////////////////////////////////////]
223 [section:eps Epsilon Generator (`eps`)]
225 The family of `eps` components allows to create pseudo generators generating
226 an empty string. This feature is sometimes useful either to force a generator
227 to fail or to succeed or to insert semantic actions into the generation process.
229 [heading Description]
231 The Epsilon (`eps`) is a multi-purpose generator that emits a zero length
234 [heading Simple Form]
236 In its simplest form, `eps` creates a component generating an empty string
237 while always succeeding:
239 eps // always emits a zero-length string
241 This form is usually used to trigger a semantic action unconditionally.
242 For example, it is useful in triggering error messages when a set of
245 r = a | b | c | eps[error()]; // Call error if a, b, and c fail to generate
247 [heading Semantic Predicate]
249 The `eps(b)` component generates an empty string as well, but
250 succeeds only if `b` is `true` and fails otherwise. It's lazy variant `eps(fb)`
251 is equivalent to `eps(b)` except it evaluates the supplied function `fb` at
252 generate time, while using the return value as the criteria to succeed.
254 Semantic predicates allow you to attach a conditional function anywhere
255 in the grammar. In this role, the epsilon takes a __karma_lazy_argument__ that
256 returns `true` or `false`. The __karma_lazy_argument__ is typically a test
257 that is called to resolve ambiguity in the grammar. A generator failure will
258 be reported when the __karma_lazy_argument__ result evaluates to `false`.
259 Otherwise an empty string will be emitted. The general form is:
263 The __karma_lazy_argument__ `fb` is called to do a semantic test. If the test
264 returns true, `rest` will be evaluated. Otherwise, the production will return
265 early without ever touching rest.
269 // forwards to <boost/spirit/home/karma/auxiliary/eps.hpp>
270 #include <boost/spirit/include/karma_eps.hpp>
272 Also, see __include_structure__.
278 [[`boost::spirit::eps // alias: boost::spirit::karma::eps`]]
283 [:__primitive_generator_concept__]
285 [variablelist Notation
286 [[`b`] [A boolean value.]]
287 [[`fb`] [A __karma_lazy_argument__ that evaluates to a boolean value.]]
290 [heading Expression Semantics]
292 Semantics of an expression is defined only where it differs from, or is
293 not defined in __primitive_generator_concept__.
296 [[Expression] [Semantics]]
297 [[`eps`] [Creates a component generating an empty string.
299 [[`eps(b)`] [Creates a component generating an empty string.
300 Succeeds if `b` is `true` (unless the underlying
301 output stream reports an error).]]
302 [[`eps(fb)`] [Creates a component generating an empty string.
303 Succeeds if `fb` returns `true` at generate time
304 (unless the underlying output stream reports an
311 [[Expression] [Attribute]]
312 [[`eps`] [__unused__]]
313 [[`eps(b)`] [__unused__]]
314 [[`eps(fb)`] [__unused__]]
321 The complexity is constant as no output is generated.
325 [note The test harness for the example(s) below is presented in the
326 __karma_basics_examples__ section.]
330 [reference_karma_includes]
332 Some using declarations:
334 [reference_karma_using_declarations_eps]
336 Basic usage of the `eps` generator:
338 [reference_karma_eps]
342 [/////////////////////////////////////////////////////////////////////////////]
343 [section:lazy Lazy Generator (`lazy`)]
345 [heading Description]
347 The family of `lazy` components allows to use a dynamically returned generator
348 component for output generation. It calls the provided function or function
349 object at generate time using its return value as the actual generator to
354 // forwards to <boost/spirit/home/karma/auxiliary/lazy.hpp>
355 #include <boost/spirit/include/karma_lazy.hpp>
357 Also, see __include_structure__.
363 [[`boost::spirit::lazy // alias: boost::spirit::karma::lazy`]]
368 [:__generator_concept__]
370 [variablelist Notation
371 [[`fg`] [A function or function object that evaluates to a generator
372 object (an object exposing the __generator_concept__). This
373 function will be invoked at generate time.]]
376 The signature of `fg` is expected to be
380 where `G`, the function's return value, is the type of the generator to be
381 invoked, and `Context` is the generator's __karma_context__ type (The
382 first argument is __unused__ to make the `Context` the second argument. This
383 is done for uniformity with __karma_actions__).
385 [heading Expression Semantics]
387 Semantics of an expression is defined only where it differs from, or is
388 not defined in __generator_concept__.
391 [[Expression] [Semantics]]
392 [[`fg`] [The __boost_phoenix__ function object `fg` will be
393 invoked at generate time. It is expected to return a
394 generator instance. This generator is then invoked
395 in order to generate the output. This generator will
396 succeed as long as the invoked generated succeeds as
397 well (unless the underlying output stream reports
399 [[`lazy(fg)`] [The function or function object will be invoked at
400 generate time. It is expected to return a generator
401 instance (note this version of `lazy` does not
402 require `fg` to be a __boost_phoenix__ function
403 object). This generator is then invoked in order to
404 generate the output. This generator will succeed as
405 long as the invoked generated succeeds as well (except
406 if the underlying output stream reports an error).]]
412 [[Expression] [Attribute]]
413 [[`fg`] [The attribute type `G` as exposed by the generator `g`
414 returned from `fg`.]]
415 [[`lazy(fg)`] [The attribute type `G` as exposed by the generator `g`
416 returned from `fg`.]]
421 The complexity of the `lazy` component is determined by the complexity of the
422 generator returned from `fg`.
426 [note The test harness for the example(s) below is presented in the
427 __karma_basics_examples__ section.]
431 [reference_karma_includes]
433 Some using declarations:
435 [reference_karma_using_declarations_lazy]
437 Basic usage of the `lazy` generator:
439 [reference_karma_lazy]