Echo Writes Code

text.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
#ifndef CRUCIBLE_PARSING_TEXT_HPP
#define CRUCIBLE_PARSING_TEXT_HPP

#include "crucible/core/constant_error.inl"
#include "crucible/core/error_chain.inl"
#include "crucible/core/none.hpp"
#include "crucible/parsing/result.inl"

#include <cstddef>
#include <string_view>

namespace crucible::parsing::text {
  struct CharacterSequenceMismatch final : core::constant_error::ConstantError<CharacterSequenceMismatch> {
    static constexpr char const message[] = "Character sequence mismatch";
  };

  struct TextError final : core::error_chain::ErrorChain<TextError, CharacterSequenceMismatch> {
    using ErrorChain::ErrorChain;

    static constexpr char const message[] = "Parse error in text primitive";
  };

  class TextState final {
  public:
    explicit TextState(std::string_view const &input);

    TextState(std::string_view const &input, std::size_t const code_unit_offset);

    [[nodiscard]]
    auto is_finished() const -> bool;

    [[nodiscard]]
    auto get_remaining_code_units_in_input() const -> std::string_view;

    [[nodiscard]]
    auto advance_by_code_units(std::size_t const code_unit_count) const -> TextState;

    [[nodiscard]]
    auto format_location() const -> std::string;

  private:
    std::string_view my_input {};

    std::size_t my_code_unit_offset { 0 };
  };

  class CharacterSequence final {
  public:
    using StateType = TextState;

    using ErrorType = TextError;

    using ValueType = core::none::None;

    using RejectType = result::Reject<StateType, ErrorType>;

    using AcceptType = result::Accept<StateType, ValueType>;

    using ResultType = core::result::Result<RejectType, AcceptType>;

    explicit CharacterSequence(std::string_view character_sequence);

    [[nodiscard]]
    auto operator()(StateType const &state) const -> ResultType;

  private:
    std::string my_character_sequence {};
  };
}

#endif // CRUCIBLE_PARSING_TEXT_HPP