1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
// Copyright Abel Sinkovics (abel@sinkovics.hu) 2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/metaparse/foldl_reject_incomplete.hpp>
#include <boost/metaparse/foldl_reject_incomplete1.hpp>
#include <boost/metaparse/lit_c.hpp>
#include <boost/metaparse/transform.hpp>
#include <boost/metaparse/one_char_except_c.hpp>
#include <boost/metaparse/one_of.hpp>
#include <boost/metaparse/always_c.hpp>
#include <boost/metaparse/build_parser.hpp>
#include <boost/metaparse/middle_of.hpp>
#include <boost/metaparse/entire_input.hpp>
#include <boost/metaparse/string.hpp>
#include <boost/detail/iterator.hpp>
#include <boost/xpressive/xpressive.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/string.hpp>
using boost::metaparse::foldl_reject_incomplete;
using boost::metaparse::foldl_reject_incomplete1;
using boost::metaparse::lit_c;
using boost::metaparse::transform;
using boost::metaparse::build_parser;
using boost::metaparse::one_of;
using boost::metaparse::always_c;
using boost::metaparse::middle_of;
using boost::metaparse::one_char_except_c;
using boost::metaparse::entire_input;
using boost::mpl::c_str;
using boost::mpl::true_;
using boost::mpl::false_;
using boost::xpressive::sregex;
using boost::xpressive::as_xpr;
/*
* Results of parsing
*/
template <class T>
struct has_value
{
typedef T type;
static const sregex value;
};
template <class T>
const sregex has_value<T>::value = T::run();
struct r_epsilon : has_value<r_epsilon>
{
static sregex run()
{
return as_xpr("");
}
};
struct r_any_char : has_value<r_any_char>
{
static sregex run()
{
return boost::xpressive::_;
}
};
struct r_char_lit
{
template <class C>
struct apply : has_value<apply<C> >
{
static sregex run()
{
return as_xpr(C::type::value);
}
};
};
struct r_append
{
template <class A, class B>
struct apply : has_value<apply<B, A> >
{
static sregex run()
{
return A::type::run() >> B::type::run();
}
};
};
/*
* The grammar
*
* regexp ::= (bracket_expr | non_bracket_expr)*
* non_bracket_expr ::= '.' | char_lit
* bracket_expr ::= '(' regexp ')'
* char_lit ::= any character except: . ( )
*/
typedef
foldl_reject_incomplete1<
one_of<
always_c<'.', r_any_char>,
transform<one_char_except_c<'.', '(', ')'>, r_char_lit>
>,
r_epsilon,
r_append
>
non_bracket_expr;
typedef middle_of<lit_c<'('>, non_bracket_expr, lit_c<')'> > bracket_expr;
typedef
foldl_reject_incomplete<
one_of<bracket_expr, non_bracket_expr>,
r_epsilon,
r_append
>
regexp;
typedef build_parser<entire_input<regexp> > regexp_parser;
void test_string(const std::string& s)
{
using boost::xpressive::regex_match;
using boost::xpressive::smatch;
using boost::mpl::apply_wrap1;
using std::cout;
using std::endl;
#if BOOST_METAPARSE_STD < 2011
typedef boost::metaparse::string<'.','(','b','c',')'> regexp;
#else
typedef BOOST_METAPARSE_STRING(".(bc)") regexp;
#endif
const sregex re = apply_wrap1<regexp_parser, regexp>::type::value;
smatch w;
cout
<< s << (regex_match(s, w, re) ? " matches " : " doesn't match ")
<< c_str<regexp>::type::value
<< endl;
}
int main()
{
test_string("abc");
test_string("aba");
}
|