1[/============================================================================== 2 Copyright (C) 2001-2011 Joel de Guzman 3 Copyright (C) 2001-2011 Hartmut Kaiser 4 Copyright (C) 2011 Bryce Lelbach 5 Copyright (C) 2016 Frank Hein, maxence business consulting gmbh 6 7 Distributed under the Boost Software License, Version 1.0. (See accompanying 8 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 9===============================================================================/] 10 11[section:directive Parser Directives] 12 13This module includes different directives usable to augment and parameterize 14other parsers. It includes the `no_case`, `lexeme`, `omit`, `raw`, `repeat`, 15`matches`, `no_skip`, `skip`, `hold`, `as<T>`, `as_string` and 16`as_wstring` directives. 17 18 19[heading Module Header] 20 21 // forwards to <boost/spirit/home/qi/directive.hpp> 22 #include <boost/spirit/include/qi_directive.hpp> 23 24Also, see __include_structure__. 25 26[/------------------------------------------------------------------------------] 27[section:lexeme Parser Directive Inhibiting Skipping (`lexeme[]`)] 28 29[heading Description] 30 31The `lexeme` directive makes its subject a primitive. In a logical point 32of view, lexemes (and primitives) are minimal atomic units (e.g. words, 33numbers, identifiers, etc). These are the things that you'd normally put 34in the lexer (hinting at the term "lexeme"), but in a lexer-less world, 35you put these in a lexeme. Seeing its subject as a primitive, the 36`lexeme` directive does an initial pre-skip (as all primitives do) and 37turns off white space skipping. 38 39At the phrase level, the parser ignores white spaces, possibly including 40comments. Use `lexeme` in situations where you want to work at the 41character level instead of the phrase level. Parsers can be made to work 42at the character level by enclosing the pertinent parts inside the 43`lexeme` directive. For example, here's a rule that parses integers: 44 45 integer = lexeme[ -(lit('+') | '-') >> +digit ]; 46 47The `lexeme` directive instructs its subject parser to work on the 48character level. Without it, the `integer` rule would have allowed 49erroneous embedded white spaces in inputs such as `"1 2 345"` which will 50be parsed as `"12345"`. 51 52[note Keep in mind that `lexeme[]` pre-skips spaces. If this is not 53desired, use the [qi_no_skip `no_skip`] directive instead.] 54 55[heading Header] 56 57 // forwards to <boost/spirit/home/qi/directive/lexeme.hpp> 58 #include <boost/spirit/include/qi_lexeme.hpp> 59 60Also, see __include_structure__. 61 62[heading Namespace] 63 64[table 65 [[Name]] 66 [[`boost::spirit::lexeme // alias: boost::spirit::qi::lexeme` ]] 67] 68 69[heading Model of] 70 71[:__unary_parser_concept__] 72 73[variablelist Notation 74 [[`a`] [A __parser_concept__.]] 75] 76 77[heading Expression Semantics] 78 79Semantics of an expression is defined only where it differs from, or is 80not defined in __unary_parser_concept__. 81 82[table 83 [[Expression] [Semantics]] 84 [[`lexeme[a]`] [Pre-skip and turn off white space skipping for the 85 subject parser, `a` (and all its children).]] 86] 87 88[heading Attributes] 89 90See __qi_comp_attr_notation__. 91 92[table 93 [[Expression] [Attribute]] 94 [[`lexeme[a]`] 95[``a: A --> lexeme[a]: A 96a: Unused --> lexeme[a]: Unused``]] 97] 98 99[heading Complexity] 100 101[:The complexity is defined by the complexity of the subject parser, `a`] 102 103[heading Example] 104 105[note The test harness for the example(s) below is presented in the 106__qi_basics_examples__ section.] 107 108Some using declarations: 109 110[reference_using_declarations_lexeme] 111 112Simple usage of `lexeme[]`: 113 114[reference_lexeme] 115 116[endsect] 117 118[/------------------------------------------------------------------------------] 119[section:no_skip Parser Directive Inhibiting Skipping Without Pre-skip (`no_skip[]`)] 120 121[heading Description] 122 123The `no_skip[]` directive turns off white space skipping. The difference to 124__qi_lexeme__ is that it does not do pre-skipping in any case. Otherwise it is 125completely equivalent to the __qi_lexeme__ directive. 126 127[heading Header] 128 129 // forwards to <boost/spirit/home/qi/directive/no_skip.hpp> 130 #include <boost/spirit/include/qi_no_skip.hpp> 131 132Also, see __include_structure__. 133 134[heading Namespace] 135 136[table 137 [[Name]] 138 [[`boost::spirit::no_skip // alias: boost::spirit::qi::no_skip` ]] 139] 140 141[heading Model of] 142 143[:__unary_parser_concept__] 144 145[variablelist Notation 146 [[`a`] [A __parser_concept__.]] 147] 148 149[heading Expression Semantics] 150 151Semantics of an expression is defined only where it differs from, or is 152not defined in __unary_parser_concept__. 153 154[table 155 [[Expression] [Semantics]] 156 [[`no_skip[a]`] [Turns off white space skipping for the 157 subject parser, `a` (and all its children). This 158 directive does not pre-skips.]] 159] 160 161[heading Attributes] 162 163See __qi_comp_attr_notation__. 164 165[table 166 [[Expression] [Attribute]] 167 [[`no_skip[a]`] 168[``a: A --> no_skip[a]: A 169a: Unused --> no_skip[a]: Unused``]] 170] 171 172[heading Complexity] 173 174[:The complexity is defined by the complexity of the subject parser, `a`] 175 176[heading Example] 177 178[note The test harness for the example(s) below is presented in the 179__qi_basics_examples__ section.] 180 181Some using declarations: 182 183[reference_using_declarations_no_skip] 184 185Simple usage of `no_skip[]`: 186 187[reference_no_skip] 188 189[endsect] 190 191[/------------------------------------------------------------------------------] 192[section:no_case Parser Directive Inhibiting Case Sensitivity (`no_case[]`)] 193 194[heading Description] 195 196The `no_case[]` directive does not consume any input. The actual 197matching is done by its subject parser. It's purpose is to force 198matching of the subject parser (and all its children) to be case 199insensitive. 200 201[heading Header] 202 203 // forwards to <boost/spirit/home/qi/directive/no_case.hpp> 204 #include <boost/spirit/include/qi_no_case.hpp> 205 206Also, see __include_structure__. 207 208[heading Namespace] 209 210[table 211 [[Name]] 212 [[`ns::no_case`]] 213] 214 215In the table above, `ns` represents a __char_encoding_namespace__. 216 217[heading Model of] 218 219The model of `no_case` is the model of its subject parser. 220 221[variablelist Notation 222 [[`a`] [A __parser_concept__.]] 223 [[`ns`] [A __char_encoding_namespace__.]] 224] 225 226[heading Expression Semantics] 227 228Semantics of an expression is defined only where it differs from, or is 229not defined in the subject's concept. 230 231[table 232 [[Expression] [Semantics]] 233 [[`ns::no_case[a]`] [Force matching of the subject parser, `a` 234 (and all its children) to be case insensitive]] 235] 236 237[heading Attributes] 238 239See __qi_comp_attr_notation__. 240 241[table 242 [[Expression] [Attribute]] 243 [[`ns::no_case[a]`] 244[``a: A --> ns::no_case[a]: A 245a: Unused --> ns::no_case[a]: Unused``]] 246] 247 248[heading Complexity] 249 250[:The complexity is defined by the complexity of the subject parser, `a`] 251 252[heading Example] 253 254[note The test harness for the example(s) below is presented in the 255__qi_basics_examples__ section.] 256 257Some using declarations: 258 259[reference_using_declarations_no_case] 260 261Simple usage of `no_case[]`: 262 263[reference_no_case] 264 265A more sophisticated use case of `no_case[]` in conjunction with a symbol 266table (see __qi_symbols__ for more details): 267 268[reference_symbols_with_no_case] 269 270[endsect] 271 272[/------------------------------------------------------------------------------] 273[section:omit Parser Directive Ignoring Attribute (`omit[]`)] 274 275[heading Description] 276 277The `omit[]` ignores the attribute of its subject parser replacing it 278with __unused__. 279 280[heading Header] 281 282 // forwards to <boost/spirit/home/qi/directive/omit.hpp> 283 #include <boost/spirit/include/qi_omit.hpp> 284 285Also, see __include_structure__. 286 287[heading Namespace] 288 289[table 290 [[Name]] 291 [[`boost::spirit::omit // alias: boost::spirit::qi::omit` ]] 292] 293 294[heading Model of] 295 296[:__unary_parser_concept__] 297 298[variablelist Notation 299 [[`a`] [A __parser_concept__.]] 300] 301 302[heading Expression Semantics] 303 304Semantics of an expression is defined only where it differs from, or is 305not defined in __unary_parser_concept__. 306 307[table 308 [[Expression] [Semantics]] 309 [[`omit[a]`] [Ignore the attribute of the subject parser, `a`]] 310] 311 312[heading Attributes] 313 314[table 315 [[Expression] [Attribute]] 316 [[`omit[a]`] [__unused_type__]] 317] 318 319[heading Complexity] 320 321[:The complexity is defined by the complexity of the subject parser, `a`] 322 323[heading Example] 324 325[note The test harness for the example(s) below is presented in the 326__qi_basics_examples__ section.] 327 328Some using declarations: 329 330[reference_using_declarations_omit] 331 332[reference_omit] 333 334[endsect] 335 336[/------------------------------------------------------------------------------] 337[section:raw Directive for Transduction Parsing (`raw[]`)] 338 339[heading Description] 340 341The `raw[]` disregards the attribute of its subject parser, instead 342exposing the half-open range `[first, last)` pointing to the matched 343characters from the input stream. The `raw[]` directive brings back the 344classic Spirit transduction (un-attributed) behavior for a subject 345parser. 346 347[heading Header] 348 349 // forwards to <boost/spirit/home/qi/directive/raw.hpp> 350 #include <boost/spirit/include/qi_raw.hpp> 351 352Also, see __include_structure__. 353 354[heading Namespace] 355 356[table 357 [[Name]] 358 [[`boost::spirit::raw // alias: boost::spirit::qi::raw` ]] 359] 360 361[heading Model of] 362 363[:__unary_parser_concept__] 364 365[variablelist Notation 366 [[`a`] [A __parser_concept__.]] 367 [[`Iter`] [A __fwditer__ type.]] 368] 369 370[heading Expression Semantics] 371 372Semantics of an expression is defined only where it differs from, or is 373not defined in __unary_parser_concept__. 374 375[table 376 [[Expression] [Semantics]] 377 [[`raw[a]`] [Disregard the attribute of the subject parser, `a`. 378 Expose instead the half-open range `[first, last)` 379 pointing to the matched characters from the input stream.]] 380] 381 382[heading Attributes] 383 384See __qi_comp_attr_notation__. 385 386[table 387 [[Expression] [Attribute]] 388 [[`raw[a]`] 389[``a: A --> raw[a]: boost::iterator_range<Iter> 390a: Unused --> raw[a]: Unused``]] 391] 392 393[note See __boost_iterator_range__.] 394 395[heading Complexity] 396 397[:The complexity is defined by the complexity of the subject parser, `a`] 398 399[heading Example] 400 401[note The test harness for the example(s) below is presented in the 402__qi_basics_examples__ section.] 403 404Some using declarations: 405 406[reference_using_declarations_raw] 407 408[reference_raw] 409 410[endsect] 411 412[/------------------------------------------------------------------------------] 413[section:repeat Repetition Parser Directive (`repeat[]`)] 414 415[heading Description] 416 417The `repeat[]` provides a more powerful and flexible mechanism for 418repeating a parser. There are grammars that are impractical and 419cumbersome, if not impossible, for the basic EBNF iteration syntax 420(__qi_kleene__ and the __qi_plus__) to specify. Examples: 421 422* A file name may have a maximum of 255 characters only. 423* A specific bitmap file format has exactly 4096 RGB color information. 424* A 256 bit binary string (1..256 1s or 0s). 425 426[heading Header] 427 428 // forwards to <boost/spirit/home/qi/directive/repeat.hpp> 429 #include <boost/spirit/include/qi_repeat.hpp> 430 431Also, see __include_structure__. 432 433[heading Namespace] 434 435[table 436 [[Name]] 437 [[`boost::spirit::repeat // alias: boost::spirit::qi::repeat` ]] 438 [[`boost::spirit::inf // alias: boost::spirit::qi::inf` ]] 439] 440 441[heading Model of] 442 443[:__unary_parser_concept__] 444 445[variablelist Notation 446 [[`a`] [A __parser_concept__.]] 447 [[`n`, `min`, `max`] [An `int` anything that can be converted to an 448 `int`, or a __qi_lazy_argument__ that evaluates to 449 anything that can be converted to an `int`.]] 450] 451 452[heading Expression Semantics] 453 454Semantics of an expression is defined only where it differs from, or is 455not defined in __unary_parser_concept__. 456 457[table 458 [[Expression] [Semantics]] 459 [[`repeat[a]`] [Repeat `a` zero or more times. Same as __qi_kleene__.]] 460 [[`repeat(n)[a]`] [Repeat `a` exactly `n` times.]] 461 [[`repeat(min, max)[a]`] [Repeat `a` at least `min` times and at most `max` times.]] 462 [[`repeat(min, inf)[a]`] [Repeat `a` at least `min` or more (continuing until `a` 463 fails or the input is consumed).]] 464] 465 466[heading Attributes] 467 468See __qi_comp_attr_notation__. 469 470[table 471 [[Expression] [Attribute]] 472 [[`repeat[a]`] 473[``a: A --> repeat[a]: vector<A> 474a: Unused --> repeat[a]: Unused``]] 475 [[`repeat(n)[a]`] 476[``a: A --> repeat(n)[a]: vector<A> 477a: Unused --> repeat(n)[a]: Unused``]] 478 [[`repeat(min, max)[a]`] 479[``a: A --> repeat(min, max)[a]: vector<A> 480a: Unused --> repeat(min, max)[a]: Unused``]] 481 [[`repeat(min, inf)[a]`] 482[``a: A --> repeat(min, inf)[a]: vector<A> 483a: Unused --> repeat(min, inf)[a]: Unused``]] 484] 485 486[heading Complexity] 487 488[:The overall complexity is defined by the complexity of its subject 489parser. The complexity of `repeat` itself is O(N), where N is the number 490of repetitions to execute.] 491 492[heading Example] 493 494[note The test harness for the example(s) below is presented in the 495__qi_basics_examples__ section.] 496 497Using the repeat directive, we can now write our examples above. 498 499Some using declarations: 500 501[reference_using_declarations_repeat] 502 503[reference_repeat] 504 505The Loop parsers can be dynamic. Consider the parsing of a binary file 506of Pascal-style length prefixed string, where the first byte determines 507the length of the incoming string. Here's a sample input: 508 509[:__pascal_string__] 510 511[reference_repeat_pascal] 512 513[endsect] 514 515[/------------------------------------------------------------------------------] 516[section:matches Directive Testing if Parser Succeeded (`matches[]`)] 517 518[heading Description] 519 520The `matches[]` directive executes the embedded parser and returns whether it 521succeeded matching. 522 523[heading Header] 524 525 // forwards to <boost/spirit/home/qi/directive/matches.hpp> 526 #include <boost/spirit/include/qi_matches.hpp> 527 528Also, see __include_structure__. 529 530[heading Namespace] 531 532[table 533 [[Name]] 534 [[`boost::spirit::matches // alias: boost::spirit::qi::matches` ]] 535] 536 537[heading Model of] 538 539[:__unary_parser_concept__] 540 541[variablelist Notation 542 [[`a`] [A __parser_concept__.]] 543] 544 545[heading Expression Semantics] 546 547Semantics of an expression is defined only where it differs from, or is 548not defined in __unary_parser_concept__. 549 550[table 551 [[Expression] [Semantics]] 552 [[`matches[a]`] [Execute the subject parser `a`, and return as its 553 attribute whether it succeeded. The directive itself 554 does always succeed.]] 555] 556 557[heading Attributes] 558 559[table 560 [[Expression] [Attribute]] 561 [[`matches[a]`] [`bool`]] 562] 563 564[heading Complexity] 565 566[:The complexity is defined by the complexity of the subject parser, `a`] 567 568[heading Example] 569 570[note The test harness for the example(s) below is presented in the 571__qi_basics_examples__ section.] 572 573Some using declarations: 574 575[reference_using_declarations_matches] 576 577[reference_matches] 578 579[endsect] 580 581[/------------------------------------------------------------------------------] 582[section:skip Parser Directive Re-Establishing Skipping (`skip[]`)] 583 584[heading Description] 585 586The `skip` directive is the inverse of __qi_lexeme__ or [qi_no_skip `no_skip`]. 587While the __qi_lexeme__ directive turns off white space 588skipping, the `skip` directive turns it on again. This is simply done by 589wrapping the parts inside the `skip` directive: 590 591 skip[a] 592 593It is also possible to supply a skip parser to the `skip` directive: 594 595 skip(p)[a] // Use `p` as a skipper for parsing `a` 596 597This makes it possible to: 598 599* Perform localized phrase level parsing while doing character level parsing. 600* Replace the current skipper anywhere with an entirely different 601 skipper while doing phrase level parsing. 602 603[heading Header] 604 605 // forwards to <boost/spirit/home/qi/directive/skip.hpp> 606 #include <boost/spirit/include/qi_skip.hpp> 607 608Also, see __include_structure__. 609 610[heading Namespace] 611 612[table 613 [[Name]] 614 [[`boost::spirit::skip // alias: boost::spirit::qi::skip` ]] 615] 616 617[heading Model of] 618 619[:__unary_parser_concept__] 620 621[variablelist Notation 622 [[`a`] [A __parser_concept__.]] 623] 624 625[heading Expression Semantics] 626 627Semantics of an expression is defined only where it differs from, or is 628not defined in __unary_parser_concept__. 629 630[table 631 [[Expression] [Semantics]] 632 633 [[`skip[a]`] [Re-establish the skipper that got inhibited by lexeme or no_skip]] 634 [[`skip(p)[a]`] [Use `p` as a skipper for parsing `a`]] 635] 636 637[heading Attributes] 638 639See __qi_comp_attr_notation__. 640 641[table 642 [[Expression] [Attribute]] 643 [[`skip[a]`] 644[``a: A --> skip[a]: A 645a: Unused --> skip[a]: Unused``]] 646 [[`skip(p)[a]`] 647[``a: A --> skip(p)[a]: A 648a: Unused --> skip(p)[a]: Unused``]] 649] 650 651[heading Complexity] 652 653[:The complexity is defined by the complexity of the subject parser, `a`] 654 655[heading Example] 656 657[note The test harness for the example(s) below is presented in the 658__qi_basics_examples__ section.] 659 660Some using declarations: 661 662[reference_using_declarations_skip] 663 664Simple usage of `skip[]`: 665 666[reference_skip] 667 668[endsect] 669 670[/------------------------------------------------------------------------------] 671[section:hold Parser Directive for Attribute Commit/Rollback (`hold[]`)] 672 673[heading Description] 674 675The `hold[]` directive helps managing attributes, mainly for alternative 676parsers. It instantiates a new attribute instance for the embedded parser. The 677value of that attribute instance is copied to the outer attribute if the 678embedded parser succeeds and it is discarded otherwise. Alternative parsers 679normally do not rollback changes made to the outer attribute by an failed 680alternative. Wrapping those alternatives into a `hold[]` directive ensures that 681only the succeeding alternative gets to modify the attribute. 682 683[heading Header] 684 685 // forwards to <boost/spirit/home/qi/directive/hold.hpp> 686 #include <boost/spirit/include/qi_hold.hpp> 687 688Also, see __include_structure__. 689 690[heading Namespace] 691 692[table 693 [[Name]] 694 [[`boost::spirit::hold // alias: boost::spirit::qi::hold` ]] 695] 696 697[heading Model of] 698 699[:__unary_parser_concept__] 700 701[variablelist Notation 702 [[`a`] [A __parser_concept__.]] 703] 704 705[heading Expression Semantics] 706 707Semantics of an expression is defined only where it differs from, or is 708not defined in __unary_parser_concept__. 709 710[table 711 [[Expression] [Semantics]] 712 713 [[`hold[a]`] [Create a new attribute instance while parsing `a`, 714 copying the result to the outer attribute only after 715 `a` succeeds.]] 716] 717 718[heading Attributes] 719 720See __qi_comp_attr_notation__. 721 722[table 723 [[Expression] [Attribute]] 724 [[`hold[a]`] 725[``a: A --> hold[a]: A 726a: Unused --> hold[a]: Unused``]] 727] 728 729[note The `hold[]` directive uses `swap()` to implement the rollback/commit 730 semantics for the attribute. For this reason the attribute type needs to 731 to be usable with `boost::swap` (needs to either define a proper overload 732 for `swap(attribute_type&, attribute_type&)` or expose a member function 733 `attribute_type::swap(attribute_type&)`.] 734 735[heading Complexity] 736 737[:The complexity is defined by the complexity of the subject parser, `a`] 738 739[heading Example] 740 741[note The test harness for the example(s) below is presented in the 742__qi_basics_examples__ section.] 743 744Some using declarations: 745 746[reference_using_declarations_hold] 747 748[reference_hold] 749 750[endsect] 751 752[/------------------------------------------------------------------------------] 753[section:as Parser Directives Forcing Atomic Assignment (`as<T>, as_string[], as_wstring[]`)] 754 755[heading Description] 756 757The `as<T>` class forces the atomic assignment of it's subject's synthesized 758attribute. Usually, repetitive parsers (such as __qi_kleene__, etc) or 759sequences exposing a `vector<A>` will assign elements to the container supplied 760as their synthesized attribute by calling __customize_push_back_container__ 761repeatedly. In some cases, this may be undesirable. The `as<T>` class creates a 762directive that will pass a temporary object of type `T` to it's subject. If the 763subject parser passes, the temporary object will be assigned to the directive's 764supplied attribute with a single call to __customize_assign_to__. If the 765subject parser fails, the directive's attribute is not mutated. 766 767[note `T` is required to be a container type. If __customize_is_container__ 768does not return true for `T`, a compile-time error will occur.] 769 770[note The `as<T>` implicitly causes commit/rollback semantics 771similar in nature to the __qi_hold__ directive.] 772 773[caution The __customize_assign_to__ customization point may end up 774using __customize_push_back_container__ to assign the temporary object to the 775supplied attribute by default, depending on the types involved. Use the 776interface described in __sec_customization_points__ to manipulate the semantics 777of this assignment operation.] 778 779[heading Header] 780 781 // forwards to <boost/spirit/home/qi/directive/as.hpp> 782 #include <boost/spirit/include/qi_as.hpp> 783 784Also, see __include_structure__. 785 786[heading Namespace] 787 788[table 789 [[Name]] 790 [[`boost::spirit::as // alias: boost::spirit::qi::as` ]] 791 [[`boost::spirit::as_string // alias: boost::spirit::qi::as_string` ]] 792 [[`boost::spirit::as_wstring // alias: boost::spirit::qi::as_wstring` ]] 793] 794 795[heading Synopsis] 796 797 template <typename T> 798 struct as; 799 800[heading Template parameters] 801 802[table 803 [[Parameter] [Description] [Default]] 804 [[`T`] [A container type. [none]] 805] 806 807[heading Model of] 808 809[:__unary_parser_concept__] 810 811[variablelist Notation 812 [[`a`] [A __parser_concept__.]] 813 [[`t`] [A container of type `T`.]] 814 [[`attr`] [The attribute supplied to the directive.]] 815] 816 817[heading Expression Semantics] 818 819Semantics of an expression is defined only where it differs from, or is 820not defined in __unary_parser_concept__. 821 822[table 823 [[Expression] [Semantics]] 824 [[`as<T>()[a]`] [Create a temporary object of `t` of type `T`, 825 and invoke the subject parser `a`, supplying 826 `t` as an attribute. If the subject parser 827 passes, assign `t` to `attr`.]] 828 [[`as_string[a]`] [Equivalent to `as<std::string>()[a]`]] 829 [[`as_wstring[a]`] [Equivalent to `as<std::wstring>()[a]`]] 830] 831 832[heading Attributes] 833 834See __qi_comp_attr_notation__. 835 836[table 837 [[Expression] [Attribute]] 838 [[`as<T>()[a]`] [`a: A --> as<T>()[a]: T`]] 839] 840 841[heading Complexity] 842 843[:The complexity is defined by the complexity of the subject parser, `a`, and 844the complexity of the assignment of the container `t` to the supplied 845attribute `attr`.] 846 847[heading Example] 848 849[note The test harness for the example(s) below is presented in the 850__qi_basics_examples__ section.] 851 852Some using declarations: 853 854[reference_using_declarations_as] 855 856Simple usage of `as<T>`, `as_string` and `as_wstring`: 857 858[reference_as] 859 860[endsect] 861[/------------------------------------------------------------------------------] 862[section:expect Expectation Directive (`expect[]`)] 863 864[heading Description] 865 866There are occasions in which it is expected that the input must match a 867particular parser or the input is invalid. Such cases generally arise 868after matching a portion of a grammar, such that the context is fully 869known. In such a situation, failure to match should result in an 870exception. For example, when parsing an e-mail address, a name, an "@" 871and a domain name must be matched or the address is invalid. 872 873The expect directive requires that the argument parser matches 874the input or an exception is emitted. Using on_error(), that exception 875can be handled by calling a handler with the context at which the 876parsing failed can be reported. 877 878The expect directive parses an operand parser expression which may be a 879single parser or a complex parser expression like a sequence. 880 881Single parser: 882 883 expect[a] 884 885Parser expression: 886 887 expect[a >> b >> ...] 888 889In the latter case while the plain __qi_sequence__ simply returns a no-match 890(returns `false`) when one of the elements fail, the expect directive 891throws an __qi_expectation_failure__`<Iter>` if any of the parsers 892(even the first parser of a sequence) fails to match. 893 894[note Spirit provides two ways to handle expectation failures by throwing an expectation exception. 895Use the __qi_expect__ if you do not need an exception to be thrown when the first parser of a sequence fails. ] 896 897[heading Header] 898 899 // forwards to <boost/spirit/home/qi/directive/expect.hpp> 900 #include <boost/spirit/include/qi_expect.hpp> 901 902Also, see __include_structure__. 903 904[heading Model of] 905 906[:__unary_parser_concept__] 907 908[variablelist Notation 909 [[`a`] [A __parser_concept__]] 910 [[`Iter`] [A __fwditer__ type]] 911] 912 913[heading Expectation Failure] 914 915When the operand parser fails to match an `expectation_failure<Iter>` is thrown: 916 917 template <typename Iter> 918 struct expectation_failure : std::runtime_error 919 { 920 Iter first; // [first, last) iterator pointing 921 Iter last; // to the error position in the input. 922 __info__ what_; // Information about the nature of the error. 923 }; 924 925[heading Expression Semantics] 926 927Semantics of an expression is defined only where it differs from, or is not 928defined in __unary_parser_concept__. 929 930[table 931 [[Expression] [Semantics]] 932 [[`expect[a]`] [Match `a`. If `a` fails, throw an `expectation_failure<Iter>`]] 933] 934 935[heading Attributes] 936 937See __qi_comp_attr_notation__. 938 939[table 940 [[Expression] [Attribute]] 941 [[`expect[a]`] 942[``a: A --> expect[a]: A 943a: Unused --> expect[a] : Unused``]] 944] 945 946[heading Complexity] 947 948[:The overall complexity of the expectation parser is defined by the 949complexity of it's argument parser. The complexity of the expect directive itself is O(1).] 950 951[heading Example] 952 953[note The test harness for the example(s) below is presented in the 954__qi_basics_examples__ section.] 955 956Some using declarations: 957 958[reference_using_declarations_expectd] 959 960[reference_expectd] 961 962[endsect] 963 964[endsect] 965