Go to the documentation of this file.
4 #ifndef LEXY_DSL_BYTE_HPP_INCLUDED
5 #define LEXY_DSL_BYTE_HPP_INCLUDED
16 template <std::
size_t N,
typename Predicate>
26 if constexpr (!std::is_void_v<Predicate>)
28 constexpr
auto predicate = Predicate{};
37 template <
typename Reader,
typename Indices = lexy::_detail::make_index_sequence<N>>
40 template <
typename Reader, std::size_t... Idx>
43 static_assert(lexy::is_byte_encoding<typename Reader::encoding>);
44 typename Reader::marker
end;
46 constexpr
explicit tp(
const Reader& reader) :
end(reader.current()) {}
52 = ((
_match(reader.peek()) ? (reader.bump(),
true) : ((
void)Idx,
false)) && ...);
53 end = reader.current();
57 template <
typename Context>
61 = std::is_void_v<Predicate> ?
"byte" : lexy::_detail::type_name<Predicate>();
69 constexpr
auto if_()
const
71 static_assert(std::is_void_v<Predicate>);
75 template <
unsigned char Low,
unsigned char High>
85 constexpr
bool operator()(
unsigned char byte)
const
87 return Low <=
byte &&
byte <= High;
91 return if_<predicate>();
94 template <
unsigned char... Bytes>
95 constexpr
auto set()
const
104 constexpr
bool operator()(
unsigned char byte)
const
106 return ((
byte == Bytes) || ...);
110 return if_<predicate>();
122 constexpr
bool operator()(
unsigned char byte)
const
128 return if_<predicate>();
136 template <std::
size_t N>
142 template <std::
size_t N>
149 template <std::
size_t N,
unsigned char Padding = 0>
152 template <
typename Context,
typename Reader,
typename Iterator>
155 constexpr
unsigned char str[] = {Padding, 0};
156 for (
auto iter =
begin; iter !=
end; ++iter)
158 if (*iter != Padding)
166 template <
typename Reader>
169 static_assert(lexy::is_byte_encoding<typename Reader::encoding>);
170 typename Reader::marker
end;
172 constexpr
auto try_parse(
const void*,
const Reader& reader)
175 auto result = parser.try_parse(reader);
180 template <
typename Context>
184 template <
typename NextParser,
typename Context,
typename... Args>
187 auto begin = reader.position();
197 template <
typename NextParser>
200 template <
typename Context,
typename Reader,
typename... Args>
203 static_assert(lexy::is_byte_encoding<typename Reader::encoding>);
204 auto begin = reader.position();
207 auto end = reader.position();
218 template <std::
size_t N,
unsigned char Padding = 0>
230 #if LEXY_IS_LITTLE_ENDIAN
237 template <std::
size_t N>
240 if constexpr (N == 1)
241 return std::uint_least8_t(0);
242 else if constexpr (N == 2)
243 return std::uint_least16_t(0);
244 else if constexpr (N == 4)
245 return std::uint_least32_t(0);
246 else if constexpr (N == 8)
247 return std::uint_least64_t(0);
250 static_assert(N == 1,
"invalid byte integer size");
255 template <std::
size_t N>
256 using bint = decltype(_bint<N>());
265 return "mismatched byte count";
272 template <std::
size_t N,
int Endianness,
typename Rule =
void>
277 template <
typename NextParser,
typename Indices = lexy::_detail::make_index_sequence<N>>
280 template <
typename NextParser, std::size_t... Idx>
283 template <
typename Context,
typename Reader,
typename... Args>
285 typename Reader::iterator
begin,
286 typename Reader::iterator
end, Args&&... args)
296 if constexpr (N == 1)
299 result =
static_cast<unsigned char>(*begin);
305 ((result =
static_cast<decltype(result)
>(result << 8),
306 result =
static_cast<decltype(result)
>(result | *
begin++), (
void)Idx),
314 ((result =
static_cast<decltype(result)
>(result << 8),
315 result =
static_cast<decltype(result)
>(result |
buffer[N - Idx - 1])),
324 template <
typename Reader>
327 static_assert(lexy::is_byte_encoding<typename Reader::encoding>);
328 typename Reader::marker
end;
330 constexpr
auto try_parse(
const void*,
const Reader& reader)
333 auto result = parser.try_parse(reader);
338 template <
typename Context>
342 template <
typename NextParser,
typename Context,
typename... Args>
345 auto begin = reader.position();
354 template <
typename NextParser>
357 template <
typename Context,
typename Reader,
typename... Args>
360 static_assert(lexy::is_byte_encoding<typename Reader::encoding>);
361 auto begin = reader.position();
362 if (!_rule::token_parse(context, reader))
364 auto end = reader.position();
371 template <
typename Token>
374 static_assert(lexy::is_token_rule<Token>);
375 static_assert(std::is_void_v<Rule>);
399 #endif // LEXY_DSL_BYTE_HPP_INCLUDED
integer_sequence< std::size_t, Indices... > index_sequence
constexpr auto bytes
Matches N arbitrary bytes.
constexpr auto if_() const
constexpr auto padding_bytes
constexpr auto little_bint16
constexpr auto little_bint32
constexpr auto big_bint64
std::conditional_t< std::is_void_v< T >, Fallback, T > type_or
static constexpr bool _match(lexy::byte_encoding::int_type cur)
decltype(_bint< N >()) bint
constexpr auto range() const
constexpr auto end(const C &c) -> decltype(c.end())
constexpr auto bint8
Matches one byte and converts it into an 8-bit integer.
constexpr auto try_parse(const void *, const Reader &reader)
constexpr auto parse(const Input &input, const ErrorCallback &callback)
Parses the production into a value, invoking the callback on error.
constexpr tp(const Reader &reader)
static constexpr void _validate(Context &context, const Reader &, Iterator begin, Iterator end)
constexpr auto try_parse(const void *, const Reader &reader)
buffer(const CharT *, const CharT *) -> buffer< deduce_encoding< CharT >>
constexpr auto little_bint64
constexpr auto bint16
Matches two bytes and converts it into an 16-bit integer.
constexpr bool try_parse(Reader reader)
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
LEXY_PARSER_FUNC auto finish(Context &context, Reader &reader, Args &&... args)
constexpr auto big_bint32
constexpr void cancel(Context &)
constexpr std::size_t range_size(Iterator begin, Sentinel end)
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
constexpr auto big_bint16
constexpr auto begin(const C &c) -> decltype(c.begin())
static LEXY_CONSTEVAL int_type eof()
lexy::_detail::type_or< Rule, _b< N, void > > _rule
constexpr auto bint32
Matches four bytes and converts it into an 32-bit integer.
constexpr void cancel(Context &)
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, typename Reader::iterator begin, typename Reader::iterator end, Args &&... args)
constexpr void report_error(Context &context, const Reader &)
constexpr auto ascii() const
static LEXY_PARSER_FUNC bool parse(Context &context, Reader &reader, Args &&... args)
static LEXY_CONSTEVAL auto name()
constexpr auto set() const
typename TokenRule::template tp< Reader > token_parser_for
constexpr auto operator()(Token) const
Matches a specific token rule instead of arbitrary bytes.
constexpr auto bint64
Matches eight bytes and converts it into an 64-bit integer.
LEXY_PARSER_FUNC auto finish(Context &context, Reader &reader, Args &&... args)