dsl/code_point.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_DSL_CODE_POINT_HPP_INCLUDED
5 #define LEXY_DSL_CODE_POINT_HPP_INCLUDED
6 
7 #include <lexy/code_point.hpp>
8 #include <lexy/dsl/base.hpp>
10 
11 namespace lexyd
12 {
13 template <typename Predicate>
14 struct _cp : char_class_base<_cp<Predicate>>
15 {
17  {
18  if constexpr (std::is_void_v<Predicate>)
19  return "code-point";
20  else
21  return lexy::_detail::type_name<Predicate>();
22  }
23 
25  {
26  if constexpr (std::is_void_v<Predicate>)
27  {
29  result.insert(0x00, 0x7F);
30  return result;
31  }
32  else
33  {
35  for (auto c = 0; c <= 0x7F; ++c)
36  if (Predicate{}(lexy::code_point(char32_t(c))))
37  result.insert(c);
38  return result;
39  }
40  }
41 
42  static constexpr bool char_class_match_cp([[maybe_unused]] char32_t cp)
43  {
44  if constexpr (std::is_void_v<Predicate>)
45  return true;
46  else
47  return Predicate{}(lexy::code_point(cp));
48  }
49 
50  //=== dsl ===//
51  template <typename P>
52  constexpr auto if_() const
53  {
54  static_assert(std::is_void_v<Predicate>);
55  return _cp<P>{};
56  }
57 
58  template <char32_t Low, char32_t High>
59  constexpr auto range() const
60  {
61  struct predicate
62  {
63  static LEXY_CONSTEVAL auto name()
64  {
65  return "code-point.range";
66  }
67 
68  constexpr bool operator()(lexy::code_point cp) const
69  {
70  return Low <= cp.value() && cp.value() <= High;
71  }
72  };
73 
74  return if_<predicate>();
75  }
76 
77  template <char32_t... CPs>
78  constexpr auto set() const
79  {
80  struct predicate
81  {
82  static LEXY_CONSTEVAL auto name()
83  {
84  return "code-point.set";
85  }
86 
87  constexpr bool operator()(lexy::code_point cp) const
88  {
89  return ((cp.value() == CPs) || ...);
90  }
91  };
92 
93  return if_<predicate>();
94  }
95 
96  constexpr auto ascii() const
97  {
98  struct predicate
99  {
100  static LEXY_CONSTEVAL auto name()
101  {
102  return "code-point.ASCII";
103  }
104 
105  constexpr bool operator()(lexy::code_point cp) const
106  {
107  return cp.is_ascii();
108  }
109  };
110 
111  return if_<predicate>();
112  }
113  constexpr auto bmp() const
114  {
115  struct predicate
116  {
117  static LEXY_CONSTEVAL auto name()
118  {
119  return "code-point.BMP";
120  }
121 
122  constexpr bool operator()(lexy::code_point cp) const
123  {
124  return cp.is_bmp();
125  }
126  };
127 
128  return if_<predicate>();
129  }
130  constexpr auto noncharacter() const
131  {
132  struct predicate
133  {
134  static LEXY_CONSTEVAL auto name()
135  {
136  return "code-point.non-character";
137  }
138 
139  constexpr bool operator()(lexy::code_point cp) const
140  {
141  return cp.is_noncharacter();
142  }
143  };
144 
145  return if_<predicate>();
146  }
147 
148  template <lexy::code_point::general_category_t Category>
149  constexpr auto general_category() const
150  {
151  struct predicate
152  {
153  static LEXY_CONSTEVAL auto name()
154  {
155  return lexy::_detail::general_category_name(Category);
156  }
157 
158  constexpr bool operator()(lexy::code_point cp) const
159  {
160  // Note: can't use `cp.is_noncharacter()` for `Cn` as `Cn` also includes all code
161  // points that are currently unassigned.
162  if constexpr (Category == lexy::code_point::Cc)
163  return cp.is_control();
164  else if constexpr (Category == lexy::code_point::Cs)
165  return cp.is_surrogate();
166  else if constexpr (Category == lexy::code_point::Co)
167  return cp.is_private_use();
168  else
169  return cp.general_category() == Category;
170  }
171  };
172 
173  return if_<predicate>();
174  }
175 
176  template <const auto& GcGroup>
177  struct _group_pred;
178  template <lexy::code_point::general_category_t... Cats,
179  const lexy::code_point::_gc_group<Cats...>& GcGroup>
181  {
182  static LEXY_CONSTEVAL auto name()
183  {
184  return GcGroup.name;
185  }
186 
187  constexpr bool operator()(lexy::code_point cp) const
188  {
189  return cp.general_category() == GcGroup;
190  }
191  };
192  template <const auto& GcGroup>
193  constexpr auto general_category() const
194  {
195  return if_<_group_pred<GcGroup>>();
196  }
197 };
198 
200 constexpr auto code_point = _cp<void>{};
201 } // namespace lexyd
202 
203 namespace lexy
204 {
205 // The void-version without predicate logically matches any input (modulo encoding errors, of
206 // course).
207 template <>
208 inline constexpr auto token_kind_of<lexy::dsl::_cp<void>> = lexy::any_token_kind;
209 } // namespace lexy
210 
211 #endif // LEXY_DSL_CODE_POINT_HPP_INCLUDED
212 
lexyd::_cp::ascii
constexpr auto ascii() const
Definition: dsl/code_point.hpp:96
LEXY_CONSTEVAL
#define LEXY_CONSTEVAL
Definition: config.hpp:98
lexy::any_token_kind
@ any_token_kind
Definition: grammar.hpp:88
lexyd::_cp::noncharacter
constexpr auto noncharacter() const
Definition: dsl/code_point.hpp:130
lexyd::_cp::if_
constexpr auto if_() const
Definition: dsl/code_point.hpp:52
lexy::code_point::is_control
constexpr bool is_control() const noexcept
Definition: code_point.hpp:45
lexyd::_cp::char_class_name
static LEXY_CONSTEVAL auto char_class_name()
Definition: dsl/code_point.hpp:16
lexyd::code_point
constexpr auto code_point
Matches a single unicode code point in the current unicode encoding.
Definition: dsl/code_point.hpp:200
lexy::code_point::is_ascii
constexpr bool is_ascii() const noexcept
Definition: code_point.hpp:32
lexyd::_cp::_group_pred< GcGroup >
Definition: dsl/code_point.hpp:180
lexy::_detail::ascii_set::insert
constexpr void insert(int c)
Definition: char_class.hpp:54
lexy
Definition: any_ref.hpp:12
char_class.hpp
lexy::code_point::general_category_t
general_category_t
Definition: code_point.hpp:76
lexy::code_point::value
constexpr auto value() const noexcept
Definition: code_point.hpp:26
lexy::code_point::is_noncharacter
constexpr bool is_noncharacter() const noexcept
Definition: code_point.hpp:59
lexy::code_point::general_category
LEXY_UNICODE_CONSTEXPR general_category_t general_category() const noexcept
lexy::code_point::is_bmp
constexpr bool is_bmp() const noexcept
Definition: code_point.hpp:36
lexyd::_cp::range
constexpr auto range() const
Definition: dsl/code_point.hpp:59
lexyd::_cp::char_class_ascii
static LEXY_CONSTEVAL auto char_class_ascii()
Definition: dsl/code_point.hpp:24
lexyd::_cp::_group_pred
Definition: dsl/code_point.hpp:177
lexyd::_cp::char_class_match_cp
static constexpr bool char_class_match_cp([[maybe_unused]] char32_t cp)
Definition: dsl/code_point.hpp:42
lexyd::char_class_base
Definition: char_class.hpp:170
lexyd::_cp::set
constexpr auto set() const
Definition: dsl/code_point.hpp:78
lexy::_detail::general_category_name
constexpr const char * general_category_name(lexy::code_point::general_category_t category)
Definition: code_point.hpp:181
lexyd::_cp
Definition: dsl/code_point.hpp:14
base.hpp
lexyd::_cp::_group_pred< GcGroup >::operator()
constexpr bool operator()(lexy::code_point cp) const
Definition: dsl/code_point.hpp:187
lexyd::_cp::bmp
constexpr auto bmp() const
Definition: dsl/code_point.hpp:113
lexy::code_point::is_surrogate
constexpr bool is_surrogate() const noexcept
Definition: code_point.hpp:49
lexy::code_point::is_private_use
constexpr bool is_private_use() const noexcept
Definition: code_point.hpp:53
lexy::code_point::_gc_group
Definition: code_point.hpp:122
lexyd::_cp::general_category
constexpr auto general_category() const
Definition: dsl/code_point.hpp:149
code_point.hpp
lexyd::_cp::_group_pred< GcGroup >::name
static LEXY_CONSTEVAL auto name()
Definition: dsl/code_point.hpp:182
lexyd
Definition: trace.hpp:22
lexy::_detail::ascii_set
Definition: char_class.hpp:14
lexy::code_point
A unicode code point.
Definition: code_point.hpp:20


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