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