concepts.qbk 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. [/==============================================================================
  2. Copyright (C) 2001-2011 Joel de Guzman
  3. Copyright (C) 2001-2011 Hartmut Kaiser
  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 Generator Concepts]
  8. __karma__ generators fall into a couple of generalized __concepts__. The
  9. /Generator/ is the most fundamental concept. All __karma__ generators are
  10. models of the /Generator/ concept. /PrimitiveGenerator/, /UnaryGenerator/,
  11. /BinaryGenerator/, /NaryGenerator/, and /Nonterminal/ are all refinements of
  12. the /Generator/ concept.
  13. The following sections provide details on these concepts.
  14. [/////////////////////////////////////////////////////////////////////////////]
  15. [section Generator]
  16. [heading Description]
  17. The /Generator/ is the most fundamental concept. A Generator has a member
  18. function, `generate`, that accepts an `OutputIterator` and
  19. returns bool as its result. The iterator receives the data being generated.
  20. The Generator's `generate` member function returns `true` if the generator
  21. succeeds. Each Generator can represent a specific pattern or algorithm, or it
  22. can be a more complex generator formed as a composition of other Generators.
  23. [variablelist Notation
  24. [[`g`] [A `Generator`.]]
  25. [[`G`] [A `Generator` type.]]
  26. [[`OutIter`] [An `OutputIterator` type.]]
  27. [[`sink`] [An `OutputIterator` instance.]]
  28. [[`Context`] [The generator's __karma_context__ type.]]
  29. [[`context`] [The generator's __karma_context__, or __unused__.]]
  30. [[`delimit`] [A delimiter Generator, or __unused__.]]
  31. [[`attrib`] [A __karma_compatible_attribute__, or __unused__.]]
  32. ]
  33. [heading Valid Expressions]
  34. In the expressions below, the behavior of the generator, `g`, as well as how
  35. `delimit` and `attrib` are handled by `g`, are left unspecified in the base
  36. `Generator` concept. These are specified in subsequent, more refined concepts
  37. and by the actual models thereof.
  38. For any Generator the following expressions must be valid:
  39. [table
  40. [[Expression] [Semantics] [Return type]]
  41. [[
  42. ``g.generate(sink, context, delimit, attrib)``]
  43. [Generate the output sequence by inserting the
  44. generated characters/tokens into `sink`. Use the
  45. `delimit` generator for delimiting. Return
  46. `true` if successful, otherwise
  47. return `false`.] [`bool`]]
  48. [[`g.what(context)`] [Get information about a Generator.] [__info__]]
  49. ]
  50. [heading Type Expressions]
  51. [table
  52. [[Expression] [Description]]
  53. [[`G::template attribute<Context>::type`] [The Generator's attribute.]]
  54. [[`traits::is_generator<G>::type`] [Metafunction that evaluates to `mpl::true_` if
  55. a certain type, `G` is a Generator, `mpl::false_`
  56. otherwise (See __mpl_boolean_constant__).]]
  57. [[`G::properties`] [An `mpl::int_` (See __mpl_int_constant__) holding
  58. a value from the `karma::generator_properties`
  59. enumeration. The default value is
  60. `generator_properties::no_properties`]]
  61. ]
  62. [heading Postcondition]
  63. Upon return from `g.generate` the following post conditions should hold:
  64. * On successful generation, `sink` receives the generated characters/tokens
  65. sequence.
  66. * No pre-delimits: `delimit` characters/tokens will not be emitted in front of
  67. any other output.
  68. * The attribute `attrib` has not been modified.
  69. [heading Models]
  70. All generators in __karma__ are models of the /Generator/ concept.
  71. [endsect] [/ Generator Concept]
  72. [/////////////////////////////////////////////////////////////////////////////]
  73. [section PrimitiveGenerator]
  74. [heading Description]
  75. /PrimitiveGenerator/ is the most basic building block that the client uses
  76. to build more complex generators.
  77. [heading Refinement of]
  78. [:__generator_concept__]
  79. [heading Post-delimit]
  80. Before exiting the `generate` member function, a PrimitiveGenerator is required
  81. to do a post-delimit. This will generate a single delimiting character/token
  82. sequence. Only PrimitiveGenerator's are required to perform this post-delimit.
  83. This is typically carried out through a call to `karma::delimit_out`:
  84. karma::delimit_out(sink, delimit);
  85. [heading Type Expressions]
  86. [table
  87. [[Expression] [Description]]
  88. [[`traits::is_primitive_generator<G>::type`] [Metafunction that evaluates to `mpl::true_` if
  89. a certain type, `G`, is a PrimitiveGenerator, `mpl::false_`
  90. otherwise (See __mpl_boolean_constant__).]]
  91. ]
  92. [heading Models]
  93. The following generators conform to this model:
  94. * __karma_eol__,
  95. * __karma_eps__,
  96. * [link spirit.karma.reference.numeric Numeric generators],
  97. * [karma_char Character generators].
  98. __fixme__ Add more links to /PrimitiveGenerator/ models here.
  99. [endsect] [/ PrimitiveGenerator Concept]
  100. [/////////////////////////////////////////////////////////////////////////////]
  101. [section UnaryGenerator]
  102. [heading Description]
  103. /UnaryGenerator/ is a composite generator that has a single subject. The
  104. UnaryGenerator may change the behavior of its subject following the
  105. __delegate_pattern__.
  106. [heading Refinement of]
  107. [:__generator_concept__]
  108. [variablelist Notation
  109. [[`g`] [A UnaryGenerator.]]
  110. [[`G`] [A UnaryGenerator type.]]
  111. ]
  112. [heading Valid Expressions]
  113. In addition to the requirements defined in __generator_concept__, for any
  114. UnaryGenerator the following must be met:
  115. [table
  116. [[Expression] [Semantics] [Return type]]
  117. [[`g.subject`] [Subject generator.] [__generator_concept__]]
  118. ]
  119. [heading Type Expressions]
  120. [table
  121. [[Expression] [Description]]
  122. [[`G::subject_type`] [The subject generator type.]]
  123. [[`traits::is_unary_generator<G>::type`] [Metafunction that evaluates to `mpl::true_` if
  124. a certain type, `G` is a UnaryGenerator, `mpl::false_`
  125. otherwise (See __mpl_boolean_constant__).]]
  126. ]
  127. [heading Invariants]
  128. For any UnaryGenerator, `G`, the following invariant always holds:
  129. * `traits::is_generator<G::subject_type>::type` evaluates to `mpl::true_`
  130. [heading Models]
  131. The following generators conform to this model:
  132. * [karma_kleene Kleene Star (unary `*`)] operator,
  133. * __karma_plus__ operator,
  134. * __karma_optional__ operator,
  135. * __karma_and_predicate__ and __karma_not_predicate__ operators,
  136. * [karma_align `left_align`], [karma_align `center`], and [karma_align `right_align`] directives,
  137. * [karma_repeat `repeat`] directive,
  138. * __karma_verbatim__ directive,
  139. * [karma_delimit `delimit`] directive,
  140. * [karma_upperlower `lower`] and [karma_upperlower `upper`] directives,
  141. * [karma_maxwidth `maxwidth`] directive,
  142. * __karma_buffer__ directive,
  143. * __karma_omit__ directive.
  144. __fixme__ Add more links to models of UnaryGenerator concept
  145. [endsect] [/ UnaryGenerator Concept]
  146. [/////////////////////////////////////////////////////////////////////////////]
  147. [section BinaryGenerator]
  148. [heading Description]
  149. /BinaryGenerator/ is a composite generator that has a two subjects, `left` and
  150. `right`. The BinaryGenerator allows its subjects to be treated in the same
  151. way as a single instance of a __generator_concept__ following the
  152. __composite_pattern__.
  153. [heading Refinement of]
  154. [:__generator_concept__]
  155. [variablelist Notation
  156. [[`g`] [A BinaryGenerator.]]
  157. [[`G`] [A BinaryGenerator type.]]
  158. ]
  159. [heading Valid Expressions]
  160. In addition to the requirements defined in __generator_concept__, for any
  161. BinaryGenerator the following must be met:
  162. [table
  163. [[Expression] [Semantics] [Return type]]
  164. [[`g.left`] [Left generator.] [__generator_concept__]]
  165. [[`g.right`] [Right generator.] [__generator_concept__]]
  166. ]
  167. [heading Type Expressions]
  168. [table
  169. [[Expression] [Description]]
  170. [[`G::left_type`] [The left generator type.]]
  171. [[`G::right_type`] [The right generator type.]]
  172. [[`traits::is_binary_generator<G>::type`] [Metafunction that evaluates to `mpl::true_` if
  173. a certain type, `G` is a BinaryGenerator, `mpl::false_`
  174. otherwise (See __mpl_boolean_constant__).]]
  175. ]
  176. [heading Invariants]
  177. For any BinaryGenerator, `G`, the following invariants always hold:
  178. * `traits::is_generator<G::left_type>::type` evaluates to `mpl::true_`
  179. * `traits::is_generator<G::right_type>::type` evaluates to `mpl::true_`
  180. [heading Models]
  181. The following generators conform to this model:
  182. * __karma_list__.
  183. __fixme__ Add more links to models of BinaryGenerator concept
  184. [endsect] [/ BinaryGenerator Concept]
  185. [/////////////////////////////////////////////////////////////////////////////]
  186. [section NaryGenerator]
  187. [heading Description]
  188. /NaryGenerator/ is a composite generator that has one or more subjects. The
  189. NaryGenerator allows its subjects to be treated in the same way as a single
  190. instance of a __generator_concept__ following the __composite_pattern__.
  191. [heading Refinement of]
  192. [:__generator_concept__]
  193. [variablelist Notation
  194. [[`g`] [A NaryGenerator.]]
  195. [[`G`] [A NaryGenerator type.]]
  196. ]
  197. [heading Valid Expressions]
  198. In addition to the requirements defined in __generator_concept__, for any
  199. NaryGenerator the following must be met:
  200. [table
  201. [[Expression] [Semantics] [Return type]]
  202. [[`g.elements`] [The tuple of elements.] [A __fusion__ Sequence of __generator_concept__ types.]]
  203. ]
  204. [heading Type Expressions]
  205. [table
  206. [[Expression] [Description]]
  207. [[`g.elements_type`] [Elements tuple type.]]
  208. [[`traits::is_nary_generator<G>::type`] [Metafunction that evaluates to `mpl::true_` if
  209. a certain type, `G` is a NaryGenerator, `mpl::false_`
  210. otherwise (See __mpl_boolean_constant__).]]
  211. ]
  212. [heading Invariants]
  213. For each element, `E`, in any NaryGenerator, `G`, the following
  214. invariant always holds:
  215. * `traits::is_generator<E>::type` evaluates to `mpl::true_`
  216. [heading Models]
  217. The following generators conform to this model:
  218. * __karma_sequence__,
  219. * __karma_alternative__.
  220. __fixme__ Add more links to models of NaryGenerator concept
  221. [endsect] [/ NaryGenerator Concept]
  222. [/////////////////////////////////////////////////////////////////////////////]
  223. [section Nonterminal]
  224. [heading Description]
  225. A Nonterminal is a symbol in a __peg__ production that represents a
  226. grammar fragment. Nonterminals may self reference to specify recursion.
  227. This is one of the most important concepts and the reason behind the
  228. word "recursive" in recursive descent generation.
  229. [heading Refinement of]
  230. [:__generator_concept__]
  231. [heading Signature]
  232. Nonterminals can have both consumed and inherited attributes. The
  233. Nonterminal's /Signature/ specifies both the consumed and inherited
  234. attributes. The specification uses the function declarator syntax:
  235. RT(A0, A1, A2, ..., AN)
  236. where `RT` is the Nonterminal's consumed attribute and `A0` ... `AN`
  237. are the Nonterminal's inherited attributes.
  238. The default value is `void()` (no consumed and inherited attributes).
  239. [heading Attributes]
  240. The Nonterminal models a C++ function. The Nonterminal's consumed attribute is
  241. analogous to the function return value as it is the type -exposed- by the
  242. Nonterminal. Its inherited attributes are analogous to function arguments.
  243. The inherited attributes (arguments) can be passed in just like any
  244. __karma_lazy_argument__, e.g.:
  245. r(expr) // Evaluate expr at parse time and pass the result to the Nonterminal r
  246. [heading `_val`]
  247. The `boost::spirit::karma::_val` placeholder can be used in __phoenix__
  248. semantic actions anywhere in the Nonterminal's definition. This
  249. __phoenix__ placeholder refers to the Nonterminal's (consumed)
  250. attribute. The `_val` placeholder acts like an immutable reference to the
  251. Nonterminal's attribute.
  252. [note Starting with __spirit__ V2.5 (distributed with Boost V1.47) the
  253. placeholder `_val` can be used in semantic actions attached to top level
  254. generator components as well. See __generator_api__ for more information.]
  255. [heading `_r1`...`r10`]
  256. The `boost::spirit::_r1`...`boost::spirit::r10` placeholders can be used
  257. in __phoenix__ semantic actions anywhere in the Nonterminal's
  258. definition. These __phoenix__ placeholders refer to the Nonterminal's
  259. inherited attributes.
  260. [heading Locals]
  261. Nonterminals can have local variables that will be created on the stack
  262. at runtime. A locals descriptor added to the Nonterminal declaration
  263. will give the Nonterminal local variables:
  264. template <typename T0, typename T1, typename T2, ..., typename TN>
  265. struct locals;
  266. where `T0` ... `TN` are the types of local variables accessible in your
  267. __phoenix__ semantic actions using the placeholders:
  268. * `boost::spirit::_a`
  269. * `boost::spirit::_b`
  270. * `boost::spirit::_c`
  271. * `boost::spirit::_d`
  272. * `boost::spirit::_e`
  273. * `boost::spirit::_f`
  274. * `boost::spirit::_g`
  275. * `boost::spirit::_h`
  276. * `boost::spirit::_i`
  277. * `boost::spirit::_j`
  278. which correspond to the Nonterminal's local variables `T0` ... `T9`.
  279. [variablelist Notation
  280. [[`x`] [A Nonterminal]]
  281. [[`X`] [A Nonterminal type]]
  282. [[`arg1`, `arg2`, ..., `argN`] [__karma_lazy_arguments__ that evaluate to each of
  283. the Nonterminal's inherited attributes.]]
  284. ]
  285. [heading Valid Expressions]
  286. In addition to the requirements defined in __generator_concept__, for any
  287. Nonterminal the following must be met:
  288. [table
  289. [[Expression] [Semantics] [Return type]]
  290. [[`x`] [In a generator expression, invoke Nonterminal `x`] [`X`]]
  291. [[`x(arg1, arg2, ..., argN)`][In a generator expression, invoke Nonterminal `x`
  292. passing in inherited attributes
  293. `arg1`...`argN`] [`X`]]
  294. [[`x.name(name)`] [Set the name of a Nonterminal] [`void`]]
  295. [[`x.name()`] [Get the name of a Nonterminal] [`std::string`]]
  296. ]
  297. [heading Type Expressions]
  298. [table
  299. [[Expression] [Description]]
  300. [[`X::sig_type`] [The Signature of `X`: In a function signature form
  301. as described above in the Signature paragraph.]]
  302. [[`X::locals_type`] [The local variables of `X`: An __mpl_fwd_sequence__.]]
  303. ]
  304. [heading Models]
  305. * __karma_rule__
  306. * __karma_grammar__
  307. [endsect]
  308. [endsect]