Home | Libraries | People | FAQ | More |
The stream_parser
is
a primitive which allows to use pre-existing standard streaming operators
for input parsing integrated with Spirit.Qi. It
provides a wrapper parser dispatching the underlying input stream to
the stream operator of the corresponding attribute type to be parsed.
Any value a
to be parsed
using the stream_parser
will result in invoking the standard streaming operator for its type
A
, for instance:
std::istream& operator>> (std::istream&, A&);
// forwards to <boost/spirit/home/qi/stream.hpp> #include <boost/spirit/include/qi_stream.hpp>
Also, see Include Structure.
Name |
---|
|
|
template <typename Char, typename Attrib> struct stream_parser;
Parameter |
Description |
Default |
---|---|---|
|
The character type to use to generate the input. This type will be used while assigning the generated characters to the underlying input iterator. |
|
|
The type of the attribute the |
|
Notation
s
A variable instance of any type with a defined matching streaming
operator>>()
or a Lazy
Argument that evaluates to any type with a defined matching
streaming operator>>()
.
Semantics of an expression is defined only where it differs from, or
is not defined in PrimitiveParser
.
Expression |
Description |
---|---|
|
Call the streaming |
|
Call the streaming |
All parsers listed in the table above are predefined specializations
of the stream_parser<Char>
basic stream parser type described
below. It is possible to directly use this type to create stream parsers
using an arbitrary underlying character type.
Expression |
Semantics |
---|---|
stream_parser< Char, Attrib >()
|
Call the streaming |
All of the stream parsers listed above require the type of the value
to parse (the associated attribute) to implement a streaming operator
conforming to the usual I/O streams conventions (where attribute_type
is the type of the value
to recognize while parse):
template <typename Istream> Istream& operator>> (Istream& os, attribute_type& attr) { // type specific input parsing return os; }
This operator will be called by the stream parsers to gather the input
for the attribute of type attribute_type
.
Note | |
---|---|
If the |
Expression |
Attribute |
---|---|
|
|
|
|
|
|
Important | |
---|---|
The attribute type |
O(N), where N is the number of characters consumed by the stream parser
Note | |
---|---|
The test harness for the example(s) below is presented in the Basics Examples section. |
A class definition used in the examples:
// a simple complex number representation z = a + bi struct complex { complex (double a = 0.0, double b = 0.0) : a(a), b(b) {} double a; double b; };
// define streaming operator for the type complex std::istream& operator>> (std::istream& is, complex& z) { char lbrace = '\0', comma = '\0', rbrace = '\0'; is >> lbrace >> z.a >> comma >> z.b >> rbrace; if (lbrace != '{' || comma != ',' || rbrace != '}') is.setstate(std::ios_base::failbit); return is; }
Using declarations and variables:
using boost::spirit::qi::stream; using boost::spirit::qi::stream_parser;
Parse a simple string using the operator>>(istream&, std::string&);
std::string str; test_parser_attr("abc", stream, str); std::cout << str << std::endl; // prints: abc
Parse our complex type using the operator>>(istream&, complex&);
complex c; test_parser_attr("{1.0,2.5}", stream_parser<char, complex>(), c); std::cout << c.a << "," << c.b << std::endl; // prints: 1.0,2.5