char.qbk 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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:char Char Generators]
  8. This module includes different character oriented generators allowing to output
  9. single characters. Currently, it includes literal chars (e.g. `'x'`, `L'x'`),
  10. `char_` (single characters, ranges and character sets) and the encoding
  11. specific character classifiers (`alnum`, `alpha`, `digit`, `xdigit`, etc.).
  12. [heading Module Header]
  13. // forwards to <boost/spirit/home/karma/char.hpp>
  14. #include <boost/spirit/include/karma_char.hpp>
  15. Also, see __include_structure__.
  16. [/////////////////////////////////////////////////////////////////////////////]
  17. [section:char_generator Character Generators (`char_`, `lit`)]
  18. [heading Description]
  19. The character generators described in this section are:
  20. The `char_` generator emits single characters. The `char_` generator has an
  21. associated __karma_char_encoding_namespace__. This is needed when doing basic
  22. operations such as forcing lower or upper case and dealing with
  23. character ranges.
  24. There are various forms of `char_`.
  25. [heading char_]
  26. The no argument form of `char_` emits any character in the associated
  27. __karma_char_encoding_namespace__.
  28. char_ // emits any character as supplied by the attribute
  29. [heading char_(ch)]
  30. The single argument form of `char_` (with a character argument) emits
  31. the supplied character.
  32. char_('x') // emits 'x'
  33. char_(L'x') // emits L'x'
  34. char_(x) // emits x (a char)
  35. [heading char_(first, last)]
  36. `char_` with two arguments, emits any character from a range of characters as
  37. supplied by the attribute.
  38. char_('a','z') // alphabetic characters
  39. char_(L'0',L'9') // digits
  40. A range of characters is created from a low-high character pair. Such a
  41. generator emits a single character that is in the range, including both
  42. endpoints. Note, the first character must be /before/ the second,
  43. according to the underlying __karma_char_encoding_namespace__.
  44. Character mapping is inherently platform dependent. It is not guaranteed
  45. in the standard for example that `'A' < 'Z'`, that is why in Spirit2, we
  46. purposely attach a specific __karma_char_encoding_namespace__ (such as ASCII,
  47. ISO-8859-1) to the `char_` generator to eliminate such ambiguities.
  48. [note *Sparse bit vectors*
  49. To accommodate 16/32 and 64 bit characters, the char-set statically
  50. switches from a `std::bitset` implementation when the character type is
  51. not greater than 8 bits, to a sparse bit/boolean set which uses a sorted
  52. vector of disjoint ranges (`range_run`). The set is constructed from
  53. ranges such that adjacent or overlapping ranges are coalesced.
  54. `range_runs` are very space-economical in situations where there are lots
  55. of ranges and a few individual disjoint values. Searching is O(log n)
  56. where n is the number of ranges.]
  57. [heading char_(def)]
  58. Lastly, when given a string (a plain C string, a `std::basic_string`,
  59. etc.), the string is regarded as a char-set definition string following
  60. a syntax that resembles posix style regular expression character sets
  61. (except that double quotes delimit the set elements instead of square
  62. brackets and there is no special negation ^ character). Examples:
  63. char_("a-zA-Z") // alphabetic characters
  64. char_("0-9a-fA-F") // hexadecimal characters
  65. char_("actgACTG") // DNA identifiers
  66. char_("\x7f\x7e") // Hexadecimal 0x7F and 0x7E
  67. These generators emit any character from a range of characters as
  68. supplied by the attribute.
  69. [heading lit(ch)]
  70. `lit`, when passed a single character, behaves like the single argument
  71. `char_` except that `lit` does not consume an attribute. A plain
  72. `char` or `wchar_t` is equivalent to a `lit`.
  73. [note `lit` is reused by the [karma_string String Generators], the
  74. char generators, and the Numeric Generators (see [signed_int signed integer],
  75. [unsigned_int unsigned integer], and [real_number real number] generators). In
  76. general, a char generator is created when you pass in a
  77. character, a string generator is created when you pass in a string, and a
  78. numeric generator is created when you use a numeric literal. The
  79. exception is when you pass a single element literal string, e.g.
  80. `lit("x")`. In this case, we optimize this to create a char generator
  81. instead of a string generator.]
  82. Examples:
  83. 'x'
  84. lit('x')
  85. lit(L'x')
  86. lit(c) // c is a char
  87. [heading Header]
  88. // forwards to <boost/spirit/home/karma/char/char.hpp>
  89. #include <boost/spirit/include/karma_char_.hpp>
  90. Also, see __include_structure__.
  91. [heading Namespace]
  92. [table
  93. [[Name]]
  94. [[`boost::spirit::lit // alias: boost::spirit::karma::lit` ]]
  95. [[`ns::char_`]]
  96. ]
  97. In the table above, `ns` represents a __karma_char_encoding_namespace__.
  98. [heading Model of]
  99. [:__primitive_generator_concept__]
  100. [variablelist Notation
  101. [[`ch`, `ch1`, `ch2`]
  102. [Character-class specific character (See __char_class_types__),
  103. or a __karma_lazy_argument__ that evaluates to a
  104. character-class specific character value]]
  105. [[`cs`] [Character-set specifier string (See
  106. __char_class_types__), or a __karma_lazy_argument__ that
  107. evaluates to a character-set specifier string, or a
  108. pointer/reference to a null-terminated array of characters.
  109. This string specifies a char-set definition string following
  110. a syntax that resembles posix style regular expression character
  111. sets (except the square brackets and the negation `^` character).]]
  112. [[`ns`] [A __karma_char_encoding_namespace__.]]
  113. [[`cg`] [A char generator, a char range generator, or a char set generator.]]]
  114. [heading Expression Semantics]
  115. Semantics of an expression is defined only where it differs from, or is
  116. not defined in __primitive_generator_concept__.
  117. [table
  118. [[Expression] [Description]]
  119. [[`ch`] [Generate the character literal `ch`. This generator
  120. never fails (unless the underlying output stream
  121. reports an error).]]
  122. [[`lit(ch)`] [Generate the character literal `ch`. This generator
  123. never fails (unless the underlying output stream
  124. reports an error).]]
  125. [[`ns::char_`] [Generate the character provided by a mandatory
  126. attribute interpreted in the character set defined
  127. by `ns`. This generator never fails (unless the
  128. underlying output stream reports an error).]]
  129. [[`ns::char_(ch)`] [Generate the character `ch` as provided by the
  130. immediate literal value the generator is initialized
  131. from. If this generator has an associated attribute
  132. it succeeds only as long as the attribute is equal
  133. to the immediate literal (unless the underlying
  134. output stream reports an error). Otherwise this
  135. generator fails and does not generate any output.]]
  136. [[`ns::char_("c")`] [Generate the character `c` as provided by the
  137. immediate literal value the generator is initialized
  138. from. If this generator has an associated attribute
  139. it succeeds only as long as the attribute is equal
  140. to the immediate literal (unless the underlying
  141. output stream reports an error). Otherwise this
  142. generator fails and does not generate any output.]]
  143. [[`ns::char_(ch1, ch2)`][Generate the character provided by a mandatory
  144. attribute interpreted in the character set defined
  145. by `ns`. The generator succeeds as long as the
  146. attribute belongs to the character range `[ch1, ch2]`
  147. (unless the underlying output stream reports an
  148. error). Otherwise this generator fails and does not
  149. generate any output.]]
  150. [[`ns::char_(cs)`] [Generate the character provided by a mandatory
  151. attribute interpreted in the character set defined
  152. by `ns`. The generator succeeds as long as the
  153. attribute belongs to the character set `cs`
  154. (unless the underlying output stream reports an
  155. error). Otherwise this generator fails and does not
  156. generate any output.]]
  157. [[`~cg`] [Negate `cg`. The result is a negated char generator
  158. that inverts the test condition of the character
  159. generator it is attached to.]]
  160. ]
  161. A character `ch` is assumed to belong to the character range defined by
  162. `ns::char_(ch1, ch2)` if its character value (binary representation)
  163. interpreted in the character set defined by `ns` is not smaller than the
  164. character value of `ch1` and not larger then the character value of `ch2` (i.e.
  165. `ch1 <= ch <= ch2`).
  166. The `charset` parameter passed to `ns::char_(charset)` must be a string
  167. containing more than one character. Every single character in this string is
  168. assumed to belong to the character set defined by this expression. An exception
  169. to this is the `'-'` character which has a special meaning if it is not
  170. specified as the first and not the last character in `charset`. If the `'-'`
  171. is used in between to characters it is interpreted as spanning a character
  172. range. A character `ch` is considered to belong to the defined character set
  173. `charset` if it matches one of the characters as specified by the string
  174. parameter described above. For example
  175. [table
  176. [[Example] [Description]]
  177. [[`char_("abc")`] ['a', 'b', and 'c']]
  178. [[`char_("a-z")`] [all characters (and including) from 'a' to 'z']]
  179. [[`char_("a-zA-Z")`] [all characters (and including) from 'a' to 'z' and 'A' and 'Z']]
  180. [[`char_("-1-9")`] ['-' and all characters (and including) from '1' to '9']]
  181. ]
  182. [heading Attributes]
  183. [table
  184. [[Expression] [Attribute]]
  185. [[`ch`] [__unused__]]
  186. [[`lit(ch)`] [__unused__]]
  187. [[`ns::char_`] [`Ch`, attribute is mandatory (otherwise compilation
  188. will fail). `Ch` is the character type of the
  189. __karma_char_encoding_namespace__, `ns`.]]
  190. [[`ns::char_(ch)`] [`Ch`, attribute is optional, if it is supplied, the
  191. generator compares the attribute with `ch` and
  192. succeeds only if both are equal, failing otherwise.
  193. `Ch` is the character type of the
  194. __karma_char_encoding_namespace__, `ns`.]]
  195. [[`ns::char_("c")`] [`Ch`, attribute is optional, if it is supplied, the
  196. generator compares the attribute with `c` and
  197. succeeds only if both are equal, failing otherwise.
  198. `Ch` is the character type of the
  199. __karma_char_encoding_namespace__, `ns`.]]
  200. [[`ns::char_(ch1, ch2)`][`Ch`, attribute is mandatory (otherwise compilation
  201. will fail), the generator succeeds if the attribute
  202. belongs to the character range `[ch1, ch2]`
  203. interpreted in the character set defined by `ns`.
  204. `Ch` is the character type of the
  205. __karma_char_encoding_namespace__, `ns`.]]
  206. [[`ns::char_(cs)`] [`Ch`, attribute is mandatory (otherwise compilation
  207. will fail), the generator succeeds if the attribute
  208. belongs to the character set `cs`, interpreted
  209. in the character set defined by `ns`.
  210. `Ch` is the character type of the
  211. __karma_char_encoding_namespace__, `ns`.]]
  212. [[`~cg`] [Attribute of `cg`]]
  213. ]
  214. [note In addition to their usual attribute of type `Ch` all listed generators
  215. accept an instance of a `boost::optional<Ch>` as well. If the
  216. `boost::optional<>` is initialized (holds a value) the generators behave
  217. as if their attribute was an instance of `Ch` and emit the value stored
  218. in the `boost::optional<>`. Otherwise the generators will fail.]
  219. [heading Complexity]
  220. [:O(1)]
  221. The complexity of `ch`, `lit(ch)`, `ns::char_`, `ns::char_(ch)`, and
  222. `ns::char_("c")` is constant as all generators emit exactly one character per
  223. invocation.
  224. The character range generator (`ns::char_(ch1, ch2)`) additionally requires
  225. constant lookup time for the verification whether the attribute belongs to
  226. the character range.
  227. The character set generator (`ns::char_(cs)`) additionally requires
  228. O(log N) lookup time for the verification whether the attribute belongs to
  229. the character set, where N is the number of characters in the character set.
  230. [heading Example]
  231. [note The test harness for the example(s) below is presented in the
  232. __karma_basics_examples__ section.]
  233. Some includes:
  234. [reference_karma_includes]
  235. Some using declarations:
  236. [reference_karma_using_declarations_char]
  237. Basic usage of `char_` generators:
  238. [reference_karma_char]
  239. [endsect]
  240. [/////////////////////////////////////////////////////////////////////////////]
  241. [section:char_class Character Classification Generators (`alnum`, `digit`, etc.)]
  242. [heading Description]
  243. The library has the full repertoire of single character generators for
  244. character classification. This includes the usual `alnum`, `alpha`,
  245. `digit`, `xdigit`, etc. generators. These generators have an associated
  246. __karma_char_encoding_namespace__. This is needed when doing basic operations
  247. such as forcing lower or upper case.
  248. [heading Header]
  249. // forwards to <boost/spirit/home/karma/char/char_class.hpp>
  250. #include <boost/spirit/include/karma_char_class.hpp>
  251. Also, see __include_structure__.
  252. [heading Namespace]
  253. [table
  254. [[Name]]
  255. [[`ns::alnum`]]
  256. [[`ns::alpha`]]
  257. [[`ns::blank`]]
  258. [[`ns::cntrl`]]
  259. [[`ns::digit`]]
  260. [[`ns::graph`]]
  261. [[`ns::lower`]]
  262. [[`ns::print`]]
  263. [[`ns::punct`]]
  264. [[`ns::space`]]
  265. [[`ns::upper`]]
  266. [[`ns::xdigit`]]
  267. ]
  268. In the table above, `ns` represents a __karma_char_encoding_namespace__ used by the
  269. corresponding character class generator. All listed generators have a mandatory
  270. attribute `Ch` and will not compile if no attribute is associated.
  271. [heading Model of]
  272. [:__primitive_generator_concept__]
  273. [variablelist Notation
  274. [[`ns`] [A __karma_char_encoding_namespace__.]]]
  275. [heading Expression Semantics]
  276. Semantics of an expression is defined only where it differs from, or is
  277. not defined in __primitive_generator_concept__.
  278. [table
  279. [[Expression] [Semantics]]
  280. [[`ns::alnum`] [If the mandatory attribute satisfies the concept of
  281. `std::isalnum` in the __karma_char_encoding_namespace__
  282. the generator succeeds after emitting
  283. its attribute (unless the underlying output stream
  284. reports an error). This generator fails otherwise
  285. while not generating anything.]]
  286. [[`ns::alpha`] [If the mandatory attribute satisfies the concept of
  287. `std::isalpha` in the __karma_char_encoding_namespace__
  288. the generator succeeds after emitting
  289. its attribute (unless the underlying output stream
  290. reports an error). This generator fails otherwise
  291. while not generating anything.]]
  292. [[`ns::blank`] [If the mandatory attribute satisfies the concept of
  293. `std::isblank` in the __karma_char_encoding_namespace__
  294. the generator succeeds after emitting
  295. its attribute (unless the underlying output stream
  296. reports an error). This generator fails otherwise
  297. while not generating anything.]]
  298. [[`ns::cntrl`] [If the mandatory attribute satisfies the concept of
  299. `std::iscntrl` in the __karma_char_encoding_namespace__
  300. the generator succeeds after emitting
  301. its attribute (unless the underlying output stream
  302. reports an error). This generator fails otherwise
  303. while not generating anything.]]
  304. [[`ns::digit`] [If the mandatory attribute satisfies the concept of
  305. `std::isdigit` in the __karma_char_encoding_namespace__
  306. the generator succeeds after emitting
  307. its attribute (unless the underlying output stream
  308. reports an error). This generator fails otherwise
  309. while not generating anything.]]
  310. [[`ns::graph`] [If the mandatory attribute satisfies the concept of
  311. `std::isgraph` in the __karma_char_encoding_namespace__
  312. the generator succeeds after emitting
  313. its attribute (unless the underlying output stream
  314. reports an error). This generator fails otherwise
  315. while not generating anything.]]
  316. [[`ns::print`] [If the mandatory attribute satisfies the concept of
  317. `std::isprint` in the __karma_char_encoding_namespace__
  318. the generator succeeds after emitting
  319. its attribute (unless the underlying output stream
  320. reports an error). This generator fails otherwise
  321. while not generating anything.]]
  322. [[`ns::punct`] [If the mandatory attribute satisfies the concept of
  323. `std::ispunct` in the __karma_char_encoding_namespace__
  324. the generator succeeds after emitting
  325. its attribute (unless the underlying output stream
  326. reports an error). This generator fails otherwise
  327. while not generating anything.]]
  328. [[`ns::xdigit`] [If the mandatory attribute satisfies the concept of
  329. `std::isxdigit` in the __karma_char_encoding_namespace__
  330. the generator succeeds after emitting
  331. its attribute (unless the underlying output stream
  332. reports an error). This generator fails otherwise
  333. while not generating anything.]]
  334. [[`ns::lower`] [If the mandatory attribute satisfies the concept of
  335. `std::islower` in the __karma_char_encoding_namespace__
  336. the generator succeeds after emitting
  337. its attribute (unless the underlying output stream
  338. reports an error). This generator fails otherwise
  339. while not generating anything.]]
  340. [[`ns::upper`] [If the mandatory attribute satisfies the concept of
  341. `std::isupper` in the __karma_char_encoding_namespace__
  342. the generator succeeds after emitting
  343. its attribute (unless the underlying output stream
  344. reports an error). This generator fails otherwise
  345. while not generating anything.]]
  346. [[`ns::space`] [If the optional attribute satisfies the concept of
  347. `std::isspace` in the __karma_char_encoding_namespace__
  348. the generator succeeds after emitting
  349. its attribute (unless the underlying output stream
  350. reports an error). This generator fails otherwise
  351. while not generating anything.If no attribute is
  352. supplied this generator emits a single space
  353. character in the character set defined by `ns`.]]
  354. ]
  355. Possible values for `ns` are described in the section __karma_char_encoding_namespace__.
  356. [note The generators `alpha` and `alnum` might seem to behave unexpected if
  357. used inside a `lower[]` or `upper[]` directive. Both directives
  358. additionally apply the semantics of `std::islower` or `std::isupper`
  359. to the respective character class. Some examples:
  360. ``
  361. std::string s;
  362. std::back_insert_iterator<std::string> out(s);
  363. generate(out, lower[alpha], 'a'); // succeeds emitting 'a'
  364. generate(out, lower[alpha], 'A'); // fails
  365. ``
  366. The generator directive `upper[]` behaves correspondingly.
  367. ]
  368. [heading Attributes]
  369. [:All listed character class generators can take any attribute `Ch`. All
  370. character class generators (except `space`) require an attribute and will
  371. fail compiling otherwise.]
  372. [note In addition to their usual attribute of type `Ch` all listed generators
  373. accept an instance of a `boost::optional<Ch>` as well. If the
  374. `boost::optional<>` is initialized (holds a value) the generators behave
  375. as if their attribute was an instance of `Ch` and emit the value stored
  376. in the `boost::optional<>`. Otherwise the generators will fail.]
  377. [heading Complexity]
  378. [:O(1)]
  379. The complexity is constant as the generators emit not more than one character
  380. per invocation.
  381. [heading Example]
  382. [note The test harness for the example(s) below is presented in the
  383. __karma_basics_examples__ section.]
  384. Some includes:
  385. [reference_karma_includes]
  386. Some using declarations:
  387. [reference_karma_using_declarations_char_class]
  388. Basic usage of an `alpha` generator:
  389. [reference_karma_char_class]
  390. [endsect]
  391. [endsect]