-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
127 lines (100 loc) · 3.96 KB
/
main.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
/*
* C++14 Parser Combinator
* Gian Lorenzo Meocci (glmeocci@gmail.com)
*
* inspired by Haskell Parsec library
* */
#include <iostream>
#include <vector>
#include <utility>
#include <memory>
#include <sstream>
#include "Include/lparser.hpp"
#include "Include/lparser_bricks.hpp"
#include "Include/kpml.hpp"
using namespace lparser;
void show_statement(const kpml::statement_t& s);
void check_statement(const parser_t<kpml::statement_t>& p, const std::string& expr_s);
int main()
{
std::cout << parse(many(space), "xyz") << std::endl;
std::cout << parse(item, "") << std::endl;
std::cout << parse(item, "3+5") << std::endl;
std::cout << parse(seq(item, item, item), "ABCDE") << std::endl;
std::cout << parse(item, "abc") << std::endl;
std::cout << parse(pure(1), "abc") << std::endl;
std::cout << parse(pipe(empty_fn<char>, item), "abc") << std::endl;
std::cout << "isDigit " << parse(digit, "123") << std::endl;
std::cout << "isLower " << parse(lower, "home") << std::endl;
std::cout << "isUpper " << parse(upper, "Home") << std::endl;
std::cout << "chareq " << parse(char_eq('I'), "Inghe") << std::endl;
std::cout << "seq " << parse(seq(char_eq('I'), char_eq('n'), char_eq('g')), "Inghe") << std::endl;
std::cout << "string_eq " << parse(string_eq("Ing"), "Inghe") << std::endl;
std::cout << "many(space) " << parse(space, " 123abc") << std::endl;
std::cout << "many(digit) " << parse(many(digit), "123abc") << std::endl;
std::cout << "some(digit) " << parse(some(digit), "123abc") << std::endl;
std::cout << "some(digit) " << parse(some(digit), "x23abc") << std::endl;
std::cout << "some(letter) " << parse(some(letter), "abc123abc") << std::endl;
std::cout << "ident " << parse(ident, "abc123 abc") << std::endl;
std::cout << "nat " << parse(nat, "1789abc") << std::endl;
std::cout << "space " << parse(space, " abc") << std::endl;
std::cout << "intg " << parse(intg, "1235") << std::endl;
std::cout << "intg " << parse(intg, "-1235 xxx") << std::endl;
std::cout << "integer " << parse(integer, " -107 95") << std::endl;
std::cout << "symbol " << parse(symbol("A"), " [ x ] ") << std::endl;
std::cout << "symbol " << parse(symbol("x"), " x = 123 ") << std::endl;
std::cout << "assign " << parse(assign, "abc = x123") << std::endl;
std::cout << "nats " << parse(nats, "[5, 8, 1982, 3 ]") << std::endl;
const std::string fp_body = "myfun(x, y, 5+ 4)";
const auto fp = parse(kpml::function_call, fp_body);
std::cout << fp_body << ": ";
show_statement(fp.get());
std::cout << std::endl;
const std::string expr_s = "((5 * 7) + myfun(x) * (31 - 9) + 21);finish!";
check_statement(parse(kpml::expr, expr_s), expr_s);
const std::string fun_def = "def my_fun(x, y) { if ((x + 1) > 0) { \"hello\" } else { if (y > 0) { y } else { is_null(x) } } } ;finish!";
check_statement(parse(kpml::function_def, fun_def), fun_def);
return 0;
}
void check_statement(const parser_t<kpml::statement_t>& p, const std::string& expr_s)
{
if (p.is_empty())
{
std::cout << "invalid expr: " << expr_s
<< ", remain: " << p.remain
<< std::endl;
}
else
{
std::cout << "expr: <" << expr_s << "> = ";
show_statement(*p.first);
std::cout << ", not parsed: " << p.remain
<< std::endl;
}
}
void show_statement(const kpml::statement_t& s)
{
if (s.is_leaf())
{
s.show_leaf(std::cout);
}
else
{
std::cout << R"({ "op": ")" << s.op << "\",";
if (!s.operands.empty())
{
std::cout << " \"operands\": [";
bool f{false};
for(const auto& e : s.operands)
{
if (f)
std::cout << ", ";
else
f = true;
show_statement(e);
}
std::cout << "]";
}
std::cout << "}";
}
}