...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
The rule is a polymorphic parser that acts as a named placeholder capturing the behavior of a Parsing Expression Grammar expression assigned to it. Naming a Parsing Expression Grammar expression allows it to be referenced later and makes it possible for the rule to call itself. This is one of the most important mechanisms and the reason behind the word "recursive" in recursive descent parsing.
// forwards to <boost/spirit/home/qi/nonterminal/rule.hpp> #include <boost/spirit/include/qi_rule.hpp>
Also, see Include Structure.
Name |
---|
|
template <typename Iterator, typename A1, typename A2, typename A3> struct rule;
Parameter |
Description |
Default |
---|---|---|
|
The underlying iterator type that the rule is expected to work on. |
none |
|
Either |
See table below. |
Here is more information about the template parameters:
Parameter |
Description |
Default |
---|---|---|
|
Specifies the rule's synthesized (return value) and inherited
attributes (arguments). More on this here: |
|
|
Specifies the rule's skipper parser. Specify this if you want the rule to skip white spaces. If this is not specified, the rule is an "implicit lexeme" and will not skip spaces. "implicit lexeme" rules can still be called with a skipper. In such a case, the rule does a pre-skip just as all lexemes and primitives do. |
|
|
Specifies the rule's local variables. See |
|
Notation
r,
r2
Rules
p
A parser expression
Iterator
The underlying iterator type that the rule is expected to work on.
A1
, A2
, A3
Either Signature
,
Skipper
or Locals
in any order.
Semantics of an expression is defined only where it differs from, or
is not defined in Nonterminal
.
Expression |
Description |
---|---|
rule<Iterator, A1, A2, A3> r(name);
|
Rule declaration. |
rule<Iterator, A1, A2, A3> r(r2);
|
Copy construct rule |
|
Assign rule |
|
return an alias of |
|
Get a copy of |
|
Rule definition. This is equivalent to |
|
Auto-rule definition. The attribute of |
|
Retrieve the current name of the rule object. |
|
Set the current name of the rule object to be |
The parser attribute of the rule is
T
, its synthesized attribute. SeeAttribute
The complexity is defined by the complexity of the RHS parser,
p
Note | |
---|---|
The test harness for the example(s) below is presented in the Basics Examples section. |
Some using declarations:
using boost::spirit::qi::rule; using boost::spirit::qi::int_; using boost::spirit::qi::locals; using boost::spirit::qi::_1; using boost::spirit::qi::_a; using boost::spirit::ascii::alpha; using boost::spirit::ascii::char_; using boost::spirit::ascii::space_type;
Basic rule:
rule<char const*> r; r = int_; test_parser("123", r);
Rule with synthesized attribute:
rule<char const*, int()> ra; ra = int_; int i; test_parser_attr("123", ra, i); assert(i == 123);
Rule with skipper and synthesized attribute:
rule<char const*, std::vector<int>(), space_type> rs; rs = *int_; std::vector<int> v; test_phrase_parser_attr("123 456 789", rs, v); assert(v[0] == 123); assert(v[1] == 456); assert(v[2] == 789);
Rule with one local variable:
rule<char const*, locals<char> > rl; rl = alpha[_a = _1] >> char_(_a); // get two identical characters test_parser("aa", rl); // pass test_parser("ax", rl); // fail