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 694 695 696
#![warn(missing_docs)]
//! This crate provides utilites for parsing byte slices. The API borrows some concepts from other
//! parser-combinator crates but heavily simplifies things by eschewing error management and
//! focusing exclusively on parsing byte slices.
//!
//! ## Overview
//!
//! The crate's entry point is the [`Matches`] struct. It contains an array of input segments that
//! have been matched. The array is populated by repeatedly calling `.add()` with a
//! [pattern](Pattern) argument.
//!
//! Patterns can be combined using combinatorial logic to express more complex rules about what a
//! byte (or sequence of bytes should look like)
//!
//! ## Examples
//!
//! __HTTP request line__
//!
//! Say you wanted to parse the request line of an HTTP message. There are three important parts to
//! extract: the method, the request target and the protocol version.
//!
//! ```text
//! GET /hello?value=world HTTP/1.1\r\n
//! ```
//!
//! Here is how you would do it:
//!
//! _Note: The rules for parsing http are a bit more nuanced than this_
//!
//! ```
//! use bparse::{Pattern, Matches};
//!
//! # fn main() {
//! # do_test().unwrap();
//! # }
//! # fn do_test() -> Option<()> {
//! // This input would probably come from a TcpStream
//! let input = b"GET /hello?value=world HTTP/1.1\r\n";
//!
//! let matches = Matches::new(input);
//!
//! // A method is an alphabetic string with at least one character
//! let method_pattern = bparse::range(b'a', b'z')
//! .or(bparse::range(b'A', b'Z'))
//! .repeats(1..);
//!
//!
//! // A request url must start with a `/` and contains a mix of alphabetic and special characters
//! let request_target_pattern = "/"
//! .and(
//! bparse::range(b'a', b'z')
//! .or(bparse::range(b'A', b'Z'))
//! .or(bparse::oneof("?=/"))
//! .repeats(0..)
//! );
//!
//! // We want to match the version exactly
//! let version_pattern = "HTTP/1.1";
//!
//! let result = matches
//! .pattern(method_pattern)?
//! .ignore(" ")?
//! .pattern(request_target_pattern)?
//! .ignore(" ")?
//! .pattern(version_pattern)?
//! .ignore("\r\n".and(bparse::end))?;
//!
//!
//! // Et voila
//! let [method, request_target, version] = result.0;
//!
//! assert_eq!(method, b"GET");
//! assert_eq!(request_target, b"/hello?value=world");
//! assert_eq!(version, b"HTTP/1.1");
//!
//! # Some(())
//! # }
//! ```
//!
//! __Hexadecimal color parser:__
//!
//! ```rust
//! use bparse::{Pattern, Matches};
//! use std::str::from_utf8;
//!
//! #[derive(Debug, PartialEq)]
//! pub struct Color {
//! pub red: u8,
//! pub green: u8,
//! pub blue: u8,
//! }
//!
//! fn main() {
//! assert_eq!(hex_color("#2F14DF"), Some(Color {
//! red: 47,
//! green: 20,
//! blue: 223,
//! }));
//! }
//!
//! fn hex_color(input: &str) -> Option<Color> {
//! let hexbyte = bparse::range(b'0', b'9')
//! .or(bparse::range(b'A', b'F'))
//! .or(bparse::range(b'a', b'f'))
//! .repeats(2);
//!
//! let [red, green, blue] = Matches::new(input.as_bytes())
//! .ignore("#")?
//! .pattern(hexbyte)?
//! .pattern(hexbyte)?
//! .pattern(hexbyte)?
//! .ignore(bparse::end)?
//! .0;
//!
//! Some(Color {
//! red: u8::from_str_radix(from_utf8(red).unwrap(), 16).unwrap(),
//! green: u8::from_str_radix(from_utf8(green).unwrap(), 16).unwrap(),
//! blue: u8::from_str_radix(from_utf8(blue).unwrap(), 16).unwrap()
//! })
//! }
//! ```
use std::ops::{RangeFrom, RangeInclusive, RangeToInclusive};
mod r#match;
pub use r#match::*;
/// An interval with a lower and (potentially unbounded) upper bound
///
/// Both bounds are inclusive
#[derive(Copy, Clone, Debug)]
pub struct Interval<T>(T, Option<T>);
impl From<usize> for Interval<usize> {
fn from(value: usize) -> Self {
Interval(value, Some(value))
}
}
impl From<RangeInclusive<usize>> for Interval<usize> {
fn from(value: RangeInclusive<usize>) -> Self {
Interval(*value.start(), Some(*value.end()))
}
}
impl From<RangeFrom<usize>> for Interval<usize> {
fn from(value: RangeFrom<usize>) -> Self {
Interval(value.start, None)
}
}
impl From<RangeToInclusive<usize>> for Interval<usize> {
fn from(value: RangeToInclusive<usize>) -> Self {
Interval(0, Some(value.end))
}
}
/// Expresses that the implementing type can be used to match a byte slice
pub trait Pattern {
/// Tests the pattern against the input slice. If the pattern matches, the matching part is
/// returned along with what is left of the input. Returns `None` if the pattern does not match
fn test<'i>(&self, input: &'i [u8]) -> Option<(&'i [u8], &'i [u8])>;
/// Expresses an alternate pattern.
///
/// Returns a new pattern that will match an input slice if either `self` or `other` match it.
///
/// # Example
///
/// ```
/// use bparse::Pattern;
///
/// let input = b"b";
/// let pattern = "a".or("b");
///
/// assert_eq!(b"b", pattern.test(input).unwrap().0);
/// ```
fn or<P>(self, other: P) -> Or<Self, P>
where
Self: Sized,
{
Or {
pattern1: self,
pattern2: other,
}
}
/// Expreses a sequence of patterns
///
/// Returns a new pattern that will test an input slice against `self` and `other` in sequence,
/// feeding the remainder from the first match as the input to the second.
///
/// # Example
///
/// ```
/// use bparse::Pattern;
///
/// let input = b"abc";
/// let pattern = "a".and("b").and("c");
///
/// assert_eq!(b"abc", pattern.test(input).unwrap().0);
/// ```
fn and<P>(self, next: P) -> And<Self, P>
where
Self: Sized,
{
And {
pattern1: self,
pattern2: next,
}
}
/// Expresses pattern repetition
///
/// Returns a new pattern that will match an input slice if `self` can match `count` times
///
/// Anything that can be safely converted to an [`Interval`] can be used as an argument .
///
/// ```
/// use bparse::{Pattern};
///
/// let input = b"ababab";
/// let pattern = "a".and("b").repeats(1..=2);
///
/// assert_eq!(b"abab", pattern.test(input).unwrap().0);
/// ```
fn repeats<I: Into<Interval<usize>>>(self, count: I) -> Repeat<Self>
where
Self: Sized,
{
Repeat {
pattern: self,
count: count.into(),
}
}
/// Expresses pattern negation
///
/// Returns a new pattern that will match only if `self` does not match
///
/// # Example
///
/// Say you want to match a string of multiple 'a's and 'b's, except the string must not end in
/// a 'b':
/// ```
/// use bparse::{Pattern, end};
///
/// let input1 = b"aabaaaa";
/// let input2 = b"aaaab";
///
/// // a pattern of either a's or b's that do not occur at the input
/// let pattern = "a".or("b".and(end.not())).repeats(0..);
///
/// assert_eq!(b"aabaaaa", pattern.test(input1).unwrap().0);
/// assert_eq!(b"aaaa", pattern.test(input2).unwrap().0);
///
///
/// ```
fn not(self) -> Not<Self>
where
Self: Sized,
{
Not { pattern: self }
}
/// Expresses an optional pattern
///
/// Returns a new pattern that will always match the input regardless of the outcome of
/// matching `self`
///
/// # Example
///
/// ```
/// use bparse::Pattern;
///
/// let input1 = b"aab";
/// let input2 = b"aa";
///
/// let pattern = "aa".and("b".optional());
///
/// assert_eq!(b"aab", pattern.test(input1).unwrap().0);
/// assert_eq!(b"aa", pattern.test(input2).unwrap().0);
/// ```
fn optional(self) -> Optional<Self>
where
Self: Sized,
{
Optional { pattern: self }
}
}
/// See [`Pattern::or`]
#[derive(Clone, Copy, Debug)]
pub struct Or<C1, C2> {
pattern1: C1,
pattern2: C2,
}
/// See [`Pattern::and`]
#[derive(Clone, Copy, Debug)]
pub struct And<C1, C2> {
pattern1: C1,
pattern2: C2,
}
/// See [`Pattern::repeats`]
#[derive(Clone, Copy, Debug)]
pub struct Repeat<P> {
pattern: P,
count: Interval<usize>,
}
/// See [`Pattern::not`]
#[derive(Clone, Copy, Debug)]
pub struct Not<P> {
pattern: P,
}
/// See [`Pattern::optional`]
#[derive(Clone, Copy, Debug)]
pub struct Optional<P> {
pattern: P,
}
impl<C1: Pattern, C2: Pattern> Pattern for Or<C1, C2> {
fn test<'i>(&self, input: &'i [u8]) -> Option<(&'i [u8], &'i [u8])> {
self.pattern1
.test(input)
.or_else(|| self.pattern2.test(input))
}
}
impl<C1: Pattern, C2: Pattern> Pattern for And<C1, C2> {
fn test<'i>(&self, input: &'i [u8]) -> Option<(&'i [u8], &'i [u8])> {
let mut offset = 0;
let Some((value, rest)) = self.pattern1.test(input) else {
return None;
};
offset += value.len();
let Some((value, rest)) = self.pattern2.test(rest) else {
return None;
};
offset += value.len();
Some((&input[..offset], rest))
}
}
impl<P: Pattern> Pattern for Repeat<P> {
fn test<'i>(&self, input: &'i [u8]) -> Option<(&'i [u8], &'i [u8])> {
let mut counter = 0;
let mut offset = 0;
let lower_bound = self.count.0;
if let Some(upper_bound) = self.count.1 {
assert!(
upper_bound >= lower_bound,
"upper bound should be greater than or equal to the lower bound"
);
};
loop {
// We hit the upper bound of pattern repetition
if let Some(upper_bound) = self.count.1 {
if counter == upper_bound {
return Some((&input[..offset], &input[offset..]));
}
};
let Some((value, _)) = self.pattern.test(&input[offset..]) else {
if counter < lower_bound {
return None;
}
return Some((&input[..offset], &input[offset..]));
};
counter += 1;
offset += value.len();
}
}
}
impl<P> Pattern for Not<P>
where
P: Pattern,
{
fn test<'i>(&self, input: &'i [u8]) -> Option<(&'i [u8], &'i [u8])> {
if self.pattern.test(input).is_some() {
return None;
};
Some((b"", input))
}
}
impl<P> Pattern for Optional<P>
where
P: Pattern,
{
fn test<'i>(&self, input: &'i [u8]) -> Option<(&'i [u8], &'i [u8])> {
self.pattern.test(input).or(Some((b"", input)))
}
}
impl<F> Pattern for F
where
F: Fn(&[u8]) -> Option<(&[u8], &[u8])>,
{
fn test<'i>(&self, input: &'i [u8]) -> Option<(&'i [u8], &'i [u8])> {
(self)(input)
}
}
impl Pattern for &str {
fn test<'i>(&self, input: &'i [u8]) -> Option<(&'i [u8], &'i [u8])> {
let bytes = self.as_bytes();
let Some(_) = input.strip_prefix(bytes) else {
return None;
};
Some((&input[..self.len()], &input[self.len()..]))
}
}
/// A pattern that fails if the input is not empty
///
/// # Example
///
/// ```
/// use bparse::{Pattern, end};
///
/// assert_eq!(end.test(b"abc"), None);
/// assert_eq!(end.test(b"").unwrap().0, b"");
/// ```
pub fn end(input: &[u8]) -> Option<(&[u8], &[u8])> {
if input.is_empty() {
Some((&[], input))
} else {
None
}
}
/// A pattern that fails if the byte at the start of the input is not an ascii digit
///
/// # Example
///
/// ```
/// use bparse::{Pattern, digit};
/// assert_eq!(digit.test(b"1").unwrap().0, b"1");
/// assert_eq!(digit.test(b"a"), None);
/// ```
pub fn digit(input: &[u8]) -> Option<(&[u8], &[u8])> {
range(b'0', b'9').test(input)
}
/// A pattern that fails if the byte at the start of the input is not an ascii alphabetic character
///
/// # Example
///
/// ```
/// use bparse::{Pattern, alpha};
/// assert_eq!(alpha.test(b"a").unwrap().0, b"a");
/// assert_eq!(alpha.test(b"1"), None);
/// ```
pub fn alpha(input: &[u8]) -> Option<(&[u8], &[u8])> {
range(b'a', b'z').or(range(b'A', b'Z')).test(input)
}
/// A pattern that fails if the byte at the start of the input is not a hexadecimal character
///
/// # Example
///
/// ```
/// use bparse::{Pattern, hex};
/// assert_eq!(hex.test(b"a").unwrap().0, b"a");
/// assert_eq!(hex.test(b"1").unwrap().0, b"1");
/// assert_eq!(hex.test(b"g"), None);
/// ```
pub fn hex(input: &[u8]) -> Option<(&[u8], &[u8])> {
range(b'a', b'f')
.or(range(b'A', b'F'))
.or(digit)
.test(input)
}
/// Returns a pattern that will match any one of the bytes in `alternatives`
///
/// This is a useful alternative to a long `Pattern::or()` chain when you have many single-byte
/// alternatives.
///
/// # Example
///
/// ```
/// use bparse::{Pattern, oneof};
///
/// let punctuation = oneof(".,\"'-?:!;");
/// assert_eq!(punctuation.test(b"!").unwrap().0, b"!");
/// assert_eq!(punctuation.test(b",").unwrap().0, b",");
/// assert_eq!(punctuation.test(b"a"), None);
/// ```
pub const fn oneof(alternatives: &str) -> OneOf {
let bytes = alternatives.as_bytes();
let mut set: [bool; 256] = [false; 256];
let mut i = 0;
while i < bytes.len() {
set[bytes[i] as usize] = true;
i += 1;
}
OneOf(set)
}
/// Inverse of [`oneof`]
///
/// # Example
///
/// ```
/// use bparse::{Pattern, noneof};
///
/// let nondigits = noneof("0123456789");
///
/// assert_eq!(nondigits.test(b"A").unwrap().0, b"A");
/// assert_eq!(nondigits.test(b"3"), None);
/// ```
///
pub const fn noneof(exclusions: &str) -> NoneOf {
let bytes = exclusions.as_bytes();
let mut set: [bool; 256] = [true; 256];
let mut i = 0;
while i < bytes.len() {
set[bytes[i] as usize] = false;
i += 1;
}
NoneOf(set)
}
/// See [`oneof()`]
#[derive(Debug, Clone, Copy)]
pub struct OneOf([bool; 256]);
impl Pattern for OneOf {
fn test<'i>(&self, input: &'i [u8]) -> Option<(&'i [u8], &'i [u8])> {
let first = *input.first()?;
self.0[first as usize].then_some((&input[0..1], &input[1..]))
}
}
/// See [`noneof()`]
pub struct NoneOf([bool; 256]);
impl Pattern for NoneOf {
fn test<'i>(&self, input: &'i [u8]) -> Option<(&'i [u8], &'i [u8])> {
let first = *input.first()?;
self.0[first as usize].then_some((&input[0..1], &input[1..]))
}
}
/// Returns a pattern that will match any byte in the closed interval `[lo, hi]`
///
/// # Example
///
/// ```
/// use bparse::{Pattern, range};
///
/// let digit = range(b'0', b'9');
///
/// assert_eq!(digit.test(b"1").unwrap().0, b"1");
/// assert_eq!(digit.test(b"a"), None);
/// ```
pub fn range(lo: u8, hi: u8) -> ByteRange {
ByteRange(lo, hi)
}
/// See [`range()`]
#[derive(Debug, Clone, Copy)]
pub struct ByteRange(u8, u8);
impl Pattern for ByteRange {
fn test<'i>(&self, input: &'i [u8]) -> Option<(&'i [u8], &'i [u8])> {
let first = *input.first()?;
if first < self.0 {
return None;
}
if first > self.1 {
return None;
}
Some((&input[0..1], &input[1..]))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[track_caller]
fn do_test(
pattern: impl Pattern,
input: &'static [u8],
result: Option<(&'static [u8], &'static [u8])>,
) {
let out = pattern.test(input);
assert_eq!(out, result);
}
#[test]
fn test_parse_patterns() {
// Parsing using string slices
do_test("", b"a1b2", Some((b"", b"a1b2")));
do_test("a", b"a1b2", Some((b"a", b"1b2")));
do_test("١", b"\xd9\xa1", Some((b"\xd9\xa1", &[])));
// Parsing using alternatives
do_test("b".or("a"), b"a1b2", Some((b"a", b"1b2")));
// Parsing in sequence
do_test("9".and("7").and("8"), b"978", Some((b"978", b"")));
// Negating a pattern
do_test(range(b'0', b'9').not(), b"a1b2", Some((b"", b"a1b2")));
// Optional
do_test(" ".optional().and("a"), b"a1b2", Some((b"a", b"1b2")));
do_test(" ".optional().and("a"), b" a1b2", Some((b" a", b"1b2")));
// Parsing using functions
fn parse_a(input: &[u8]) -> Option<(&[u8], &[u8])> {
"a".test(input)
}
fn parse_1(input: &[u8]) -> Option<(&[u8], &[u8])> {
"1".test(input)
}
let huh = parse_a.and(parse_1);
do_test(huh, b"a1b2", Some((b"a1", b"b2")));
do_test(huh, b"a1b2", Some((b"a1", b"b2")));
}
#[test]
fn test_builtin_patterns() {
do_test(
digit.repeats(0..),
b"0123456789",
Some((b"0123456789", b"")),
);
do_test(
alpha.repeats(0..),
b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
Some((b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", b"")),
);
do_test(
hex.repeats(0..),
b"abcdefABCDEF0123456789",
Some((b"abcdefABCDEF0123456789", b"")),
);
do_test(oneof("&@*%#!?").repeats(0..), b"#!q", Some((b"#!", b"q")));
do_test(noneof("ABC").repeats(0..), b"123C", Some((b"123", b"C")));
do_test(range(b'`', b'b'), b"a1b2", Some((b"a", b"1b2")));
}
#[test]
fn test_parse_repetition() {
// Bounded on both ends
do_test("a".repeats(0..=0), b"aabb", Some((b"", b"aabb")));
do_test("a".repeats(1..=1), b"aabb", Some((b"a", b"abb")));
do_test("a".repeats(1..=2), b"aabb", Some((b"aa", b"bb")));
do_test("a".repeats(1..=10), b"aabb", Some((b"aa", b"bb")));
// Lower bound
do_test("a".repeats(0..), b"aaaab", Some((b"aaaa", b"b")));
do_test("a".repeats(4..), b"aaaab", Some((b"aaaa", b"b")));
do_test("a".repeats(5..), b"aaaab", None);
// Upper bound
do_test("a".repeats(..=3), b"bb", Some((b"", b"bb")));
do_test("a".repeats(..=0), b"aaabb", Some((b"", b"aaabb")));
do_test("a".repeats(..=1), b"aaabb", Some((b"a", b"aabb")));
do_test("a".repeats(..=10), b"aaabb", Some((b"aaa", b"bb")));
}
}
#[doc = include_str!("../README.md")]
#[cfg(doctest)]
pub struct ReadmeDocTests {}