| 1 | /*============================================================================= |
| 2 | Copyright (c) 2001-2011 Hartmut Kaiser |
| 3 | |
| 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 | #if !defined(BOOST_SPIRIT_STREAM_MAY_05_2007_1228PM) |
| 8 | #define BOOST_SPIRIT_STREAM_MAY_05_2007_1228PM |
| 9 | |
| 10 | #if defined(_MSC_VER) |
| 11 | #pragma once |
| 12 | #endif |
| 13 | |
| 14 | #include <boost/spirit/home/qi/detail/string_parse.hpp> |
| 15 | #include <boost/spirit/home/qi/stream/detail/match_manip.hpp> |
| 16 | #include <boost/spirit/home/support/detail/hold_any.hpp> |
| 17 | #include <boost/proto/traits.hpp> |
| 18 | #include <istream> |
| 19 | |
| 20 | /////////////////////////////////////////////////////////////////////////////// |
| 21 | namespace boost { namespace spirit |
| 22 | { |
| 23 | /////////////////////////////////////////////////////////////////////////// |
| 24 | // Enablers |
| 25 | /////////////////////////////////////////////////////////////////////////// |
| 26 | template <> |
| 27 | struct use_terminal<qi::domain, tag::stream> // enables stream |
| 28 | : mpl::true_ {}; |
| 29 | |
| 30 | template <> |
| 31 | struct use_terminal<qi::domain, tag::wstream> // enables wstream |
| 32 | : mpl::true_ {}; |
| 33 | }} |
| 34 | |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | namespace boost { namespace spirit { namespace qi |
| 37 | { |
| 38 | #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS |
| 39 | using spirit::stream; |
| 40 | using spirit::wstream; |
| 41 | #endif |
| 42 | using spirit::stream_type; |
| 43 | using spirit::wstream_type; |
| 44 | |
| 45 | namespace detail |
| 46 | { |
| 47 | #ifdef _MSC_VER |
| 48 | # pragma warning(push) |
| 49 | # pragma warning(disable: 4512) // assignment operator could not be generated. |
| 50 | #endif |
| 51 | template <typename Iterator> |
| 52 | struct psbuf |
| 53 | : std::basic_streambuf<typename std::iterator_traits<Iterator>::value_type> |
| 54 | { |
| 55 | psbuf(Iterator first_, Iterator const& last_) |
| 56 | : first(first_), last(last_) {} |
| 57 | |
| 58 | protected: |
| 59 | typename psbuf::int_type underflow() BOOST_OVERRIDE |
| 60 | { |
| 61 | return first == last ? psbuf::traits_type::eof() |
| 62 | : psbuf::traits_type::to_int_type(*first); |
| 63 | } |
| 64 | |
| 65 | typename psbuf::int_type uflow() BOOST_OVERRIDE |
| 66 | { |
| 67 | return first == last ? psbuf::traits_type::eof() |
| 68 | : psbuf::traits_type::to_int_type(*first++); |
| 69 | } |
| 70 | |
| 71 | public: |
| 72 | Iterator first; |
| 73 | Iterator const& last; |
| 74 | }; |
| 75 | #ifdef _MSC_VER |
| 76 | # pragma warning(pop) |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | template <typename Char = char, typename T = spirit::basic_hold_any<char> > |
| 81 | struct stream_parser |
| 82 | : primitive_parser<stream_parser<Char, T> > |
| 83 | { |
| 84 | template <typename Context, typename Iterator> |
| 85 | struct attribute |
| 86 | { |
| 87 | typedef T type; |
| 88 | }; |
| 89 | |
| 90 | template <typename Iterator, typename Context |
| 91 | , typename Skipper, typename Attribute> |
| 92 | bool parse(Iterator& first, Iterator const& last |
| 93 | , Context& /*context*/, Skipper const& skipper |
| 94 | , Attribute& attr_) const |
| 95 | { |
| 96 | qi::skip_over(first, last, skipper); |
| 97 | |
| 98 | detail::psbuf<Iterator> pseudobuf(first, last); |
| 99 | std::basic_istream<Char> in(&pseudobuf); |
| 100 | in >> attr_; // use existing operator>>() |
| 101 | |
| 102 | // advance the iterator if everything is ok |
| 103 | if (in) { |
| 104 | first = pseudobuf.first; |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | template <typename Context> |
| 112 | info what(Context& /*context*/) const |
| 113 | { |
| 114 | return info("stream" ); |
| 115 | } |
| 116 | }; |
| 117 | |
| 118 | template <typename T, typename Char = char> |
| 119 | struct typed_stream |
| 120 | : proto::terminal<stream_parser<Char, T> >::type |
| 121 | { |
| 122 | }; |
| 123 | |
| 124 | /////////////////////////////////////////////////////////////////////////// |
| 125 | // Parser generators: make_xxx function (objects) |
| 126 | /////////////////////////////////////////////////////////////////////////// |
| 127 | template <typename Char> |
| 128 | struct make_stream |
| 129 | { |
| 130 | typedef stream_parser<Char> result_type; |
| 131 | result_type operator()(unused_type, unused_type) const |
| 132 | { |
| 133 | return result_type(); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | template <typename Modifiers> |
| 138 | struct make_primitive<tag::stream, Modifiers> : make_stream<char> {}; |
| 139 | |
| 140 | template <typename Modifiers> |
| 141 | struct make_primitive<tag::wstream, Modifiers> : make_stream<wchar_t> {}; |
| 142 | }}} |
| 143 | |
| 144 | #endif |
| 145 | |