bits.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_DSL_BITS_HPP_INCLUDED
5 #define LEXY_DSL_BITS_HPP_INCLUDED
6 
7 #include <lexy/dsl/base.hpp>
8 #include <lexy/dsl/token.hpp>
9 
10 //=== bit rules ===//
11 namespace lexyd::bit
12 {
14 {
15  unsigned mask;
16  unsigned value;
17 };
18 
19 struct _bit_rule
20 {};
21 
22 struct _b0 : _bit_rule
23 {
24  static constexpr auto size = 1u;
25 
26  static constexpr void apply(_bit_pattern& p)
27  {
28  p.mask <<= 1;
29  p.value <<= 1;
30 
31  p.mask |= 1;
32  }
33 };
34 
36 inline constexpr auto _0 = _b0{};
37 
38 struct _b1 : _bit_rule
39 {
40  static constexpr auto size = 1u;
41 
42  static constexpr void apply(_bit_pattern& p)
43  {
44  p.mask <<= 1;
45  p.value <<= 1;
46 
47  p.mask |= 1;
48  p.value |= 1;
49  }
50 };
51 
53 inline constexpr auto _1 = _b1{};
54 
55 template <unsigned Value>
56 struct _n : _bit_rule
57 {
58  static_assert(Value <= 0xF);
59 
60  static constexpr auto size = 4u;
61 
62  static constexpr void apply(_bit_pattern& p)
63  {
64  p.mask <<= 4;
65  p.value <<= 4;
66 
67  p.mask |= 0b1111;
68  p.value |= Value;
69  }
70 };
71 
73 template <unsigned Value>
74 constexpr auto nibble = _n<Value>{};
75 
76 template <unsigned N>
77 struct _b : _bit_rule
78 {
79  static_assert(N > 0);
80 
81  static constexpr auto size = N;
82 
83  static constexpr void apply(_bit_pattern& p)
84  {
85  p.mask <<= N;
86  p.value <<= N;
87  }
88 };
89 
91 inline constexpr auto _ = _b<1>{};
92 
94 template <unsigned N>
95 constexpr auto any = _b<N>{};
96 } // namespace lexyd::bit
97 
98 //=== bits ===//
99 namespace lexyd
100 {
101 template <unsigned Mask, unsigned Value>
102 struct _bits : token_base<_bits<Mask, Value>>
103 {
104  template <typename Reader>
105  struct tp
106  {
107  typename Reader::iterator end;
108 
109  constexpr explicit tp(const Reader& reader) : end(reader.position()) {}
110 
111  constexpr bool try_parse(Reader reader)
112  {
113  static_assert(std::is_same_v<typename Reader::encoding, lexy::byte_encoding>);
114 
115  auto byte = reader.peek();
116  if (byte == Reader::encoding::eof()
117  || ((static_cast<unsigned char>(byte) & Mask) != Value))
118  return false;
119 
120  reader.bump();
121  end = reader.position();
122  return true;
123  }
124 
125  template <typename Context>
126  constexpr void report_error(Context& context, const Reader&)
127  {
129  context.on(_ev::error{}, err);
130  }
131  };
132 };
133 
135 template <typename... Bits>
136 constexpr auto bits(Bits...)
137 {
138  static_assert((std::is_base_of_v<bit::_bit_rule, Bits> && ...), "bits() requires bit rules");
139  static_assert((0 + ... + Bits::size) == 8, "must specify 8 bit at a time");
140 
141  constexpr auto pattern = [] {
142  bit::_bit_pattern result{0, 0};
143  (Bits::apply(result), ...);
144  return result;
145  }();
146 
147  return _bits<pattern.mask, pattern.value>{};
148 }
149 } // namespace lexyd
150 
151 #endif // LEXY_DSL_BITS_HPP_INCLUDED
152 
cx::size
constexpr auto size(const C &c) -> decltype(c.size())
Definition: wildcards.hpp:636
lexyd::bits
constexpr auto bits(Bits...)
Matches the specific bit pattern.
Definition: bits.hpp:136
lexyd::position
constexpr auto position
Produces an iterator to the current reader position without parsing anything.
Definition: position.hpp:79
lexyd::bit::_b0::size
static constexpr auto size
Definition: bits.hpp:24
lexyd::bit::_n::size
static constexpr auto size
Definition: bits.hpp:60
token.hpp
lexyd::bit::any
constexpr auto any
Matches N arbitrary bits.
Definition: bits.hpp:95
lexyd::bit::_b0::apply
static constexpr void apply(_bit_pattern &p)
Definition: bits.hpp:26
lexyd::bit::_1
constexpr auto _1
Matches a 1 bit.
Definition: bits.hpp:53
lexyd::bit::_0
constexpr auto _0
Matches a 0 bit.
Definition: bits.hpp:36
lexyd::_bits::tp::report_error
constexpr void report_error(Context &context, const Reader &)
Definition: bits.hpp:126
lexyd::bit::_b::size
static constexpr auto size
Definition: bits.hpp:81
lexyd::bit::_n::apply
static constexpr void apply(_bit_pattern &p)
Definition: bits.hpp:62
lexyd::_bits::tp
Definition: bits.hpp:105
lexy::error
Generic failure.
Definition: error.hpp:14
lexyd::bit::_
constexpr auto _
Matches any bit.
Definition: bits.hpp:91
lexy::parse_events::error
Definition: dsl/base.hpp:55
lexyd::token_base
Definition: dsl/token.hpp:42
lexyd::_bits::tp::try_parse
constexpr bool try_parse(Reader reader)
Definition: bits.hpp:111
lexyd::bit::_n
Definition: bits.hpp:56
lexyd::bit
Definition: bits.hpp:11
lexyd::bit::_b1
Definition: bits.hpp:38
lexyd::_bits::tp::end
Reader::iterator end
Definition: bits.hpp:107
lexyd::bit::_bit_pattern::mask
unsigned mask
Definition: bits.hpp:15
lexyd::bit::_b::apply
static constexpr void apply(_bit_pattern &p)
Definition: bits.hpp:83
lexyd::_bits
Definition: bits.hpp:102
lexyd::bit::_b1::apply
static constexpr void apply(_bit_pattern &p)
Definition: bits.hpp:42
base.hpp
lexyd::bit::nibble
constexpr auto nibble
Matches a specific nibble, i.e. four bits.
Definition: bits.hpp:74
lexyd::bit::_b0
Definition: bits.hpp:22
lexyd::bit::_b
Definition: bits.hpp:77
lexyd::bit::_bit_pattern
Definition: bits.hpp:13
lexyd::bit::_b1::size
static constexpr auto size
Definition: bits.hpp:40
lexyd::_bits::tp::tp
constexpr tp(const Reader &reader)
Definition: bits.hpp:109
lexyd
Definition: trace.hpp:22
lexyd::bit::_bit_pattern::value
unsigned value
Definition: bits.hpp:16
lexyd::eof
constexpr auto eof
Matches EOF.
Definition: eof.hpp:72
lexyd::p
constexpr auto p
Parses the production.
Definition: production.hpp:127
lexyd::bit::_bit_rule
Definition: bits.hpp:19


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Tue Jun 25 2024 02:20:35