input/base.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2024 Jonathan Müller and lexy contributors
2 // SPDX-License-Identifier: BSL-1.0
3 
4 #ifndef LEXY_INPUT_BASE_HPP_INCLUDED
5 #define LEXY_INPUT_BASE_HPP_INCLUDED
6 
9 #include <lexy/encoding.hpp>
10 
11 namespace lexy
12 {
13 // A generic reader from an iterator range.
14 template <typename Encoding, typename Iterator, typename Sentinel = Iterator>
15 class _rr
16 {
17 public:
18  using encoding = Encoding;
19  using iterator = Iterator;
20 
21  struct marker
22  {
24 
25  constexpr iterator position() const noexcept
26  {
27  return _it;
28  }
29  };
30 
31  constexpr explicit _rr(Iterator begin, Sentinel end) noexcept : _cur(begin), _end(end)
32  {
34  }
35 
36  constexpr auto peek() const noexcept
37  {
38  if (_cur == _end)
39  return encoding::eof();
40  else
41  return encoding::to_int_type(static_cast<typename encoding::char_type>(*_cur));
42  }
43 
44  constexpr void bump() noexcept
45  {
47  ++_cur;
48  }
49 
50  constexpr iterator position() const noexcept
51  {
52  return _cur;
53  }
54 
55  constexpr marker current() const noexcept
56  {
57  return {_cur};
58  }
59  constexpr void reset(marker m) noexcept
60  {
62  _cur = m._it;
63  }
64 
65 private:
66  Iterator _cur;
68 };
69 
70 // A special version where the iterators are pointers.
71 template <typename Encoding>
72 LEXY_INSTANTIATION_NEWTYPE(_pr, _rr, Encoding, const typename Encoding::char_type*);
73 
74 // Aliases for the most common encodings.
79 
80 template <typename Encoding, typename Iterator, typename Sentinel>
81 constexpr auto _range_reader(Iterator begin, Sentinel end)
82 {
83  if constexpr (std::is_pointer_v<Iterator>)
84  {
85  if constexpr (std::is_same_v<Encoding, lexy::default_encoding>)
86  return _prd(begin, end);
87  else if constexpr (std::is_same_v<Encoding, lexy::utf8_encoding>)
88  return _pr8(begin, end);
89  else if constexpr (std::is_same_v<Encoding, lexy::utf8_char_encoding>)
90  return _prc(begin, end);
91  else if constexpr (std::is_same_v<Encoding, lexy::byte_encoding>)
92  return _prb(begin, end);
93  else
94  return _pr<Encoding>(begin, end);
95  }
96  else
97  {
99  }
100 }
101 } // namespace lexy
102 
103 namespace lexy
104 {
105 template <typename Input>
106 using input_reader = decltype(LEXY_DECLVAL(Input).reader());
107 
108 template <typename Input>
109 constexpr bool input_is_view = std::is_trivially_copyable_v<Input>;
110 
111 template <typename Reader, typename CharT>
113  = (std::is_same_v<CharT, typename Reader::encoding::char_type>)
114  || Reader::encoding::template is_secondary_char_type<CharT>();
115 } // namespace lexy
116 
117 namespace lexy
118 {
119 template <typename Reader>
121 {
122  Reader _reader;
123 
124  constexpr explicit _partial_input(Reader r) : _reader(r) {}
125 
126  constexpr Reader reader() const&
127  {
128  return _reader;
129  }
130 };
131 
132 template <typename Reader>
133 constexpr auto partial_input(const Reader&, typename Reader::iterator begin,
134  typename Reader::iterator end)
135 {
136  return _partial_input(_range_reader<typename Reader::encoding>(begin, end));
137 }
138 
140 template <typename Reader>
141 constexpr auto partial_input(const Reader& reader, typename Reader::iterator end)
142 {
143  return partial_input(reader, reader.position(), end);
144 }
145 } // namespace lexy
146 
147 #endif // LEXY_INPUT_BASE_HPP_INCLUDED
148 
lexy::_rr::marker::_it
iterator _it
Definition: input/base.hpp:23
lexy::LEXY_INSTANTIATION_NEWTYPE
LEXY_INSTANTIATION_NEWTYPE(_pr, _rr, Encoding, const typename Encoding::char_type *)
magic_enum::char_type
string_view::value_type char_type
Definition: magic_enum.hpp:145
lexy::_rr::peek
constexpr auto peek() const noexcept
Definition: input/base.hpp:36
lexy::_detail::precedes
constexpr bool precedes([[maybe_unused]] Iterator first, [[maybe_unused]] Sentinel after)
Definition: iterator.hpp:82
iterator.hpp
config.hpp
lexy::_rr::position
constexpr iterator position() const noexcept
Definition: input/base.hpp:50
encoding.hpp
lexy::_rr::iterator
Iterator iterator
Definition: input/base.hpp:19
lexy::_partial_input
Definition: input/base.hpp:120
lexy::_rr::bump
constexpr void bump() noexcept
Definition: input/base.hpp:44
lexy
Definition: any_ref.hpp:12
LEXY_PRECONDITION
#define LEXY_PRECONDITION(Expr)
Definition: assert.hpp:36
cx::end
constexpr auto end(const C &c) -> decltype(c.end())
Definition: wildcards.hpp:686
lexy::_rr::encoding
Encoding encoding
Definition: input/base.hpp:18
lexy::char_type_compatible_with_reader
constexpr bool char_type_compatible_with_reader
Definition: input/base.hpp:113
lexy::_rr::reset
constexpr void reset(marker m) noexcept
Definition: input/base.hpp:59
lexy::byte_encoding
An encoding where the input is just raw bytes, not characters.
Definition: encoding.hpp:180
lexy::_rr::_cur
Iterator _cur
Definition: input/base.hpp:66
lexy::_rr::_end
LEXY_EMPTY_MEMBER Sentinel _end
Definition: input/base.hpp:67
lexy::_partial_input::_partial_input
constexpr _partial_input(Reader r)
Definition: input/base.hpp:124
lexy::utf8_encoding
An encoding where the input is assumed to be valid UTF-8.
Definition: encoding.hpp:84
lexy::_rr::marker
Definition: input/base.hpp:21
lexy::_rr::current
constexpr marker current() const noexcept
Definition: input/base.hpp:55
lexy::_partial_input::reader
constexpr Reader reader() const &
Definition: input/base.hpp:126
lexy::default_encoding
An encoding where the input is some 8bit encoding (ASCII, UTF-8, extended ASCII etc....
Definition: encoding.hpp:27
lexy::partial_input
constexpr auto partial_input(const Reader &, typename Reader::iterator begin, typename Reader::iterator end)
Definition: input/base.hpp:133
lexy::_range_reader
constexpr auto _range_reader(Iterator begin, Sentinel end)
Definition: input/base.hpp:81
lexy::utf8_char_encoding
An encoding where the input is assumed to be valid UTF-8, but the char type is char.
Definition: encoding.hpp:108
lexy::_rr
Definition: input/base.hpp:15
cx::begin
constexpr auto begin(const C &c) -> decltype(c.begin())
Definition: wildcards.hpp:661
lexy::_partial_input::_reader
Reader _reader
Definition: input/base.hpp:122
LEXY_EMPTY_MEMBER
#define LEXY_EMPTY_MEMBER
Definition: config.hpp:193
lexy::input_is_view
constexpr bool input_is_view
Definition: input/base.hpp:109
LEXY_DECLVAL
#define LEXY_DECLVAL(...)
Definition: config.hpp:32
lexy::_rr::_rr
constexpr _rr(Iterator begin, Sentinel end) noexcept
Definition: input/base.hpp:31
lexyd::eof
constexpr auto eof
Matches EOF.
Definition: eof.hpp:72
lexy::input_reader
decltype(LEXY_DECLVAL(Input).reader()) input_reader
Definition: input/base.hpp:106
lexy::_rr::marker::position
constexpr iterator position() const noexcept
Definition: input/base.hpp:25


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:16