stream.qbk 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. [/==============================================================================
  2. Copyright (C) 2001-2011 Hartmut Kaiser
  3. Copyright (C) 2001-2011 Joel de Guzman
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ===============================================================================/]
  7. [section:stream Stream Parsers]
  8. This module includes the description of the different variants of the `stream`
  9. parser. It can be used to utilize existing streaming operators
  10. (`operator>>(std::istream&, ...)`) for input parsing.
  11. [heading Header]
  12. // forwards to <boost/spirit/home/qi/stream.hpp>
  13. #include <boost/spirit/include/qi_stream.hpp>
  14. Also, see __include_structure__.
  15. [section:stream Stream Parsers (`stream`, `wstream`, etc.)]
  16. [heading Description]
  17. The `stream_parser` is a primitive which allows to use pre-existing standard
  18. streaming operators for input parsing integrated with __qi__. It
  19. provides a wrapper parser dispatching the underlying input stream to the stream
  20. operator of the corresponding attribute type to be parsed. Any value `a` to be
  21. parsed using the `stream_parser` will result in invoking the standard streaming
  22. operator for its type `A`, for instance:
  23. std::istream& operator>> (std::istream&, A&);
  24. [heading Header]
  25. // forwards to <boost/spirit/home/qi/stream.hpp>
  26. #include <boost/spirit/include/qi_stream.hpp>
  27. Also, see __include_structure__.
  28. [heading Namespace]
  29. [table
  30. [[Name]]
  31. [[`boost::spirit::stream // alias: boost::spirit::qi::stream`]]
  32. [[`boost::spirit::wstream // alias: boost::spirit::qi::wstream`]]
  33. ]
  34. [heading Synopsis]
  35. template <typename Char, typename Attrib>
  36. struct stream_parser;
  37. [heading Template parameters]
  38. [table
  39. [[Parameter] [Description] [Default]]
  40. [[`Char`] [The character type to use to generate
  41. the input. This type will be used while
  42. assigning the generated characters to the
  43. underlying input iterator.] [`char`]]
  44. [[`Attrib`] [The type of the attribute the `stream_parser` is
  45. expected to parse its input into.] [`spirit::basic_hold_any<Char>`]]
  46. ]
  47. [heading Model of]
  48. [:__primitive_parser_concept__]
  49. [variablelist Notation
  50. [[`s`] [A variable instance of any type with a defined matching
  51. streaming `operator>>()` or a __qi_lazy_argument__ that
  52. evaluates to any type with a defined matching streaming
  53. `operator>>()`.]]
  54. ]
  55. [heading Expression Semantics]
  56. Semantics of an expression is defined only where it differs from, or is
  57. not defined in __primitive_parser_concept__.
  58. [table
  59. [[Expression] [Description]]
  60. [[`stream`] [Call the streaming `operator>>()` for the type
  61. of the mandatory attribute. The input recognized
  62. by this operator will be the result of the
  63. `stream` parser. This parser never fails
  64. (unless the underlying input stream reports an
  65. error). The character type of the I/O istream
  66. is assumed to be `char`.]]
  67. [[`wstream`] [Call the streaming `operator>>()` for the type
  68. of the mandatory attribute. The input recognized
  69. by this operator will be the result of the
  70. `wstream` parser. This parser never fails
  71. (unless the underlying input stream reports an
  72. error). The character type of the I/O istream
  73. is assumed to be `wchar_t`.]]
  74. ]
  75. All parsers listed in the table above are predefined specializations of the
  76. `stream_parser<Char>` basic stream parser type described below. It is
  77. possible to directly use this type to create stream parsers using an
  78. arbitrary underlying character type.
  79. [table
  80. [[Expression] [Semantics]]
  81. [
  82. [``stream_parser<
  83. Char, Attrib
  84. >()``] [Call the streaming `operator>>()` for the type
  85. of the optional attribute, `Attrib`. The input recognized
  86. by this operator will be the result of the
  87. `stream_parser<>` parser. This parser never fails
  88. (unless the underlying input stream reports an
  89. error). The character type of the I/O istream
  90. is assumed to be `Char`]]
  91. ]
  92. [heading Additional Requirements]
  93. All of the stream parsers listed above require the type of the value to
  94. parse (the associated attribute) to implement a streaming operator conforming
  95. to the usual I/O streams conventions (where `attribute_type` is the type of the
  96. value to recognize while parse):
  97. template <typename Istream>
  98. Istream& operator>> (Istream& os, attribute_type& attr)
  99. {
  100. // type specific input parsing
  101. return os;
  102. }
  103. This operator will be called by the stream parsers to gather the input for
  104. the attribute of type `attribute_type`.
  105. [note If the `stream` parser is invoked inside a [qi_match `match`]
  106. (or [qi_match `phrase_match`]) stream manipulator the `Istream`
  107. passed to the `operator>>()` will have registered (imbued) the same
  108. standard locale instance as the stream the [qi_match `match`] (or
  109. [qi_match `phrase_match`]) manipulator has been used with.
  110. This ensures all facets registered (imbued) with the original I/O
  111. stream object are used during input parsing.
  112. ]
  113. [heading Attributes]
  114. [table
  115. [[Expression] [Attribute]]
  116. [[`stream`] [`spirit::hold_any`]]
  117. [[`wstream`] [`spirit::whold_any`]]
  118. [[`stream_parser<Char, Attrib>()`] [`Attrib`]]
  119. ]
  120. [important The attribute type `spirit::hold_any` exposed by some of the stream
  121. parsers is semantically and syntactically equivalent to
  122. the type implemented by __boost_any__. It has been added to /Spirit/
  123. as it has better performance and a smaller footprint than
  124. __boost_any__.
  125. ]
  126. [heading Complexity]
  127. [:O(N), where N is the number of characters consumed by the stream parser]
  128. [heading Example]
  129. [note The test harness for the example(s) below is presented in the
  130. __qi_basics_examples__ section.]
  131. A class definition used in the examples:
  132. [reference_qi_complex]
  133. [reference_qi_stream_complex]
  134. [reference_qi_stream]
  135. [endsect]
  136. [endsect]