numeric.qbk 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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:numeric Numeric Parsers]
  8. The library includes a couple of predefined objects for parsing signed
  9. and unsigned integers and real numbers. These parsers are fully
  10. parametric. Most of the important aspects of numeric parsing can be
  11. finely adjusted to suit. This includes the radix base, the minimum and
  12. maximum number of allowable digits, the exponent, the fraction etc.
  13. Policies control the real number parsers' behavior. There are some
  14. predefined policies covering the most common real number formats but the
  15. user can supply her own when needed.
  16. The numeric parsers are fine tuned (employing loop unrolling and
  17. extensive template metaprogramming) with exceptional performance that
  18. rivals the low level C functions such as `atof`, `strtod`, `atol`,
  19. `strtol`. Benchmarks reveal up to 4X speed over the C counterparts. This
  20. goes to show that you can write extremely tight generic C++ code that
  21. rivals, if not surpasses C.
  22. [heading Module Header]
  23. // forwards to <boost/spirit/home/qi/numeric.hpp>
  24. #include <boost/spirit/include/qi_numeric.hpp>
  25. Also, see __include_structure__.
  26. [/------------------------------------------------------------------------------]
  27. [section:uint Unsigned Integer Parsers (`uint_`, etc.)]
  28. [heading Description]
  29. The `uint_parser` class is the simplest among the members of the
  30. numerics package. The `uint_parser` can parse unsigned integers of
  31. arbitrary length and size. The `uint_parser` parser can be used to parse
  32. ordinary primitive C/C++ integers or even user defined scalars such as
  33. bigints (unlimited precision integers) as long as the type follows
  34. certain expression requirements (documented below). The `uint_parser` is
  35. a template class. Template parameters fine tune its behavior.
  36. [heading Header]
  37. // forwards to <boost/spirit/home/qi/numeric/uint.hpp>
  38. #include <boost/spirit/include/qi_uint.hpp>
  39. Also, see __include_structure__.
  40. [heading Namespace]
  41. [table
  42. [[Name]]
  43. [[`boost::spirit::lit // alias: boost::spirit::qi::lit`]]
  44. [[`boost::spirit::bin // alias: boost::spirit::qi::bin`]]
  45. [[`boost::spirit::oct // alias: boost::spirit::qi::oct`]]
  46. [[`boost::spirit::hex // alias: boost::spirit::qi::hex`]]
  47. [[`boost::spirit::ushort_ // alias: boost::spirit::qi::ushort_`]]
  48. [[`boost::spirit::ulong_ // alias: boost::spirit::qi::ulong_`]]
  49. [[`boost::spirit::uint_ // alias: boost::spirit::qi::uint_`]]
  50. [[`boost::spirit::ulong_long // alias: boost::spirit::qi::ulong_long`]]
  51. ]
  52. [note `ulong_long` is only available on platforms where the preprocessor
  53. constant `BOOST_HAS_LONG_LONG` is defined (i.e. on platforms having
  54. native support for `unsigned long long` (64 bit) unsigned integer
  55. types).]
  56. [note `lit` is reused by the [qi_lit_char Character Parsers], and the Numeric
  57. Parsers. In general, a char parser is created when you pass in a
  58. character, and a numeric parser is created when you use a numeric
  59. literal.]
  60. [heading Synopsis]
  61. template <
  62. typename T
  63. , unsigned Radix
  64. , unsigned MinDigits
  65. , int MaxDigits>
  66. struct uint_parser;
  67. [heading Template parameters]
  68. [table
  69. [[Parameter] [Description] [Default]]
  70. [[`T`] [The numeric base type of the
  71. numeric parser.] [none]]
  72. [[`Radix`] [The radix base. This can be
  73. any base from 2..10 and 16] [10]]
  74. [[`MinDigits`] [The minimum number of digits
  75. allowable.] [1]]
  76. [[`MaxDigits`] [The maximum number of digits
  77. allowable. If this is -1, then the
  78. maximum limit becomes unbounded.] [-1]]
  79. ]
  80. [heading Model of]
  81. [:__primitive_parser_concept__]
  82. [variablelist Notation
  83. [[`n`] [An object of `T`, the numeric base type.]]
  84. [[`num`] [Numeric literal, any unsigned integer value, or a
  85. __qi_lazy_argument__ that evaluates to a unsigned integer
  86. value.]]
  87. ]
  88. [heading Expression Semantics]
  89. Semantics of an expression is defined only where it differs from, or is
  90. not defined in __primitive_parser_concept__.
  91. [table
  92. [
  93. [Expression]
  94. [Semantics]
  95. ][
  96. [``
  97. ushort_
  98. uint_
  99. ulong_
  100. ulong_long
  101. ``]
  102. [Parse an unsigned integer using the default radix (10).]
  103. ][
  104. [``
  105. lit(num)
  106. ushort_(num)
  107. uint_(num)
  108. ulong_(num)
  109. ulong_long(num)
  110. ``]
  111. [Match the literal `num` using the default radix (10). The parser will fail
  112. if the parsed value is not equal to the specified value.]
  113. ][
  114. [``
  115. bin
  116. oct
  117. hex
  118. ``]
  119. [Parse an unsigned integer using radix 2 for `bin`, radix 8 for `oct`, and
  120. radix 16 for `hex`.]
  121. ][
  122. [``
  123. bin(num)
  124. oct(num)
  125. hex(num)
  126. ``]
  127. [Match the literal `num` using radix 2 for `bin`, radix 8 for `oct`, and
  128. radix 16 for `hex`. The parser will fail
  129. if the parsed value is not equal to the specified value.]
  130. ][
  131. [``
  132. uint_parser<
  133. T, Radix, MinDigits, MaxDigits
  134. >()
  135. ``]
  136. [Parse an unsigned integer of type `T` using radix `Radix`, with
  137. a minimum of `MinDigits` and a maximum of `MaxDigits`.]
  138. ][
  139. [``
  140. uint_parser<
  141. T, Radix, MinDigits, MaxDigits
  142. >()(num)
  143. ``]
  144. [Match the literal `num` of type `T` using radix `Radix`, with
  145. a minimum of `MinDigits` and a maximum of `MaxDigits`. The parser will fail
  146. if the parsed value is not equal to the specified value.]
  147. ]
  148. ]
  149. [important All numeric parsers check for overflow conditions based on the type
  150. `T` the corresponding `uint_parser<>` has been instantiated with. If the
  151. parsed number overflows this type the parsing fails. Please be aware
  152. that the overflow check is not based on the type of the supplied
  153. attribute but solely depends on the template parameter `T`.]
  154. [heading Attributes]
  155. [table
  156. [
  157. [Expression]
  158. [Attribute]
  159. ][
  160. [``
  161. lit(num)
  162. ``]
  163. [__unused__]
  164. ][
  165. [``
  166. ushort_
  167. ushort_(num)
  168. ``]
  169. [`unsigned short`]
  170. ][
  171. [``
  172. uint_
  173. uint_(num)
  174. bin
  175. bin(num)
  176. oct
  177. oct(num)
  178. hex
  179. hex(num)
  180. ``]
  181. [`unsigned int`]
  182. ][
  183. [``
  184. ulong_
  185. ulong_(num)
  186. ``]
  187. [`unsigned long`]
  188. ][
  189. [``
  190. ulong_long
  191. ulong_long(num)
  192. ``]
  193. [`boost::ulong_long_type`]
  194. ][
  195. [``
  196. uint_parser<
  197. T, Radix, MinDigits, MaxDigits
  198. >()
  199. uint_parser<
  200. T, Radix, MinDigits, MaxDigits
  201. >()(num)
  202. ``]
  203. [`T`]
  204. ]
  205. ]
  206. [heading Complexity]
  207. [:O(N), where N is the number of digits being parsed.]
  208. [heading Minimum Expression Requirements for `T`]
  209. For the numeric base type, `T`, the expression requirements below must be
  210. valid:
  211. [table
  212. [[Expression] [Semantics]]
  213. [[`T()`] [Default construct.]]
  214. [[`T(0)`] [Construct from an `int`.]]
  215. [[`n + n`] [Addition.]]
  216. [[`n * n`] [Multiplication.]]
  217. [[`std::numeric_limits<T>::is_bounded`] [`true` or `false` if `T` bounded.]]
  218. [[`std::numeric_limits<T>::digits`] [Maximum Digits for `T`, radix digits.
  219. Required only if `T` is bounded.]]
  220. [[`std::numeric_limits<T>::digits10`] [Maximum Digits for `T`, base 10.
  221. Required only if `T` is bounded.]]
  222. [[`std::numeric_limits<T>::max()`] [Maximum value for `T`.
  223. Required only if `T` is bounded.]]
  224. [[`std::numeric_limits<T>::min()`] [Minimum value for `T`.
  225. Required only if `T` is bounded.]]
  226. ]
  227. [heading Example]
  228. [note The test harness for the example(s) below is presented in the
  229. __qi_basics_examples__ section.]
  230. Some using declarations:
  231. [reference_using_declarations_uint]
  232. Basic unsigned integers:
  233. [reference_uint]
  234. [reference_thousand_separated]
  235. [endsect] [/ Unsigned Integers]
  236. [/------------------------------------------------------------------------------]
  237. [section:int Signed Integer Parsers (`int_`, etc.)]
  238. [heading Description]
  239. The `int_parser` can parse signed integers of arbitrary length and size.
  240. This is almost the same as the `uint_parser`. The only difference is the
  241. additional task of parsing the `'+'` or `'-'` sign preceding the number.
  242. The class interface is the same as that of the `uint_parser`.
  243. The `int_parser` parser can be used to parse ordinary primitive C/C++
  244. integers or even user defined scalars such as bigints (unlimited
  245. precision integers) as long as the type follows certain expression
  246. requirements (documented below).
  247. [heading Header]
  248. // forwards to <boost/spirit/home/qi/numeric/int.hpp>
  249. #include <boost/spirit/include/qi_int.hpp>
  250. Also, see __include_structure__.
  251. [heading Namespace]
  252. [table
  253. [[Name]]
  254. [[`boost::spirit::lit // alias: boost::spirit::qi::lit`]]
  255. [[`boost::spirit::short_ // alias: boost::spirit::qi::short_`]]
  256. [[`boost::spirit::int_ // alias: boost::spirit::qi::int_`]]
  257. [[`boost::spirit::long_ // alias: boost::spirit::qi::long_`]]
  258. [[`boost::spirit::long_long // alias: boost::spirit::qi::long_long`]]
  259. ]
  260. [note `long_long` is only available on platforms where the preprocessor
  261. constant `BOOST_HAS_LONG_LONG` is defined (i.e. on platforms having
  262. native support for `signed long long` (64 bit) unsigned integer types).]
  263. [note `lit` is reused by the [qi_lit_char Character Parsers], and the Numeric
  264. Parsers. In general, a char parser is created when you pass in a
  265. character, and a numeric parser is created when you use a numeric
  266. literal.]
  267. [heading Synopsis]
  268. template <
  269. typename T
  270. , unsigned Radix
  271. , unsigned MinDigits
  272. , int MaxDigits>
  273. struct int_parser;
  274. [heading Template parameters]
  275. [table
  276. [[Parameter] [Description] [Default]]
  277. [[`T`] [The numeric base type of the
  278. numeric parser.] [none]]
  279. [[`Radix`] [The radix base. This can be
  280. any base from 2..10 and 16] [10]]
  281. [[`MinDigits`] [The minimum number of digits
  282. allowable.] [1]]
  283. [[`MaxDigits`] [The maximum number of digits
  284. allowable. If this is -1, then the
  285. maximum limit becomes unbounded.] [-1]]
  286. ]
  287. [heading Model of]
  288. [:__primitive_parser_concept__]
  289. [variablelist Notation
  290. [[`n`] [An object of `T`, the numeric base type.]]
  291. [[`num`] [Numeric literal, any signed integer value, or a
  292. __qi_lazy_argument__ that evaluates to a signed integer
  293. value.]]
  294. ]
  295. [heading Expression Semantics]
  296. Semantics of an expression is defined only where it differs from, or is
  297. not defined in __primitive_parser_concept__.
  298. [table
  299. [
  300. [Expression]
  301. [Semantics]
  302. ][
  303. [``
  304. short_
  305. int_
  306. long_
  307. long_long
  308. ``]
  309. [Parse a signed integer using the default radix (10).]
  310. ][
  311. [``
  312. lit(num)
  313. short_(num)
  314. int_(num)
  315. long_(num)
  316. long_long(num)
  317. ``]
  318. [Match the literal `num` using the default radix (10). The parser will fail
  319. if the parsed value is not equal to the specified value.]
  320. ][
  321. [``
  322. int_parser<
  323. T, Radix, MinDigits, MaxDigits
  324. >()
  325. ``]
  326. [Parse a signed integer of type `T` using radix `Radix`, with
  327. a minimum of `MinDigits` and a maximum of `MaxDigits`.]
  328. ][
  329. [``
  330. int_parser<
  331. T, Radix, MinDigits, MaxDigits
  332. >()(num)
  333. ``]
  334. [Match the literal `num` of type `T` using radix `Radix`, with
  335. a minimum of `MinDigits` and a maximum of `MaxDigits`. The parser will fail
  336. if the parsed value is not equal to the specified value.]
  337. ]
  338. ]
  339. [important All numeric parsers check for overflow conditions based on the type `T`
  340. the corresponding `int_parser<>` has been instantiated with. If the
  341. parsed number overflows this type the parsing fails. Please be aware
  342. that the overflow check is not based on the type of the supplied
  343. attribute but solely depends on the template parameter `T`.]
  344. [heading Attributes]
  345. [table
  346. [
  347. [Expression]
  348. [Attribute]
  349. ][
  350. [``
  351. lit(num)
  352. ``]
  353. [__unused__]
  354. ][
  355. [``
  356. short_
  357. short_(num)
  358. ``]
  359. [`short`]
  360. ][
  361. [``
  362. int_
  363. int_(num)
  364. ``]
  365. [`int`]
  366. ][
  367. [``
  368. long_
  369. long_(num)
  370. ``]
  371. [`long`]
  372. ][
  373. [``
  374. long_long
  375. long_long(num)
  376. ``]
  377. [`boost::long_long_type`]
  378. ][
  379. [``
  380. int_parser<
  381. T, Radix, MinDigits, MaxDigits
  382. >()
  383. int_parser<
  384. T, Radix, MinDigits, MaxDigits
  385. >()(num)
  386. ``]
  387. [`T`]
  388. ]
  389. ]
  390. [heading Complexity]
  391. [:O(N), where N is the number of digits being parsed plus the sign.]
  392. [heading Minimum Expression Requirements for `T`]
  393. For the numeric base type, `T`, the expression requirements below must be
  394. valid:
  395. [table
  396. [[Expression] [Semantics]]
  397. [[`T()`] [Default construct.]]
  398. [[`T(0)`] [Construct from an `int`.]]
  399. [[`n + n`] [Addition.]]
  400. [[`n - n`] [Subtraction.]]
  401. [[`n * n`] [Multiplication.]]
  402. [[`std::numeric_limits<T>::is_bounded`] [`true` or `false` if `T` bounded.]]
  403. [[`std::numeric_limits<T>::digits`] [Maximum Digits for `T`, radix digits.
  404. Required only if `T` is bounded.]]
  405. [[`std::numeric_limits<T>::digits10`] [Maximum Digits for `T`, base 10.
  406. Required only if `T` is bounded.]]
  407. [[`std::numeric_limits<T>::max()`] [Maximum value for `T`.
  408. Required only if `T` is bounded.]]
  409. [[`std::numeric_limits<T>::min()`] [Minimum value for `T`.
  410. Required only if `T` is bounded.]]
  411. ]
  412. [heading Example]
  413. [note The test harness for the example(s) below is presented in the
  414. __qi_basics_examples__ section.]
  415. Some using declarations:
  416. [reference_using_declarations_int]
  417. Basic signed integers:
  418. [reference_int]
  419. [endsect] [/ Signed Integers]
  420. [/------------------------------------------------------------------------------]
  421. [section:real Real Number Parsers (`float_`, `double_`, etc.)]
  422. [heading Description]
  423. The `real_parser` can parse real numbers of arbitrary length and size
  424. limited by its template parameter, `T`. The numeric base type `T` can be
  425. a user defined numeric type such as fixed_point (fixed point reals) and
  426. bignum (unlimited precision numbers) as long as the type follows certain
  427. expression requirements (documented below).
  428. [heading Header]
  429. // forwards to <boost/spirit/home/qi/numeric/real.hpp>
  430. #include <boost/spirit/include/qi_real.hpp>
  431. Also, see __include_structure__.
  432. [heading Namespace]
  433. [table
  434. [[Name]]
  435. [[`boost::spirit::lit // alias: boost::spirit::qi::lit`]]
  436. [[`boost::spirit::float_ // alias: boost::spirit::qi::float_`]]
  437. [[`boost::spirit::double_ // alias: boost::spirit::qi::double_`]]
  438. [[`boost::spirit::long_double // alias: boost::spirit::qi::long_double`]]
  439. ]
  440. [note `lit` is reused by the [qi_lit_char Character Parsers], and the Numeric
  441. Parsers. In general, a char parser is created when you pass in a
  442. character, and a numeric parser is created when you use a numeric
  443. literal.]
  444. [heading Synopsis]
  445. template <typename T, typename RealPolicies>
  446. struct real_parser;
  447. [heading Template parameters]
  448. [table
  449. [[Parameter] [Description] [Default]]
  450. [[`T`] [The numeric base type of the
  451. numeric parser.] [none]]
  452. [[`RealPolicies`] [Policies control the
  453. parser's behavior.] [`real_policies<T>`]]
  454. ]
  455. [heading Model of]
  456. [:__primitive_parser_concept__]
  457. [variablelist Notation
  458. [[`n`] [An object of `T`, the numeric base type.]]
  459. [[`num`] [Numeric literal, any real value, or a __qi_lazy_argument__
  460. that evaluates to a real value.]]
  461. [[`RP`] [A `RealPolicies` (type).]]
  462. [[`exp`] [A `int` exponent.]]
  463. [[`b`] [A `bool` flag.]]
  464. [[`f`, `l`] [__fwditer__. first/last iterator pair.]]
  465. ]
  466. [heading Expression Semantics]
  467. Semantics of an expression is defined only where it differs from, or is
  468. not defined in __primitive_parser_concept__.
  469. [table
  470. [
  471. [Expression]
  472. [Semantics]
  473. ][
  474. [``
  475. float_
  476. double_
  477. long_double
  478. ``]
  479. [Parse a real using the default policies (`real_policies<T>`).]
  480. ][
  481. [``
  482. lit(num)
  483. float_(num)
  484. double_(num)
  485. long_double(num)
  486. ``]
  487. [Match the literal `num` using the default policies (`real_policies<T>`).
  488. The parser will fail if the parsed value is not equal to the specified
  489. value.]
  490. ][
  491. [``
  492. real_parser<
  493. T, RealPolicies
  494. >()
  495. ``]
  496. [Parse a real of type `T` using `RealPolicies`.]
  497. ][
  498. [``
  499. real_parser<
  500. T, RealPolicies
  501. >()(num)
  502. ``]
  503. [Match the literal `num` of type `T` using `RealPolicies`. The parser will fail
  504. if the parsed value is not equal to the specified value.]
  505. ]
  506. ]
  507. [heading Attributes]
  508. [table
  509. [
  510. [Expression]
  511. [Attribute]
  512. ][
  513. [``
  514. lit(num)
  515. ``]
  516. [__unused__]
  517. ][
  518. [``
  519. float_
  520. float_(num)
  521. ``]
  522. [`float`]
  523. ][
  524. [``
  525. double_
  526. double_(num)
  527. ``]
  528. [`double`]
  529. ][
  530. [``
  531. long_double
  532. long_double(num)
  533. ``]
  534. [`long double`]
  535. ][
  536. [``
  537. real_parser<
  538. T, RealPolicies
  539. >()
  540. real_parser<
  541. T, RealPolicies
  542. >()(num)
  543. ``]
  544. [`T`]
  545. ]
  546. ]
  547. [heading Complexity]
  548. [:O(N), where N is the number of characters (including the digits,
  549. exponent, sign, etc.) being parsed.]
  550. [heading Minimum Expression Requirements for `T`]
  551. The numeric base type, `T`, the minimum expression requirements listed
  552. below must be valid. Take note that additional requirements may be
  553. imposed by custom policies.
  554. [table
  555. [[Expression] [Semantics]]
  556. [[`T()`] [Default construct.]]
  557. [[`T(0)`] [Construct from an `int`.]]
  558. [[`n + n`] [Addition.]]
  559. [[`n - n`] [Subtraction.]]
  560. [[`n * n`] [Multiplication.]]
  561. [[`std::numeric_limits<T>::is_bounded`] [`true` or `false` if `T` bounded.]]
  562. [[`std::numeric_limits<T>::digits`] [Maximum Digits for `T`, radix digits.
  563. Required only if `T` is bounded.]]
  564. [[`std::numeric_limits<T>::digits10`] [Maximum Digits for `T`, base 10.
  565. Required only if `T` is bounded.]]
  566. [[`std::numeric_limits<T>::max()`] [Maximum value for `T`.
  567. Required only if `T` is bounded.]]
  568. [[`std::numeric_limits<T>::min()`] [Minimum value for `T`.
  569. Required only if `T` is bounded.]]
  570. [[`boost::spirit::traits::scale(exp, n)`]
  571. [Multiply `n` by `10^exp`. Default implementation
  572. is provided for `float`, `double` and `long double`.]]
  573. [[`boost::spirit::traits::negate(b, n)`]
  574. [Negate `n` if `b` is `true`. Default implementation
  575. is provided for `float`, `double` and `long double`.]]
  576. ]
  577. [note The additional spirit real number traits above are provided to
  578. allow custom implementations to implement efficient real number parsers.
  579. For example, for certain custom real numbers, scaling to a base 10
  580. exponent is a very cheap operation.]
  581. [heading `RealPolicies`]
  582. The `RealPolicies` template parameter is a class that groups all the
  583. policies that control the parser's behavior. Policies control the real
  584. number parsers' behavior.
  585. The default is `real_policies<T>`. The default is provided to take care
  586. of the most common case (there are many ways to represent, and hence
  587. parse, real numbers). In most cases, the default policies are sufficient
  588. and can be used straight out of the box. They are designed to parse
  589. C/C++ style floating point numbers of the form `nnn.fff.Eeee` where
  590. `nnn` is the whole number part, `fff` is the fractional part, `E` is
  591. `'e'` or `'E'` and `eee` is the exponent optionally preceded by `'-'` or
  592. `'+'` with the additional detection of NaN and Inf as mandated by the
  593. C99 Standard and proposed for inclusion into the C++0x Standard: nan,
  594. nan(...), inf and infinity (the matching is case-insensitive). This
  595. corresponds to the following grammar:
  596. sign
  597. = lit('+') | '-'
  598. ;
  599. nan
  600. = no_case["nan"]
  601. >> -('(' >> *(char_ - ')') >> ')')
  602. ;
  603. inf
  604. = no_case[lit("inf") >> -lit("inity")]
  605. ;
  606. floating_literal
  607. = -sign >>
  608. ( nan
  609. | inf
  610. | fractional_constant >> -exponent_part
  611. | +digit >> exponent_part
  612. )
  613. ;
  614. fractional_constant
  615. = *digit >> '.' >> +digit
  616. | +digit >> -lit('.')
  617. ;
  618. exponent_part
  619. = (lit('e') | 'E') >> -sign >> +digit
  620. ;
  621. There are four `RealPolicies` predefined for immediate use:
  622. [table Predefined Policies
  623. [[Policies] [Description]]
  624. [[`ureal_policies<double>`] [Without sign.]]
  625. [[`real_policies<double>`] [With sign.]]
  626. [[`strict_ureal_policies<double>`] [Without sign, dot required.]]
  627. [[`strict_real_policies<double>`] [With sign, dot required.]]
  628. ]
  629. [note Integers are considered a subset of real numbers, so for instance,
  630. `double_` recognizes integer numbers (without a dot) just as well. To
  631. avoid this ambiguity, `strict_ureal_policies` and `strict_real_policies`
  632. require a dot to be present for a number to be considered a successful
  633. match.]
  634. [heading `RealPolicies` Expression Requirements]
  635. For models of `RealPolicies` the following expressions must be valid:
  636. [table
  637. [[Expression] [Semantics]]
  638. [[`RP::allow_leading_dot`] [Allow leading dot.]]
  639. [[`RP::allow_trailing_dot`] [Allow trailing dot.]]
  640. [[`RP::expect_dot`] [Require a dot.]]
  641. [[`RP::parse_sign(f, l)`] [Parse the prefix sign (e.g. '-').
  642. Return `true` if successful, otherwise `false`.]]
  643. [[`RP::parse_n(f, l, n)`] [Parse the integer at the left of the decimal point.
  644. Return `true` if successful, otherwise `false`.
  645. If successful, place the result into `n`.]]
  646. [[`RP::parse_dot(f, l)`] [Parse the decimal point.
  647. Return `true` if successful, otherwise `false`.]]
  648. [[`RP::parse_frac_n(f, l, n)`] [Parse the fraction after the decimal point.
  649. Return `true` if successful, otherwise `false`.
  650. If successful, place the result into `n`.]]
  651. [[`RP::parse_exp(f, l)`] [Parse the exponent prefix (e.g. 'e').
  652. Return `true` if successful, otherwise `false`.]]
  653. [[`RP::parse_exp_n(f, l, n)`] [Parse the actual exponent.
  654. Return `true` if successful, otherwise `false`.
  655. If successful, place the result into `n`.]]
  656. [[`RP::parse_nan(f, l, n)`] [Parse a NaN.
  657. Return `true` if successful, otherwise `false`.
  658. If successful, place the result into `n`.]]
  659. [[`RP::parse_inf(f, l, n)`] [Parse an Inf.
  660. Return `true` if successful, otherwise `false`.
  661. If successful, place the result into `n`.]]
  662. ]
  663. The `parse_nan` and `parse_inf` functions get called whenever
  664. a number to parse does not start with a digit (after having
  665. successfully parsed an optional sign).
  666. The functions should return true if a Nan or Inf has been found. In this
  667. case the attribute `n` should be set to the matched value (NaN or Inf).
  668. The optional sign will be automatically applied afterwards.
  669. [heading `RealPolicies` Specializations]
  670. The easiest way to implement a proper real parsing policy is to derive a
  671. new type from the type `real_policies` while overriding the aspects
  672. of the parsing which need to be changed. For example, here's the
  673. implementation of the predefined `strict_real_policies`:
  674. template <typename T>
  675. struct strict_real_policies : real_policies<T>
  676. {
  677. static bool const expect_dot = true;
  678. };
  679. [heading Example]
  680. [note The test harness for the example(s) below is presented in the
  681. __qi_basics_examples__ section.]
  682. Some using declarations:
  683. [reference_using_declarations_real]
  684. Basic real number parsing:
  685. [reference_real]
  686. A custom real number policy:
  687. [reference_test_real_policy]
  688. And its use:
  689. [reference_custom_real]
  690. [endsect] [/ Real Numbers]
  691. [/------------------------------------------------------------------------------]
  692. [section:boolean Boolean Parser (`bool_`)]
  693. [heading Description]
  694. The `bool_parser` can parse booleans of arbitrary type, `B`. The boolean base
  695. type `T` can be a user defined boolean type as long as the type follows certain
  696. expression requirements (documented below).
  697. [heading Header]
  698. // forwards to <boost/spirit/home/qi/numeric/bool.hpp>
  699. #include <boost/spirit/include/qi_bool.hpp>
  700. Also, see __include_structure__.
  701. [heading Namespace]
  702. [table
  703. [[Name]]
  704. [[`boost::spirit::bool_ // alias: boost::spirit::qi::bool_`]]
  705. [[`boost::spirit::true_ // alias: boost::spirit::qi::true_`]]
  706. [[`boost::spirit::false_ // alias: boost::spirit::qi::false_`]]
  707. ]
  708. [heading Synopsis]
  709. template <typename T, typename BooleanPolicies>
  710. struct bool_parser;
  711. [heading Template parameters]
  712. [table
  713. [[Parameter] [Description] [Default]]
  714. [[`B`] [The boolean type of the
  715. boolean parser.] [`bool`]]
  716. [[`BooleanPolicies`] [Policies control the
  717. parser's behavior.] [`bool_policies<B>`]]
  718. ]
  719. [heading Model of]
  720. [:__primitive_parser_concept__]
  721. [variablelist Notation
  722. [[`BP`] [A boolean `Policies` (type).]]
  723. [[`b`] [An object of `B`, the numeric base type.]]
  724. [[`boolean`] [Numeric literal, any boolean value, or a
  725. __qi_lazy_argument__ that evaluates to a boolean value.]]
  726. [[`f`, `l`] [__fwditer__. first/last iterator pair.]]
  727. [[`attr`] [An attribute value.]]
  728. [[`Context`] [The type of the parse context of the current invocation of
  729. the `bool_` parser.]]
  730. [[`ctx`] [An instance of the parse context, `Context`.]]
  731. ]
  732. [heading Expression Semantics]
  733. Semantics of an expression is defined only where it differs from, or is
  734. not defined in __primitive_parser_concept__.
  735. [table
  736. [
  737. [Expression]
  738. [Semantics]
  739. ][
  740. [``
  741. bool_
  742. ``]
  743. [Parse a boolean using the default policies (`bool_policies<T>`).]
  744. ][
  745. [``
  746. lit(boolean)
  747. bool_(boolean)
  748. ``]
  749. [Match the literal `boolean` using the default policies (`bool_policies<T>`).
  750. The parser will fail if the parsed value is not equal to the specified
  751. value.]
  752. ][
  753. [``
  754. true_
  755. false_
  756. ``]
  757. [Match `"true"` and `"false"`, respectively.]
  758. ][
  759. [``
  760. bool_parser<
  761. T, BoolPolicies
  762. >()
  763. ``]
  764. [Parse a real of type `T` using `BoolPolicies`.]
  765. ][
  766. [``
  767. bool_parser<
  768. T, BoolPolicies
  769. >()(boolean)
  770. ``]
  771. [Match the literal `boolean` of type `T` using `BoolPolicies`. The parser will fail
  772. if the parsed value is not equal to the specified value.]
  773. ]
  774. ]
  775. [note All boolean parsers properly respect the __qi_no_case__`[]` directive.]
  776. [heading Attributes]
  777. [table
  778. [
  779. [Expression]
  780. [Attribute]
  781. ][
  782. [``
  783. lit(boolean)
  784. ``]
  785. [__unused__]
  786. ][
  787. [``
  788. true_
  789. false_
  790. bool_
  791. bool_(boolean)
  792. ``]
  793. [`bool`]
  794. ][
  795. [``
  796. bool_parser<
  797. T, BoolPolicies
  798. >()
  799. bool_parser<
  800. T, BoolPolicies
  801. >()(num)
  802. ``]
  803. [`T`]
  804. ]
  805. ]
  806. [heading Complexity]
  807. [:O(N), where N is the number of characters being parsed.]
  808. [heading Minimum Expression Requirements for `B`]
  809. The boolean type, `B`, the minimum expression requirements listed
  810. below must be valid. Take note that additional requirements may be
  811. imposed by custom policies.
  812. [table
  813. [[Expression] [Semantics]]
  814. [[`B(bool)`] [Constructible from a `bool`.]]
  815. ]
  816. [heading Boolean `Policies`]
  817. The boolean `Policies` template parameter is a class that groups all the
  818. policies that control the parser's behavior. Policies control the boolean
  819. parsers' behavior.
  820. The default is `bool_policies<bool>`. The default is provided to take care
  821. of the most common case (there are many ways to represent, and hence
  822. parse, boolean numbers). In most cases, the default policies are sufficient
  823. and can be used straight out of the box. They are designed to parse
  824. boolean value of the form `"true"` and `"false"`.
  825. [heading Boolean `Policies` Expression Requirements]
  826. For models of boolean `Policies` the following expressions must be valid:
  827. [table
  828. [[Expression] [Semantics]]
  829. [[`BP::parse_true(f, l, attr)`] [Parse a `true` value.]]
  830. [[`BP::parse_false(f, l, attr)`] [Parse a `false` value.]]
  831. ]
  832. The functions should return true if the required representations of `true` or
  833. `false` have been found. In this case the attribute `n` should be set to the
  834. matched value (`true` or `false`).
  835. [heading Boolean `Policies` Specializations]
  836. The easiest way to implement a proper boolean parsing policy is to derive a
  837. new type from the type `bool_policies` while overriding the aspects
  838. of the parsing which need to be changed. For example, here's the
  839. implementation of a boolean parsing policy interpreting the string `"eurt"`
  840. (i.e. "true" spelled backwards) as `false`:
  841. struct backwards_bool_policies : qi::bool_policies<>
  842. {
  843. // we want to interpret a 'true' spelled backwards as 'false'
  844. template <typename Iterator, typename Attribute>
  845. static bool
  846. parse_false(Iterator& first, Iterator const& last, Attribute& attr)
  847. {
  848. namespace qi = boost::spirit::qi;
  849. if (qi::detail::string_parse("eurt", first, last, qi::unused))
  850. {
  851. spirit::traits::assign_to(false, attr); // result is false
  852. return true;
  853. }
  854. return false;
  855. }
  856. };
  857. [heading Example]
  858. [note The test harness for the example(s) below is presented in the
  859. __qi_basics_examples__ section.]
  860. Some using declarations:
  861. [reference_using_declarations_bool]
  862. Basic real number parsing:
  863. [reference_bool]
  864. A custom real number policy:
  865. [reference_test_bool_policy]
  866. And its use:
  867. [reference_custom_bool]
  868. [endsect] [/ Real Numbers]
  869. [endsect]