3 <meta http-equiv=
"Content-Type" content=
"text/html; charset=US-ASCII">
4 <title>More on Actions
</title>
5 <link rel=
"stylesheet" href=
"../../../../../../doc/src/boostbook.css" type=
"text/css">
6 <meta name=
"generator" content=
"DocBook XSL Stylesheets V1.75.2">
7 <link rel=
"home" href=
"../../index.html" title=
"Chapter 1. Phoenix 3.2.0">
8 <link rel=
"up" href=
"../inside.html" title=
"Inside Phoenix">
9 <link rel=
"prev" href=
"expression/boilerplate_macros.html" title=
"Boilerplate Macros">
10 <link rel=
"next" href=
"rules.html" title=
"Predefined Expressions and Rules">
12 <body bgcolor=
"white" text=
"black" link=
"#0000FF" vlink=
"#840084" alink=
"#0000FF">
13 <table cellpadding=
"2" width=
"100%"><tr>
14 <td valign=
"top"><img alt=
"Boost C++ Libraries" width=
"277" height=
"86" src=
"../../../../../../boost.png"></td>
15 <td align=
"center"><a href=
"../../../../../../index.html">Home
</a></td>
16 <td align=
"center"><a href=
"../../../../../../libs/libraries.htm">Libraries
</a></td>
17 <td align=
"center"><a href=
"http://www.boost.org/users/people.html">People
</a></td>
18 <td align=
"center"><a href=
"http://www.boost.org/users/faq.html">FAQ
</a></td>
19 <td align=
"center"><a href=
"../../../../../../more/index.htm">More
</a></td>
22 <div class=
"spirit-nav">
23 <a accesskey=
"p" href=
"expression/boilerplate_macros.html"><img src=
"../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../inside.html"><img src=
"../../../../../../doc/src/images/up.png" alt=
"Up"></a><a accesskey=
"h" href=
"../../index.html"><img src=
"../../../../../../doc/src/images/home.png" alt=
"Home"></a><a accesskey=
"n" href=
"rules.html"><img src=
"../../../../../../doc/src/images/next.png" alt=
"Next"></a>
26 <div class=
"titlepage"><div><div><h3 class=
"title">
27 <a name=
"phoenix.inside.actions"></a><a class=
"link" href=
"actions.html" title=
"More on Actions">More on Actions
</a>
28 </h3></div></div></div>
30 As you know from the
<a class=
"link" href=
"actor.html" title=
"Actors in Detail">Actors in Detail
</a>
31 section, Actions are what brings life to a Phoenix expression tree.
34 When dealing with a Phoenix expression tree, it gets evaluated top-down.
37 <pre class=
"programlisting"><span class=
"identifier">_1
</span> <span class=
"special">+
</span> <span class=
"number">3</span> <span class=
"special">*
</span> <span class=
"identifier">_2
</span>
40 Can be visualized as an AST in the following way:
43 <span class=
"inlinemediaobject"><img src=
"../../images/simple_ast.png"></span>
46 In terms of actions this means:
48 <div class=
"itemizedlist"><ul class=
"itemizedlist" type=
"disc">
50 <code class=
"computeroutput"><span class=
"identifier">rule
</span><span class=
"special">::
</span><span class=
"identifier">plus
</span></code> is matched
54 <div class=
"itemizedlist"><ul class=
"itemizedlist" type=
"circle"><li class=
"listitem">
55 <code class=
"computeroutput"><span class=
"identifier">rule
</span><span class=
"special">::
</span><span class=
"identifier">placeholder
</span></code> is matched
60 <div class=
"itemizedlist"><ul class=
"itemizedlist" type=
"circle"><li class=
"listitem">
61 <code class=
"computeroutput"><span class=
"identifier">rule
</span><span class=
"special">::
</span><span class=
"identifier">multiplies
</span></code> is matched
62 <div class=
"itemizedlist"><ul class=
"itemizedlist" type=
"square">
65 <div class=
"itemizedlist"><ul class=
"itemizedlist" type=
"disc"><li class=
"listitem">
66 <code class=
"computeroutput"><span class=
"identifier">rule
</span><span class=
"special">::
</span><span class=
"identifier">value
</span></code>
72 <div class=
"itemizedlist"><ul class=
"itemizedlist" type=
"disc"><li class=
"listitem">
73 <code class=
"computeroutput"><span class=
"identifier">rule
</span><span class=
"special">::
</span><span class=
"identifier">placeholder
</span></code>
82 Every time a rule is matched, an action will be called. The action determines
83 how the Phoenix AST will be traversed.
86 <a name=
"phoenix.inside.actions.h0"></a>
87 <span><a name=
"phoenix.inside.actions.writing_an_action"></a></span><a class=
"link" href=
"actions.html#phoenix.inside.actions.writing_an_action">Writing
91 As mentioned in
<a class=
"link" href=
"actor.html" title=
"Actors in Detail">Actors in Detail
</a>
92 actions are
<a href=
"http://www.boost.org/doc/libs/release/doc/html/PrimitiveTransform.html" target=
"_top">Proto
93 Primitive Transforms
</a> for convenience Phoenix provides an abstraction
96 <pre class=
"programlisting"><span class=
"keyword">template
</span> <span class=
"special"><</span><span class=
"keyword">typename
</span> <span class=
"identifier">Fun
</span><span class=
"special">></span>
97 <span class=
"keyword">struct
</span> <span class=
"identifier">call
</span><span class=
"special">;
</span>
100 This is similar to
<a href=
"http://www.boost.org/doc/libs/release/doc/html/boost/proto/call.html" target=
"_top"><code class=
"computeroutput"><span class=
"identifier">proto
</span><span class=
"special">::
</span><span class=
"identifier">call
</span></code></a> but does more. It calls the
101 <code class=
"computeroutput"><span class=
"identifier">Fun
</span></code> function object passed
102 as template parameter with the
<code class=
"computeroutput"><span class=
"identifier">Context
</span></code>
103 and the children of the expression associated with the rule.
106 Lets have an (simplified) example on how to write an evaluation action for
107 <code class=
"computeroutput"><span class=
"identifier">rule
</span><span class=
"special">::
</span><span class=
"identifier">plus
</span></code>:
109 <pre class=
"programlisting"><span class=
"keyword">struct
</span> <span class=
"identifier">plus_eval
</span>
110 <span class=
"special">{
</span>
111 <span class=
"keyword">typedef
</span> <span class=
"keyword">int
</span> <span class=
"identifier">result_type
</span><span class=
"special">;
</span>
113 <span class=
"keyword">template
</span> <span class=
"special"><</span><span class=
"keyword">typename
</span> <span class=
"identifier">Lhs
</span><span class=
"special">,
</span> <span class=
"keyword">typename
</span> <span class=
"identifier">Rhs
</span><span class=
"special">,
</span> <span class=
"keyword">typename
</span> <span class=
"identifier">Context
</span><span class=
"special">></span>
114 <span class=
"identifier">result_type
</span> <span class=
"keyword">operator
</span><span class=
"special">()(
</span><span class=
"identifier">Lhs
</span> <span class=
"keyword">const
</span><span class=
"special">&</span> <span class=
"identifier">lhs
</span><span class=
"special">,
</span> <span class=
"identifier">Rhs
</span> <span class=
"keyword">const
</span> <span class=
"special">&</span><span class=
"identifier">rhs
</span><span class=
"special">,
</span> <span class=
"identifier">Context
</span> <span class=
"special">&</span> <span class=
"identifier">ctx
</span><span class=
"special">)
</span>
115 <span class=
"special">{
</span>
116 <span class=
"keyword">return
</span> <span class=
"identifier">eval
</span><span class=
"special">(
</span><span class=
"identifier">lhs
</span><span class=
"special">,
</span> <span class=
"identifier">ctx
</span><span class=
"special">)
</span> <span class=
"special">+
</span> <span class=
"identifier">eval
</span><span class=
"special">(
</span><span class=
"identifier">rhs
</span><span class=
"special">,
</span> <span class=
"identifier">ctx
</span><span class=
"special">);
</span>
117 <span class=
"special">}
</span>
118 <span class=
"special">};
</span>
120 <span class=
"keyword">template
</span> <span class=
"special"><></span>
121 <span class=
"keyword">struct
</span> <span class=
"identifier">default_actions
</span><span class=
"special">::
</span><span class=
"identifier">when
</span><span class=
"special"><</span><span class=
"identifier">rule
</span><span class=
"special">::
</span><span class=
"identifier">plus
</span><span class=
"special">></span>
122 <span class=
"special">:
</span> <span class=
"identifier">call
</span><span class=
"special"><</span><span class=
"identifier">plus_eval
</span><span class=
"special">></span>
123 <span class=
"special">{};
</span>
126 That's it. When evaluating a
<code class=
"computeroutput"><span class=
"identifier">plus
</span></code>
127 expression, the
<code class=
"computeroutput"><span class=
"identifier">plus_eval
</span></code>
128 callable gets called with the left hand side and right hand side expression
129 and the associated Context.
132 <span class=
"bold"><strong>But there is more:
</strong></span> As Actions
<span class=
"emphasis"><em>can
</em></span>
133 be full fletched
<a href=
"http://www.boost.org/doc/libs/release/doc/html/Transform.html" target=
"_top">Proto
134 Transforms
</a>, you can in fact use any proto expression you can imagine
135 as the action. Phoenix predifines a set of callables and transform to deal
136 with the Context information passed along and of course every Phoenix expression
137 can be used as a Phoenix grammar or
<a href=
"http://www.boost.org/doc/libs/release/doc/html/boost/proto/pass_through.html" target=
"_top">Proto
138 Pass Through Transform
</a>.
140 <div class=
"variablelist">
141 <p class=
"title"><b></b></p>
143 <dt><span class=
"term"><code class=
"computeroutput"><span class=
"identifier">functional
</span><span class=
"special">::
</span><span class=
"identifier">context
</span><span class=
"special">(
</span><span class=
"identifier">Env
</span><span class=
"special">,
</span> <span class=
"identifier">Actions
</span><span class=
"special">)
</span></code></span></dt>
145 A
<a href=
"http://www.boost.org/doc/libs/release/doc/html/Callable.html" target=
"_top">Proto
146 Callable Transform
</a> that creates a new context out of the
<code class=
"computeroutput"><span class=
"identifier">Env
</span></code> and
<code class=
"computeroutput"><span class=
"identifier">Actions
</span></code>
149 <dt><span class=
"term"><code class=
"computeroutput"><span class=
"identifier">functional
</span><span class=
"special">::
</span><span class=
"identifier">env
</span><span class=
"special">(
</span><span class=
"identifier">Context
</span><span class=
"special">)
</span></code></span></dt>
151 A
<a href=
"http://www.boost.org/doc/libs/release/doc/html/Callable.html" target=
"_top">Proto
152 Callable Transform
</a> that returns the environment out of the
153 <code class=
"computeroutput"><span class=
"identifier">Context
</span></code> parameter
155 <dt><span class=
"term"><code class=
"computeroutput"><span class=
"identifier">functional
</span><span class=
"special">::
</span><span class=
"identifier">actions
</span><span class=
"special">(
</span><span class=
"identifier">Context
</span><span class=
"special">)
</span></code></span></dt>
157 A
<a href=
"http://www.boost.org/doc/libs/release/doc/html/Callable.html" target=
"_top">Proto
158 Callable Transform
</a> that returns the actions out of the
<code class=
"computeroutput"><span class=
"identifier">Context
</span></code> parameter
160 <dt><span class=
"term"><code class=
"computeroutput"><span class=
"identifier">_context
</span></code></span></dt>
162 A
<a href=
"http://www.boost.org/doc/libs/release/doc/html/PrimitiveTransform.html" target=
"_top">Proto
163 Primitive Transform
</a> that returns the current context
165 <dt><span class=
"term"><code class=
"computeroutput"><span class=
"identifier">_env
</span></code></span></dt>
167 A
<a href=
"http://www.boost.org/doc/libs/release/doc/html/PrimitiveTransform.html" target=
"_top">Proto
168 Primitive Transform
</a> that returns the current environment
170 <dt><span class=
"term"><code class=
"computeroutput"><span class=
"identifier">_actions
</span></code></span></dt>
172 A
<a href=
"http://www.boost.org/doc/libs/release/doc/html/PrimitiveTransform.html" target=
"_top">Proto
173 Primitive Transform
</a> that returns the current actions
175 <dt><span class=
"term"><code class=
"computeroutput"><span class=
"identifier">context
</span><span class=
"special">(
</span><span class=
"identifier">env
</span><span class=
"special">,
</span> <span class=
"identifier">actions
</span><span class=
"special">)
</span></code></span></dt>
177 A regular function that creates a context
179 <dt><span class=
"term"><code class=
"computeroutput"><span class=
"identifier">env
</span><span class=
"special">(
</span><span class=
"identifier">ctx
</span><span class=
"special">)
</span></code></span></dt>
181 A regular function that returns the environment from the given context
183 <dt><span class=
"term"><code class=
"computeroutput"><span class=
"identifier">actions
</span><span class=
"special">(
</span><span class=
"identifier">ctx
</span><span class=
"special">)
</span></code></span></dt>
185 A regular function that returns the actions from the given context
190 Phoenix is equipped with a predefined set of expressions, rules and actions
191 to make all the stuff work you learned in the
<a class=
"link" href=
"../starter_kit.html" title=
"Starter Kit">Starter
192 Kit
</a> and
<a class=
"link" href=
"../modules.html" title=
"Modules">Modules
</a> sections. See
193 the
<a class=
"link" href=
"rules.html" title=
"Predefined Expressions and Rules">next section
</a> for more details!
196 <table xmlns:
rev=
"http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width=
"100%"><tr>
197 <td align=
"left"></td>
198 <td align=
"right"><div class=
"copyright-footer">Copyright
© 2002-
2005,
2010,
2014,
2015 Joel de Guzman, Dan Marsden, Thomas
199 Heller, John Fletcher
<p>
200 Distributed under the Boost Software License, Version
1.0. (See accompanying
201 file LICENSE_1_0.txt or copy at
<a href=
"http://www.boost.org/LICENSE_1_0.txt" target=
"_top">http://www.boost.org/LICENSE_1_0.txt
</a>)
206 <div class=
"spirit-nav">
207 <a accesskey=
"p" href=
"expression/boilerplate_macros.html"><img src=
"../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../inside.html"><img src=
"../../../../../../doc/src/images/up.png" alt=
"Up"></a><a accesskey=
"h" href=
"../../index.html"><img src=
"../../../../../../doc/src/images/home.png" alt=
"Home"></a><a accesskey=
"n" href=
"rules.html"><img src=
"../../../../../../doc/src/images/next.png" alt=
"Next"></a>