lexeme.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_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 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  template <typename OtherReader, typename = std::enable_if_t<std::is_same_v<
34  typename Reader::iterator, typename OtherReader::iterator>>>
35  constexpr operator lexeme<OtherReader>() const noexcept
36  {
37  return lexeme<OtherReader>(this->begin(), this->end());
38  }
39 
40  constexpr bool empty() const noexcept
41  {
42  return _begin == _end;
43  }
44 
45  constexpr iterator begin() const noexcept
46  {
47  return _begin;
48  }
49  constexpr iterator end() const noexcept
50  {
51  return _end;
52  }
53 
54  constexpr const char_type* data() const noexcept
55  {
56  static_assert(std::is_pointer_v<iterator>);
57  return _begin;
58  }
59 
60  constexpr std::size_t size() const noexcept
61  {
62  return static_cast<std::size_t>(_end - _begin);
63  }
64 
65  constexpr char_type operator[](std::size_t idx) const noexcept
66  {
67  LEXY_PRECONDITION(idx < size());
68  return _begin[idx];
69  }
70 
71 private:
73 };
74 
75 template <typename Reader>
76 lexeme(const Reader&, typename Reader::iterator) -> lexeme<typename Reader::canonical_reader>;
77 
78 template <typename Input>
80 } // namespace lexy
81 
82 namespace lexy::_detail
83 {
84 template <typename Reader>
86 {
87  if constexpr (lexy::_detail::is_random_access_iterator<typename Reader::iterator>)
88  {
89  if (lhs.size() != rhs.size())
90  return false;
91  }
92 
93  auto lhs_cur = lhs.begin();
94  auto rhs_cur = rhs.begin();
95  while (lhs_cur != lhs.end() && rhs_cur != rhs.end())
96  {
97  if (*lhs_cur != *rhs_cur)
98  return false;
99  ++lhs_cur;
100  ++rhs_cur;
101  }
102  return lhs_cur == lhs.end() && rhs_cur == rhs.end();
103 }
104 } // namespace lexy::_detail
105 
106 #endif // LEXY_LEXEME_HPP_INCLUDED
107 
iterator.hpp
config.hpp
encoding.hpp
lexy::lexeme::_begin
iterator _begin
Definition: lexeme.hpp:72
lexy::lexeme::operator[]
constexpr char_type operator[](std::size_t idx) const noexcept
Definition: lexeme.hpp:65
lexy::lexeme::empty
constexpr bool empty() const noexcept
Definition: lexeme.hpp:40
lexy::lexeme< Input >::iterator
typename Input ::iterator iterator
Definition: lexeme.hpp:20
lexy::lexeme::_end
iterator _end
Definition: lexeme.hpp:72
lexy::lexeme::begin
constexpr iterator begin() const noexcept
Definition: lexeme.hpp:45
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:54
lexy::lexeme::size
constexpr std::size_t size() const noexcept
Definition: lexeme.hpp:60
LEXY_DECAY_DECLTYPE
#define LEXY_DECAY_DECLTYPE(...)
Definition: config.hpp:34
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:49
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< Input >::char_type
LEXY_DECAY_DECLTYPE(*LEXY_DECLVAL(iterator &)) char_type
Definition: lexeme.hpp:21
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:85
LEXY_DECLVAL
#define LEXY_DECLVAL(...)
Definition: config.hpp:32
detail::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: json.hpp:3095


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