when.xml 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3. Copyright 2012 Eric Niebler
  4. Distributed under the Boost
  5. 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. -->
  8. <header name="boost/proto/transform/when.hpp">
  9. <para>
  10. Definition of the
  11. <computeroutput>
  12. <classname alt="boost::proto::when">proto::when&lt;&gt;</classname>
  13. </computeroutput> and
  14. <computeroutput>
  15. <classname alt="boost::proto::otherwise">proto::otherwise&lt;&gt;</classname>
  16. </computeroutput> transforms.
  17. </para>
  18. <namespace name="boost">
  19. <namespace name="proto">
  20. <!-- struct transforms_type -->
  21. <struct name="transforms_type">
  22. <purpose>
  23. The type used to define the global <code><globalname>proto::transforms</globalname></code>,
  24. a key for use when creating and accessing a slot in a transform environment for
  25. a set of external transforms.
  26. </purpose>
  27. <description>
  28. <para>
  29. The <code>proto::transforms_type</code> type, along with the <code><globalname>proto::transforms</globalname></code>
  30. global, are declared using the <code><macroname>BOOST_PROTO_DEFINE_ENV_VAR</macroname>()</code> macro.
  31. </para>
  32. </description>
  33. <method-group name="public member functions">
  34. <overloaded-method name="operator=">
  35. <signature cv="const">
  36. <template>
  37. <template-type-parameter name="Value"/>
  38. </template>
  39. <type><classname>env</classname>&lt;transforms_type, <replaceable>see-below</replaceable>&gt;</type>
  40. <parameter name="value">
  41. <paramtype>Value &amp;</paramtype>
  42. </parameter>
  43. </signature>
  44. <signature cv="const">
  45. <template>
  46. <template-type-parameter name="Value"/>
  47. </template>
  48. <type><classname>env</classname>&lt;transforms_type, <replaceable>see-below</replaceable>&gt;</type>
  49. <parameter name="value">
  50. <paramtype>Value const &amp;</paramtype>
  51. </parameter>
  52. </signature>
  53. <description>
  54. <para>
  55. If <code>Value</code> is a specialization <code>boost::reference_wrapper&lt;T&gt;</code>,
  56. this function returns <code><classname>env</classname>&lt;transforms_type, T &amp;&gt;(value.get())</code>.
  57. </para>
  58. <para>
  59. Else, if the type <code>Value</code> is non-copyable (i.e., a function, an array, abstract, or an ostream),
  60. this function returns <code><classname>env</classname>&lt;transforms_type, Value <replaceable>cv</replaceable> &amp;&gt;(value)</code>,
  61. where <code><replaceable>cv</replaceable></code> is <code>const</code> for the second overload, and empty
  62. for the first.
  63. </para>
  64. <para>
  65. Otherwise, this function returns <code><classname>env</classname>&lt;transforms_type, Value&gt;(value)</code>.
  66. </para>
  67. </description>
  68. </overloaded-method>
  69. </method-group>
  70. </struct>
  71. <data-member name="transforms">
  72. <description>
  73. <para>
  74. A key key for use when creating and accessing a slot in a transform environment for
  75. a set of external transforms.
  76. </para>
  77. </description>
  78. <type><classname>proto::transforms_type</classname> const</type>
  79. </data-member>
  80. <struct name="when">
  81. <template>
  82. <template-type-parameter name="Grammar"/>
  83. <template-type-parameter name="PrimitiveTransform">
  84. <default>Grammar</default>
  85. </template-type-parameter>
  86. </template>
  87. <purpose>A grammar element and a <conceptname>PrimitiveTransform</conceptname> that associates
  88. a transform with the grammar.</purpose>
  89. <description>
  90. <para>
  91. Use <computeroutput>proto::when&lt;&gt;</computeroutput> to override a grammar's default
  92. transform with a custom transform. It is for used when composing larger transforms by
  93. associating smaller transforms with individual rules in your grammar, as in the following
  94. transform which counts the number of terminals in an expression.
  95. <programlisting>// Count the terminals in an expression tree.
  96. // Must be invoked with initial state == mpl::int_&lt;0&gt;().
  97. struct CountLeaves :
  98. <classname>proto::or_</classname>&lt;
  99. proto::when&lt;<classname>proto::terminal</classname>&lt;<classname>proto::_</classname>&gt;, mpl::next&lt;<classname>proto::_state</classname>&gt;()&gt;,
  100. proto::otherwise&lt;<classname>proto::fold</classname>&lt;<classname>proto::_</classname>, <classname>proto::_state</classname>, CountLeaves&gt; &gt;
  101. &gt;
  102. {};</programlisting>
  103. </para>
  104. <para>
  105. In <computeroutput>proto::when&lt;G, T&gt;</computeroutput>, when <computeroutput>T</computeroutput>
  106. is a class type it is a <conceptname>PrimitiveTransform</conceptname> and the following equivalencies hold:
  107. </para>
  108. <itemizedlist>
  109. <listitem>
  110. <para>
  111. <computeroutput>boost::result_of&lt;proto::when&lt;G,T&gt;(E,S,V)&gt;::type</computeroutput> is the same as
  112. <computeroutput>boost::result_of&lt;T(E,S,V)&gt;::type</computeroutput>.
  113. </para>
  114. </listitem>
  115. <listitem>
  116. <para>
  117. <computeroutput>proto::when&lt;G,T&gt;()(e,s,d)</computeroutput> is the same as
  118. <computeroutput>T()(e,s,d)</computeroutput>.
  119. </para>
  120. </listitem>
  121. </itemizedlist>
  122. </description>
  123. <inherit><type>PrimitiveTransform</type></inherit>
  124. <typedef name="proto_grammar">
  125. <type>typename Grammar::proto_grammar</type>
  126. </typedef>
  127. </struct>
  128. <struct-specialization name="when">
  129. <template>
  130. <template-type-parameter name="Grammar"/>
  131. <template-type-parameter name="Fun"/>
  132. </template>
  133. <specialization>
  134. <template-arg>Grammar</template-arg>
  135. <template-arg>Fun *</template-arg>
  136. </specialization>
  137. <inherit><type><classname>proto::when</classname>&lt; Grammar, Fun &gt;</type></inherit>
  138. <purpose>A specialization that treats function pointer <conceptname>Transform</conceptname>s as if they
  139. were function type <conceptname>Transform</conceptname>s.</purpose>
  140. <description>
  141. <para>
  142. This specialization requires that <computeroutput>Fun</computeroutput> is actually a function type.
  143. </para>
  144. <para>
  145. This specialization is required for nested transforms such as
  146. <computeroutput>proto::when&lt;G, T0(T1(_))&gt;</computeroutput>. In C++, functions that are used
  147. as parameters to other functions automatically decay to funtion pointer types. In other words, the
  148. type <computeroutput>T0(T1(_))</computeroutput> is indistinguishable from
  149. <computeroutput>T0(T1(*)(_))</computeroutput>. This specialization is required to handle these
  150. nested function pointer type transforms properly.
  151. </para>
  152. </description>
  153. </struct-specialization>
  154. <struct-specialization name="when">
  155. <template>
  156. <template-type-parameter name="Grammar"/>
  157. <template-type-parameter name="R"/>
  158. <template-type-parameter name="A" pack="1"/>
  159. </template>
  160. <specialization>
  161. <template-arg>Grammar</template-arg>
  162. <template-arg>R(A...)</template-arg>
  163. </specialization>
  164. <inherit><type><classname>proto::transform</classname>&lt; when&lt;Grammar, R(A...)&gt; &gt;</type></inherit>
  165. <purpose>A grammar element and a <conceptname>Transform</conceptname> that associates a
  166. transform with the grammar. </purpose>
  167. <description>
  168. <para>
  169. Use <computeroutput>proto::when&lt;&gt;</computeroutput> to override a grammar's default
  170. transform with a custom transform. It is for use when composing larger transforms by associating
  171. smaller transforms with individual rules in your grammar.
  172. </para>
  173. <para>
  174. The <computeroutput>when&lt;G, R(A...)&gt;</computeroutput> form accepts either a
  175. <conceptname>CallableTransform</conceptname> or an <conceptname>ObjectTransform</conceptname> as its
  176. second parameter. <computeroutput>proto::when&lt;&gt;</computeroutput> uses
  177. <computeroutput><classname>proto::is_callable</classname>&lt;R&gt;::value</computeroutput> to
  178. distinguish between the two, and uses
  179. <computeroutput><classname>proto::call&lt;&gt;</classname></computeroutput> to evaluate
  180. <conceptname>CallableTransform</conceptname>s and
  181. <computeroutput><classname>proto::make&lt;&gt;</classname></computeroutput> to evaluate
  182. <conceptname>ObjectTransform</conceptname>s.
  183. </para>
  184. </description>
  185. <struct name="impl">
  186. <template>
  187. <template-type-parameter name="Expr"/>
  188. <template-type-parameter name="State"/>
  189. <template-type-parameter name="Data"/>
  190. </template>
  191. <inherit><type><classname>proto::transform_impl</classname>&lt; Expr, State, Data &gt;</type></inherit>
  192. <typedef name="call_">
  193. <purpose>For exposition only</purpose>
  194. <type><classname>proto::call</classname>&lt;R(A...)&gt;</type>
  195. </typedef>
  196. <typedef name="make_">
  197. <purpose>For exposition only</purpose>
  198. <type><classname>proto::make</classname>&lt;R(A...)&gt;</type>
  199. </typedef>
  200. <typedef name="which">
  201. <purpose>For exposition only</purpose>
  202. <type>typename mpl::if_&lt;<classname>proto::is_callable</classname>&lt;R&gt;,call_,make_&gt;::type</type>
  203. </typedef>
  204. <typedef name="result_type">
  205. <type>typename boost::result_of&lt;which(Expr, State, Data)&gt;::type</type>
  206. </typedef>
  207. <method-group name="public member functions">
  208. <method name="operator()" cv="const">
  209. <type>result_type</type>
  210. <parameter name="expr">
  211. <paramtype>typename impl::expr_param</paramtype>
  212. <description>
  213. <para>The current expression </para>
  214. </description>
  215. </parameter>
  216. <parameter name="state">
  217. <paramtype>typename impl::state_param</paramtype>
  218. <description>
  219. <para>The current state </para>
  220. </description>
  221. </parameter>
  222. <parameter name="data">
  223. <paramtype>typename impl::data_param</paramtype>
  224. <description>
  225. <para>An arbitrary data </para>
  226. </description>
  227. </parameter>
  228. <description>
  229. <para>
  230. Evaluate <computeroutput>R(A...)</computeroutput> as a transform either with
  231. <computeroutput><classname>proto::call&lt;&gt;</classname></computeroutput> or with
  232. <computeroutput><classname>proto::make&lt;&gt;</classname></computeroutput> depending
  233. on whether <computeroutput><classname>proto::is_callable</classname>&lt;R&gt;::value</computeroutput>
  234. is <computeroutput>true</computeroutput> or <computeroutput>false</computeroutput>.
  235. </para>
  236. </description>
  237. <requires>
  238. <para>
  239. <computeroutput><classname>proto::matches</classname>&lt;Expr, Grammar&gt;::value</computeroutput>
  240. is <computeroutput>true</computeroutput>.
  241. </para>
  242. </requires>
  243. <returns>
  244. <para>
  245. <computeroutput>which()(expr, state, data)</computeroutput>
  246. </para>
  247. </returns>
  248. </method>
  249. </method-group>
  250. </struct>
  251. <typedef name="proto_grammar">
  252. <type>typename Grammar::proto_grammar</type>
  253. </typedef>
  254. </struct-specialization>
  255. <struct-specialization name="when">
  256. <template>
  257. <template-type-parameter name="Grammar"/>
  258. <template-type-parameter name="R"/>
  259. <template-type-parameter name="A" pack="1"/>
  260. </template>
  261. <specialization>
  262. <template-arg>Grammar</template-arg>
  263. <template-arg>R(A..., ...)</template-arg>
  264. </specialization>
  265. <inherit><type><classname>proto::transform</classname>&lt; when&lt;Grammar, R(A..., ...)&gt; &gt;</type></inherit>
  266. <purpose>A grammar element and a <conceptname>Transform</conceptname> that associates a
  267. transform with the grammar. </purpose>
  268. <description>
  269. <para>
  270. Use <computeroutput>proto::when&lt;&gt;</computeroutput> to override a grammar's default
  271. transform with a custom transform. It is for use when composing larger transforms by associating
  272. smaller transforms with individual rules in your grammar.
  273. </para>
  274. <para>
  275. The <computeroutput>when&lt;G, R(A..., ...)&gt;</computeroutput> form accepts either a
  276. <conceptname>CallableTransform</conceptname> or an <conceptname>ObjectTransform</conceptname> as its
  277. second parameter. <computeroutput>proto::when&lt;&gt;</computeroutput> uses
  278. <computeroutput><classname>proto::is_callable</classname>&lt;R&gt;::value</computeroutput> to
  279. distinguish between the two, and uses
  280. <computeroutput><classname>proto::call&lt;&gt;</classname></computeroutput> to evaluate
  281. <conceptname>CallableTransform</conceptname>s and
  282. <computeroutput><classname>proto::make&lt;&gt;</classname></computeroutput> to evaluate
  283. <conceptname>ObjectTransform</conceptname>s.
  284. </para>
  285. <para>
  286. <emphasis role="bold">Note:</emphasis> In the specialization
  287. <computeroutput>when&lt;G, R(A..., ...)&gt;</computeroutput>, the first ellipsis denotes a
  288. C++11-style variadic template (which is emulated for C++98 compilers). The second ellipsis
  289. is a C-style vararg.
  290. </para>
  291. </description>
  292. <struct name="impl">
  293. <template>
  294. <template-type-parameter name="Expr"/>
  295. <template-type-parameter name="State"/>
  296. <template-type-parameter name="Data"/>
  297. </template>
  298. <inherit><type><classname>proto::transform_impl</classname>&lt; Expr, State, Data &gt;</type></inherit>
  299. <typedef name="call_">
  300. <purpose>For exposition only</purpose>
  301. <type><classname>proto::call</classname>&lt;R(A..., ...)&gt;</type>
  302. </typedef>
  303. <typedef name="make_">
  304. <purpose>For exposition only</purpose>
  305. <type><classname>proto::make</classname>&lt;R(A..., ...)&gt;</type>
  306. </typedef>
  307. <typedef name="which">
  308. <purpose>For exposition only</purpose>
  309. <type>typename mpl::if_&lt;<classname>proto::is_callable</classname>&lt;R&gt;,call_,make_&gt;::type</type>
  310. </typedef>
  311. <typedef name="result_type">
  312. <type>typename boost::result_of&lt;which(Expr, State, Data)&gt;::type</type>
  313. </typedef>
  314. <method-group name="public member functions">
  315. <method name="operator()" cv="const">
  316. <type>result_type</type>
  317. <parameter name="expr">
  318. <paramtype>typename impl::expr_param</paramtype>
  319. <description>
  320. <para>The current expression </para>
  321. </description>
  322. </parameter>
  323. <parameter name="state">
  324. <paramtype>typename impl::state_param</paramtype>
  325. <description>
  326. <para>The current state </para>
  327. </description>
  328. </parameter>
  329. <parameter name="data">
  330. <paramtype>typename impl::data_param</paramtype>
  331. <description>
  332. <para>An arbitrary data </para>
  333. </description>
  334. </parameter>
  335. <description>
  336. <para>
  337. Evaluate <computeroutput>R(A..., ...)</computeroutput> as a transform either with
  338. <computeroutput><classname>proto::call&lt;&gt;</classname></computeroutput> or with
  339. <computeroutput><classname>proto::make&lt;&gt;</classname></computeroutput> depending
  340. on whether <computeroutput><classname>proto::is_callable</classname>&lt;R&gt;::value</computeroutput>
  341. is <computeroutput>true</computeroutput> or <computeroutput>false</computeroutput>.
  342. </para>
  343. </description>
  344. <requires>
  345. <para>
  346. <computeroutput><classname>proto::matches</classname>&lt;Expr, Grammar&gt;::value</computeroutput>
  347. is <computeroutput>true</computeroutput>.
  348. </para>
  349. </requires>
  350. <returns>
  351. <para>
  352. <computeroutput>which()(expr, state, data)</computeroutput>
  353. </para>
  354. </returns>
  355. </method>
  356. </method-group>
  357. </struct>
  358. <typedef name="proto_grammar">
  359. <type>typename Grammar::proto_grammar</type>
  360. </typedef>
  361. </struct-specialization>
  362. <struct-specialization name="when">
  363. <template>
  364. <template-type-parameter name="Grammar"/>
  365. </template>
  366. <specialization>
  367. <template-arg>Grammar</template-arg>
  368. <template-arg><classname>proto::external_transform</classname></template-arg>
  369. </specialization>
  370. <inherit><type>
  371. <classname>proto::transform</classname>&lt; when&lt;Grammar, <classname>proto::external_transform</classname>&gt; &gt;</type></inherit>
  372. <purpose>A grammar element that associates an externally-specified transform with the grammar.
  373. The transform is looked up in the Data parameter using the Grammar as a key.</purpose>
  374. <description>
  375. <para>
  376. Use <computeroutput>proto::when&lt;&gt;</computeroutput> to override a grammar's default
  377. transform with a custom transform. It is for use when composing larger transforms by associating
  378. smaller transforms with individual rules in your grammar.
  379. </para>
  380. <para>
  381. The <computeroutput>when&lt;G, <classname>proto::external_transform</classname>&gt;</computeroutput>
  382. indicates that the associated transform is not yet known. It should be looked up when the transform
  383. is about to be applied. It is found by looking it up in the passed-in Data parameter, which
  384. behaves like a compile-time map from grammar types to transform types. The map is indexed using
  385. <computeroutput>Grammar</computeroutput> as a key. The associated value type is used as the transform
  386. to apply. In this way, the same grammar can be used to define multiple evaluating strategies that
  387. can be added post-hoc.
  388. </para>
  389. <para>
  390. See <computeroutput><classname>proto::external_transforms</classname></computeroutput> for an example.
  391. </para>
  392. </description>
  393. <struct name="impl">
  394. <template>
  395. <template-type-parameter name="Expr"/>
  396. <template-type-parameter name="State"/>
  397. <template-type-parameter name="Data"/>
  398. </template>
  399. <inherit><type>
  400. boost::remove_reference&lt;
  401. typename mpl::eval_if_c&lt;
  402. <classname>proto::result_of::has_env_var</classname>&lt;Data, <classname>proto::transforms_type</classname>&gt;::value,
  403. <classname>proto::result_of::env_var</classname>&lt;Data, <classname>proto::transforms_type</classname>&gt;,
  404. <classname>proto::result_of::env_var</classname>&lt;Data, <classname>proto::data_type</classname>&gt;
  405. &gt;::type
  406. &gt;::type
  407. ::template when&lt; Grammar &gt;
  408. ::template impl&lt; Expr, State, Data &gt;</type></inherit>
  409. <description>
  410. <para>
  411. The implementation of the <code>impl</code> struct depends on whether the <code>Data</code>
  412. parameter is a transform environment that contains a value corresponding to the
  413. <classname>proto::transforms_type</classname> key. If so, that value is treated as a
  414. map from rules to transforms. Otherwise, the <code>Data</code> type itself is treated
  415. as such a map.
  416. </para>
  417. </description>
  418. </struct>
  419. <typedef name="proto_grammar">
  420. <type>typename Grammar::proto_grammar</type>
  421. </typedef>
  422. </struct-specialization>
  423. <struct name="otherwise">
  424. <template>
  425. <template-type-parameter name="Fun"/>
  426. </template>
  427. <inherit><type><classname>proto::when</classname>&lt; <classname>proto::_</classname>, Fun &gt;</type></inherit>
  428. <purpose>
  429. Syntactic sugar for <computeroutput><classname>proto::when</classname>&lt; <classname>proto::_</classname>, Fun &gt;</computeroutput>,
  430. for use in grammars to handle all the cases not yet handled.
  431. </purpose>
  432. <description>
  433. <para>
  434. Use <computeroutput>proto::otherwise&lt;T&gt;</computeroutput> in your grammars as a synonym for
  435. <computeroutput><classname>proto::when</classname>&lt; <classname>proto::_</classname>, Fun &gt;</computeroutput>
  436. as in the following transform which counts the number of terminals in an expression.
  437. </para>
  438. <para>
  439. <programlisting>// Count the terminals in an expression tree.
  440. // Must be invoked with initial state == mpl::int_&lt;0&gt;().
  441. struct CountLeaves :
  442. <classname>proto::or_</classname>&lt;
  443. proto::when&lt;<classname>proto::terminal</classname>&lt;<classname>proto::_</classname>&gt;, mpl::next&lt;<classname>proto::_state</classname>&gt;()&gt;,
  444. proto::otherwise&lt;<classname>proto::fold</classname>&lt;<classname>proto::_</classname>, <classname>proto::_state</classname>, CountLeaves&gt; &gt;
  445. &gt;
  446. {};</programlisting>
  447. </para>
  448. </description>
  449. </struct>
  450. <struct name="external_transform">
  451. <purpose>A placeholder for use as the second parameter for <computeroutput><classname>proto::when</classname></computeroutput>
  452. to indicate that the rule's transform is specified externally.</purpose>
  453. <description>
  454. <para>
  455. See <computeroutput><classname>proto::external_transforms</classname></computeroutput> for an example.
  456. </para>
  457. </description>
  458. </struct>
  459. <struct name="external_transforms">
  460. <template>
  461. <template-type-parameter name="When" pack="1"/>
  462. </template>
  463. <purpose>A map from grammars to transforms, used as a way to externally associate transforms.</purpose>
  464. <typedef name="map_type">
  465. <purpose>For exposition only.</purpose>
  466. <type>mpl::map&lt; typename to_mpl_pair&lt; When &gt;::type... &gt;</type>
  467. </typedef>
  468. <struct name="when">
  469. <template>
  470. <template-type-parameter name="Grammar"/>
  471. </template>
  472. <inherit><type><classname>proto::otherwise</classname>&lt; typename mpl::at&lt; map_type, Grammar &gt;::type &gt;</type></inherit>
  473. </struct>
  474. <description>
  475. <para>
  476. It is sometimes desirable to define a grammar that can be customized with different sets of transforms.
  477. To do that, where you would normally specify a transform within a grammar, you can instead put
  478. <computeroutput><classname>proto::external_transform</classname></computeroutput>; for example:
  479. <computeroutput>proto::when&lt; some_grammar, proto::external_transform &gt;</computeroutput>. Then, when
  480. invoking the grammar, you can pass an approriately-defined instance of <computeroutput>proto::external_transforms</computeroutput>
  481. as the Data parameter. When an expression matches <computeroutput>some_grammar</computeroutput>, Proto
  482. will look up the approprite transform in the Data parameter using <computeroutput>some_grammar</computeroutput>
  483. as a key.
  484. </para>
  485. <para>
  486. <programlisting>struct int_terminal
  487. : <classname>proto::terminal</classname>&lt;int&gt;
  488. {};
  489. struct char_terminal
  490. : <classname>proto::terminal</classname>&lt;char&gt;
  491. {};
  492. struct my_grammar
  493. : <classname>proto::or_</classname>&lt;
  494. // The next two grammar rules are customization points.
  495. // The associated transforms are specified externally
  496. // using external_transforms below.
  497. <classname>proto::when</classname>&lt; int_terminal, <classname>proto::external_transform</classname> &gt;
  498. , <classname>proto::when</classname>&lt; char_terminal, <classname>proto::external_transform</classname> &gt;
  499. , <classname>proto::when</classname>&lt;
  500. <classname>proto::plus</classname>&lt; my_grammar, my_grammar &gt;
  501. , <classname>proto::fold</classname>&lt; <classname>proto::_</classname>, int(), my_grammar &gt;
  502. &gt;
  503. &gt;
  504. {};
  505. // Here is where the transforms are associated with the
  506. // grammar rules above.
  507. struct my_transforms
  508. : proto::external_transforms&lt;
  509. <classname>proto::when</classname>&lt;int_terminal, print(<classname>proto::_value</classname>)&gt;
  510. , <classname>proto::when</classname>&lt;char_terminal, print(<classname>proto::_value</classname>)&gt;
  511. &gt;
  512. {};
  513. // ...
  514. <classname>proto::literal</classname>&lt;int&gt; i(1);
  515. <classname>proto::literal</classname>&lt;char&gt; c('a');
  516. my_transforms trx;
  517. // Evaluate "i+c" using my_grammar with the specified transforms:
  518. my_grammar()(i + c, 0, trx);
  519. // If you would also like to pass arbitrary data along with the
  520. // transforms, you can use a transform environment, as so:
  521. my_grammar()(i + c, 0, (proto::data = 42, proto::transforms = trx));</programlisting>
  522. </para>
  523. </description>
  524. </struct>
  525. </namespace>
  526. </namespace>
  527. </header>