Echo Writes Code

combinators.inl

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
#ifndef CRUCIBLE_PARSING_COMBINATORS_INL
#define CRUCIBLE_PARSING_COMBINATORS_INL

#include "crucible/parsing/combinators.hpp"

#include <sstream>

namespace crucible::parsing::combinators {
  template<typename LHS_PARSER, typename RHS_PARSER>
  OneOf<LHS_PARSER, RHS_PARSER>::OneOf(LHS_PARSER const &lhs_parser, RHS_PARSER const &rhs_parser) :
    my_lhs_parser { lhs_parser },
    my_rhs_parser { rhs_parser } {
  }

  template<typename LHS_PARSER, typename RHS_PARSER>
  auto OneOf<LHS_PARSER, RHS_PARSER>::operator()(StateType const &state) const -> ResultType {
    auto const lhs_result { my_lhs_parser(state) };
    auto const rhs_result { my_rhs_parser(state) };

    if (lhs_result.has_rhs() && rhs_result.has_rhs()) {
      return errors::make_reject(state, errors::AmbiguousMatch {});
    }

    if (lhs_result.has_rhs()) {
      return lhs_result;
    }

    if (rhs_result.has_rhs()) {
      return rhs_result;
    }

    return errors::make_reject(state, errors::UnexpectedInput {});
  }

  template<typename LHS_PARSER, typename RHS_PARSER>
  Pair<LHS_PARSER, RHS_PARSER>::Pair(LHS_PARSER const &lhs_parser, RHS_PARSER const &rhs_parser) :
    my_lhs_parser { lhs_parser },
    my_rhs_parser { rhs_parser } {
  }

  template<typename LHS_PARSER, typename RHS_PARSER>
  auto Pair<LHS_PARSER, RHS_PARSER>::operator()(StateType const &state) const -> ResultType {
    auto const lhs_result { my_lhs_parser(state) };

    if (lhs_result.has_lhs()) {
      auto const &reject = lhs_result.get_lhs();
      auto const &error = reject.get_error();
      return errors::make_reject(state, error);
    }

    auto const &lhs_accept { lhs_result.get_rhs() };
    auto const &lhs_next_state { lhs_accept.get_state() };

    auto const rhs_result { my_rhs_parser(lhs_next_state) };

    if (rhs_result.has_lhs()) {
      auto const &reject = rhs_result.get_lhs();
      auto const &error = reject.get_error();
      return errors::make_reject(state, error);
    }

    auto const &rhs_accept { rhs_result.get_rhs() };
    auto const &rhs_next_state { rhs_accept.get_state() };

    auto const &lhs_value { lhs_accept.get_value() };
    auto const &rhs_value { rhs_accept.get_value() };
    return errors::make_accept(rhs_next_state, std::make_pair(lhs_value, rhs_value));
  }

  template<typename HEAD_PARSER, typename ...TAIL_PARSERS>
  Tuple<HEAD_PARSER, TAIL_PARSERS...>::Tuple(HEAD_PARSER const &head_parser, TAIL_PARSERS &&...tail_parsers) :
    my_head_parser { head_parser },
    my_tail_parser { std::forward<TAIL_PARSERS>(tail_parsers)... } {
  }

  template<typename HEAD_PARSER, typename ...TAIL_PARSERS>
  auto Tuple<HEAD_PARSER, TAIL_PARSERS...>::operator()(StateType const &state) const -> ResultType {
    auto const head_result { my_head_parser(state) };

    if (head_result.has_lhs()) {
      auto const &head_reject { head_result.get_lhs() };
      auto const &head_error { head_reject.get_error() };
      return errors::make_reject(state, head_error);
    }

    auto const &head_accept { head_result.get_rhs() };
    auto const &next_state { head_accept.get_state() };

    auto const tail_result { my_tail_parser(next_state) };

    if (tail_result.has_lhs()) {
      auto const &tail_reject { tail_result.get_lhs() };
      auto const &tail_error { tail_reject.get_error() };
      return errors::make_reject(state, tail_error);
    }

    auto const &tail_accept { tail_result.get_rhs() };
    auto const &final_state { tail_accept.get_state() };

    auto const &head_value { head_accept.get_value() };
    auto const &tail_value { tail_accept.get_value() };

    return errors::make_accept(final_state, std::tuple_cat(std::make_tuple(head_value), tail_value));
  }

  template<typename PARSER>
  Tuple<PARSER>::Tuple(PARSER const &parser) :
    my_parser { parser } {
  }

  template<typename PARSER>
  auto Tuple<PARSER>::operator()(StateType const &state) const -> ResultType {
    auto const result = my_parser(state);

    if (result.has_lhs()) {
      auto const &reject { result.get_lhs() };
      auto const &error { reject.get_error() };
      return errors::make_reject(state, error);
    }

    auto const &accept { result.get_rhs() };
    auto const &final_state { accept.get_state() };
    auto const &value { accept.get_value() };

    return errors::make_accept(final_state, std::make_tuple(value));
  }

  template<typename PARSER, typename FUNCTION>
  Into<PARSER, FUNCTION>::Into(PARSER const &parser, FUNCTION const &function) :
    my_parser { parser },
    my_function { function } {
  }

  template<typename PARSER, typename FUNCTION>
  auto Into<PARSER, FUNCTION>::operator()(StateType const &state) const -> ResultType {
    auto const lhs_result = my_parser(state);

    if (lhs_result.has_lhs()) {
      auto const &reject = lhs_result.get_lhs();
      auto const &error = reject.get_error();
      return errors::make_reject(state, error);
    }

    auto const &lhs_accept = lhs_result.get_rhs();
    auto const rhs_parser = my_function(lhs_accept.get_value());

    auto const rhs_result = rhs_parser(lhs_accept.get_state());

    if (rhs_result.has_lhs()) {
      auto const &reject = rhs_result.get_lhs();
      auto const &error = reject.get_error();
      return errors::make_reject(state, error);
    }

    auto const &rhs_accept = rhs_result.get_rhs();
    return errors::make_accept(rhs_accept.get_state(), rhs_accept.get_value());
  }

  template<typename PARSER>
  SequenceOf<PARSER>::SequenceOf(PARSER const &parser, std::size_t const repetitions) :
    my_parser { parser },
    my_repetitions { repetitions } {
  }

  template<typename PARSER>
  auto SequenceOf<PARSER>::operator()(StateType const &state) const -> ResultType {
    std::vector<ElementType> values {};
    StateType next_state { state };

    for (std::size_t i { 0 }; i < my_repetitions; ++i) {
      auto const result { my_parser(next_state) };

      if (result.has_lhs()) {
        auto const &reject = result.get_lhs();
        auto const &error = reject.get_error();
        return errors::make_reject(state, error);
      }

      auto const &accept { result.get_rhs() };
      values.emplace_back(accept.get_value());
      next_state = accept.get_state();
    }

    return errors::make_accept(next_state, values);
  }

  template<typename LHS_PARSER, typename RHS_PARSER>
  auto make_one_of(LHS_PARSER const &lhs_parser, RHS_PARSER const &rhs_parser) -> OneOf<LHS_PARSER, RHS_PARSER> {
    return { lhs_parser, rhs_parser };
  }

  template<typename LHS_PARSER, typename RHS_PARSER>
  auto make_pair(LHS_PARSER const &lhs_parser, RHS_PARSER const &rhs_parser) -> Pair<LHS_PARSER, RHS_PARSER> {
    return { lhs_parser, rhs_parser };
  }

  template<typename ...PARSERS>
  auto make_tuple(PARSERS &&...parsers) -> Tuple<PARSERS...> {
    return { std::forward<PARSERS>(parsers)... };
  }

  template<typename PARSER, typename FUNCTION>
  auto make_into(PARSER const &parser, FUNCTION const &function) -> Into<PARSER, FUNCTION> {
    return { parser, function };
  }

  template<typename PARSER>
  auto make_sequence_of(PARSER const &parser, std::size_t const repetitions) -> SequenceOf<PARSER> {
    return { parser, repetitions };
  }

  namespace operators {
    template<typename LHS_PARSER, typename RHS_PARSER>
    auto operator^(LHS_PARSER const &lhs_parser, RHS_PARSER const &rhs_parser) -> OneOf<LHS_PARSER, RHS_PARSER> {
      return { lhs_parser, rhs_parser };
    }

    template<typename LHS_PARSER, typename RHS_PARSER>
    auto operator&(LHS_PARSER const &lhs_parser, RHS_PARSER const &rhs_parser) -> Pair<LHS_PARSER, RHS_PARSER> {
      return { lhs_parser, rhs_parser };
    }

    template<typename PARSER, typename FUNCTION>
    auto operator>>(PARSER const &parser, FUNCTION const &function) -> Into<PARSER, FUNCTION> {
      return { parser, function };
    }

    template<typename PARSER>
    auto operator*(PARSER const &parser, std::size_t const repetitions) -> SequenceOf<PARSER> {
      return { parser, repetitions };
    }
  }
}

#endif // CRUCIBLE_PARSING_COMBINATORS_INL