-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtst_parsec.cpp
450 lines (344 loc) · 10.5 KB
/
tst_parsec.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include <QString>
#include <QtTest>
#include <variant>
#include <iostream>
#include "common.h"
#include "ps/ps.h"
class PSTest : public QObject
{
Q_OBJECT
public:
PSTest();
private Q_SLOTS:
void singleDigitParserTest();
void digitParserTest();
void litParserTest();
void lowerCaseCharParserTest();
void upperCaseCharParserTest();
void symbolParserTest();
void manyCombinatorTest();
void parseFailureTest();
void tryThrowParserLTest();
void tryThrowParserTTest();
void safeVSTryPTest();
void bindPureTest();
void applicativeTest();
void sequencedParsersTest();
void forgetCombinatorsTest();
void seqCombinatorsTest();
void binarySeqCombinatorsTest();
void alt1Test();
void alt2Test();
void internalParsersTest();
void employeeTest();
};
PSTest::PSTest()
{
}
template <typename A>
void printError(const ps::ParserResult<A>& pr)
{
if (isLeft(pr))
{
auto err = ps::getError(pr);
std::cout << "\nError:\n" << err.message << "\n";
}
}
void PSTest::singleDigitParserTest()
{
using namespace ps;
ParserResult<Char> result = parse<Char>(digit, "1");
printError(result);
QVERIFY(isRight(result));
Char r = getParsed<Char>(result);
QVERIFY(r == '1');
}
void PSTest::digitParserTest()
{
using namespace ps;
ParserResult<Char> result = parse<Char>(digit, "1abc");
QVERIFY(isRight(result));
Char r = getParsed<Char>(result);
QVERIFY(r == '1');
}
void PSTest::litParserTest()
{
using namespace ps;
ParserResult<std::string> result = parse<std::string>(lit("str"), "str12");
QVERIFY(isRight(result));
std::string r = getParsed<std::string>(result);
QVERIFY(r == "str");
}
void PSTest::lowerCaseCharParserTest()
{
using namespace ps;
ParserResult<Char> result = parse<Char>(lower, "abc");
QVERIFY(isRight(result));
Char r = getParsed<Char>(result);
QVERIFY(r == 'a');
}
void PSTest::upperCaseCharParserTest()
{
using namespace ps;
ParserResult<Char> result = parse<Char>(upper, "BCD");
QVERIFY(isRight(result));
Char r = getParsed<Char>(result);
QVERIFY(r == 'B');
}
void PSTest::symbolParserTest()
{
using namespace ps;
ParserResult<Char> result = parse<Char>(symbol('B'), "BCD");
QVERIFY(isRight(result));
Char r = getParsed<Char>(result);
QVERIFY(r == 'B');
}
struct R2
{
ps::Many<ps::Char> ds;
ps::Char ch;
};
void PSTest::manyCombinatorTest()
{
using namespace ps;
ParserT<Many<Char>> p = manyPL<Char>(digitThrowPL);
ParserResult<Many<Char>> result = parse(p, "4321");
QVERIFY(isRight(result));
Many<Char> parsed = getParsed(result);
QVERIFY(parsed.size() == 4);
QVERIFY(parsed.front() == '4');
parsed.pop_front();
QVERIFY(parsed.front() == '3');
parsed.pop_front();
QVERIFY(parsed.front() == '2');
parsed.pop_front();
QVERIFY(parsed.front() == '1');
}
void PSTest::parseFailureTest()
{
using namespace ps;
ParserResult<Char> result = parse<Char>(digit, "abc");
QVERIFY(isLeft(result));
QVERIFY(std::get<ParserFailed>(result).message == "Failed to parse digit: not a digit.");
}
void PSTest::tryThrowParserLTest()
{
using namespace ps;
auto f = [](const ParserResult<Char>&)
{
return pure<ParserResult<Char>>(ParserSucceeded<Char> { '1' } );
};
ParserT<ParserResult<Char>> pt = tryP(digitThrowPL);
auto pt2 = bind<ParserResult<Char>, ParserResult<Char>>(pt, f);
auto result = parse(pt2, "abc");
QVERIFY(isLeft(result));
}
void PSTest::tryThrowParserTTest()
{
// This does not work.
// using namespace ps;
// auto f = [](const ParserResult<Char>&)
// {
// return pure<ParserResult<Char>>(ParserSucceeded<Char> { '1' } );
// };
// ParserT<ParserResult<Char>> pt = tryP(digit);
// auto pt2 = bind<ParserResult<Char>, ParserResult<Char>>(pt, f);
// auto result = parse(pt2, "abc");
// QVERIFY(isRight(result));
// QVERIFY(getParsed(result) == '1');
}
/*
-- f should fail with err1
f = do
try (parserA >> parserB >> fail err1)
>>= \eRes -> fail err2
-- g should fail with err2
g = do
safe (parserA >> parserB >> fail err1)
>>= \eRes -> fail err2
*/
void PSTest::safeVSTryPTest()
{
using namespace ps;
auto f = [](ParserResult<Unit>) {
throw std::runtime_error("err2");
return pure(unit);
};
ParserL<Unit> internalP =
bind<Char, Unit>(digitThrowPL, [](Char) { return
bind<Char, Unit>(digitThrowPL, [](Char) {
throw std::runtime_error("err1");
return purePL(unit);
});
});
ParserT<Unit> triedP = bind<ParserResult<Unit>, Unit>(tryP(internalP), f);
ParserT<Unit> safedP = bind<ParserResult<Unit>, Unit>(safeP(internalP), f);
auto result1 = parse(triedP, "123");
auto result2 = parse(safedP, "123");
QVERIFY(isLeft(result1));
QVERIFY(getError(result1).message == "err1");
QVERIFY(isLeft(result2));
QVERIFY(getError(result2).message == "err2");
}
struct R
{
ps::Char dg0;
ps::Char ch1;
ps::Char ch2;
};
void PSTest::bindPureTest()
{
using namespace ps;
auto p = bind<Char, R>(digit, [=](Char d1) { return
pure<R>(R{d1, 'a', '0'});
});
ParserResult<R> result = parse(p, "1b2");
QVERIFY(isRight(result));
R r = getParsed<R>(result);
QVERIFY(r.dg0 == '1');
QVERIFY(r.ch1 == 'a');
QVERIFY(r.ch2 == '0');
}
void PSTest::applicativeTest()
{
using namespace ps;
auto mkR = [](Char dg0, Char ch1, Char ch2) { return R { dg0, ch1, ch2 }; };
auto p = app<R, Char, Char, Char>(mkR, digit, lower, digit);
ParserResult<R> result = parse(p, "1b2");
QVERIFY(isRight(result));
R r = getParsed<R>(result);
QVERIFY(r.dg0 == '1');
QVERIFY(r.ch1 == 'b');
QVERIFY(r.ch2 == '2');
}
void PSTest::sequencedParsersTest()
{
using namespace ps;
auto p = bind<Char, R>(digit, [=](Char dg0) { return
bind<Char, R>(lower, [=](Char ch1) { return
bind<Char, R>(symbol('2'), [=](Char ch2) { return
pure<R>(R{dg0, ch1, ch2});
}); }); });
ParserResult<R> result = parse(p, "1b2");
QVERIFY(isRight(result));
R r = getParsed<R>(result);
QVERIFY(r.dg0 == '1');
QVERIFY(r.ch1 == 'b');
QVERIFY(r.ch2 == '2');
}
void PSTest::forgetCombinatorsTest()
{
using namespace ps;
auto fst = forgetFirst(letter, digit);
auto snd = forgetSecond(letter, digit);
ParserResult<Char> result1 = parse(fst, "a1");
ParserResult<Char> result2 = parse(snd, "a1");
QVERIFY(isRight(result1));
QVERIFY(isRight(result2));
QVERIFY(getParsed<Char>(result1) == '1');
QVERIFY(getParsed<Char>(result2) == 'a');
}
void PSTest::seqCombinatorsTest()
{
using namespace ps;
ParserResult<Char> result1 = parse(seq(upper, lower), "Aa");
ParserResult<Char> result2 = parse(seq(upper, lower, digit), "Aa1");
ParserResult<Char> result3 = parse(seq(upper, lower, digit, symbol('!')), "Aa1!");
QVERIFY(isRight(result1));
QVERIFY(isRight(result2));
QVERIFY(isRight(result3));
QVERIFY(getParsed<Char>(result1) == 'a');
QVERIFY(getParsed<Char>(result2) == '1');
QVERIFY(getParsed<Char>(result3) == '!');
}
void PSTest::binarySeqCombinatorsTest()
{
using namespace ps;
ParserResult<Char> result1 = parse(upper >> lower, "Aa");
ParserResult<Char> result2 = parse(upper >> lower >> digit, "Aa1");
ParserResult<Char> result3 = parse(upper >> (lower << digit), "Aa1");
QVERIFY(isRight(result1));
QVERIFY(isRight(result2));
QVERIFY(isRight(result3));
QVERIFY(getParsed<Char>(result1) == 'a');
QVERIFY(getParsed<Char>(result2) == '1');
QVERIFY(getParsed<Char>(result3) == 'a');
}
void PSTest::alt1Test()
{
using namespace ps;
ParserResult<Char> result1 = parse(alt(upperPL, lowerPL), "A");
ParserResult<Char> result2 = parse(alt(upperPL, lowerPL), "a");
QVERIFY(isRight(result1));
QVERIFY(isRight(result2));
QVERIFY(getParsed(result1) == 'A');
QVERIFY(getParsed(result2) == 'a');
}
void PSTest::alt2Test()
{
using namespace ps;
ParserResult<Char> result1 = parse(alt(upperThrowPL, lowerThrowPL), "A");
ParserResult<Char> result2 = parse(alt(upperThrowPL, lowerThrowPL), "a");
QVERIFY(isRight(result1));
QVERIFY(isRight(result2));
QVERIFY(getParsed(result1) == 'A');
QVERIFY(getParsed(result2) == 'a');
}
void PSTest::internalParsersTest()
{
using namespace ps;
ParserL<Char> p1 =
bind<Char, Char>(upperThrowPL, [=](auto) { return
bind<Char, Char>(lowerThrowPL, [=](auto) { return lowerThrowPL; }); });
ParserL<Char> p2 =
bind<Char, Char>(upperThrowPL, [=](auto) { return
bind<Char, Char>(upperThrowPL, [=](auto) { return upperThrowPL; }); });
ParserT<Char> p =
bind<Char, Char>(digit,
[=](auto) { return alt(p1, p2); });
ParserResult<Char> result = parse<Char>(p, "1AAA2");
printError(result);
QVERIFY(isRight(result));
Char ch = getParsed<Char>(result);
QVERIFY(ch == 'A');
}
struct Employee
{
int age;
std::string firstName;
std::string lastName;
double salary;
};
void PSTest::employeeTest()
{
// using namespace ps;
// auto mkEmployee = [](
// int a, const std::string& sn, const std::string& fn, double s) {
// return Employee {a, sn, fn, s};
// };
// auto quotedString = between(symbol('"'), strP);
// auto prefix = lit("employee") >> between(spaces(), symbol('{'));
// auto postfix = between(spaces(), symbol('}'));
// auto comma = between(spaces(), symbol(','));
// ParserT<Employee> employeeParser =
// app<Employee, int, std::string, std::string, double>(
// mkEmployee,
// prefix >> intP, // age
// comma >> quotedString, // first name
// comma >> quotedString, // last name
// comma >> (doubleP << postfix)); // salary
// auto s = "employee {35, “Jane”, “Street”, 50000.0}";
// ParserResult<Employee> result = parse(employeeParser, s);
// if (isLeft(result)) {
// std::cout << "Parse error: " << getError(result).message;
// }
// else {
// Employee employee = getParsed(result);
// QVERIFY(employee.age == 35);
// QVERIFY(employee.firstName == "Jane");
// QVERIFY(employee.lastName == "Street");
// QVERIFY(employee.salary >= 50000.0 && employee.salary <= 50000.1);
// }
}
QTEST_APPLESS_MAIN(PSTest)
#include "tst_parsec.moc"