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
use crate::input::{InputError, MapWithInputExt};
use crate::parser::combinator::{
Map, MapResult, Optional, Or, RepeatArrayVec, RepeatN, RepeatVec, WithPrefix, WithSuffix,
};
use crate::parser::error::{ParseError, WithErrorMsg};
use crate::parser::iterator::ParserIterator;
use crate::parser::simple::{Constant, Eol};
use crate::parser::then::{Then, Then2, Unimplemented};
/// [`Result`] type returned by [`Parser::parse`].
pub type ParseResult<'i, T> = Result<(T, &'i [u8]), (ParseError, &'i [u8])>;
/// Parser trait.
///
/// Implementations should avoid allocating where possible.
pub trait Parser: Sized {
/// Type of the value produced by [`parse`](Self::parse) when successful.
///
/// Generic over the input `'i` lifetime.
type Output<'i>;
/// Type of the chained parser returned by [`then`](Self::then).
///
/// This is used to allow multiple [`then`](Self::then) calls to extend one tuple, instead of
/// nesting tuples inside each other.
type Then<T: Parser>: Then<Self, T>;
/// Parse the given sequence of bytes.
///
/// Returns a tuple of the successfully parsed [`Output`](Self::Output) value and the
/// remaining bytes, or a tuple containing a [`ParseError`] and the location of the error.
///
/// The returned slices *must* be subslices of the input slice, otherwise [`InputError::new`]
/// (in [`parse_all`](Self::parse_all)) will panic.
///
/// # Examples
/// ```
/// # use utils::parser::{self, Parser};
/// assert_eq!(parser::u32().parse(b"1234abc"), Ok((1234, &b"abc"[..])));
/// assert!(parser::u32().parse(b"abc1234").is_err());
/// ```
fn parse<'i>(&self, input: &'i [u8]) -> ParseResult<'i, Self::Output<'i>>;
// Provided methods
/// Sequence another parser after this one.
///
/// # Examples
/// ```
/// # use utils::parser::{self, Parser};
/// assert_eq!(
/// parser::i32()
/// .then(parser::i32())
/// .parse(b"123-123"),
/// Ok(((123, -123), &b""[..]))
/// );
/// ```
fn then<T: Parser>(self, next: T) -> Self::Then<T> {
Then::then(self, next)
}
/// Attempt to parse using this parser, followed by provided parser.
///
/// If this parser succeeds, the alternative provider won't be tried. If both error, the error
/// from the parser which parsed further into the input is returned (preferring the first error
/// if both errored at the same position).
///
/// See also [`parser::one_of`](super::one_of()).
///
/// # Examples
/// ```
/// # use utils::parser::{self, ParseError, Parser};
/// let parser = parser::u8()
/// .map(|x| u32::from(x) * 1001001)
/// .or(parser::u32());
/// assert_eq!(
/// parser.parse(b"123"),
/// Ok((123123123, &b""[..]))
/// );
/// assert_eq!(
/// parser.parse(b"1000"),
/// Ok((1000, &b""[..]))
/// );
/// ```
fn or<T: for<'i> Parser<Output<'i> = Self::Output<'i>>>(self, alternative: T) -> Or<Self, T> {
Or {
first: self,
second: alternative,
}
}
/// Map the output of this parser using the supplied function.
///
/// # Examples
/// ```
/// # use utils::parser::{self, Parser};
/// assert_eq!(
/// parser::u32()
/// .map(|x| x * 2)
/// .parse(b"123"),
/// Ok((246, &b""[..]))
/// );
/// ```
fn map<O, F: for<'i> Fn(Self::Output<'i>) -> O>(self, f: F) -> Map<Self, F> {
Map {
parser: self,
map_fn: f,
}
}
/// Map the output of this parser using the supplied fallible function.
///
/// Errors must be `&'static str`, which will be mapped to [`ParseError::Custom`].
///
/// # Examples
/// ```
/// # use utils::parser::{self, ParseError, Parser};
/// let parser = parser::u8()
/// .map_res(|x| x.checked_mul(2).ok_or("input too large"));
/// assert_eq!(
/// parser.parse(b"123"),
/// Ok((246, &b""[..]))
/// );
/// assert_eq!(
/// parser.parse(b"200"),
/// Err((ParseError::Custom("input too large"), &b"200"[..]))
/// );
/// ```
fn map_res<O, F: for<'i> Fn(Self::Output<'i>) -> Result<O, &'static str>>(
self,
f: F,
) -> MapResult<Self, F> {
MapResult {
parser: self,
map_fn: f,
}
}
/// Wrap [`Output`](Self::Output) in [`Option`], returning [`None`] on error.
///
/// # Examples
/// ```
/// # use utils::parser::{self, ParseError, Parser};
/// let parser = parser::u32()
/// .optional();
/// assert_eq!(
/// parser.parse(b"123"),
/// Ok((Some(123), &b""[..]))
/// );
/// assert_eq!(
/// parser.parse(b"abc"),
/// Ok((None, &b"abc"[..]))
/// );
/// ```
fn optional(self) -> Optional<Self> {
Optional { parser: self }
}
/// Repeat this parser `N` times, returning an [`array`].
///
/// If the number of items is variable use [`repeat_arrayvec`](Self::repeat_arrayvec) or
/// [`repeat`](Self::repeat).
///
/// # Examples
/// ```
/// # use utils::parser::{self, Parser};
/// assert_eq!(
/// parser::u32()
/// .repeat_n(",") // N = 3 is inferred
/// .parse(b"12,34,56"),
/// Ok(([12, 34, 56], &b""[..]))
/// );
/// ```
fn repeat_n<const N: usize, S: Parser>(self, separator: S) -> RepeatN<N, Self, S>
where
for<'i> Self::Output<'i>: Copy + Default,
{
RepeatN {
parser: self,
separator,
}
}
/// Repeat this parser while it matches, returning a [`ArrayVec`](crate::array::ArrayVec).
///
/// This parser can parse up to `N` items. If more items match, it will return an error.
///
/// See [`repeat`](Self::repeat) if the upper bound is large or not known, and
/// [`repeat_n`](Self::repeat_n) if the number of items is consistent.
///
/// # Examples
/// ```
/// # use utils::parser::{self, Parser};
/// let parser = parser::u32()
/// .repeat(",", 3);
/// assert_eq!(parser.parse(b"12,34,56,78"), Ok((vec![12, 34, 56, 78], &b""[..])));
/// assert!(parser.parse(b"12,34").is_err());
/// ```
fn repeat_arrayvec<const N: usize, S: Parser>(
self,
separator: S,
min_elements: usize,
) -> RepeatArrayVec<N, Self, S>
where
for<'a> Self::Output<'a>: Copy + Default,
{
RepeatArrayVec {
parser: self,
separator,
min_elements,
}
}
/// Repeat this parser while it matches, returning a [`Vec`].
///
/// To avoid allocating, prefer [`repeat_n`](Self::repeat_n) if the number of items is
/// consistent and known in advance, or [`repeat_arrayvec`](Self::repeat_arrayvec) if the number
/// of items is variable but has a known upper bound.
///
/// # Examples
/// ```
/// # use utils::parser::{self, Parser};
/// let parser = parser::u32()
/// .repeat(",", 3);
/// assert_eq!(parser.parse(b"12,34,56,78"), Ok((vec![12, 34, 56, 78], &b""[..])));
/// assert!(parser.parse(b"12,34").is_err());
/// ```
fn repeat<S: Parser>(self, separator: S, min_elements: usize) -> RepeatVec<Self, S> {
RepeatVec {
parser: self,
separator,
min_elements,
}
}
/// Parse a prefix (normally a string literal) before this parser.
///
/// The result of the prefix parser is discarded.
///
/// # Examples
/// ```
/// # use utils::parser::{self, Parser};
/// assert_eq!(
/// parser::u32()
/// .with_prefix("abc")
/// .parse(b"abc123"),
/// Ok((123, &b""[..]))
/// );
/// ```
fn with_prefix<T: Parser>(self, prefix: T) -> WithPrefix<Self, T> {
WithPrefix {
parser: self,
prefix,
}
}
/// Parse a suffix (normally a string literal) after this parser.
///
/// The result of the suffix parser is discarded.
///
/// # Examples
/// ```
/// # use utils::parser::{self, Parser};
/// assert_eq!(
/// parser::u32()
/// .with_suffix("abc")
/// .parse(b"123abc"),
/// Ok((123, &b""[..]))
/// );
/// ```
fn with_suffix<T: Parser>(self, suffix: T) -> WithSuffix<Self, T> {
WithSuffix {
parser: self,
suffix,
}
}
/// Replace this parser's error message with the provided string.
///
/// # Examples
/// ```
/// # use utils::parser::{self, ParseError, Parser};
/// let parser = parser::u8()
/// .error_msg("expected power level");
/// assert_eq!(
/// parser.parse(b"123"),
/// Ok((123, &b""[..]))
/// );
/// assert_eq!(
/// parser.parse(b"abc"),
/// Err((ParseError::Custom("expected power level"), &b"abc"[..]))
/// );
/// ```
fn error_msg(self, message: &'static str) -> WithErrorMsg<Self> {
WithErrorMsg {
parser: self,
message,
}
}
/// Apply this parser once, checking the provided input is fully consumed.
///
/// # Examples
/// ```
/// # use utils::parser::{self, Parser};
/// assert_eq!(parser::u32().parse_complete("1234").unwrap(), 1234);
/// assert!(parser::u32().parse_complete("1234abc").is_err());
/// ```
fn parse_complete<'i>(&self, input: &'i str) -> Result<Self::Output<'i>, InputError> {
match self.parse(input.as_bytes()).map_with_input(input)? {
(v, []) => Ok(v),
(_, remaining) => Err(InputError::new(input, remaining, "expected end of input")),
}
}
/// Apply this parser repeatedly until the provided input is fully consumed.
///
/// Equivalent to `parser.repeat(parser::noop(), 0).parse_complete(input)`.
///
/// # Examples
/// ```
/// # use utils::parser::{self, Parser};
/// assert_eq!(
/// parser::u32()
/// .then(parser::u32().with_prefix("x"))
/// .with_suffix(",".or(parser::eof()))
/// .parse_all("1x2,3x4,1234x5678")
/// .unwrap(),
/// vec![
/// (1, 2),
/// (3, 4),
/// (1234, 5678),
/// ]
/// );
/// ```
fn parse_all<'i>(&self, input: &'i str) -> Result<Vec<Self::Output<'i>>, InputError> {
ParserRef(self)
.repeat(Constant(()), 0)
.parse_complete(input)
}
/// Similar to [`parse_all`](Self::parse_all) but expects a newline after each item.
///
/// Equivalent to `parser.with_suffix(`[`parser::eol()`](super::eol)`).parse_all(input)`.
///
/// # Examples
/// ```
/// # use utils::parser::{self, Parser};
/// assert_eq!(
/// parser::u32()
/// .then(parser::u32().with_prefix("x"))
/// .parse_lines("1x2\n3x4\n1234x5678")
/// .unwrap(),
/// vec![
/// (1, 2),
/// (3, 4),
/// (1234, 5678),
/// ]
/// );
/// ```
fn parse_lines<'i>(&self, input: &'i str) -> Result<Vec<Self::Output<'i>>, InputError> {
ParserRef(self)
.with_suffix(Eol())
.repeat(Constant(()), 0)
.parse_complete(input)
}
/// Create an iterator which applies this parser repeatedly until the provided input is fully
/// consumed.
///
/// The returned iterator will lazily parse the provided input string, producing a sequence of
/// [`Result`] values. Once the end of input is reached, or an error is returned, the parser
/// will always return [`None`].
///
/// # Examples
/// ```
/// # use utils::input::InputError;
/// # use utils::parser::{self, Parser};
/// let iterator = parser::u32()
/// .with_suffix(parser::eol())
/// .parse_iterator("12\n34\n56\n78");
/// for item in iterator {
/// println!("{}", item?);
/// }
/// # Ok::<(), InputError>(())
/// ```
///
/// ```
/// # use utils::parser::{self, Parser};
/// let mut iterator = parser::u32()
/// .with_suffix(parser::eol())
/// .parse_iterator("12\n34\nnot a integer");
/// assert_eq!(iterator.next().unwrap().unwrap(), 12);
/// assert_eq!(iterator.next().unwrap().unwrap(), 34);
/// assert!(iterator.next().unwrap().is_err());
/// assert!(iterator.next().is_none());
/// ```
///
/// ```
/// # use utils::input::InputError;
/// # use utils::parser::{self, Parser};
/// let filtered = parser::u32()
/// .with_suffix(parser::eol())
/// .parse_iterator("11\n22\n33\n44\n55")
/// .filter(|r| r.is_err() || r.as_ref().is_ok_and(|v| v % 2 == 0))
/// .collect::<Result<Vec<u32>, InputError>>()?;
/// assert_eq!(filtered, vec![22, 44]);
/// # Ok::<(), InputError>(())
/// ```
fn parse_iterator(self, input: &str) -> ParserIterator<Self> {
ParserIterator {
input,
remaining: input.as_bytes(),
parser: self,
}
}
}
// Workaround to allow using methods which consume a parser in methods which take references.
struct ParserRef<'a, P>(&'a P);
impl<'a, P: Parser> Parser for ParserRef<'a, P> {
type Output<'i> = P::Output<'i>;
type Then<T: Parser> = Unimplemented;
#[inline]
fn parse<'i>(&self, input: &'i [u8]) -> ParseResult<'i, Self::Output<'i>> {
self.0.parse(input)
}
}
/// Matches the string literal exactly.
///
/// Normally used with [`with_prefix`](Parser::with_prefix)/[`with_suffix`](Parser::with_suffix).
impl Parser for &'static str {
type Output<'i> = ();
type Then<T: Parser> = Then2<Self, T>;
#[inline]
fn parse<'i>(&self, input: &'i [u8]) -> ParseResult<'i, Self::Output<'i>> {
// This is faster than using strip_prefix for the common case where the string is a short
// string literal known at compile time.
if input.len() >= self.len() && self.bytes().zip(input).all(|(a, &b)| a == b) {
Ok(((), &input[self.len()..]))
} else {
Err((ParseError::ExpectedLiteral(self), input))
}
}
}
/// Matches the byte exactly.
///
/// Normally used with [`with_prefix`](Parser::with_prefix)/[`with_suffix`](Parser::with_suffix).
impl Parser for u8 {
type Output<'i> = ();
type Then<T: Parser> = Then2<Self, T>;
#[inline]
fn parse<'i>(&self, input: &'i [u8]) -> ParseResult<'i, Self::Output<'i>> {
if input.first() == Some(self) {
Ok(((), &input[1..]))
} else {
Err((ParseError::ExpectedByte(*self), input))
}
}
}
/// Allow custom functions and closures to be used as parsers.
impl<O, F: Fn(&[u8]) -> ParseResult<O>> Parser for F {
type Output<'i> = O;
type Then<T: Parser> = Then2<Self, T>;
#[inline]
fn parse<'i>(&self, input: &'i [u8]) -> ParseResult<'i, Self::Output<'i>> {
self(input)
}
}
/// Trait for types that have a canonical parser.
pub trait Parseable {
type Parser: for<'i> Parser<Output<'i> = Self>;
const PARSER: Self::Parser;
}