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_SEQUENTIAL_OR_HPP)
|
---|
12 | #define BOOST_SPIRIT_SEQUENTIAL_OR_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 | // sequential-or class
|
---|
24 | //
|
---|
25 | // Handles expressions of the form:
|
---|
26 | //
|
---|
27 | // a || b
|
---|
28 | //
|
---|
29 | // Equivalent to
|
---|
30 | //
|
---|
31 | // a | b | a >> b;
|
---|
32 | //
|
---|
33 | // where a and b are parsers. The expression returns a composite
|
---|
34 | // parser that matches matches a or b in sequence. One (not both) of
|
---|
35 | // the operands may be a literal char, wchar_t or a primitive string
|
---|
36 | // char const*, wchar_t const*.
|
---|
37 | //
|
---|
38 | ///////////////////////////////////////////////////////////////////////////
|
---|
39 | struct sequential_or_parser_gen;
|
---|
40 |
|
---|
41 | template <typename A, typename B>
|
---|
42 | struct sequential_or : public binary<A, B, parser<sequential_or<A, B> > >
|
---|
43 | {
|
---|
44 | typedef sequential_or<A, B> self_t;
|
---|
45 | typedef binary_parser_category parser_category_t;
|
---|
46 | typedef sequential_or_parser_gen parser_generator_t;
|
---|
47 | typedef binary<A, B, parser<self_t> > base_t;
|
---|
48 |
|
---|
49 | sequential_or(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 ma = this->left().parse(scan))
|
---|
61 | {
|
---|
62 | save = scan.first;
|
---|
63 | if (result_t mb = this->right().parse(scan))
|
---|
64 | {
|
---|
65 | // matched a b
|
---|
66 | scan.concat_match(ma, mb);
|
---|
67 | return ma;
|
---|
68 | }
|
---|
69 | else
|
---|
70 | {
|
---|
71 | // matched a
|
---|
72 | scan.first = save;
|
---|
73 | return ma;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | scan.first = save;
|
---|
77 | }
|
---|
78 |
|
---|
79 | // matched b
|
---|
80 | return this->right().parse(scan);
|
---|
81 | }
|
---|
82 | };
|
---|
83 |
|
---|
84 | struct sequential_or_parser_gen
|
---|
85 | {
|
---|
86 | template <typename A, typename B>
|
---|
87 | struct result
|
---|
88 | {
|
---|
89 | typedef
|
---|
90 | sequential_or<
|
---|
91 | typename as_parser<A>::type
|
---|
92 | , typename as_parser<B>::type
|
---|
93 | >
|
---|
94 | type;
|
---|
95 | };
|
---|
96 |
|
---|
97 | template <typename A, typename B>
|
---|
98 | static sequential_or<
|
---|
99 | typename as_parser<A>::type
|
---|
100 | , typename as_parser<B>::type
|
---|
101 | >
|
---|
102 | generate(A const& a, B const& b)
|
---|
103 | {
|
---|
104 | return sequential_or<BOOST_DEDUCED_TYPENAME as_parser<A>::type,
|
---|
105 | BOOST_DEDUCED_TYPENAME as_parser<B>::type>
|
---|
106 | (as_parser<A>::convert(a), as_parser<B>::convert(b));
|
---|
107 | }
|
---|
108 | };
|
---|
109 |
|
---|
110 | template <typename A, typename B>
|
---|
111 | sequential_or<A, B>
|
---|
112 | operator||(parser<A> const& a, parser<B> const& b);
|
---|
113 |
|
---|
114 | template <typename A>
|
---|
115 | sequential_or<A, chlit<char> >
|
---|
116 | operator||(parser<A> const& a, char b);
|
---|
117 |
|
---|
118 | template <typename B>
|
---|
119 | sequential_or<chlit<char>, B>
|
---|
120 | operator||(char a, parser<B> const& b);
|
---|
121 |
|
---|
122 | template <typename A>
|
---|
123 | sequential_or<A, strlit<char const*> >
|
---|
124 | operator||(parser<A> const& a, char const* b);
|
---|
125 |
|
---|
126 | template <typename B>
|
---|
127 | sequential_or<strlit<char const*>, B>
|
---|
128 | operator||(char const* a, parser<B> const& b);
|
---|
129 |
|
---|
130 | template <typename A>
|
---|
131 | sequential_or<A, chlit<wchar_t> >
|
---|
132 | operator||(parser<A> const& a, wchar_t b);
|
---|
133 |
|
---|
134 | template <typename B>
|
---|
135 | sequential_or<chlit<wchar_t>, B>
|
---|
136 | operator||(wchar_t a, parser<B> const& b);
|
---|
137 |
|
---|
138 | template <typename A>
|
---|
139 | sequential_or<A, strlit<wchar_t const*> >
|
---|
140 | operator||(parser<A> const& a, wchar_t const* b);
|
---|
141 |
|
---|
142 | template <typename B>
|
---|
143 | sequential_or<strlit<wchar_t const*>, B>
|
---|
144 | operator||(wchar_t const* a, parser<B> const& b);
|
---|
145 |
|
---|
146 | }} // namespace boost::spirit
|
---|
147 |
|
---|
148 | #endif
|
---|
149 |
|
---|
150 | #include <boost/spirit/core/composite/impl/sequential_or.ipp>
|
---|