input/base.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2023 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  constexpr explicit _rr(Iterator begin, Sentinel end) noexcept : _cur(begin), _end(end)
22  {
24  }
25 
26  constexpr auto peek() const noexcept
27  {
28  if (_cur == _end)
29  return encoding::eof();
30  else
31  return encoding::to_int_type(static_cast<typename encoding::char_type>(*_cur));
32  }
33 
34  constexpr void bump() noexcept
35  {
37  ++_cur;
38  }
39 
40  constexpr iterator position() const noexcept
41  {
42  return _cur;
43  }
44 
45  constexpr void set_position(iterator new_pos) noexcept
46  {
48  _cur = new_pos;
49  }
50 
51 private:
52  Iterator _cur;
54 };
55 
56 // A special version where the iterators are pointers.
57 template <typename Encoding>
58 LEXY_INSTANTIATION_NEWTYPE(_pr, _rr, Encoding, const typename Encoding::char_type*);
59 
60 // Aliases for the most common encodings.
65 
66 template <typename Encoding, typename Iterator, typename Sentinel>
67 constexpr auto _range_reader(Iterator begin, Sentinel end)
68 {
69  if constexpr (std::is_pointer_v<Iterator>)
70  {
71  if constexpr (std::is_same_v<Encoding, lexy::default_encoding>)
72  return _prd(begin, end);
73  else if constexpr (std::is_same_v<Encoding, lexy::utf8_encoding>)
74  return _pr8(begin, end);
75  else if constexpr (std::is_same_v<Encoding, lexy::utf8_char_encoding>)
76  return _prc(begin, end);
77  else if constexpr (std::is_same_v<Encoding, lexy::byte_encoding>)
78  return _prb(begin, end);
79  else
80  return _pr<Encoding>(begin, end);
81  }
82  else
83  {
85  }
86 }
87 } // namespace lexy
88 
89 namespace lexy
90 {
91 template <typename Input>
92 using input_reader = decltype(LEXY_DECLVAL(Input).reader());
93 
94 template <typename Input>
95 constexpr bool input_is_view = std::is_trivially_copyable_v<Input>;
96 
97 template <typename Reader, typename CharT>
99  = (std::is_same_v<CharT, typename Reader::encoding::char_type>)
100  || Reader::encoding::template is_secondary_char_type<CharT>();
101 } // namespace lexy
102 
103 namespace lexy
104 {
105 template <typename Reader>
107 {
108  Reader _reader;
109 
110  constexpr explicit _partial_input(Reader r) : _reader(r) {}
111 
112  constexpr Reader reader() const&
113  {
114  return _reader;
115  }
116 };
117 
118 template <typename Reader>
119 constexpr auto partial_input(const Reader&, typename Reader::iterator begin,
120  typename Reader::iterator end)
121 {
122  return _partial_input(_range_reader<typename Reader::encoding>(begin, end));
123 }
124 
126 template <typename Reader>
127 constexpr auto partial_input(const Reader& reader, typename Reader::iterator end)
128 {
129  return partial_input(reader, reader.position(), end);
130 }
131 } // namespace lexy
132 
133 #endif // LEXY_INPUT_BASE_HPP_INCLUDED
134 
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:26
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:40
encoding.hpp
lexy::_rr::iterator
Iterator iterator
Definition: input/base.hpp:19
lexy::_partial_input
Definition: input/base.hpp:106
lexy::_rr::bump
constexpr void bump() noexcept
Definition: input/base.hpp:34
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::_rr::set_position
constexpr void set_position(iterator new_pos) noexcept
Definition: input/base.hpp:45
lexy::char_type_compatible_with_reader
constexpr bool char_type_compatible_with_reader
Definition: input/base.hpp:99
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:52
lexy::_rr::_end
LEXY_EMPTY_MEMBER Sentinel _end
Definition: input/base.hpp:53
lexy::_partial_input::_partial_input
constexpr _partial_input(Reader r)
Definition: input/base.hpp:110
lexy::utf8_encoding
An encoding where the input is assumed to be valid UTF-8.
Definition: encoding.hpp:84
lexy::_partial_input::reader
constexpr Reader reader() const &
Definition: input/base.hpp:112
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:119
lexy::_range_reader
constexpr auto _range_reader(Iterator begin, Sentinel end)
Definition: input/base.hpp:67
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:108
LEXY_EMPTY_MEMBER
#define LEXY_EMPTY_MEMBER
Definition: config.hpp:170
lexy::input_is_view
constexpr bool input_is_view
Definition: input/base.hpp:95
LEXY_DECLVAL
#define LEXY_DECLVAL(...)
Definition: config.hpp:24
lexy::_rr::_rr
constexpr _rr(Iterator begin, Sentinel end) noexcept
Definition: input/base.hpp:21
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:92


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07