Skip to content

Commit

Permalink
Merge pull request #595 from zyansheep/revert-mapextra-lifetime-commit
Browse files Browse the repository at this point in the history
Revert MapExtra lifetime commit (and re-move to input module)
  • Loading branch information
zesterer authored Feb 7, 2024
2 parents b6afd8c + a97bc4c commit 184b4f3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 48 deletions.
12 changes: 6 additions & 6 deletions src/combinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ where
I: Input<'a>,
E: ParserExtra<'a, I>,
A: Parser<'a, I, OA, E>,
F: Fn(OA, &mut MapExtra<'a, '_, I, E>) -> O,
F: Fn(OA, &mut MapExtra<'a, '_, '_, I, E>) -> O,
{
#[inline(always)]
fn go<M: Mode>(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult<M, O> {
Expand All @@ -373,7 +373,7 @@ where
I: Input<'a>,
E: ParserExtra<'a, I>,
A: IterParser<'a, I, OA, E>,
F: Fn(OA, &mut MapExtra<'a, '_, I, E>) -> O,
F: Fn(OA, &mut MapExtra<'a, '_, '_, I, E>) -> O,
{
type IterState<M: Mode> = A::IterState<M>
where
Expand Down Expand Up @@ -579,7 +579,7 @@ where
I: Input<'a>,
E: ParserExtra<'a, I>,
A: Parser<'a, I, OA, E>,
F: Fn(OA, &mut MapExtra<'a, '_, I, E>) -> Result<O, E::Error>,
F: Fn(OA, &mut MapExtra<'a, '_, '_, I, E>) -> Result<O, E::Error>,
{
#[inline(always)]
fn go<M: Mode>(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult<M, O> {
Expand Down Expand Up @@ -2362,7 +2362,7 @@ where
A: IterParser<'a, I, OA, E>,
B: Parser<'a, I, O, E>,
E: ParserExtra<'a, I>,
F: Fn(OA, O, &mut MapExtra<'a, '_, I, E>) -> O,
F: Fn(OA, O, &mut MapExtra<'a, '_, '_, I, E>) -> O,
{
#[inline(always)]
fn go<M: Mode>(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult<M, O>
Expand Down Expand Up @@ -2497,7 +2497,7 @@ where
A: Parser<'a, I, O, E>,
B: IterParser<'a, I, OB, E>,
E: ParserExtra<'a, I>,
F: Fn(O, OB, &mut MapExtra<'a, '_, I, E>) -> O,
F: Fn(O, OB, &mut MapExtra<'a, '_, '_, I, E>) -> O,
{
#[inline(always)]
fn go<M: Mode>(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult<M, O>
Expand Down Expand Up @@ -2688,7 +2688,7 @@ where
I: Input<'a>,
E: ParserExtra<'a, I>,
A: Parser<'a, I, OA, E>,
F: Fn(OA, &mut MapExtra<'a, '_, I, E>, &mut Emitter<E::Error>) -> U,
F: Fn(OA, &mut MapExtra<'a, '_, '_, I, E>, &mut Emitter<E::Error>) -> U,
{
#[inline(always)]
fn go<M: Mode>(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult<M, U>
Expand Down
40 changes: 16 additions & 24 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1518,36 +1518,28 @@ impl<E> Emitter<E> {
}

/// See [`Parser::map_with`].
pub struct MapExtra<'a, 'b, I: Input<'a>, E: ParserExtra<'a, I>> {
before: I::Offset,
after: I::Offset,
inp: &'b I,
state: &'b mut E::State,
ctx: &'b E::Context,
/// Note: 'a is the lifetime of the Input, 'b is the lifetime of the embedded InputRef, 'parse if the lifetime of the parser and corresponding state.
pub struct MapExtra<'a, 'b, 'parse, I: Input<'a>, E: ParserExtra<'a, I>> {
before: Offset<'a, 'parse, I>,
inp: &'b mut InputRef<'a, 'parse, I, E>,
}

impl<'a, 'b, I: Input<'a>, E: ParserExtra<'a, I>> MapExtra<'a, 'b, I, E> {
#[inline(always)]
pub(crate) fn new<'parse>(
impl<'a, 'b, 'parse, I: Input<'a>, E: ParserExtra<'a, I>> MapExtra<'a, 'b, 'parse, I, E> {
/// Create new MapExtra
/// SAFETY: `before` Offset must be from a previous call to inp.offset().
pub(crate) fn new(
before: Offset<'a, 'parse, I>,
inp: &'b mut InputRef<'a, 'parse, I, E>,
) -> Self {
Self {
before: before.offset,
after: inp.offset,
ctx: inp.ctx,
state: inp.state,
inp: inp.input,
}
MapExtra { before, inp }
}

/// Get the span corresponding to the output.
// SAFETY: The offsets both came from the same input
// TODO: Should this make `MapExtra::new` unsafe? Probably, but it's an internal API and we simply wouldn't
// ever abuse it in this way, even accidentally.
#[inline(always)]
pub fn span(&self) -> I::Span {
// SAFETY: The offsets both came from the same input
// TODO: Should this make `MapExtra::new` unsafe? Probably, but it's an internal API and we simply wouldn't
// ever abuse it in this way, even accidentally.
unsafe { self.inp.span(self.before..self.after) }
self.inp.span(self.before..self.inp.offset())
}

/// Get the slice corresponding to the output.
Expand All @@ -1556,18 +1548,18 @@ impl<'a, 'b, I: Input<'a>, E: ParserExtra<'a, I>> MapExtra<'a, 'b, I, E> {
where
I: SliceInput<'a>,
{
self.inp.slice(self.before..self.after)
self.inp.slice(self.before..self.inp.offset())
}

/// Get the parser state.
#[inline(always)]
pub fn state(&mut self) -> &mut E::State {
self.state
self.inp.state()
}

/// Get the current parser context.
#[inline(always)]
pub fn ctx(&self) -> &E::Context {
self.ctx
self.inp.ctx()
}
}
13 changes: 8 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,10 @@ pub trait Parser<'a, I: Input<'a>, O, E: ParserExtra<'a, I> = extra::Default>:
/// assert_eq!(palindrome_parser().parse("hello olleh").into_result().as_deref(), Ok(" olleh"));
/// assert!(palindrome_parser().parse("abccb").into_result().is_err());
/// ```
fn map_with<U, F: Fn(O, &mut MapExtra<'a, '_, I, E>) -> U>(self, f: F) -> MapWith<Self, O, F>
fn map_with<U, F: Fn(O, &mut MapExtra<'a, '_, '_, I, E>) -> U>(
self,
f: F,
) -> MapWith<Self, O, F>
where
Self: Sized,
{
Expand Down Expand Up @@ -771,7 +774,7 @@ pub trait Parser<'a, I: Input<'a>, O, E: ParserExtra<'a, I> = extra::Default>:
/// [`Parser::validate`] instead.
///
/// The output type of this parser is `U`, the [`Ok`] return value of the function.
fn try_map_with<U, F: Fn(O, &mut MapExtra<'a, '_, I, E>) -> Result<U, E::Error>>(
fn try_map_with<U, F: Fn(O, &mut MapExtra<'a, '_, '_, I, E>) -> Result<U, E::Error>>(
self,
f: F,
) -> TryMapWith<Self, O, F>
Expand Down Expand Up @@ -1621,7 +1624,7 @@ pub trait Parser<'a, I: Input<'a>, O, E: ParserExtra<'a, I> = extra::Default>:
#[cfg_attr(debug_assertions, track_caller)]
fn foldl_with<B, F, OB>(self, other: B, f: F) -> FoldlWith<F, Self, B, OB, E>
where
F: Fn(O, OB, &mut MapExtra<'a, '_, I, E>) -> O,
F: Fn(O, OB, &mut MapExtra<'a, '_, '_, I, E>) -> O,
B: IterParser<'a, I, OB, E>,
Self: Sized,
{
Expand Down Expand Up @@ -1937,7 +1940,7 @@ pub trait Parser<'a, I: Input<'a>, O, E: ParserExtra<'a, I> = extra::Default>:
fn validate<U, F>(self, f: F) -> Validate<Self, O, F>
where
Self: Sized,
F: Fn(O, &mut MapExtra<'a, '_, I, E>, &mut Emitter<E::Error>) -> U,
F: Fn(O, &mut MapExtra<'a, '_, '_, I, E>, &mut Emitter<E::Error>) -> U,
{
Validate {
parser: self,
Expand Down Expand Up @@ -2490,7 +2493,7 @@ where
#[cfg_attr(debug_assertions, track_caller)]
fn foldr_with<B, F, OA>(self, other: B, f: F) -> FoldrWith<F, Self, B, O, E>
where
F: Fn(O, OA, &mut MapExtra<'a, '_, I, E>) -> OA,
F: Fn(O, OA, &mut MapExtra<'a, '_, '_, I, E>) -> OA,
B: Parser<'a, I, OA, E>,
Self: Sized,
{
Expand Down
20 changes: 11 additions & 9 deletions src/pratt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ where
_lhs: O,
_op: Self::Op,
_rhs: O,
_extra: &mut MapExtra<'a, '_, I, E>,
_extra: &mut MapExtra<'a, '_, '_, I, E>,
) -> O {
unreachable!()
}
fn fold_prefix(&self, _op: Self::Op, _rhs: O, _extra: &mut MapExtra<'a, '_, I, E>) -> O {
fn fold_prefix(&self, _op: Self::Op, _rhs: O, _extra: &mut MapExtra<'a, '_, '_, I, E>) -> O {
unreachable!()
}
fn fold_postfix(&self, _lhs: O, _op: Self::Op, _extra: &mut MapExtra<'a, '_, I, E>) -> O {
fn fold_postfix(&self, _lhs: O, _op: Self::Op, _extra: &mut MapExtra<'a, '_, '_, I, E>) -> O {
unreachable!()
}
}
Expand Down Expand Up @@ -231,7 +231,7 @@ macro_rules! infix_op {
const IS_INFIX: bool = true;
#[inline(always)] fn op_parser(&self) -> &Self::OpParser { &self.op_parser }
#[inline(always)] fn associativity(&self) -> Associativity { self.associativity }
#[inline(always)] fn fold_infix(&self, $lhs: O, $op: Self::Op, $rhs: O, $extra: &mut MapExtra<'a, '_, I, E>) -> O { let $f = &self.fold; $invoke }
#[inline(always)] fn fold_infix(&self, $lhs: O, $op: Self::Op, $rhs: O, $extra: &mut MapExtra<'a, '_, '_, I, E>) -> O { let $f = &self.fold; $invoke }
}
};
}
Expand All @@ -242,7 +242,9 @@ infix_op!(|f: Fn(O, O) -> O, lhs, _op, rhs, _extra| f(lhs, rhs));
infix_op!(|f: Fn(O, Op, O) -> O, lhs, op, rhs, _extra| f(lhs, op, rhs));
// Allow `|lhs, op, rhs, extra| <expr>` to be used as a fold closure for infix operators
infix_op!(
|f: Fn(O, Op, O, &mut MapExtra<'a, '_, I, E>) -> O, lhs, op, rhs, extra| f(lhs, op, rhs, extra)
|f: Fn(O, Op, O, &mut MapExtra<'a, '_, '_, I, E>) -> O, lhs, op, rhs, extra| f(
lhs, op, rhs, extra
)
);

/// See [`prefix`].
Expand Down Expand Up @@ -308,7 +310,7 @@ macro_rules! prefix_op {
const IS_PREFIX: bool = true;
#[inline(always)] fn op_parser(&self) -> &Self::OpParser { &self.op_parser }
#[inline(always)] fn associativity(&self) -> Associativity { Associativity::Left(self.binding_power) }
#[inline(always)] fn fold_prefix(&self, $op: Self::Op, $rhs: O, $extra: &mut MapExtra<'a, '_, I, E>) -> O { let $f = &self.fold; $invoke }
#[inline(always)] fn fold_prefix(&self, $op: Self::Op, $rhs: O, $extra: &mut MapExtra<'a, '_, '_, I, E>) -> O { let $f = &self.fold; $invoke }
}
};
}
Expand All @@ -318,7 +320,7 @@ prefix_op!(|f: Fn(O) -> O, _op, rhs, _extra| f(rhs));
// Allow `|op, rhs| <expr>` to be used as a fold closure for prefix operators
prefix_op!(|f: Fn(Op, O) -> O, op, rhs, _extra| f(op, rhs));
// Allow `|op, rhs, span| <expr>` to be used as a fold closure for prefix operators
prefix_op!(|f: Fn(Op, O, &mut MapExtra<'a, '_, I, E>) -> O, op, rhs, extra| f(op, rhs, extra));
prefix_op!(|f: Fn(Op, O, &mut MapExtra<'a, '_, '_, I, E>) -> O, op, rhs, extra| f(op, rhs, extra));

/// See [`postfix`].
pub struct Postfix<A, F, Op, Args> {
Expand Down Expand Up @@ -383,7 +385,7 @@ macro_rules! postfix_op {
const IS_POSTFIX: bool = true;
#[inline(always)] fn op_parser(&self) -> &Self::OpParser { &self.op_parser }
#[inline(always)] fn associativity(&self) -> Associativity { Associativity::Left(self.binding_power) }
#[inline(always)] fn fold_postfix(&self, $lhs: O, $op: Self::Op, $extra: &mut MapExtra<'a, '_, I, E>) -> O { let $f = &self.fold; $invoke }
#[inline(always)] fn fold_postfix(&self, $lhs: O, $op: Self::Op, $extra: &mut MapExtra<'a, '_, '_, I, E>) -> O { let $f = &self.fold; $invoke }
}
};
}
Expand All @@ -393,7 +395,7 @@ postfix_op!(|f: Fn(O) -> O, lhs, _op, _extra| f(lhs));
// Allow `|lhs, op| <expr>` to be used as a fold closure for postfix operators
postfix_op!(|f: Fn(O, Op) -> O, lhs, op, _extra| f(lhs, op));
// Allow `|lhs, op, span| <expr>` to be used as a fold closure for postfix operators
postfix_op!(|f: Fn(O, Op, &mut MapExtra<'a, '_, I, E>) -> O, lhs, op, extra| f(lhs, op, extra));
postfix_op!(|f: Fn(O, Op, &mut MapExtra<'a, '_, '_, I, E>) -> O, lhs, op, extra| f(lhs, op, extra));

/// See [`Parser::pratt`].
#[derive(Copy, Clone)]
Expand Down
8 changes: 4 additions & 4 deletions src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ where
I: Input<'a>,
I::Token: Clone + 'a,
E: ParserExtra<'a, I>,
F: Fn(I::Token, &mut MapExtra<'a, '_, I, E>) -> Option<O>,
F: Fn(I::Token, &mut MapExtra<'a, '_, '_, I, E>) -> Option<O>,
{
Select {
filter,
Expand All @@ -452,7 +452,7 @@ where
I: ValueInput<'a>,
I::Token: Clone + 'a,
E: ParserExtra<'a, I>,
F: Fn(I::Token, &mut MapExtra<'a, '_, I, E>) -> Option<O>,
F: Fn(I::Token, &mut MapExtra<'a, '_, '_, I, E>) -> Option<O>,
{
#[inline]
fn go<M: Mode>(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult<M, O> {
Expand Down Expand Up @@ -496,7 +496,7 @@ where
I: BorrowInput<'a>,
I::Token: 'a,
E: ParserExtra<'a, I>,
F: Fn(&'a I::Token, &mut MapExtra<'a, '_, I, E>) -> Option<O>,
F: Fn(&'a I::Token, &mut MapExtra<'a, '_, '_, I, E>) -> Option<O>,
{
SelectRef {
filter,
Expand All @@ -509,7 +509,7 @@ where
I: BorrowInput<'a>,
I::Token: 'a,
E: ParserExtra<'a, I>,
F: Fn(&'a I::Token, &mut MapExtra<'a, '_, I, E>) -> Option<O>,
F: Fn(&'a I::Token, &mut MapExtra<'a, '_, '_, I, E>) -> Option<O>,
{
#[inline]
fn go<M: Mode>(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult<M, O> {
Expand Down

0 comments on commit 184b4f3

Please sign in to comment.