Semantic actions may be attached to any point in the grammar specification.
They allow to call a function or function object in order to provide the
value to be output by the generator the semantic action is attached to.
Semantic actions are associated with a generator using the syntax
g is an arbitrary
// forwards to <boost/spirit/home/karma/action.hpp> #include <boost/spirit/include/karma_action.hpp>
Also, see Include Structure.
Instances of a generator,
Attribute type exposed by a generator,
A (semantic action) function with signature
void(Attrib&, Context&, bool&). The third parameter is a boolean
flag that can be set to false to force the generator to fail. Both
Context and the boolean
flag are optional. For more information see below.
The attribute to be used to generate output from.
The type of the generator execution context. For more information see below.
Semantics of an expression is defined only where it differs from, or is
not defined in
Call semantic action,
The possible signatures for functions to be used as semantic actions are:
template <typename Attrib> void fa(Attrib& attr); template <typename Attrib, typename Context> void fa(Attrib& attr, Context& context); template <typename Attrib, typename Context> void fa(Attrib& attr, Context& context, bool& pass);
The function or function object is expected to return the value to generate
output from by assigning it to the first parameter,
Attrib is the attribute
type of the generator the semantic action is attached to.
Context is the
type of the generator execution context. This type is unspecified and depends
on the context the generator is invoked in. The value
is used by semantic actions written using Phoenix
to access various context dependent attributes and values. For more information
about Phoenix placeholder
expressions usable in semantic actions see
The third parameter,
can be used by the semantic action to force the associated generator to
fail. If pass is set to
the action generator will immediately return
as well, while not invoking
and not generating any output.
The complexity of the action generator is defined by the complexity of the generator the semantic action is attached to and the complexity of the function or function object used as the semantic action.
Please note that the use of semantic actions in Spirit.Karma generally forces the library to create a copy of the attribute, which might be a costly operation. Always consider using other means of associating a value with a particular generator first.
The test harness for the example(s) below is presented in the Basics Examples section.
#include <boost/spirit/include/karma.hpp> #include <boost/spirit/include/phoenix_core.hpp> #include <boost/spirit/include/phoenix_operator.hpp> #include <boost/fusion/include/std_pair.hpp> #include <iostream> #include <string>
Some using declarations:
using boost::spirit::karma::int_; using boost::spirit::karma::string; using boost::spirit::karma::_1; using boost::phoenix::ref; using boost::phoenix::val;
int i = 42; test_generator("42", int_[_1 = ref(i)]); test_generator("abc", string[_1 = val("abc")]);
More examples for semantic actions can be found here: Examples of Semantic Actions.