string.qbk 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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:string String Generators]
  8. This module includes different string oriented generators allowing to output
  9. character sequences. It includes the `symbols` generator and variants of the
  10. `string` generator.
  11. [heading Module Header]
  12. // forwards to <boost/spirit/home/karma/string.hpp>
  13. #include <boost/spirit/include/karma_string.hpp>
  14. Also, see __include_structure__.
  15. [section:string String Generators (`string`, `lit`)]
  16. [heading Description]
  17. The string generators described in this section are:
  18. The `string` generator emits a string of characters. The `string` generator
  19. is implicitly verbatim: the `delimit` parser is not applied in between
  20. characters of the string. The `string` generator has an associated
  21. __karma_char_encoding_namespace__. This is needed when doing basic operations
  22. such as forcing lower or upper case. Examples:
  23. string("Hello")
  24. string(L"Hello")
  25. string(s) // s is a std::string
  26. `lit`, like `string`, also emits a string of characters. The main
  27. difference is that `lit` does not consumes an attribute. A plain
  28. string like `"hello"` or a `std::basic_string` is equivalent to a `lit`.
  29. Examples:
  30. "Hello"
  31. lit("Hello")
  32. lit(L"Hello")
  33. lit(s) // s is a std::string
  34. [heading Header]
  35. // forwards to <boost/spirit/home/karma/string/lit.hpp>
  36. #include <boost/spirit/include/karma_string.hpp>
  37. Also, see __include_structure__.
  38. [heading Namespace]
  39. [table
  40. [[Name]]
  41. [[`boost::spirit::lit // alias: boost::spirit::karma::lit`]]
  42. [[`ns::string`]]
  43. ]
  44. In the table above, `ns` represents a __karma_char_encoding_namespace__ used by the
  45. corresponding string generator.
  46. [heading Model of]
  47. [:__primitive_generator_concept__]
  48. [variablelist Notation
  49. [[`s`] [Character-class specific string (See __char_class_types__),
  50. or a __karma_lazy_argument__ that evaluates to a
  51. character-class specific string value]]
  52. [[`S`] [The type of a character-class specific string `s`.]]
  53. [[`ns`] [A __karma_char_encoding_namespace__.]]]
  54. [heading Expression Semantics]
  55. Semantics of an expression is defined only where it differs from, or is
  56. not defined in __primitive_generator_concept__.
  57. [table
  58. [[Expression] [Description]]
  59. [[`s`] [Generate the string literal `s`. This generator
  60. never fails (unless the underlying output stream
  61. reports an error).]]
  62. [[`lit(s)`] [Generate the string literal `s`. This generator
  63. never fails (unless the underlying output stream
  64. reports an error).]]
  65. [[`ns::string`] [Generate the string provided by a mandatory
  66. attribute interpreted in the character set defined
  67. by `ns`. This generator never fails (unless the
  68. underlying output stream reports an error).]]
  69. [[`ns::string(s)`] [Generate the string `s` as provided by the
  70. immediate literal value the generator is initialized
  71. from. If this generator has an associated attribute
  72. it succeeds only if the attribute is equal
  73. to the immediate literal (unless the underlying
  74. output stream reports an error). Otherwise this
  75. generator fails and does not generate any output.]]
  76. ]
  77. [note The generators `lit(s)` and `string(s)` can be initialized either
  78. using a string literal value (i.e. `"abc"`), or using a
  79. `std::basic_string<char_type, ...>`, where `char_type` is the required
  80. value type of the underlying character sequence.]
  81. [caution The generator `string(s)` up to version 2.4.1 of Spirit has an
  82. undocumented feature. Given argument `s` generator succeds as long as
  83. `s` is a prefix of given attribute. This problem has been fixed in
  84. Spirit V2.4.2.]
  85. [heading Attributes]
  86. [table
  87. [[Expression] [Attribute]]
  88. [[`s`] [__unused__]]
  89. [[`lit(s)`] [__unused__]]
  90. [[`ns::string`] [`S`, attribute is mandatory (otherwise compilation
  91. will fail)]]
  92. [[`ns::string(s)`] [`S`, attribute is optional, if it is supplied, the
  93. generator compares the attribute with `s` and
  94. succeeds only if both are equal, failing otherwise]]
  95. ]
  96. [note In addition to their usual attribute of type `S` all listed generators
  97. accept an instance of a `boost::optional<S>` as well. If the
  98. `boost::optional<>` is initialized (holds a value) the generators behave
  99. as if their attribute was an instance of `S` and emit the value stored
  100. in the `boost::optional<>`. Otherwise the generators will fail.]
  101. [heading Complexity]
  102. [:O(N), where N is the number of characters emitted by the string generator]
  103. [heading Example]
  104. [note The test harness for the example(s) below is presented in the
  105. __karma_basics_examples__ section.]
  106. Some includes:
  107. [reference_karma_includes]
  108. Some using declarations:
  109. [reference_karma_using_declarations_string]
  110. Basic usage of `string` generators:
  111. [reference_karma_string]
  112. [endsect]
  113. [/------------------------------------------------------------------------------]
  114. [section:symbols Symbols Generator (`symbols`)]
  115. [heading Description]
  116. The class `symbols` implements an 'inverse' symbol table: an associative
  117. container (or map) of key-value pairs where the values are (most of the time)
  118. strings. It maps the value to be generated (the key) to any other value which
  119. will be emitted instead of the original key.
  120. The Karma symbol table class `symbols` is-a generator, an instance of which may
  121. be used anywhere in the grammar specification. It is an example of a
  122. dynamic generator. A dynamic generator is characterized by its ability to
  123. modify its behavior at run time. Initially, an empty symbols object
  124. will emit nothing. At any time, symbols may be added, thus, dynamically
  125. altering its behavior.
  126. [heading Header]
  127. // forwards to <boost/spirit/home/karma/string/symbols.hpp>
  128. #include <boost/spirit/include/karma_symbols.hpp>
  129. Also, see __include_structure__.
  130. [heading Namespace]
  131. [table
  132. [[Name]]
  133. [[`boost::spirit::karma::symbols`]]
  134. ]
  135. [heading Synopsis]
  136. template <typename Attrib, typename T, typename Lookup
  137. , typename CharEncoding, typename Tag>
  138. struct symbols;
  139. [heading Template parameters]
  140. [table
  141. [[Parameter] [Description] [Default]]
  142. [[`Attrib`] [The type of the original attribute to be used as
  143. the key into the symbol generator (the symbol).] [`char`]]
  144. [[`T`] [The data type associated
  145. with each key.] [__unused_type__]]
  146. [[`Lookup`] [The symbol search implementation]
  147. [if T is `unused_type`, `std::set<Attrib>`,
  148. and `std::map<Attrib, T>` otherwise]]
  149. [[`CharEncoding`] [Used for character set selection, normally not
  150. used by end user.] [__unused_type__]]
  151. [[`Tag`] [Used for character set selection, normally not
  152. used by end user.] [__unused_type__]]
  153. ]
  154. [heading Model of]
  155. [:__primitive_generator_concept__]
  156. [variablelist Notation
  157. [[`Sym`] [A `symbols` type.]]
  158. [[`Attrib`] [An attribute type.]]
  159. [[`T`] [A data type.]]
  160. [[`sym`, `sym2`][`symbols` objects.]]
  161. [[`sseq`] [An __stl__ container of strings.]]
  162. [[`dseq`] [An __stl__ container of data with `value_type` `T`.]]
  163. [[`s1`...`sN`] [A __string__.]]
  164. [[`d1`...`dN`] [Objects of type `T`.]]
  165. [[`f`] [A callable function or function object.]]
  166. [[`f`, `l`] [`ForwardIterator` first/last pair.]]
  167. ]
  168. [heading Expression Semantics]
  169. Semantics of an expression is defined only where it differs from, or is not
  170. defined in __primitive_generator_concept__.
  171. [table
  172. [[Expression] [Semantics]]
  173. [[`Sym()`] [Construct an empty symbols object instance named `"symbols"`.]]
  174. [[`Sym(name)`] [Construct an empty symbols object instance named `name`.]]
  175. [[`Sym(sym2)`] [Copy construct a symbols from `sym2` (Another `symbols` object).]]
  176. [[`Sym(sseq)`] [Construct symbols from `sseq` (An __stl__ container of
  177. symbols of type `Attrib`) named `"symbols"`.]]
  178. [[`Sym(sseq, name)`] [Construct symbols from `sseq` (an __stl__ container of
  179. symbols of type `Attrib`) named `name`.]]
  180. [[`Sym(sseq, dseq)`] [Construct symbols from `sseq` and `dseq`
  181. (An __stl__ container of symbols of type `Attrib` and an
  182. __stl__ container of data with `value_type` `T`)
  183. which is named `"symbols"`.]]
  184. [[`Sym(sseq, dseq, name)`] [Construct symbols from `sseq` and `dseq`
  185. (An __stl__ container of symbols of type `Attrib` and an
  186. __stl__ container of data with `value_type` `T`)
  187. which is named `name`.]]
  188. [[`sym = sym2`] [Assign `sym2` to `sym`.]]
  189. [[`sym = s1, s2, ..., sN`] [Assign one or more symbols (`s1`...`sN`) to `sym`. The
  190. associated data values of type `T` are default constructed.]]
  191. [[`sym += s1, s2, ..., sN`] [Add one or more symbols (`s1`...`sN`) to `sym`. The
  192. associated data values of type `T` are default constructed.]]
  193. [[`sym.add(s1)(s2)...(sN)`] [Add one or more symbols (`s1`...`sN`) to `sym`. The
  194. associated data values of type `T` are default constructed.]]
  195. [[`sym.add(s1, d1)(s2, d2)...(sN, dN)`]
  196. [Add one or more symbols (`s1`...`sN`)
  197. with associated data (`d1`...`dN`) to `sym`.]]
  198. [[`sym -= s1, s2, ..., sN`] [Remove one or more symbols (`s1`...`sN`) from `sym`.]]
  199. [[`sym.remove(s1)(s2)...(sN)`] [Remove one or more symbols (`s1`...`sN`) from `sym`.]]
  200. [[`sym.clear()`] [Erase all of the symbols in `sym`.]]
  201. [[`sym.at(s)`] [Return a reference to the object associated
  202. with symbol, `s`. If `sym` does not already
  203. contain such an object, `at` inserts the default
  204. object `T()`.]]
  205. [[`sym.find(s)`] [Return a pointer to the object associated
  206. with symbol, `s`. If `sym` does not already
  207. contain such an object, `find` returns a null
  208. pointer.]]
  209. [[`sym.for_each(f)`] [For each symbol in `sym` `s` invoke
  210. `f(typename Lookup::value_type)`.]]
  211. [[`sym.name()`] [Retrieve the current name of the symbols object.]]
  212. [[`sym.name(name)`] [Set the current name of the symbols object to be `name`.]]
  213. ]
  214. The symbols generator uses the supplied attribute as the key to be looked up
  215. in the internal associative container. If the key exists the generator emits
  216. the associated value and succeeds (unless the underlying output stream reports
  217. an error). If the value type stored in the symbol generator is __unused_type__
  218. it will emit the key instead. If the key does not exist the generator fails
  219. while not emitting anything.
  220. [heading Attributes]
  221. The attribute of `symbol<Attrib, T>` is `Attrib`.
  222. If the supplied attribute is a __fusion__ sequence, then the symbol table
  223. generator will use the first element of that __fusion__ sequence as the key
  224. to be used for lookup. The type of that first element needs to be convertible
  225. to `Attrib`. In this case the second element of the __fusion__ sequence is used
  226. as the attribute while calling a generator derived from the value stored in the
  227. symbol table for the found entry.
  228. If the supplied attribute is a container type (__customize_is_container__
  229. resolves to `mpl::true_`), then the symbol table generator will use the first
  230. element stored in that container as the key to be used for lookup. The
  231. `value_type` (returned by __customize_container_value__) has to be convertible
  232. to `Attrib`. In this case the second element stored in that container is used
  233. as the attribute while calling a generator derived from the value stored in the
  234. symbol table for the found entry.
  235. If the supplied attribute is not a __fusion__ sequence and not a container
  236. type, the supplied attribute is directly used as the key for item lookup. The
  237. attribute is used as the attribute while calling a generator derived from the
  238. value stored in the symbol table for the found entry.
  239. In any case, because the supplied key (i.e. either the first element of the
  240. __fusion__ sequence, the first container element, or the attribute otherwise)
  241. is passed as the attribute to a generator derived from the value
  242. stored in the symbol table for the found entry, the symbol table may store
  243. generators, which will produce output based on that value. For instance:
  244. // The symbol table maps a single character key to a rule<>
  245. // The rule<> exposes an attribute of char as well
  246. rule<output_iterator_type, char()> r1 = char_;
  247. symbols<char, rule<output_iterator_type, char()> > sym;
  248. sym.add
  249. ('j', r1.alias())
  250. ('h', r1.alias())
  251. ('t', r1.alias())
  252. ('k', r1.alias())
  253. ;
  254. // Supplying a fusion vector as the attribute will use the first element
  255. // (the 'j') as the key to be looked up, while the second element (the 'J')
  256. // is passed on as the attribute to the rule<> stored in the symbol table.
  257. // Consequently, the example generates a single 'J'.
  258. BOOST_ASSERT(test("J", sym, make_vector('j', 'J')));
  259. [heading Complexity]
  260. The default implementation uses a `std::map<>` or a `std::set<>` with a
  261. complexity of:
  262. [:O(log n)]
  263. Where n is the number of stored symbols.
  264. [heading Example]
  265. [note The test harness for the example(s) below is presented in the
  266. __karma_basics_examples__ section.]
  267. Some includes:
  268. [reference_karma_includes]
  269. Some using declarations:
  270. [reference_karma_using_declarations_symbols]
  271. Basic usage of `symbol` generators:
  272. [reference_karma_symbols]
  273. [endsect] [/ symbols]
  274. [endsect]