Skip to content

Commit b0d6495

Browse files
committed
rust: convert pgsql to nom 8
Provide Nom 8 variants for some of our common parser functions, which can become the defaults after a complete Nom 8 conversion.
1 parent 8718dcd commit b0d6495

3 files changed

Lines changed: 163 additions & 123 deletions

File tree

rust/src/common.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,46 @@ pub mod nom7 {
6262
}
6363
}
6464

65+
pub mod nom8 {
66+
use nom8::bytes::streaming::{tag, take_until};
67+
use nom8::error::{Error, ParseError};
68+
use nom8::ErrorConvert;
69+
use nom8::{IResult, Parser};
70+
71+
/// Reimplementation of `take_until_and_consume` for nom 8
72+
///
73+
/// `take_until` does not consume the matched tag, and
74+
/// `take_until_and_consume` was removed in nom 7. This function
75+
/// provides an implementation (specialized for `&[u8]`).
76+
pub fn take_until_and_consume<'a, E: ParseError<&'a [u8]>>(
77+
t: &'a [u8],
78+
) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], &'a [u8], E> {
79+
move |i: &'a [u8]| {
80+
let (i, res) = take_until(t).parse(i)?;
81+
let (i, _) = tag(t).parse(i)?;
82+
Ok((i, res))
83+
}
84+
}
85+
86+
/// Specialized version of the nom 8 `bits` combinator
87+
///
88+
/// The `bits combinator has trouble inferring the transient error type
89+
/// used by the tuple parser, because the function is generic and any
90+
/// error type would be valid.
91+
/// Use an explicit error type (as described in
92+
/// https://docs.rs/nom/7.1.0/nom/bits/fn.bits.html) to solve this problem, and
93+
/// specialize this function for `&[u8]`.
94+
pub fn bits<'a, O, E, P>(parser: P) -> impl FnMut(&'a [u8]) -> IResult<&'a [u8], O, E>
95+
where
96+
E: ParseError<&'a [u8]>,
97+
Error<(&'a [u8], usize)>: ErrorConvert<E>,
98+
P: FnMut((&'a [u8], usize)) -> IResult<(&'a [u8], usize), O, Error<(&'a [u8], usize)>>,
99+
{
100+
// use full path to disambiguate nom `bits` from this current function name
101+
nom8::bits::bits(parser)
102+
}
103+
}
104+
65105
/// Convert a String to C-compatible string
66106
///
67107
/// This function will consume the provided data and use the underlying bytes to construct a new

0 commit comments

Comments
 (0)