Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

Qi Seek Parser Directive

Description

The seek[] parser-directive skips all input until the subject parser matches. On failure, the current position is the end of the input.

Header
// forwards to <boost/spirit/repository/home/qi/directive/seek.hpp>
#include <boost/spirit/repository/include/qi_seek.hpp>

Also, see Include Structure.

Namespace

Name

boost::spirit::repository::qi::seek

Model of

UnaryParser

Notation

a

A Parser.

Expression Semantics

Semantics of an expression is defined only where it differs from, or is not defined in UnaryParser.

Expression

Semantics

seek[a]

Advances until the parser a matches.

Attributes

See Compound Attribute Notation.

Expression

Attribute

seek[a]

a: A --> seek[a]: A
a: Unused --> seek[a]: Unused

Complexity

The overall complexity is defined by the complexity of its subject parser. The complexity of seek itself is O(N), where N is the number of unsuccessful matches.

[Note] Note

seeking sequence with skipping

Using seek[a >> b] with skipping is inefficient, because when sequence fails, the backtracked position is non-skipped. The solution is to ensure the input will always be pre-skipped, for example:

seek[lexeme[skip[a >> b]]]

does the trick.

Example

The following example shows a simple use case of the seek[] directive, parsing C-style comment. (For the full source of the example, see seek.cpp)

Some namespace aliases:

namespace qi = boost::spirit::qi;
namespace repo = boost::spirit::repository;

The input string and its iterators:

std::string str("/*C-style comment*/");
iterator it = str.begin();
iterator end = str.end();

Parsing and showing the result:

if (qi::parse(it, end, "/*" >> repo::qi::seek["*/"]))
{
    std::cout << "-------------------------------- \n";
    std::cout << "Parsing succeeded.\n";
    std::cout << "---------------------------------\n";
}
else
{
    std::cout << "-------------------------------- \n";
    std::cout << "Unterminated /* comment.\n";
    std::cout << "-------------------------------- \n";
}


PrevUpHomeNext