lexeme.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_LEXEME_HPP_INCLUDED
5 #define LEXY_LEXEME_HPP_INCLUDED
6 
10 #include <lexy/encoding.hpp>
11 #include <lexy/input/base.hpp>
12 
13 namespace lexy
14 {
15 template <typename Reader>
16 class lexeme
17 {
18 public:
19  using encoding = typename Reader::encoding;
20  using char_type = typename encoding::char_type;
21  using iterator = typename Reader::iterator;
22 
23  constexpr lexeme() noexcept : _begin(), _end() {}
24  constexpr lexeme(iterator begin, iterator end) noexcept : _begin(begin), _end(end) {}
25  constexpr lexeme(iterator pos, std::size_t size) noexcept
26  : _begin(pos), _end(_detail::next(pos, size))
27  {}
28 
29  constexpr explicit lexeme(const Reader& reader, iterator begin) noexcept
30  : _begin(begin), _end(reader.position())
31  {}
32 
33  constexpr bool empty() const noexcept
34  {
35  return _begin == _end;
36  }
37 
38  constexpr iterator begin() const noexcept
39  {
40  return _begin;
41  }
42  constexpr iterator end() const noexcept
43  {
44  return _end;
45  }
46 
47  constexpr const char_type* data() const noexcept
48  {
49  static_assert(std::is_pointer_v<iterator>);
50  return _begin;
51  }
52 
53  constexpr std::size_t size() const noexcept
54  {
55  return static_cast<std::size_t>(_end - _begin);
56  }
57 
58  constexpr char_type operator[](std::size_t idx) const noexcept
59  {
60  LEXY_PRECONDITION(idx < size());
61  return _begin[idx];
62  }
63 
64 private:
66 };
67 
68 template <typename Reader>
69 lexeme(const Reader&, typename Reader::iterator) -> lexeme<typename Reader::canonical_reader>;
70 
71 template <typename Input>
73 } // namespace lexy
74 
75 namespace lexy::_detail
76 {
77 template <typename Reader>
79 {
80  if constexpr (lexy::_detail::is_random_access_iterator<typename Reader::iterator>)
81  {
82  if (lhs.size() != rhs.size())
83  return false;
84  }
85 
86  auto lhs_cur = lhs.begin();
87  auto rhs_cur = rhs.begin();
88  while (lhs_cur != lhs.end() && rhs_cur != rhs.end())
89  {
90  if (*lhs_cur != *rhs_cur)
91  return false;
92  ++lhs_cur;
93  ++rhs_cur;
94  }
95  return lhs_cur == lhs.end() && rhs_cur == rhs.end();
96 }
97 } // namespace lexy::_detail
98 
99 #endif // LEXY_LEXEME_HPP_INCLUDED
100 
lexy::lexeme< Input >::char_type
typename encoding::char_type char_type
Definition: lexeme.hpp:20
magic_enum::char_type
string_view::value_type char_type
Definition: magic_enum.hpp:145
iterator.hpp
config.hpp
encoding.hpp
lexy::lexeme::_begin
iterator _begin
Definition: lexeme.hpp:65
lexy::lexeme::operator[]
constexpr char_type operator[](std::size_t idx) const noexcept
Definition: lexeme.hpp:58
lexy::lexeme::empty
constexpr bool empty() const noexcept
Definition: lexeme.hpp:33
lexy::lexeme< Input >::iterator
typename Input ::iterator iterator
Definition: lexeme.hpp:21
lexy::lexeme::_end
iterator _end
Definition: lexeme.hpp:65
lexy::lexeme::begin
constexpr iterator begin() const noexcept
Definition: lexeme.hpp:38
lexy::_detail::next
constexpr Iterator next(Iterator iter)
Definition: iterator.hpp:38
lexy::lexeme::lexeme
constexpr lexeme(const Reader &reader, iterator begin) noexcept
Definition: lexeme.hpp:29
lexy
Definition: any_ref.hpp:12
LEXY_PRECONDITION
#define LEXY_PRECONDITION(Expr)
Definition: assert.hpp:36
lexy::lexeme::lexeme
constexpr lexeme() noexcept
Definition: lexeme.hpp:23
lexy::lexeme::data
constexpr const char_type * data() const noexcept
Definition: lexeme.hpp:47
lexy::lexeme::size
constexpr std::size_t size() const noexcept
Definition: lexeme.hpp:53
lexy::lexeme< Input >::encoding
typename Input ::encoding encoding
Definition: lexeme.hpp:19
assert.hpp
lexy::lexeme::lexeme
constexpr lexeme(iterator begin, iterator end) noexcept
Definition: lexeme.hpp:24
lexy::lexeme::end
constexpr iterator end() const noexcept
Definition: lexeme.hpp:42
lexy::_detail
Definition: any_ref.hpp:12
lexy::lexeme::lexeme
constexpr lexeme(iterator pos, std::size_t size) noexcept
Definition: lexeme.hpp:25
lexy::lexeme
Definition: lexeme.hpp:16
lexy::lexeme
lexeme(const Reader &, typename Reader::iterator) -> lexeme< typename Reader::canonical_reader >
base.hpp
lexy::_detail::equal_lexemes
constexpr bool equal_lexemes(lexeme< Reader > lhs, lexeme< Reader > rhs)
Definition: lexeme.hpp:78


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