...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
The hold[]
directive helps managing attributes, mainly for alternative parsers.
It instantiates a new attribute instance for the embedded parser. The
value of that attribute instance is copied to the outer attribute if
the embedded parser succeeds and it is discarded otherwise. Alternative
parsers normally do not rollback changes made to the outer attribute
by an failed alternative. Wrapping those alternatives into a hold[]
directive ensures that only the succeeding alternative gets to modify
the attribute.
// forwards to <boost/spirit/home/qi/directive/hold.hpp> #include <boost/spirit/include/qi_hold.hpp>
Also, see Include Structure.
Name |
---|
|
Notation
a
A Parser
.
Semantics of an expression is defined only where it differs from, or
is not defined in UnaryParser
.
Expression |
Semantics |
---|---|
|
Create a new attribute instance while parsing |
See Compound Attribute Notation.
Expression |
Attribute |
---|---|
|
a: A --> hold[a]: A a: Unused --> hold[a]: Unused
|
Note | |
---|---|
The |
The complexity is defined by the complexity of the subject parser,
a
Note | |
---|---|
The test harness for the example(s) below is presented in the Basics Examples section. |
Some using declarations:
using boost::spirit::qi::hold; using boost::spirit::qi::int_; using boost::spirit::qi::attr;
The use of hold[]
here will make sure the changes to the attribute caused by the (failing)
first alternative will not be visible after the whole parsing succeeded.
std::vector<int> v; test_phrase_parser_attr("123", hold[int_ >> ':' >> int_] | int_ >> attr(0), v); std::cout << v[0] << "," << v[1] << std::endl; // will output: >123,0<