source: NonGTP/Boost/boost/spirit/core/composite/alternative.hpp @ 857

Revision 857, 4.5 KB checked in by igarcia, 18 years ago (diff)
Line 
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_ALTERNATIVE_HPP)
12#define BOOST_SPIRIT_ALTERNATIVE_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
19namespace boost { namespace spirit {
20
21    ///////////////////////////////////////////////////////////////////////////
22    //
23    //  alternative class
24    //
25    //      Handles expressions of the form:
26    //
27    //          a | b
28    //
29    //      where a and b are parsers. The expression returns a composite
30    //      parser that matches a or b. One (not both) of the operands may
31    //      be a literal char, wchar_t or a primitive string char const*,
32    //      wchar_t const*.
33    //
34    //      The expression is short circuit evaluated. b is never touched
35    //      when a is returns a successful match.
36    //
37    ///////////////////////////////////////////////////////////////////////////
38    struct alternative_parser_gen;
39   
40    template <typename A, typename B>
41    struct alternative
42    :   public binary<A, B, parser<alternative<A, B> > >
43    {
44        typedef alternative<A, B>               self_t;
45        typedef binary_parser_category          parser_category_t;
46        typedef alternative_parser_gen          parser_generator_t;
47        typedef binary<A, B, parser<self_t> >   base_t;
48   
49        alternative(A const& a, B const& b)
50        : base_t(a, b) {}
51   
52        template <typename ScannerT>
53        typename parser_result<self_t, ScannerT>::type
54        parse(ScannerT const& scan) const
55        {
56            typedef typename parser_result<self_t, ScannerT>::type result_t;
57            typedef typename ScannerT::iterator_t iterator_t;
58            { // scope for save
59                iterator_t save = scan.first;
60                if (result_t hit = this->left().parse(scan))
61                    return hit;
62                scan.first = save;
63            }
64            return this->right().parse(scan);
65        }
66    };
67   
68    struct alternative_parser_gen
69    {
70        template <typename A, typename B>
71        struct result
72        {
73            typedef
74                alternative<
75                    typename as_parser<A>::type
76                  , typename as_parser<B>::type
77                >
78            type;
79        };
80   
81        template <typename A, typename B>
82        static alternative<
83            typename as_parser<A>::type
84          , typename as_parser<B>::type
85        >
86        generate(A const& a, B const& b)
87        {
88            return alternative<BOOST_DEDUCED_TYPENAME as_parser<A>::type,
89                BOOST_DEDUCED_TYPENAME as_parser<B>::type>
90                    (as_parser<A>::convert(a), as_parser<B>::convert(b));
91        }
92    };
93   
94    template <typename A, typename B>
95    alternative<A, B>
96    operator|(parser<A> const& a, parser<B> const& b);
97   
98    template <typename A>
99    alternative<A, chlit<char> >
100    operator|(parser<A> const& a, char b);
101   
102    template <typename B>
103    alternative<chlit<char>, B>
104    operator|(char a, parser<B> const& b);
105   
106    template <typename A>
107    alternative<A, strlit<char const*> >
108    operator|(parser<A> const& a, char const* b);
109   
110    template <typename B>
111    alternative<strlit<char const*>, B>
112    operator|(char const* a, parser<B> const& b);
113   
114    template <typename A>
115    alternative<A, chlit<wchar_t> >
116    operator|(parser<A> const& a, wchar_t b);
117   
118    template <typename B>
119    alternative<chlit<wchar_t>, B>
120    operator|(wchar_t a, parser<B> const& b);
121   
122    template <typename A>
123    alternative<A, strlit<wchar_t const*> >
124    operator|(parser<A> const& a, wchar_t const* b);
125   
126    template <typename B>
127    alternative<strlit<wchar_t const*>, B>
128    operator|(wchar_t const* a, parser<B> const& b);
129
130}} // namespace boost::spirit
131
132#endif
133
134#include <boost/spirit/core/composite/impl/alternative.ipp>
Note: See TracBrowser for help on using the repository browser.