The function parse
implements the logic for parsing
strings according to grammar rules.
A grammar rule type, henceforth called a "rule", provides an algorithm for parsing an input string. An instance of the rule is used to store the results.
Code |
Output |
---|---|
// VFALCO we should not show this example /* urls::string_view s = "http:after_scheme"; const char* it = s.begin(); auto rv = urls::grammar::parse(it, s.end(), urls::scheme_rule() ); if( ! rv ) { std::cout << "scheme: " << rv->scheme << '\n'; std::cout << "suffix: " << it << '\n'; } */ |
scheme: http suffix: :after_scheme |
In this example, the function parse
returns true
if the specified range of characters begins with
a scheme. When the operation completes successfully, the rule instance holds
the results.
The iterator is updated to the position where the rule ended, leaving the suffix at the range between the new iterator and the old end iterator. This behavior is useful when parsing a sequence of rules.
Code |
Output |
---|---|
// VFALCO This needs refactoring /* urls::string_view s = "?key=value#anchor"; const char* it = s.begin(); urls::error_code ec; if (urls::grammar::parse(it, s.end(), ec, r1)) { auto r2 = urls::grammar::parse( it, s.end(), urls::fragment_part_rule ); if( r2 ) { std::cout << "query: " << r1.query_part << '\n'; std::cout << "fragment: " << std::get<1>(*r2.value()).encoded() << '\n'; } } */ |
query: ?key=value fragment: anchor |
Parsing a sequence of rules is such a common pattern that a special overload is provided:
Code |
Output |
---|---|
// VFALCO This needs refactoring /* urls::string_view s = "?key=value#anchor"; urls::query_part_rule r1; const char* it = s.begin(); urls::error_code ec; auto r2 = urls::grammar::parse( it, s.end(), ec, urls::fragment_part_rule ); if( ! ec.failed() ) { std::cout << "query: " << r1.query_part << '\n'; std::cout << "fragment: " << r2.fragment.encoded() << '\n'; } */ |
query: ?key=value fragment: anchor |
If all the logic has been represented in a single rule, we often want to parse a complete string as a rule.
Code |
Output |
---|---|
/* VFALCO This will be removed
urls::string_view s = "http://www.boost.org";
urls::uri_rule r;
urls::error_code ec;
if (urls::grammar::parse_string(s, ec, r))
{
std::cout << "scheme: " << r.scheme_part.scheme << '\n';
std::cout << "host: " << r.hier_part.authority.host.host_part << '\n';
}
*/
|
scheme: http host: www.boost.org |