expression.qbk 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. [/==============================================================================
  2. Copyright (C) 2001-2010 Joel de Guzman
  3. Copyright (C) 2001-2005 Dan Marsden
  4. Copyright (C) 2001-2010 Thomas Heller
  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. ===============================================================================/]
  8. [section:expression Phoenix Expressions]
  9. A Phoenix Expression is a model of the __proto_expr__ Concept. These expressions
  10. are wrapped inside an [link phoenix.inside.actor Actor] template. The `actor` provides
  11. the function call operator which evaluates the expressions.
  12. The `actor` is the domain specific wrapper around Phoenix expressions.
  13. By design, Phoenix Expressions do not carry any information on how they will be
  14. evaluated later on. They are the data structure on which the `Actions` will work.
  15. The library provides a convenience template to define expressions:
  16. template <template <typename> Actor, typename Tag, typename A0, ..., typename A1>
  17. struct expr_ext
  18. : proto::transform<expr_ext<Actor, Tag, A0, ..., A1> >
  19. {
  20. typedef __unspecified__ base_expr;
  21. typedef Actor<base_expr> type;
  22. typedef __unspecified__ proto_grammar;
  23. static type make(A0 a0, ..., A1 a1);
  24. };
  25. template <typename Tag, typename A0, ..., typename A1>
  26. struct expr : expr_ext<actor, Tag, A0, ..., A1> {};
  27. [*Notation]
  28. [variablelist
  29. [[`A0...AN`] [Child node types]]
  30. [[`a0...aN`] [Child node objects]]
  31. [[`G0...GN`] [__proto__ grammar types]]
  32. ]
  33. [*Expression Semantics]
  34. [table
  35. [[Expression] [Semantics]]
  36. [[`expr<Tag, A0...AN>::type`] [The type of Expression having tag `Tag` and `A0...AN` children]]
  37. [[`expr<Tag, G0...GN>`] [A __proto__ grammar and __proto_pass_through_transform__]]
  38. [[`expr<Tag, A0...AN>::make(a0...aN)`] [Returns a Phoenix Expression]]
  39. ]
  40. [note
  41. You might have noticed the template template argument `Actor` used in
  42. `expr_ext`. This can be a user supplied custom Actor adding other member
  43. functions or objects than the default `actor` template. See
  44. [link phoenix.examples.extending_actors Extending Actors] for more details.
  45. ]
  46. [heading meta_grammar]
  47. Defining expressions is only part of the game to make it a valid Phoenix Expression.
  48. In order to use the expressions in the Phoenix domain, we need to "register" them
  49. to our grammar.
  50. The `meta_grammar` is a struct for exactly that purpose. It is an openly extendable __proto__ Grammar:
  51. struct meta_grammar
  52. : proto::switch_<meta_grammar>
  53. {
  54. template <typename Tag, typename Dummy>
  55. struct case_
  56. : proto::not_<proto::_>
  57. {};
  58. };
  59. As you can see, by default the `meta_grammar` matches nothing. With every [link phoenix.modules Module]
  60. you include this grammar gets extended by various expressions.
  61. [heading Example]
  62. Define an expression:
  63. template <typename Lhs, typename Rhs>
  64. struct plus
  65. : expr<proto::tag::plus, Lhs, Rhs>
  66. {};
  67. And add it to the grammar:
  68. template <>
  69. struct meta_grammar::case_<proto::tag::plus>
  70. : enable_rule<
  71. plus<
  72. meta_grammar
  73. , meta_grammar
  74. >
  75. >
  76. {};
  77. Define a generator function to make the life of our potential users easier:
  78. template <typename Lhs, typename Rhs>
  79. typename plus<Lhs, Rhs>::type
  80. plus(Lhs const & lhs, Rhs const & rhs)
  81. {
  82. return expression::plus<Lhs, Rhs>::make(lhs, rhs);
  83. }
  84. Look if it really works:
  85. plus(6, 5)();
  86. returns 11!
  87. proto::display_expr(plus(5, 6));
  88. prints:
  89. plus(
  90. terminal(6)
  91. , terminal(5)
  92. )
  93. See [@../../example/define_expression.cpp define_expression.cpp] for the full example.
  94. [note
  95. The example shown here only works because `default_actions` knows how to handle
  96. an expression having the `proto::tag::plus` and two children. This is because
  97. `default_actions` uses the `proto::_default<meta_grammar>` transform to
  98. evaluate operators and functions. Learn more about actions
  99. [link phoenix.inside.actions here].
  100. ]
  101. [section Boilerplate Macros]
  102. When having more and more expressions, you start to realize that this is a very
  103. repetetive task. Phoenix provides boilerplate macros that make defining Phoenix
  104. Expressions as you have seen in the
  105. [link phoenix.inside.expression previous section] look like a piece of cake.
  106. [/
  107. These expressions generate the following:
  108. * A tag (in the underlying namespace tag)
  109. * An expression
  110. * an expression type in namespace expression
  111. ]
  112. [section BOOST_PHOENIX_DEFINE_EXPRESSION]
  113. [heading Description]
  114. `BOOST_PHOENIX_DEFINE_EXPRESSION` is a macro that can be used to generate all the
  115. necessary boilerplate to create Phoenix Expressions
  116. [heading Synopsis]
  117. BOOST_PHOENIX_DEFINE_EXPRESSION(
  118. (namespace_seq)(name)
  119. , (child_grammar0)
  120. (child_grammar1)
  121. ...
  122. )
  123. [heading Semantics]
  124. The above macro generates the necessary code for an expression `name` in
  125. namespace `namespace_seq`. The sequence of `(child_grammarN)` declares how many
  126. children the expression will have and what `proto::grammar` they match.
  127. The macro should be used at global scope. `namespace_seq` shall be the sequence
  128. of namespaces under which the following symbols will be defined:
  129. namespace tag
  130. {
  131. struct name;
  132. }
  133. namespace expression
  134. {
  135. template <typename A0, typename A1 ... typename AN>
  136. struct name
  137. : boost::phoenix::expr<
  138. tag::name
  139. , A0
  140. , A1
  141. ...
  142. , AN
  143. >
  144. }
  145. namespace rule
  146. {
  147. struct name
  148. : boost::phoenix::expr<
  149. child_grammar0
  150. , child_grammar1
  151. ...
  152. , child_grammarN
  153. >
  154. {};
  155. }
  156. namespace functional
  157. {
  158. struct make_name; // A polymorphic function object that can be called to create the expression node
  159. }
  160. namespace result_of
  161. {
  162. template <typename A0, typename A1 ... typename AN>
  163. struct make_name; // The result type of the expression node
  164. }
  165. // convenience polymorphic function to create an expression node
  166. template <typename A0, typename A1 ... typename AN>
  167. result_of::make_name<A0, A1 ... AN>
  168. make_name(A0 const & a0, A1 const & a1 ... AN const & an);
  169. This macros also adds a specialization for `meta_grammar::case_<tag::name>` to
  170. enable the rule for further use in actions.
  171. [heading Header]
  172. #include <boost/phoenix/core/expression.hpp>
  173. [heading Example]
  174. The example from the previous section can be rewritten as:
  175. BOOST_PHOENIX_DEFINE_EXPRESSION(
  176. (plus)
  177. , (meta_grammar) // Lhs
  178. (meta_grammar) // Rhs
  179. )
  180. template <typename Lhs, typename Rhs>
  181. typename plus<Lhs, Rhs>::type
  182. plus(Lhs const & lhs, Rhs const & rhs)
  183. {
  184. return expression::plus<Lhs, Rhs>::make(lhs, rhs);
  185. }
  186. [endsect]
  187. [section BOOST_PHOENIX_DEFINE_EXPRESSION_VARARG]
  188. [heading Description]
  189. `BOOST_PHOENIX_DEFINE_EXPRESSION_VARARG` is a macro that can be used to generate all the
  190. necessary boilerplate to create Phoenix Expressions
  191. [heading Synopsis]
  192. BOOST_PHOENIX_DEFINE_EXPRESSION_VARARG(
  193. (namespace_seq)(name)
  194. , (child_grammar0)
  195. (child_grammar1)
  196. ...
  197. (child_grammarN)
  198. , N
  199. )
  200. [heading Semantics]
  201. The above macro generates the necessary code for an expression `name` in
  202. namespace `namespace_seq`. `N` is the maximum number of variable children.
  203. All but the last elements in the grammar sequence are required children of
  204. the expression, and the last denotes a variable number of children. The number
  205. of children an expression of this kind can hold is therefor `N-1` plus the size of
  206. the sequence
  207. The macro should be used at global scope. `namespace_seq` shall be the sequence
  208. of namespaces under which the following symbols will be defined:
  209. namespace tag
  210. {
  211. struct name;
  212. }
  213. namespace expression
  214. {
  215. template <typename A0, typename A1 ... typename AN>
  216. struct name
  217. : boost::phoenix::expr<
  218. tag::name
  219. , A0
  220. , A1
  221. ...
  222. , AN
  223. >
  224. {};
  225. }
  226. namespace rule
  227. {
  228. struct name
  229. : expression::name<
  230. child_grammar0
  231. , child_grammar1
  232. ...
  233. , proto::vararg<child_grammarN>
  234. >
  235. {};
  236. }
  237. namespace functional
  238. {
  239. struct make_name; // A polymorphic function object that can be called to create the expression node
  240. }
  241. namespace result_of
  242. {
  243. template <typename A0, typename A1 ... typename AN>
  244. struct make_name; // The result type of the expression node
  245. }
  246. // convenience polymorphic function to create an expression node
  247. template <typename A0, typename A1 ... typename AN>
  248. result_of::make_name<A0, A1 ... AN>
  249. make_name(A0 const & a0, A1 const & a1 ... AN const & an);
  250. This macros also adds a specialization for `meta_grammar::case_<tag::name>` to
  251. enable the rule for further use in actions.
  252. [heading Header]
  253. #include <boost/phoenix/core/expression.hpp>
  254. [heading Example]
  255. BOOST_PHOENIX_DEFINE_EXPRESSION_VARARG(
  256. (boost)(phoenix)(mem_fun_ptr)
  257. , (meta_grammar) // Pointer to Object
  258. (meta_grammar) // Member pointer
  259. (meta_grammar) // Variable number of arguments
  260. , BOOST_PHOENIX_LIMIT
  261. )
  262. This defines the member function pointer operator expression as described in
  263. [link phoenix.modules.operator operators].
  264. [endsect]
  265. [section BOOST_PHOENIX_DEFINE_EXPRESSION_EXT]
  266. [heading Description]
  267. `BOOST_PHOENIX_DEFINE_EXPRESSION_EXT` is a macro that can be used to generate all the
  268. necessary boilerplate to create Phoenix Expressions
  269. [heading Synopsis]
  270. BOOST_PHOENIX_DEFINE_EXPRESSION_EXT(
  271. actor
  272. , (namespace_seq)(name)
  273. , (child_grammar0)
  274. (child_grammar1)
  275. ...
  276. (child_grammarN)
  277. , N
  278. )
  279. [heading Semantics]
  280. The above macro generates the necessary code for an expression `name` in
  281. namespace `namespace_seq`. The sequence of `(child_grammarN)` declares how many
  282. children the expression will have and what `proto::grammar` they match.
  283. The macro should be used at global scope. `namespace_seq` shall be the sequence
  284. of namespaces under which the following symbols will be defined:
  285. namespace tag
  286. {
  287. struct name;
  288. }
  289. namespace expression
  290. {
  291. template <typename A0, typename A1 ... typename AN>
  292. struct name
  293. : boost::phoenix::expr_ext<
  294. actor
  295. , tag::name
  296. , A0
  297. , A1
  298. ...
  299. , AN
  300. >
  301. }
  302. namespace rule
  303. {
  304. struct name
  305. : boost::phoenix::expr<
  306. child_grammar0
  307. , child_grammar1
  308. ...
  309. , child_grammarN
  310. >
  311. {};
  312. }
  313. namespace functional
  314. {
  315. struct make_name; // A polymorphic function object that can be called to create the expression node
  316. }
  317. namespace result_of
  318. {
  319. template <typename A0, typename A1 ... typename AN>
  320. struct make_name; // The result type of the expression node
  321. }
  322. // convenience polymorphic function to create an expression node
  323. template <typename A0, typename A1 ... typename AN>
  324. result_of::make_name<A0, A1 ... AN>
  325. make_name(A0 const & a0, A1 const & a1 ... AN const & an);
  326. This macros also adds a specialization for `meta_grammar::case_<tag::name>` to
  327. enable the rule for further use in actions.
  328. [heading Header]
  329. #include <boost/phoenix/core/expression.hpp>
  330. [heading Example]
  331. BOOST_PHOENIX_DEFINE_EXPRESSION_EXT(
  332. if_actor
  333. , (boost)(phoenix)(if_)
  334. , (meta_grammar) // Cond
  335. (meta_grammar) // Then
  336. )
  337. This defines the if_ expression. The custom actor defines `else_` as a member.
  338. [endsect]
  339. [section BOOST_PHOENIX_DEFINE_EXPRESSION_EXT_VARARG]
  340. [heading Description]
  341. `BOOST_PHOENIX_DEFINE_EXPRESSION_EXT_VARARG` is a macro that can be used to generate all the
  342. necessary boilerplate to create Phoenix Expressions
  343. [heading Synopsis]
  344. BOOST_PHOENIX_DEFINE_EXPRESSION_EXT_VARARG(
  345. actor
  346. , (namespace_seq)(name)
  347. , (child_grammar0)
  348. (child_grammar1)
  349. ...
  350. (child_grammarN)
  351. , N
  352. )
  353. [heading Semantics]
  354. The above macro generates the necessary code for an expression `name` in
  355. namespace `namespace_seq`. `N` is the maximum number of variable children.
  356. All but the last elements in the grammar sequence are required children of
  357. the expression, and the last denotes a variable number of children. The number
  358. of children an expression of this kind can hold is therefor `N-1` plus the size of
  359. the sequence
  360. The macro should be used at global scope. `namespace_seq` shall be the sequence
  361. of namespaces under which the following symbols will be defined:
  362. namespace tag
  363. {
  364. struct name;
  365. }
  366. namespace expression
  367. {
  368. template <typename A0, typename A1 ... typename AN>
  369. struct name
  370. : boost::phoenix::expr_ext<
  371. actor
  372. , tag::name
  373. , A0
  374. , A1
  375. ...
  376. , AN
  377. >
  378. {};
  379. }
  380. namespace rule
  381. {
  382. struct name
  383. : expression::name<
  384. child_grammar0
  385. , child_grammar1
  386. ...
  387. , proto::vararg<child_grammarN>
  388. >
  389. {};
  390. }
  391. namespace functional
  392. {
  393. struct make_name; // A polymorphic function object that can be called to create the expression node
  394. }
  395. namespace result_of
  396. {
  397. template <typename A0, typename A1 ... typename AN>
  398. struct make_name; // The result type of the expression node
  399. }
  400. // convenience polymorphic function to create an expression node
  401. template <typename A0, typename A1 ... typename AN>
  402. result_of::make_name<A0, A1 ... AN>
  403. make_name(A0 const & a0, A1 const & a1 ... AN const & an);
  404. This macros also adds a specialization for `meta_grammar::case_<tag::name>` to
  405. enable the rule for further use in actions.
  406. [heading Header]
  407. #include <boost/phoenix/core/expression.hpp>
  408. [heading Example]
  409. TBD
  410. [endsect]
  411. [endsect]
  412. [endsect]