From b06ebdeeca8f6fe983e50fa5cac10fa899b64ece Mon Sep 17 00:00:00 2001 From: zyansheep Date: Tue, 6 Feb 2024 13:48:53 -0500 Subject: [PATCH] Revert MapExtra lifetime commit (and re-move to input module) --- src/combinator.rs | 12 ++++++------ src/input.rs | 40 ++++++++++++++++------------------------ src/lib.rs | 13 ++++++++----- src/pratt.rs | 20 +++++++++++--------- src/primitive.rs | 8 ++++---- 5 files changed, 45 insertions(+), 48 deletions(-) diff --git a/src/combinator.rs b/src/combinator.rs index b4c7928c..9070b74d 100644 --- a/src/combinator.rs +++ b/src/combinator.rs @@ -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(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult { @@ -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 = A::IterState where @@ -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, + F: Fn(OA, &mut MapExtra<'a, '_, '_, I, E>) -> Result, { #[inline(always)] fn go(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult { @@ -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(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult @@ -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(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult @@ -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) -> U, + F: Fn(OA, &mut MapExtra<'a, '_, '_, I, E>, &mut Emitter) -> U, { #[inline(always)] fn go(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult diff --git a/src/input.rs b/src/input.rs index 00b22ad9..6dc699e2 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1518,36 +1518,28 @@ impl Emitter { } /// 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. @@ -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() } } diff --git a/src/lib.rs b/src/lib.rs index e85f30bc..d4f25442 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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>(self, f: F) -> MapWith + fn map_with) -> U>( + self, + f: F, + ) -> MapWith where Self: Sized, { @@ -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) -> Result>( + fn try_map_with) -> Result>( self, f: F, ) -> TryMapWith @@ -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(self, other: B, f: F) -> FoldlWith 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, { @@ -1937,7 +1940,7 @@ pub trait Parser<'a, I: Input<'a>, O, E: ParserExtra<'a, I> = extra::Default>: fn validate(self, f: F) -> Validate where Self: Sized, - F: Fn(O, &mut MapExtra<'a, '_, I, E>, &mut Emitter) -> U, + F: Fn(O, &mut MapExtra<'a, '_, '_, I, E>, &mut Emitter) -> U, { Validate { parser: self, @@ -2490,7 +2493,7 @@ where #[cfg_attr(debug_assertions, track_caller)] fn foldr_with(self, other: B, f: F) -> FoldrWith 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, { diff --git a/src/pratt.rs b/src/pratt.rs index cc617cf6..c6951b82 100644 --- a/src/pratt.rs +++ b/src/pratt.rs @@ -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!() } } @@ -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 } } }; } @@ -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| ` 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`]. @@ -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 } } }; } @@ -318,7 +320,7 @@ prefix_op!(|f: Fn(O) -> O, _op, rhs, _extra| f(rhs)); // Allow `|op, rhs| ` 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| ` 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 { @@ -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 } } }; } @@ -393,7 +395,7 @@ postfix_op!(|f: Fn(O) -> O, lhs, _op, _extra| f(lhs)); // Allow `|lhs, op| ` 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| ` 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)] diff --git a/src/primitive.rs b/src/primitive.rs index 89122b8b..889c778f 100644 --- a/src/primitive.rs +++ b/src/primitive.rs @@ -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, + F: Fn(I::Token, &mut MapExtra<'a, '_, '_, I, E>) -> Option, { Select { filter, @@ -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, + F: Fn(I::Token, &mut MapExtra<'a, '_, '_, I, E>) -> Option, { #[inline] fn go(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult { @@ -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, + F: Fn(&'a I::Token, &mut MapExtra<'a, '_, '_, I, E>) -> Option, { SelectRef { filter, @@ -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, + F: Fn(&'a I::Token, &mut MapExtra<'a, '_, '_, I, E>) -> Option, { #[inline] fn go(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult {