auxiliary.qbk 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. [/==============================================================================
  2. Copyright (C) 2001-2011 Hartmut Kaiser
  3. Copyright (C) 2001-2011 Joel de Guzman
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ===============================================================================/]
  7. [section:auxiliary Auxiliary Generators]
  8. This module includes different auxiliary generators not fitting into any of the
  9. other categories. It includes the `attr_cast`, `eol`, `eps`, and `lazy`
  10. generators.
  11. [heading Module Header]
  12. // forwards to <boost/spirit/home/karma/auxiliary.hpp>
  13. #include <boost/spirit/include/karma_auxiliary.hpp>
  14. Also, see __include_structure__.
  15. [/////////////////////////////////////////////////////////////////////////////]
  16. [section:attr_cast Attribute Transformation Pseudo Generator (`attr_cast`)]
  17. [heading Description]
  18. The `attr_cast<Exposed, Transformed>()` component invokes the embedded generator
  19. while supplying an attribute of type `Transformed`. The supplied attribute gets created
  20. from the original attribute (of type `Exposed`) passed to this component using the
  21. customization point __customize_transform_attribute__.
  22. [heading Header]
  23. // forwards to <boost/spirit/home/karma/auxiliary/attr_cast.hpp>
  24. #include <boost/spirit/include/karma_attr_cast.hpp>
  25. Also, see __include_structure__.
  26. [heading Namespace]
  27. [table
  28. [[Name]]
  29. [[`boost::spirit::attr_cast // alias: boost::spirit::karma::attr_cast`]]
  30. ]
  31. [heading Synopsis]
  32. template <Exposed, Transformed>
  33. <unspecified> attr_cast(<unspecified>);
  34. [heading Template parameters]
  35. [table
  36. [[Parameter] [Description] [Default]]
  37. [[`Exposed`] [The type of the attribute supplied to the `attr_cast`.] [__unused_type__]]
  38. [[`Transformed`][The type of the attribute expected by the embedded
  39. generator `g`.] [__unused_type__]]
  40. ]
  41. The `attr_cast` is a function template. It is possible to invoke it using the
  42. following schemes:
  43. attr_cast(g)
  44. attr_cast<Exposed>(g)
  45. attr_cast<Exposed, Transformed>(g)
  46. depending on which of the attribute types can be deduced properly if not
  47. explicitly specified.
  48. [heading Model of]
  49. [:__unary_generator_concept__]
  50. [variablelist Notation
  51. [[`g`] [A generator object.]]
  52. ]
  53. [heading Expression Semantics]
  54. Semantics of an expression is defined only where it differs from, or is
  55. not defined in __unary_generator_concept__.
  56. [table
  57. [[Expression] [Semantics]]
  58. [[`attr_cast(g)`] [Create a component invoking the
  59. generator `g` while passing an attribute of the type
  60. as normally expected by `g`. The type of the supplied
  61. attribute will be transformed to the type
  62. `g` exposes as its attribute type (by using the
  63. attribute customization point __customize_transform_attribute__).
  64. This generator does not fail unless `g` fails.]]
  65. [[`attr_cast<Exposed>(g)`] [Create a component invoking the
  66. generator `g` while passing an attribute of the type
  67. as normally expected by `g`. The supplied attribute
  68. is expected to be of the type `Exposed`, it will be
  69. transformed to the type `g` exposes as its attribute type
  70. (using the attribute customization point
  71. __customize_transform_attribute__).
  72. This generator does not fail unless `g` fails.]]
  73. [[`attr_cast<Exposed, Transformed>(g)`] [Create a component invoking the
  74. generator `g` while passing an attribute of type
  75. `Transformed`. The supplied attribute is expected
  76. to be of the type `Exposed`, it will be transformed
  77. to the type `Transformed` (using the attribute
  78. customization point __customize_transform_attribute__).
  79. This generator does not fail unless `g` fails.]]
  80. ]
  81. [heading Attributes]
  82. [table
  83. [[Expression] [Attribute]]
  84. [[`attr_cast(g)`] [`g: A --> attr_cast(g): A`]]
  85. [[`attr_cast<Exposed>(g)`] [`g: A --> attr_cast<Exposed>(g): Exposed`]]
  86. [[`attr_cast<Exposed, Transformed>(g)`]
  87. [`g: A --> attr_cast<Exposed, Transformed>(g): Exposed`]]
  88. ]
  89. [heading Complexity]
  90. [:The complexity of this component is fully defined by the complexity of the
  91. embedded generator `g`.]
  92. [heading Example]
  93. [note The test harness for the example(s) below is presented in the
  94. __karma_basics_examples__ section.]
  95. Some includes:
  96. [reference_karma_includes]
  97. Some using declarations:
  98. [reference_karma_using_declarations_attr_cast]
  99. The example references data structure `int_data` which needs a specialization of
  100. the customization point __customize_transform_attribute__:
  101. [reference_karma_auxiliary_attr_cast_data1]
  102. Now we use the `attr_cast` pseudo generator to invoke the attribute
  103. transformation:
  104. [reference_karma_attr_cast1]
  105. [endsect]
  106. [/////////////////////////////////////////////////////////////////////////////]
  107. [section:eol End of Line Generator (`eol`)]
  108. [heading Description]
  109. The `eol` component generates a single newline character. It is equivalent
  110. to `lit('\n')` or simply '\\n' (please see the [karma_char `char_`] generator
  111. module for more details).
  112. [heading Header]
  113. // forwards to <boost/spirit/home/karma/auxiliary/eol.hpp>
  114. #include <boost/spirit/include/karma_eol.hpp>
  115. Also, see __include_structure__.
  116. [heading Namespace]
  117. [table
  118. [[Name]]
  119. [[`boost::spirit::eol // alias: boost::spirit::karma::eol`]]
  120. ]
  121. [heading Model of]
  122. [:__primitive_generator_concept__]
  123. [heading Expression Semantics]
  124. Semantics of an expression is defined only where it differs from, or is
  125. not defined in __primitive_generator_concept__.
  126. [table
  127. [[Expression] [Semantics]]
  128. [[`eol`] [Create a component generating a single end of line
  129. character in the output. This generator never fails
  130. (unless the underlying output stream reports an
  131. error).]]
  132. ]
  133. [heading Attributes]
  134. [table
  135. [[Expression] [Attribute]]
  136. [[`eol`] [__unused__]]
  137. ]
  138. [heading Complexity]
  139. [:O(1)]
  140. The complexity is constant as a single character is generated in the output.
  141. [heading Example]
  142. [note The test harness for the example(s) below is presented in the
  143. __karma_basics_examples__ section.]
  144. Some includes:
  145. [reference_karma_includes]
  146. Some using declarations:
  147. [reference_karma_using_declarations_eol]
  148. Basic usage of the `eol` generator:
  149. [reference_karma_eol]
  150. [endsect]
  151. [/////////////////////////////////////////////////////////////////////////////]
  152. [section:eps Epsilon Generator (`eps`)]
  153. The family of `eps` components allows to create pseudo generators generating
  154. an empty string. This feature is sometimes useful either to force a generator
  155. to fail or to succeed or to insert semantic actions into the generation process.
  156. [heading Description]
  157. The Epsilon (`eps`) is a multi-purpose generator that emits a zero length
  158. string.
  159. [heading Simple Form]
  160. In its simplest form, `eps` creates a component generating an empty string
  161. while always succeeding:
  162. eps // always emits a zero-length string
  163. This form is usually used to trigger a semantic action unconditionally.
  164. For example, it is useful in triggering error messages when a set of
  165. alternatives fail:
  166. r = a | b | c | eps[error()]; // Call error if a, b, and c fail to generate
  167. [heading Semantic Predicate]
  168. The `eps(b)` component generates an empty string as well, but
  169. succeeds only if `b` is `true` and fails otherwise. It's lazy variant `eps(fb)`
  170. is equivalent to `eps(b)` except it evaluates the supplied function `fb` at
  171. generate time, while using the return value as the criteria to succeed.
  172. Semantic predicates allow you to attach a conditional function anywhere
  173. in the grammar. In this role, the epsilon takes a __karma_lazy_argument__ that
  174. returns `true` or `false`. The __karma_lazy_argument__ is typically a test
  175. that is called to resolve ambiguity in the grammar. A generator failure will
  176. be reported when the __karma_lazy_argument__ result evaluates to `false`.
  177. Otherwise an empty string will be emitted. The general form is:
  178. eps_p(fb) << rest;
  179. The __karma_lazy_argument__ `fb` is called to do a semantic test. If the test
  180. returns true, `rest` will be evaluated. Otherwise, the production will return
  181. early without ever touching rest.
  182. [heading Header]
  183. // forwards to <boost/spirit/home/karma/auxiliary/eps.hpp>
  184. #include <boost/spirit/include/karma_eps.hpp>
  185. Also, see __include_structure__.
  186. [heading Namespace]
  187. [table
  188. [[Name]]
  189. [[`boost::spirit::eps // alias: boost::spirit::karma::eps`]]
  190. ]
  191. [heading Model of]
  192. [:__primitive_generator_concept__]
  193. [variablelist Notation
  194. [[`b`] [A boolean value.]]
  195. [[`fb`] [A __karma_lazy_argument__ that evaluates to a boolean value.]]
  196. ]
  197. [heading Expression Semantics]
  198. Semantics of an expression is defined only where it differs from, or is
  199. not defined in __primitive_generator_concept__.
  200. [table
  201. [[Expression] [Semantics]]
  202. [[`eps`] [Creates a component generating an empty string.
  203. Succeeds always.]]
  204. [[`eps(b)`] [Creates a component generating an empty string.
  205. Succeeds if `b` is `true` (unless the underlying
  206. output stream reports an error).]]
  207. [[`eps(fb)`] [Creates a component generating an empty string.
  208. Succeeds if `fb` returns `true` at generate time
  209. (unless the underlying output stream reports an
  210. error).]]
  211. ]
  212. [heading Attributes]
  213. [table
  214. [[Expression] [Attribute]]
  215. [[`eps`] [__unused__]]
  216. [[`eps(b)`] [__unused__]]
  217. [[`eps(fb)`] [__unused__]]
  218. ]
  219. [heading Complexity]
  220. [:O(1)]
  221. The complexity is constant as no output is generated.
  222. [heading Example]
  223. [note The test harness for the example(s) below is presented in the
  224. __karma_basics_examples__ section.]
  225. Some includes:
  226. [reference_karma_includes]
  227. Some using declarations:
  228. [reference_karma_using_declarations_eps]
  229. Basic usage of the `eps` generator:
  230. [reference_karma_eps]
  231. [endsect]
  232. [/////////////////////////////////////////////////////////////////////////////]
  233. [section:lazy Lazy Generator (`lazy`)]
  234. [heading Description]
  235. The family of `lazy` components allows to use a dynamically returned generator
  236. component for output generation. It calls the provided function or function
  237. object at generate time using its return value as the actual generator to
  238. produce the output.
  239. [heading Header]
  240. // forwards to <boost/spirit/home/karma/auxiliary/lazy.hpp>
  241. #include <boost/spirit/include/karma_lazy.hpp>
  242. Also, see __include_structure__.
  243. [heading Namespace]
  244. [table
  245. [[Name]]
  246. [[`boost::spirit::lazy // alias: boost::spirit::karma::lazy`]]
  247. ]
  248. [heading Model of]
  249. [:__generator_concept__]
  250. [variablelist Notation
  251. [[`fg`] [A function or function object that evaluates to a generator
  252. object (an object exposing the __generator_concept__). This
  253. function will be invoked at generate time.]]
  254. ]
  255. The signature of `fg` is expected to be
  256. G f(Unused, Context)
  257. where `G`, the function's return value, is the type of the generator to be
  258. invoked, and `Context` is the generator's __karma_context__ type (The
  259. first argument is __unused__ to make the `Context` the second argument. This
  260. is done for uniformity with __karma_actions__).
  261. [heading Expression Semantics]
  262. Semantics of an expression is defined only where it differs from, or is
  263. not defined in __generator_concept__.
  264. [table
  265. [[Expression] [Semantics]]
  266. [[`fg`] [The __phoenix__ function object `fg` will be
  267. invoked at generate time. It is expected to return a
  268. generator instance. This generator is then invoked
  269. in order to generate the output. This generator will
  270. succeed as long as the invoked generated succeeds as
  271. well (unless the underlying output stream reports
  272. an error).]]
  273. [[`lazy(fg)`] [The function or function object will be invoked at
  274. generate time. It is expected to return a generator
  275. instance (note this version of `lazy` does not
  276. require `fg` to be a __phoenix__ function
  277. object). This generator is then invoked in order to
  278. generate the output. This generator will succeed as
  279. long as the invoked generated succeeds as well (except
  280. if the underlying output stream reports an error).]]
  281. ]
  282. [heading Attributes]
  283. [table
  284. [[Expression] [Attribute]]
  285. [[`fg`] [The attribute type `G` as exposed by the generator `g`
  286. returned from `fg`.]]
  287. [[`lazy(fg)`] [The attribute type `G` as exposed by the generator `g`
  288. returned from `fg`.]]
  289. ]
  290. [heading Complexity]
  291. The complexity of the `lazy` component is determined by the complexity of the
  292. generator returned from `fg`.
  293. [heading Example]
  294. [note The test harness for the example(s) below is presented in the
  295. __karma_basics_examples__ section.]
  296. Some includes:
  297. [reference_karma_includes]
  298. Some using declarations:
  299. [reference_karma_using_declarations_lazy]
  300. Basic usage of the `lazy` generator:
  301. [reference_karma_lazy]
  302. [endsect]
  303. [endsect]