/
toy.cpp
247 lines (203 loc) · 6.64 KB
/
toy.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <iostream>
#include <utility>
#include <cstring>
#include <boost/mpl/identity.hpp>
namespace boost { namespace spirit { namespace x3
{
template <typename Derived>
struct parser
{
Derived const& derived() const
{
return *static_cast<Derived const*>(this);
}
};
template <typename Char>
struct char_parser : parser<char_parser<Char>>
{
char_parser(Char ch) : ch(ch) {}
template <typename Iterator, typename Context>
bool parse(Iterator& first, Iterator last, Context const& ctx) const
{
if (first != last && *first == ch)
{
++first;
return true;
}
return false;
}
Char ch;
};
template <typename Char>
inline char_parser<Char> char_(Char ch)
{
return char_parser<Char>(ch);
};
template <typename Left, typename Right>
struct sequence_parser : parser<sequence_parser<Left, Right>>
{
sequence_parser(Left left, Right right)
: left(left), right(right) {}
template <typename Iterator, typename Context>
bool parse(Iterator& first, Iterator last, Context const& ctx) const
{
return left.parse(first, last, ctx)
&& right.parse(first, last, ctx);
}
Left left;
Right right;
};
template <typename Left, typename Right>
inline sequence_parser<Left, Right> operator>>(
parser<Left> const& left, parser<Right> const& right)
{
return sequence_parser<Left, Right>(
left.derived(), right.derived());
}
template <typename Left, typename Right>
struct alternative_parser : parser<alternative_parser<Left, Right>>
{
alternative_parser(Left left, Right right)
: left(left), right(right) {}
template <typename Iterator, typename Context>
bool parse(Iterator& first, Iterator last, Context const& ctx) const
{
if (left.parse(first, last, ctx))
return true;
return right.parse(first, last, ctx);
}
Left left;
Right right;
};
template <typename Left, typename Right>
inline alternative_parser<Left, Right> operator|(
parser<Left> const& left, parser<Right> const& right)
{
return alternative_parser<Left, Right>(
left.derived(), right.derived());
}
template <typename ID, typename T, typename NextContext>
struct context
{
context(T const& val, NextContext const& next_ctx)
: val(val), next_ctx(next_ctx) {}
T const& get(mpl::identity<ID>) const
{
return val;
}
template <typename Identity>
decltype(std::declval<NextContext>().get(Identity()))
get(Identity id) const
{
return next_ctx.get(id);
}
T const& val;
NextContext const& next_ctx;
};
struct empty_context
{
struct undefined {};
template <typename ID>
undefined get(ID) const
{
return undefined();
}
};
template <typename ID, typename RHS>
struct rule_definition : parser<rule_definition<ID, RHS>>
{
rule_definition(RHS rhs)
: rhs(rhs) {}
template <typename Iterator, typename Context>
bool parse(Iterator& first, Iterator last, Context const& ctx) const
{
context<ID, RHS, Context> this_ctx(rhs, ctx);
return rhs.parse(first, last, this_ctx);
}
RHS rhs;
};
template <typename ID>
struct rule : parser<rule<ID>>
{
template <typename Derived>
rule_definition<ID, Derived>
operator=(parser<Derived> const& definition) const
{
return rule_definition<ID, Derived>(definition.derived());
}
template <typename Iterator, typename Context>
bool parse(Iterator& first, Iterator last, Context const& ctx) const
{
return ctx.get(mpl::identity<ID>()).parse(first, last, ctx);
}
};
template <typename Iterator, typename Derived>
inline bool parse(parser<Derived> const& p, Iterator& first, Iterator last)
{
empty_context ctx;
return p.derived().parse(first, last, ctx);
}
}}}
///////////////////////////////////////////////////////////////////////////////
// test code
template <typename Parser>
bool test_parse(Parser const& p, char const* in)
{
return parse(p, in, in + std::strlen(in));
}
namespace parser
{
using namespace boost::spirit::x3;
namespace g_definition
{
auto const x = rule<class x>();
auto const ax = char_('a') >> x;
auto const g =
x = char_('x') | ax;
}
using g_definition::g;
}
int main()
{
{ // a non-recursive parser
using namespace boost::spirit::x3;
auto abc = char_('a') >> char_('b') >> char_('c');
std::cout << test_parse(abc, "abc") << std::endl;
std::cout << test_parse(abc, "abx") << std::endl;
std::cout << "==========================================" << std::endl;
}
{ // a recursive rule
using namespace boost::spirit::x3;
auto const x = rule<class x>();
auto const ax = char_('a') >> x;
auto const start = (x = char_('x') | ax);
std::cout << test_parse(start, "x") << std::endl;
std::cout << test_parse(start, "ax") << std::endl;
std::cout << test_parse(start, "aaaaax") << std::endl;
std::cout << test_parse(start, "aaz") << std::endl;
std::cout << "==========================================" << std::endl;
}
{ // a grammar ( gcc and clang only: see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3582.html )
using namespace boost::spirit::x3;
auto g = []()
{
rule<class x> x;
auto ax = char_('a') >> x;
return x = char_('x') | ax;
}();
std::cout << test_parse(g, "x") << std::endl;
std::cout << test_parse(g, "ax") << std::endl;
std::cout << test_parse(g, "aaaaax") << std::endl;
std::cout << test_parse(g, "aaz") << std::endl;
std::cout << "==========================================" << std::endl;
}
{ // another grammar using namespaces (standard c++, see grammar g definition above in namespace parser.)
using parser::g;
std::cout << test_parse(g, "x") << std::endl;
std::cout << test_parse(g, "ax") << std::endl;
std::cout << test_parse(g, "aaaaax") << std::endl;
std::cout << test_parse(g, "aaz") << std::endl;
std::cout << "==========================================" << std::endl;
}
return 0;
}