]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | <html> |
2 | <head> | |
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"> | |
11 | </head> | |
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> | |
20 | </tr></table> | |
21 | <hr> | |
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> | |
24 | </div> | |
25 | <div class="section"> | |
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> | |
29 | <p> | |
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. | |
32 | </p> | |
33 | <p> | |
34 | When dealing with a Phoenix expression tree, it gets evaluated top-down. | |
35 | Example: | |
36 | </p> | |
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> | |
38 | </pre> | |
39 | <p> | |
40 | Can be visualized as an AST in the following way: | |
41 | </p> | |
42 | <p> | |
43 | <span class="inlinemediaobject"><img src="../../images/simple_ast.png"></span> | |
44 | </p> | |
45 | <p> | |
46 | In terms of actions this means: | |
47 | </p> | |
48 | <div class="itemizedlist"><ul class="itemizedlist" type="disc"> | |
49 | <li class="listitem"> | |
50 | <code class="computeroutput"><span class="identifier">rule</span><span class="special">::</span><span class="identifier">plus</span></code> is matched | |
51 | </li> | |
52 | <li class="listitem"> | |
53 | evaluate left: | |
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 | |
56 | </li></ul></div> | |
57 | </li> | |
58 | <li class="listitem"> | |
59 | evaluate right: | |
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"> | |
63 | <li class="listitem"> | |
64 | evaluate left: | |
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> | |
67 | is matched | |
68 | </li></ul></div> | |
69 | </li> | |
70 | <li class="listitem"> | |
71 | evaluate right: | |
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> | |
74 | is matched | |
75 | </li></ul></div> | |
76 | </li> | |
77 | </ul></div> | |
78 | </li></ul></div> | |
79 | </li> | |
80 | </ul></div> | |
81 | <p> | |
82 | Every time a rule is matched, an action will be called. The action determines | |
83 | how the Phoenix AST will be traversed. | |
84 | </p> | |
85 | <h5> | |
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 | |
88 | an Action</a> | |
89 | </h5> | |
90 | <p> | |
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 | |
94 | to this: | |
95 | </p> | |
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> | |
98 | </pre> | |
99 | <p> | |
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. | |
104 | </p> | |
105 | <p> | |
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>: | |
108 | </p> | |
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> | |
112 | ||
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> | |
119 | ||
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> | |
124 | </pre> | |
125 | <p> | |
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. | |
130 | </p> | |
131 | <p> | |
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>. | |
139 | </p> | |
140 | <div class="variablelist"> | |
141 | <p class="title"><b></b></p> | |
142 | <dl> | |
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> | |
144 | <dd><p> | |
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> | |
147 | parameter | |
148 | </p></dd> | |
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> | |
150 | <dd><p> | |
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 | |
154 | </p></dd> | |
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> | |
156 | <dd><p> | |
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 | |
159 | </p></dd> | |
160 | <dt><span class="term"><code class="computeroutput"><span class="identifier">_context</span></code></span></dt> | |
161 | <dd><p> | |
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 | |
164 | </p></dd> | |
165 | <dt><span class="term"><code class="computeroutput"><span class="identifier">_env</span></code></span></dt> | |
166 | <dd><p> | |
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 | |
169 | </p></dd> | |
170 | <dt><span class="term"><code class="computeroutput"><span class="identifier">_actions</span></code></span></dt> | |
171 | <dd><p> | |
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 | |
174 | </p></dd> | |
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> | |
176 | <dd><p> | |
177 | A regular function that creates a context | |
178 | </p></dd> | |
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> | |
180 | <dd><p> | |
181 | A regular function that returns the environment from the given context | |
182 | </p></dd> | |
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> | |
184 | <dd><p> | |
185 | A regular function that returns the actions from the given context | |
186 | </p></dd> | |
187 | </dl> | |
188 | </div> | |
189 | <p> | |
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! | |
194 | </p> | |
195 | </div> | |
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>) | |
202 | </p> | |
203 | </div></td> | |
204 | </tr></table> | |
205 | <hr> | |
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> | |
208 | </div> | |
209 | </body> | |
210 | </html> |