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_INTERSECTION_HPP)
|
---|
12 | #define BOOST_SPIRIT_INTERSECTION_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 | // intersection 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 and 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 no-match.
|
---|
36 | //
|
---|
37 | ///////////////////////////////////////////////////////////////////////////
|
---|
38 | struct intersection_parser_gen;
|
---|
39 |
|
---|
40 | template <typename A, typename B>
|
---|
41 | struct intersection
|
---|
42 | : public binary<A, B, parser<intersection<A, B> > >
|
---|
43 | {
|
---|
44 | typedef intersection<A, B> self_t;
|
---|
45 | typedef binary_parser_category parser_category_t;
|
---|
46 | typedef intersection_parser_gen parser_generator_t;
|
---|
47 | typedef binary<A, B, parser<self_t> > base_t;
|
---|
48 |
|
---|
49 | intersection(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 | iterator_t save = scan.first;
|
---|
59 | if (result_t hl = this->left().parse(scan))
|
---|
60 | {
|
---|
61 | ScannerT bscan(scan.first, scan.first, scan);
|
---|
62 | scan.first = save;
|
---|
63 | result_t hr = this->right().parse(bscan);
|
---|
64 | if (hl.length() == hr.length())
|
---|
65 | return hl;
|
---|
66 | }
|
---|
67 |
|
---|
68 | return scan.no_match();
|
---|
69 | }
|
---|
70 | };
|
---|
71 |
|
---|
72 | struct intersection_parser_gen
|
---|
73 | {
|
---|
74 | template <typename A, typename B>
|
---|
75 | struct result
|
---|
76 | {
|
---|
77 | typedef
|
---|
78 | intersection<
|
---|
79 | typename as_parser<A>::type
|
---|
80 | , typename as_parser<B>::type
|
---|
81 | >
|
---|
82 | type;
|
---|
83 | };
|
---|
84 |
|
---|
85 | template <typename A, typename B>
|
---|
86 | static intersection<
|
---|
87 | typename as_parser<A>::type
|
---|
88 | , typename as_parser<B>::type
|
---|
89 | >
|
---|
90 | generate(A const& a, B const& b)
|
---|
91 | {
|
---|
92 | return intersection<BOOST_DEDUCED_TYPENAME as_parser<A>::type,
|
---|
93 | BOOST_DEDUCED_TYPENAME as_parser<B>::type>
|
---|
94 | (as_parser<A>::convert(a), as_parser<B>::convert(b));
|
---|
95 | }
|
---|
96 | };
|
---|
97 |
|
---|
98 | template <typename A, typename B>
|
---|
99 | intersection<A, B>
|
---|
100 | operator&(parser<A> const& a, parser<B> const& b);
|
---|
101 |
|
---|
102 | template <typename A>
|
---|
103 | intersection<A, chlit<char> >
|
---|
104 | operator&(parser<A> const& a, char b);
|
---|
105 |
|
---|
106 | template <typename B>
|
---|
107 | intersection<chlit<char>, B>
|
---|
108 | operator&(char a, parser<B> const& b);
|
---|
109 |
|
---|
110 | template <typename A>
|
---|
111 | intersection<A, strlit<char const*> >
|
---|
112 | operator&(parser<A> const& a, char const* b);
|
---|
113 |
|
---|
114 | template <typename B>
|
---|
115 | intersection<strlit<char const*>, B>
|
---|
116 | operator&(char const* a, parser<B> const& b);
|
---|
117 |
|
---|
118 | template <typename A>
|
---|
119 | intersection<A, chlit<wchar_t> >
|
---|
120 | operator&(parser<A> const& a, wchar_t b);
|
---|
121 |
|
---|
122 | template <typename B>
|
---|
123 | intersection<chlit<wchar_t>, B>
|
---|
124 | operator&(wchar_t a, parser<B> const& b);
|
---|
125 |
|
---|
126 | template <typename A>
|
---|
127 | intersection<A, strlit<wchar_t const*> >
|
---|
128 | operator&(parser<A> const& a, wchar_t const* b);
|
---|
129 |
|
---|
130 | template <typename B>
|
---|
131 | intersection<strlit<wchar_t const*>, B>
|
---|
132 | operator&(wchar_t const* a, parser<B> const& b);
|
---|
133 |
|
---|
134 | }} // namespace boost::spirit
|
---|
135 |
|
---|
136 | #endif
|
---|
137 |
|
---|
138 | #include <boost/spirit/core/composite/impl/intersection.ipp>
|
---|