[857] | 1 | /*=============================================================================
|
---|
| 2 | Copyright (c) 1998-2003 Joel de Guzman
|
---|
| 3 | Copyright (c) 2001 Daniel Nuffer
|
---|
| 4 | Copyright (c) 2002 Hartmut Kaiser
|
---|
| 5 | http://spirit.sourceforge.net/
|
---|
| 6 |
|
---|
| 7 | Use, modification and distribution is subject to the Boost Software
|
---|
| 8 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
---|
| 9 | http://www.boost.org/LICENSE_1_0.txt)
|
---|
| 10 | =============================================================================*/
|
---|
| 11 | #if !defined(BOOST_SPIRIT_OPTIONAL_HPP)
|
---|
| 12 | #define BOOST_SPIRIT_OPTIONAL_HPP
|
---|
| 13 |
|
---|
| 14 | #include <boost/spirit/core/parser.hpp>
|
---|
| 15 | #include <boost/spirit/core/primitives/primitives.hpp>
|
---|
| 16 | #include <boost/spirit/core/composite/composite.hpp>
|
---|
| 17 | #include <boost/spirit/meta/as_parser.hpp>
|
---|
| 18 |
|
---|
| 19 | namespace boost { namespace spirit {
|
---|
| 20 |
|
---|
| 21 | ///////////////////////////////////////////////////////////////////////////
|
---|
| 22 | //
|
---|
| 23 | // optional class
|
---|
| 24 | //
|
---|
| 25 | // Handles expressions of the form:
|
---|
| 26 | //
|
---|
| 27 | // !a
|
---|
| 28 | //
|
---|
| 29 | // where a is a parser. The expression returns a composite
|
---|
| 30 | // parser that matches its subject zero (0) or one (1) time.
|
---|
| 31 | //
|
---|
| 32 | ///////////////////////////////////////////////////////////////////////////
|
---|
| 33 | struct optional_parser_gen;
|
---|
| 34 |
|
---|
| 35 | template <typename S>
|
---|
| 36 | struct optional
|
---|
| 37 | : public unary<S, parser<optional<S> > >
|
---|
| 38 | {
|
---|
| 39 | typedef optional<S> self_t;
|
---|
| 40 | typedef unary_parser_category parser_category_t;
|
---|
| 41 | typedef optional_parser_gen parser_generator_t;
|
---|
| 42 | typedef unary<S, parser<self_t> > base_t;
|
---|
| 43 |
|
---|
| 44 | optional(S const& a)
|
---|
| 45 | : base_t(a) {}
|
---|
| 46 |
|
---|
| 47 | template <typename ScannerT>
|
---|
| 48 | typename parser_result<self_t, ScannerT>::type
|
---|
| 49 | parse(ScannerT const& scan) const
|
---|
| 50 | {
|
---|
| 51 | typedef typename parser_result<self_t, ScannerT>::type result_t;
|
---|
| 52 | typedef typename ScannerT::iterator_t iterator_t;
|
---|
| 53 | iterator_t save = scan.first;
|
---|
| 54 | if (result_t r = this->subject().parse(scan))
|
---|
| 55 | {
|
---|
| 56 | return r;
|
---|
| 57 | }
|
---|
| 58 | else
|
---|
| 59 | {
|
---|
| 60 | scan.first = save;
|
---|
| 61 | return scan.empty_match();
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 | };
|
---|
| 65 |
|
---|
| 66 | struct optional_parser_gen
|
---|
| 67 | {
|
---|
| 68 | template <typename S>
|
---|
| 69 | struct result
|
---|
| 70 | {
|
---|
| 71 | typedef optional<S> type;
|
---|
| 72 | };
|
---|
| 73 |
|
---|
| 74 | template <typename S>
|
---|
| 75 | static optional<S>
|
---|
| 76 | generate(parser<S> const& a)
|
---|
| 77 | {
|
---|
| 78 | return optional<S>(a.derived());
|
---|
| 79 | }
|
---|
| 80 | };
|
---|
| 81 |
|
---|
| 82 | template <typename S>
|
---|
| 83 | optional<S>
|
---|
| 84 | operator!(parser<S> const& a);
|
---|
| 85 |
|
---|
| 86 | }} // namespace boost::spirit
|
---|
| 87 |
|
---|
| 88 | #endif
|
---|
| 89 |
|
---|
| 90 | #include <boost/spirit/core/composite/impl/optional.ipp>
|
---|