gramex 0.1.1

common language for advance parsers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
//! # bytes matching
//! `gramex` supports byte matching through `&[u8]` [byte](u8) [slices](primitive@slice).
//!
//! `&[u8]` implements [`MatchBy`] for [`u8`], [`&u8`](u8), [`&[u8]`](primitive@slice), [`[u8; N]`](primitive@array), [`&[u8; N]`](primitive@array) and [`&Vec<u8>`](Vec<u8>).
//!
//! it also support range matching by [`u8`].
//!
//! ```
//! assert!(matches!([1, 2, 3]: [u8], 1 0x02 0b0000_0011));
//! assert!(matches!([1, 2, 3]: [u8], {[1u8, 2, 3]}));
//! assert!(matches!([1, 2, 3]: [u8], 1? !0xff 0x00..0x7f));
//! ```
//!
//! it also support matching by any type implementing [`AsRef<[u8]>`](AsRef) through [`bytes_of`].
//! ```
//! assert!(matches!(b"abc": [u8], {bytes_of("abc")}));
//! ```
//!
//! integers can be bytewise matched through [`{int.to_(le|be)_bytes()}`](u64::to_le_bytes).
//! ```
//! assert!(matches!([1, 0, 0, 0]: [u8], {1u32.to_le_bytes()}));
//! ```
//!
//! # bits matching
//! `gramex` supports bit matching through [`Bits`].
//!
//! [`Bits`] is a bitfield wrapper supporting bit matching upto 64 bits, from most to least significant bit.
//!
//! it supports matching by [`Bits`] and [`&Bits`](Bits), through the `bn` functions that creates an `n` sized [`Bits`] field.
//!
//! it also support range matching through the `bnr` functions.
//!
//! ```
//! assert!(matches!(b8(0x12): Bits, {b4(0x1)} {b2(0)} {b2(0b11)}));
//! assert!(matches!(b12(0x123): Bits, {b4(0x1)}? {b4(0)}+ {b4r(0..=7)}));
//! ```
//!
//! `gramex` also support bit matching in the other direction (from least to most significant bit) through the [`LBits`] wrapper.
//!
//! ```
//! assert!(matches!(b8(0x12): LBits, {b4(0x2)} {b2(0b01)} {b2(0)}));
//! ```

use std::{
	fmt::Display,
	ops::{Range, RangeInclusive},
};

use crate::{MatchAble, MatchBy, MatchSignal, MatchStatus, Matcher};

impl MatchAble for [u8] {
	type Slice<'a> = &'a [u8];
	#[inline]
	fn len(&self) -> usize {
		self.len()
	}
	#[inline]
	fn slice(&self, range: Range<usize>) -> Self::Slice<'_> {
		&self[range]
	}
}

impl MatchBy<u8> for [u8] {
	#[inline]
	fn match_by(&self, matcher: u8, ind: &mut usize, _status: &MatchStatus) -> MatchSignal {
		if *ind + 1 > self.len() {
			MatchSignal::InComplete
		} else if self[*ind] == matcher {
			*ind += 1;
			MatchSignal::Matched
		} else {
			MatchSignal::MisMatched
		}
	}
}
impl<'a> MatchBy<&'a u8> for [u8] {
	#[inline]
	fn match_by(&self, matcher: &'a u8, ind: &mut usize, _status: &MatchStatus) -> MatchSignal {
		if *ind + 1 > self.len() {
			MatchSignal::InComplete
		} else if self[*ind] == *matcher {
			*ind += 1;
			MatchSignal::Matched
		} else {
			MatchSignal::MisMatched
		}
	}
}

impl<'a> MatchBy<&'a [u8]> for [u8] {
	#[inline]
	fn match_by(&self, matcher: &'a [u8], ind: &mut usize, _status: &MatchStatus) -> MatchSignal {
		if *ind + matcher.len() > self.len() {
			MatchSignal::InComplete
		} else if self[*ind..].starts_with(matcher) {
			*ind += matcher.len();
			MatchSignal::Matched
		} else {
			MatchSignal::MisMatched
		}
	}
}
impl<'a, const N: usize> MatchBy<&'a [u8; N]> for [u8] {
	#[inline]
	fn match_by(
		&self, matcher: &'a [u8; N], ind: &mut usize, _status: &MatchStatus,
	) -> MatchSignal {
		if *ind + matcher.len() > self.len() {
			MatchSignal::InComplete
		} else if self[*ind..].starts_with(matcher) {
			*ind += matcher.len();
			MatchSignal::Matched
		} else {
			MatchSignal::MisMatched
		}
	}
}
impl<const N: usize> MatchBy<[u8; N]> for [u8] {
	#[inline]
	fn match_by(&self, matcher: [u8; N], ind: &mut usize, _status: &MatchStatus) -> MatchSignal {
		if *ind + matcher.len() > self.len() {
			MatchSignal::InComplete
		} else if self[*ind..].starts_with(&matcher) {
			*ind += matcher.len();
			MatchSignal::Matched
		} else {
			MatchSignal::MisMatched
		}
	}
}
impl<'a> MatchBy<&'a Vec<u8>> for [u8] {
	#[inline]
	fn match_by(
		&self, matcher: &'a Vec<u8>, ind: &mut usize, _status: &MatchStatus,
	) -> MatchSignal {
		if *ind + matcher.len() > self.len() {
			MatchSignal::InComplete
		} else if self[*ind..].starts_with(matcher) {
			*ind += matcher.len();
			MatchSignal::Matched
		} else {
			MatchSignal::MisMatched
		}
	}
}

impl MatchBy<RangeInclusive<u8>> for [u8] {
	#[inline]
	fn match_by(
		&self, matcher: RangeInclusive<u8>, ind: &mut usize, _status: &MatchStatus,
	) -> MatchSignal {
		if *ind + 1 > self.len() {
			MatchSignal::InComplete
		} else if matcher.contains(&self[*ind]) {
			*ind += 1;
			MatchSignal::Matched
		} else {
			MatchSignal::MisMatched
		}
	}
}

/// return the bytes of the value.
///
/// it is a shortcut for [`AsRef<[u8]>::as_ref`](AsRef::as_ref).
///
/// # example
/// ```
/// assert!(matches!(b"abc": [u8], {bytes_of("abc")}));
/// ```
#[inline]
pub fn bytes_of<T: AsRef<[u8]> + ?Sized>(value: &T) -> &[u8] {
	value.as_ref()
}

/// a sized bit field.
///
/// `Bits` is a [`u64`] with length, it is the [`MatchAble`] that is used in bit matching.
///
/// it can be sized from 1 to 64 bits, and indexed by bit from most significant bit to the lowest one.
///
/// can be created / converted from and into any int, little endian [`[u8; N]`](primitive@array) and [`&[u8]`](primitive@slice).
///
/// # example
/// ```
/// let bits = Bits::from(0x123u32);
/// assert_eq!(bits, Bits { value: 0x123, len: 32 });
/// assert_eq!(bits.try_into(), Ok(0x123i32));
///
/// let bits = Bits::from([0x12, 0x34]);
/// assert_eq!(bits, Bits { value: 0x3412, len: 16 });
/// assert_eq!(bits.try_into(), Ok([0x12, 0x34]));
///
/// assert!(matches!(bits: Bits, {b8(0x34)} {b8(0x12)}));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Bits {
	pub value: u64,
	pub len: usize,
}
/// a sized bit field, in the other direction.
///
/// `LBits` is a [`Bits`] but indexed from least significant bit to the most significant one.
///
/// it is used in bit matching in the reverse order.
///
/// convertable between the types [`Bits`] is convertable to, in addition to and from [`Bits`] (with no change in value).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct LBits {
	pub value: u64,
	pub len: usize,
}
impl From<Bits> for LBits {
	fn from(value: Bits) -> Self {
		LBits { value: value.value, len: value.len }
	}
}
impl From<LBits> for Bits {
	fn from(value: LBits) -> Self {
		Bits { value: value.value, len: value.len }
	}
}
impl PartialEq<u64> for Bits {
	fn eq(&self, other: &u64) -> bool {
		self.value == *other
	}
}
impl PartialEq<u64> for LBits {
	fn eq(&self, other: &u64) -> bool {
		self.value == *other
	}
}
impl PartialEq<LBits> for Bits {
	fn eq(&self, other: &LBits) -> bool {
		self.value == other.value && self.len == other.len
	}
}

/// error occuring when [`Bits`] is converted from / to a smaller type.
///
/// this error occurs when the value of [`Bits`] can not fit into the target type, based on the [`Bits.length`] field, even if the value can fit.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BitOverflowError {
	pub target_len: usize,
	pub value_len: usize,
}
impl Display for BitOverflowError {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		let Self { target_len, value_len } = self;
		write!(f, "target length {target_len} is smaller than value length {value_len}",)
	}
}
macro_rules! bits_conv {
	[$($ty:ty), +] => {
		$(
			impl From<$ty> for Bits {
				fn from(value: $ty) -> Self {
					Bits { value: value as u64, len: size_of::<$ty>() * 8 }
				}
			}
			impl TryInto<$ty> for Bits {
				type Error = BitOverflowError;
				fn try_into(self) -> Result<$ty, BitOverflowError> {
					if self.len > size_of::<$ty>() * 8 {
						return Err(BitOverflowError {
							target_len: size_of::<$ty>() * 8,
							value_len: self.len
						});
					}
					Ok(self.value as $ty)
				}
			}
			impl From<$ty> for LBits {
				fn from(value: $ty) -> Self {
					LBits { value: value as u64, len: size_of::<$ty>() * 8 }
				}
			}
			impl TryInto<$ty> for LBits {
				type Error = BitOverflowError;
				fn try_into(self) -> Result<$ty, BitOverflowError> {
					if self.len > size_of::<$ty>() * 8 {
						return Err(BitOverflowError {
							target_len: size_of::<$ty>() * 8,
							value_len: self.len
						});
					}
					Ok(self.value as $ty)
				}
			}
		)+
	};
}
bits_conv![u8, u16, u32, u64, i8, i16, i32, i64];
impl<const N: usize> From<[u8; N]> for Bits {
	fn from(value: [u8; N]) -> Self {
		assert!(N <= 8);
		let mut buf = [0u8; 8];
		buf[..N].copy_from_slice(&value);
		Bits { value: u64::from_le_bytes(buf), len: N * 8 }
	}
}
impl<const N: usize> TryInto<[u8; N]> for Bits {
	type Error = BitOverflowError;
	fn try_into(self) -> Result<[u8; N], BitOverflowError> {
		if self.len > N * 8 {
			return Err(BitOverflowError { target_len: N * 8, value_len: self.len });
		}
		let mut buf = [0u8; N];
		buf.copy_from_slice(&self.value.to_le_bytes()[..N]);
		Ok(buf)
	}
}
impl<const N: usize> From<[u8; N]> for LBits {
	fn from(value: [u8; N]) -> Self {
		assert!(N <= 8);
		let mut buf = [0u8; 8];
		buf[..N].copy_from_slice(&value);
		LBits { value: u64::from_le_bytes(buf), len: N * 8 }
	}
}
impl<const N: usize> TryInto<[u8; N]> for LBits {
	type Error = BitOverflowError;
	fn try_into(self) -> Result<[u8; N], BitOverflowError> {
		if self.len > N * 8 {
			return Err(BitOverflowError { target_len: N * 8, value_len: self.len });
		}
		let mut buf = [0u8; N];
		buf.copy_from_slice(&self.value.to_le_bytes()[..N]);
		Ok(buf)
	}
}
impl TryFrom<&[u8]> for Bits {
	type Error = BitOverflowError;
	fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
		if value.len() > 8 {
			return Err(BitOverflowError { target_len: 64, value_len: value.len() * 8 });
		}
		let mut buf = [0u8; 8];
		buf[..value.len()].copy_from_slice(value);
		Ok(Bits { value: u64::from_le_bytes(buf), len: value.len() * 8 })
	}
}
impl Bits {
	pub fn new(value: u64, len: usize) -> Bits {
		Bits { value, len }
	}
}
impl TryFrom<&[u8]> for LBits {
	type Error = BitOverflowError;
	fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
		if value.len() > 8 {
			return Err(BitOverflowError { target_len: 64, value_len: value.len() * 8 });
		}
		let mut buf = [0u8; 8];
		buf[..value.len()].copy_from_slice(value);
		Ok(LBits { value: u64::from_le_bytes(buf), len: value.len() * 8 })
	}
}
impl LBits {
	pub fn new(value: u64, len: usize) -> LBits {
		LBits { value, len }
	}
}
impl std::fmt::Binary for Bits {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		let s = format!("{:064b}", self.value);
		write!(f, "0b{}", &s[64 - self.len..])
	}
}
impl std::fmt::Binary for LBits {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		let s = format!("{:064b}", self.value);
		write!(f, "0b{}", &s[64 - self.len..])
	}
}
impl AsRef<Bits> for Bits {
	fn as_ref(&self) -> &Bits {
		self
	}
}
impl AsRef<LBits> for LBits {
	fn as_ref(&self) -> &LBits {
		self
	}
}
/// extract bits from a [`u64`], indexed from least significant bit
fn bitextract_lsb(value: u64, start: usize, end: usize) -> u64 {
	(value >> start) & u64::MAX >> (64 - (end - start))
}
/// extract bits from a [`u64`], indexed from most significant bit.
///
/// of `len` size occuping the low.
fn bitextract_msb(value: u64, len: usize, start: usize, end: usize) -> u64 {
	(value >> (len - end)) & (u64::MAX >> (64 - (end - start)))
}
impl MatchAble for Bits {
	type Slice<'a> = Bits;
	#[inline]
	fn len(&self) -> usize {
		self.len
	}
	#[inline]
	fn slice(&self, range: Range<usize>) -> Bits {
		let Range { start, end } = range;
		if start == end {
			return Bits { value: 0, len: 0 };
		}
		Bits { value: bitextract_msb(self.value, self.len, start, end), len: end - start }
	}
	#[inline]
	fn get_n(
		&self, ind: &mut usize, n: usize, _status: &MatchStatus,
	) -> Result<Self::Slice<'_>, MatchSignal> {
		if *ind + n > self.len {
			Err(MatchSignal::InComplete)
		} else {
			*ind += n;
			Ok(self.slice(*ind - n..*ind))
		}
	}
	#[inline]
	fn skip_n(&self, ind: &mut usize, n: usize, _status: &MatchStatus) -> MatchSignal {
		if *ind + n > self.len {
			MatchSignal::InComplete
		} else {
			*ind += n;
			MatchSignal::Matched
		}
	}
}
impl MatchBy<Bits> for Bits {
	#[inline]
	fn match_by(&self, matcher: Bits, ind: &mut usize, _status: &MatchStatus) -> MatchSignal {
		if *ind + matcher.len > self.len {
			MatchSignal::InComplete
		} else if bitextract_msb(self.value, self.len, *ind, *ind + matcher.len) == matcher.value {
			*ind += matcher.len;
			MatchSignal::Matched
		} else {
			MatchSignal::MisMatched
		}
	}
}

impl<'a> MatchBy<&'a Bits> for Bits {
	#[inline]
	fn match_by(&self, matcher: &'a Bits, ind: &mut usize, status: &MatchStatus) -> MatchSignal {
		Bits::match_by(self, *matcher, ind, status)
	}
}

impl MatchAble for LBits {
	type Slice<'a> = LBits;
	#[inline]
	fn len(&self) -> usize {
		self.len
	}
	#[inline]
	fn slice(&self, range: Range<usize>) -> LBits {
		let Range { start, end } = range;
		if start == end {
			return LBits { value: 0, len: 0 };
		}
		LBits { value: bitextract_lsb(self.value, start, end), len: end - start }
	}
	#[inline]
	fn get_n(
		&self, ind: &mut usize, n: usize, _status: &MatchStatus,
	) -> Result<Self::Slice<'_>, MatchSignal> {
		if *ind + n > self.len {
			Err(MatchSignal::InComplete)
		} else {
			*ind += n;
			Ok(self.slice(*ind - n..*ind))
		}
	}
	#[inline]
	fn skip_n(&self, ind: &mut usize, n: usize, _status: &MatchStatus) -> MatchSignal {
		if *ind + n > self.len {
			MatchSignal::InComplete
		} else {
			*ind += n;
			MatchSignal::Matched
		}
	}
}
impl MatchBy<Bits> for LBits {
	#[inline]
	fn match_by(&self, matcher: Bits, ind: &mut usize, _status: &MatchStatus) -> MatchSignal {
		if *ind + matcher.len > self.len {
			MatchSignal::InComplete
		} else if bitextract_lsb(self.value, *ind, *ind + matcher.len) == matcher.value {
			*ind += matcher.len;
			MatchSignal::Matched
		} else {
			MatchSignal::MisMatched
		}
	}
}
impl<'a> MatchBy<&'a Bits> for LBits {
	#[inline]
	fn match_by(&self, matcher: &'a Bits, ind: &mut usize, status: &MatchStatus) -> MatchSignal {
		LBits::match_by(self, *matcher, ind, status)
	}
}
/// a `RangeInclusive` of bits.
///
/// used to implement range matching for [`Bits`], created by `bnr` functions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BitsRange {
	pub range: RangeInclusive<u64>,
	pub len: usize,
}
impl MatchBy<BitsRange> for Bits {
	#[inline]
	fn match_by(&self, matcher: BitsRange, ind: &mut usize, _status: &MatchStatus) -> MatchSignal {
		if *ind + matcher.len > self.len {
			MatchSignal::InComplete
		} else {
			let value = bitextract_msb(self.value, self.len, *ind, *ind + matcher.len);
			if matcher.range.contains(&value) {
				*ind += matcher.len;
				MatchSignal::Matched
			} else {
				MatchSignal::MisMatched
			}
		}
	}
}
impl MatchBy<BitsRange> for LBits {
	#[inline]
	fn match_by(&self, matcher: BitsRange, ind: &mut usize, _status: &MatchStatus) -> MatchSignal {
		if *ind + matcher.len > self.len {
			MatchSignal::InComplete
		} else if matcher.range.contains(&bitextract_lsb(self.value, *ind, *ind + matcher.len)) {
			*ind += matcher.len;
			MatchSignal::Matched
		} else {
			MatchSignal::MisMatched
		}
	}
}
macro_rules! b_fns {
	($matched:literal, $max_range:literal, [$($n:literal),+]) => {
		$(paste::paste! {
			#[doc = concat!("creates a ", $n, " bit [`Bits`] field.
			
# example
```
assert!(matches!(Bits::new(", $matched, ", ", $n, "): Bits, {b", $n, "(", $matched, ")}));
```")]
			#[inline]
			pub fn [<b $n>](value: u64) -> Bits {
				assert!(value <= u64::MAX >> (64 - $n));
				Bits { value, len: $n }
			}
			#[doc = concat!("creates a ", $n, " bit [`BitsRange`].
			
# example
```
assert!(matches!(Bits::new(", $matched, ", ", $n, "): Bits, {b", $n, "r(0x0..=", $max_range, ")}));
```")]
			#[inline]
			pub fn [<b $n r>](range: RangeInclusive<u64>) -> BitsRange {
				assert!(*range.start() <= u64::MAX >> (64 - $n));
				assert!(*range.end() <= u64::MAX >> (64 - $n));
				BitsRange { range, len: $n }
			}
		})+
	};
}
b_fns!("0x1", "0x1", [1, 2, 3, 4]);
b_fns!("0x12", "0x1F", [5, 6, 7, 8]);
#[rustfmt::skip]
b_fns!("0x123", "0x1FF", [ 
	                                9,  10, 11, 12, 13, 14, 15, 16,
	17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
	33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
	49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64
]);

/// matches a sized byte section by a [`Bits`] [`Matcher`].
///
/// `word` creates a `[u8]` matcher that extract a `size` sized little endian section and matches it against the `bit_matcher` as a `size * 8` bits bitfield.
///
/// # example
/// ```
/// assert!(matches!([1,2,3]: [u8], {word(3, matcher!(for Bits, {b8(3)} {b8(2)} {b8(1)}))}));
/// ```
#[inline]
pub fn word(size: u8, bit_matcher: impl Matcher<Bits>) -> impl Matcher<[u8]> {
	assert!(size <= 8);
	let size = size as usize;
	move |value, ind, status| {
		if *ind + size > value.len() {
			MatchSignal::InComplete
		} else {
			let mut buf = [0u8; 8];
			buf[..size].copy_from_slice(&value[*ind..*ind + size]);
			let bits = Bits { value: u64::from_le_bytes(buf), len: size * 8 };

			let sig = bit_matcher.do_match(&bits, &mut 0, status);
			if sig != MatchSignal::Matched {
				return sig;
			}
			*ind += size;
			MatchSignal::Matched
		}
	}
}
/// like [`word`] but by a [`LBits`] [`Matcher`].
///
/// # example
/// ```
/// assert!(matches!([1,2,3]: [u8], {wordl(3, matcher!(for Bits, {b8(1)} {b8(2)} {b8(3)}))}));
/// ```
#[inline]
pub fn wordl(size: u8, bit_matcher: impl Matcher<LBits>) -> impl Matcher<[u8]> {
	assert!(size <= 8);
	let size = size as usize;
	move |value, ind, status| {
		if *ind + size > value.len() {
			MatchSignal::InComplete
		} else {
			let mut buf = [0u8; 8];
			buf[..size].copy_from_slice(&value[*ind..*ind + size]);
			let bits = LBits { value: u64::from_le_bytes(buf), len: size * 8 };

			let sig = bit_matcher.do_match(&bits, &mut 0, status);
			if sig != MatchSignal::Matched {
				return sig;
			}
			*ind += size;
			MatchSignal::Matched
		}
	}
}

macro_rules! w_macros {
	($d:tt, $($n:literal),*) => {
		paste::paste! {$(
			#[macro_export]
			#[doc(hidden)]
			macro_rules! [<w $n>] {
				($d ($d matcher:tt)+) => {
					$crate::bits::word($n, $crate::matcher!(for $crate::bits::Bits, $d ($d matcher)+))
				}
			}
			#[doc(inline)]
			#[doc = concat!("matches a ", $n, " byte section by a [`Bits`] [grammer expression](crate::docs::gram_ref).
			
this is a shortcut for [`word(", $n, ", matcher!(for Bits, ...))`](word).")]
			pub use [<w $n>];

			#[macro_export]
			#[doc(hidden)]
			macro_rules! [<w $n l>] {
				($d ($d matcher:tt)+) => {
					$crate::bits::wordl($n, $crate::matcher!(for $crate::bits::LBits, $d ($d matcher)+))
				}
			}
			#[doc(inline)]
			#[doc = concat!("matches a ", $n, " byte section by a [`LBits`] [grammer expression](crate::docs::gram_ref).
			
this is a shortcut for [`wordl(", $n, ", matcher!(for Bits, ...))`](wordl).")]
			pub use [<w $n l>];
		)*}
	};
}
w_macros!($, 1, 2, 3, 4, 5, 6, 7, 8);

/// check if the current index is aligned to `size` bit boundary.
///
/// # example
/// ```
/// assert!(matches!(b8(0x12): Bits, {b4(1)} {aligned(4)} {b4(2)}));
/// ```
pub fn aligned(size: u8) -> impl Matcher<Bits> {
	move |_, ind, _| {
		if *ind % size as usize == 0 { MatchSignal::Matched } else { MatchSignal::MisMatched }
	}
}
/// like [`aligned`] but for [`LBits`]
///
/// # example
/// ```
/// assert!(matches!(LBits::new(8, 0x12)): LBits, {b4(2)} {alignedl(4)} {b4(1)}));
/// ```
pub fn alignedl(size: u8) -> impl Matcher<LBits> {
	move |_, ind, _| {
		if *ind % size as usize == 0 { MatchSignal::Matched } else { MatchSignal::MisMatched }
	}
}