combinators.hpp
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
#ifndef CRUCIBLE_PARSING_COMBINATORS_HPP #define CRUCIBLE_PARSING_COMBINATORS_HPP #include "crucible/parsing/errors.inl" #include <tuple> #include <type_traits> #include <utility> #include <vector> namespace crucible::parsing::combinators { template<typename LHS_PARSER, typename RHS_PARSER> class OneOf final { static_assert(std::is_same_v<typename LHS_PARSER::StateType, typename RHS_PARSER::StateType>); static_assert(std::is_same_v<typename LHS_PARSER::ValueType, typename RHS_PARSER::ValueType>); public: using StateType = typename LHS_PARSER::StateType; using ValueType = typename LHS_PARSER::ValueType; using ResultType = errors::ParsingResult<StateType, ValueType>; OneOf(LHS_PARSER const &lhs_parser, RHS_PARSER const &rhs_parser); [[nodiscard]] auto operator()(StateType const &state) const -> ResultType; private: LHS_PARSER my_lhs_parser {}; RHS_PARSER my_rhs_parser {}; }; template<typename LHS_PARSER, typename RHS_PARSER> class Pair final { static_assert(std::is_same_v<typename LHS_PARSER::StateType, typename RHS_PARSER::StateType>); public: using StateType = typename LHS_PARSER::StateType; using ValueType = std::pair<typename LHS_PARSER::ValueType, typename RHS_PARSER::ValueType>; using ResultType = errors::ParsingResult<StateType, ValueType>; Pair(LHS_PARSER const &lhs_parser, RHS_PARSER const &rhs_parser); [[nodiscard]] auto operator()(StateType const &state) const -> ResultType; private: LHS_PARSER my_lhs_parser {}; RHS_PARSER my_rhs_parser {}; }; template<typename HEAD_PARSER, typename ...TAIL_PARSERS> class Tuple final { using TailParserType = Tuple<TAIL_PARSERS...>; static_assert(std::is_same_v<typename HEAD_PARSER::StateType, typename TailParserType::StateType>); public: using StateType = typename HEAD_PARSER::StateType; using ValueType = std::tuple<typename HEAD_PARSER::ValueType, typename TAIL_PARSERS::ValueType...>; using ResultType = errors::ParsingResult<StateType, ValueType>; Tuple(HEAD_PARSER const &head_parser, TAIL_PARSERS &&...tail_parsers); [[nodiscard]] auto operator()(StateType const &state) const -> ResultType; private: HEAD_PARSER my_head_parser {}; TailParserType my_tail_parser {}; }; template<typename PARSER> class Tuple<PARSER> final { public: using StateType = typename PARSER::StateType; using ValueType = std::tuple<typename PARSER::ValueType>; using ResultType = errors::ParsingResult<StateType, ValueType>; explicit Tuple(PARSER const &parser); [[nodiscard]] auto operator()(StateType const &state) const -> ResultType; private: PARSER my_parser {}; }; template<typename T, typename PARSER> class Into final { public: using StateType = typename PARSER::StateType; using ValueType = T; using ResultType = errors::ParsingResult<StateType, ValueType>; Into(PARSER const &parser); [[nodiscard]] auto operator()(StateType const &state) const -> ResultType; private: PARSER my_parser {}; }; template<typename T, typename PARSER> class Map final { public: using StateType = typename PARSER::StateType; using ValueType = T; using ResultType = errors::ParsingResult<StateType, ValueType>; Map(std::function<T(typename PARSER::ValueType)> const &function, PARSER const &parser); [[nodiscard]] auto operator()(StateType const &state) const -> ResultType; private: PARSER my_parser {}; std::function<T(typename PARSER::ValueType)> my_function {}; }; template<typename PARSER> class SequenceOf final { public: using ParserType = PARSER; using ElementType = typename ParserType::ValueType; using StateType = typename ParserType::StateType; using ValueType = std::vector<ElementType>; using ResultType = errors::ParsingResult<StateType, ValueType>; SequenceOf(PARSER const &parser, std::size_t const repetitions); [[nodiscard]] auto operator()(StateType const &state) const -> ResultType; private: PARSER my_parser {}; std::size_t my_repetitions { 0 }; }; template<typename LHS_PARSER, typename RHS_PARSER> [[nodiscard]] auto make_one_of(LHS_PARSER const &lhs_parser, RHS_PARSER const &rhs_parser) -> OneOf<LHS_PARSER, RHS_PARSER>; template<typename LHS_PARSER, typename RHS_PARSER> [[nodiscard]] auto make_pair(LHS_PARSER const &lhs_parser, RHS_PARSER const &rhs_parser) -> Pair<LHS_PARSER, RHS_PARSER>; template<typename ...PARSERS> [[nodiscard]] auto make_tuple(PARSERS &&...parsers) -> Tuple<PARSERS...>; template<typename T, typename PARSER> [[nodiscard]] auto make_into(PARSER const &parser) -> Into<T, PARSER>; template<typename T, typename PARSER> [[nodiscard]] auto make_map(std::function<T(typename PARSER::ValueType)> const &function, PARSER const &parser) -> Map<T, PARSER>; template<typename PARSER> [[nodiscard]] auto make_sequence_of(PARSER const &parser, std::size_t const repetitions) -> SequenceOf<PARSER>; namespace operators { template<typename LHS_PARSER, typename RHS_PARSER> [[nodiscard]] auto operator^(LHS_PARSER const &lhs_parser, RHS_PARSER const &rhs_parser) -> OneOf<LHS_PARSER, RHS_PARSER>; template<typename LHS_PARSER, typename RHS_PARSER> [[nodiscard]] auto operator&(LHS_PARSER const &lhs_parser, RHS_PARSER const &rhs_parser) -> Pair<LHS_PARSER, RHS_PARSER>; template<typename PARSER> [[nodiscard]] auto operator*(PARSER const &parser, std::size_t const repetitions) -> SequenceOf<PARSER>; } } #endif // CRUCIBLE_PARSING_COMBINATORS_HPP