From a7a3c1fcbbee21195c4dfe9e0eb9a8f4511e1e29 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Sat, 26 Oct 2024 17:39:47 +0100 Subject: [PATCH] Removed sealed versions of parser traits in favour of doc(hidden) sealing --- src/blanket.rs | 36 +------------- src/combinator.rs | 116 +++++++++++++++++++++++----------------------- src/either.rs | 10 ++-- src/extension.rs | 2 +- src/label.rs | 2 +- src/lib.rs | 104 +++++++++++++++++++++++++++++++---------- src/number.rs | 8 +--- src/pratt.rs | 2 +- src/primitive.rs | 40 ++++++++-------- src/private.rs | 89 ----------------------------------- src/recovery.rs | 2 +- src/recursive.rs | 4 +- src/regex.rs | 2 +- src/text.rs | 2 +- 14 files changed, 173 insertions(+), 246 deletions(-) diff --git a/src/blanket.rs b/src/blanket.rs index 9e6c2594..00b74ad7 100644 --- a/src/blanket.rs +++ b/src/blanket.rs @@ -1,6 +1,6 @@ use super::*; -impl<'a, T, I, O, E> ParserSealed<'a, I, O, E> for &T +impl<'a, T, I, O, E> Parser<'a, I, O, E> for &T where T: ?Sized + Parser<'a, I, O, E>, I: Input<'a>, @@ -16,7 +16,7 @@ where go_extra!(O); } -impl<'a, T, I, O, E> ConfigParserSealed<'a, I, O, E> for &T +impl<'a, T, I, O, E> ConfigParser<'a, I, O, E> for &T where T: ?Sized + ConfigParser<'a, I, O, E>, I: Input<'a>, @@ -33,35 +33,3 @@ where go_cfg_extra!(O); } - -impl<'a, I, O, E, P> Parser<'a, I, O, E> for P -where - I: Input<'a>, - E: ParserExtra<'a, I>, - P: ?Sized + ParserSealed<'a, I, O, E>, -{ -} - -impl<'a, I, O, E, P> ConfigParser<'a, I, O, E> for P -where - I: Input<'a>, - E: ParserExtra<'a, I>, - P: ?Sized + ConfigParserSealed<'a, I, O, E>, -{ -} - -impl<'a, I, O, E, P> IterParser<'a, I, O, E> for P -where - I: Input<'a>, - E: ParserExtra<'a, I>, - P: IterParserSealed<'a, I, O, E>, -{ -} - -impl<'a, I, O, E, P> ConfigIterParser<'a, I, O, E> for P -where - I: Input<'a>, - E: ParserExtra<'a, I>, - P: ConfigIterParserSealed<'a, I, O, E>, -{ -} diff --git a/src/combinator.rs b/src/combinator.rs index e3bc32da..70497e33 100644 --- a/src/combinator.rs +++ b/src/combinator.rs @@ -20,7 +20,7 @@ pub struct Configure { pub(crate) cfg: F, } -impl<'a, I, O, E, A, F> ParserSealed<'a, I, O, E> for Configure +impl<'a, I, O, E, A, F> Parser<'a, I, O, E> for Configure where A: ConfigParser<'a, I, O, E>, F: Fn(A::Config, &E::Context) -> A::Config, @@ -58,7 +58,7 @@ impl Clone for IterConfigure { } } -impl<'a, I, OA, E, A, F> ParserSealed<'a, I, (), E> for IterConfigure +impl<'a, I, OA, E, A, F> Parser<'a, I, (), E> for IterConfigure where A: ConfigIterParser<'a, I, OA, E>, F: Fn(A::Config, &E::Context) -> A::Config, @@ -80,7 +80,7 @@ where go_extra!(()); } -impl<'a, I, O, E, A, F> IterParserSealed<'a, I, O, E> for IterConfigure +impl<'a, I, O, E, A, F> IterParser<'a, I, O, E> for IterConfigure where A: ConfigIterParser<'a, I, O, E>, F: Fn(A::Config, &E::Context) -> A::Config, @@ -132,7 +132,7 @@ impl Clone for TryIterConfigure { } } -impl<'a, I, OA, E, A, F> ParserSealed<'a, I, (), E> for TryIterConfigure +impl<'a, I, OA, E, A, F> Parser<'a, I, (), E> for TryIterConfigure where A: ConfigIterParser<'a, I, OA, E>, F: Fn(A::Config, &E::Context, I::Span) -> Result, @@ -154,7 +154,7 @@ where go_extra!(()); } -impl<'a, I, O, E, A, F> IterParserSealed<'a, I, O, E> for TryIterConfigure +impl<'a, I, O, E, A, F> IterParser<'a, I, O, E> for TryIterConfigure where A: ConfigIterParser<'a, I, O, E>, F: Fn(A::Config, &E::Context, I::Span) -> Result, @@ -203,7 +203,7 @@ impl Clone for ToSlice { } } -impl<'a, A, I, O, E> ParserSealed<'a, I, I::Slice, E> for ToSlice +impl<'a, A, I, O, E> Parser<'a, I, I::Slice, E> for ToSlice where A: Parser<'a, I, O, E>, I: SliceInput<'a>, @@ -239,7 +239,7 @@ impl Clone for Filter { } } -impl<'a, A, I, O, E, F> ParserSealed<'a, I, O, E> for Filter +impl<'a, A, I, O, E, F> Parser<'a, I, O, E> for Filter where I: Input<'a>, E: ParserExtra<'a, I>, @@ -282,7 +282,7 @@ impl Clone for Map { } } -impl<'a, I, O, E, A, OA, F> ParserSealed<'a, I, O, E> for Map +impl<'a, I, O, E, A, OA, F> Parser<'a, I, O, E> for Map where I: Input<'a>, E: ParserExtra<'a, I>, @@ -298,7 +298,7 @@ where go_extra!(O); } -impl<'a, I, O, E, A, OA, F> IterParserSealed<'a, I, O, E> for Map +impl<'a, I, O, E, A, OA, F> IterParser<'a, I, O, E> for Map where I: Input<'a>, E: ParserExtra<'a, I>, @@ -351,7 +351,7 @@ impl Clone for MapWith { } } -impl<'a, I, O, E, A, OA, F> ParserSealed<'a, I, O, E> for MapWith +impl<'a, I, O, E, A, OA, F> Parser<'a, I, O, E> for MapWith where I: Input<'a>, E: ParserExtra<'a, I>, @@ -370,7 +370,7 @@ where go_extra!(O); } -impl<'a, I, O, E, A, OA, F> IterParserSealed<'a, I, O, E> for MapWith +impl<'a, I, O, E, A, OA, F> IterParser<'a, I, O, E> for MapWith where I: Input<'a>, E: ParserExtra<'a, I>, @@ -430,7 +430,7 @@ impl Clone for MapGroup { } #[cfg(feature = "nightly")] -impl<'a, I, O, E, A, OA, F> ParserSealed<'a, I, O, E> for MapGroup +impl<'a, I, O, E, A, OA, F> Parser<'a, I, O, E> for MapGroup where I: Input<'a>, E: ParserExtra<'a, I>, @@ -448,7 +448,7 @@ where } #[cfg(feature = "nightly")] -impl<'a, I, O, E, A, OA, F> IterParserSealed<'a, I, O, E> for MapGroup +impl<'a, I, O, E, A, OA, F> IterParser<'a, I, O, E> for MapGroup where I: Input<'a>, E: ParserExtra<'a, I>, @@ -500,7 +500,7 @@ impl Clone for ToSpan { } } -impl<'a, I, OA, E, A> ParserSealed<'a, I, I::Span, E> for ToSpan +impl<'a, I, OA, E, A> Parser<'a, I, I::Span, E> for ToSpan where I: Input<'a>, E: ParserExtra<'a, I>, @@ -535,7 +535,7 @@ impl Clone for TryMap { } } -impl<'a, I, O, E, A, OA, F> ParserSealed<'a, I, O, E> for TryMap +impl<'a, I, O, E, A, OA, F> Parser<'a, I, O, E> for TryMap where I: Input<'a>, E: ParserExtra<'a, I>, @@ -578,7 +578,7 @@ impl Clone for TryMapWith { } } -impl<'a, I, O, E, A, OA, F> ParserSealed<'a, I, O, E> for TryMapWith +impl<'a, I, O, E, A, OA, F> Parser<'a, I, O, E> for TryMapWith where I: Input<'a>, E: ParserExtra<'a, I>, @@ -620,7 +620,7 @@ impl Clone for To { } } -impl<'a, I, O, E, A, OA> ParserSealed<'a, I, O, E> for To +impl<'a, I, O, E, A, OA> Parser<'a, I, O, E> for To where I: Input<'a>, E: ParserExtra<'a, I>, @@ -653,7 +653,7 @@ impl Clone for IntoIter { } } -impl<'a, A, O, I, E> IterParserSealed<'a, I, O::Item, E> for IntoIter +impl<'a, A, O, I, E> IterParser<'a, I, O::Item, E> for IntoIter where I: Input<'a>, E: ParserExtra<'a, I>, @@ -702,7 +702,7 @@ impl Clone for Ignored { } } -impl<'a, I, E, A, OA> ParserSealed<'a, I, (), E> for Ignored +impl<'a, I, E, A, OA> Parser<'a, I, (), E> for Ignored where I: Input<'a>, E: ParserExtra<'a, I>, @@ -736,7 +736,7 @@ impl Clone for Unwrapped { } } -impl<'a, I, E, A, O, U> ParserSealed<'a, I, O, E> for Unwrapped> +impl<'a, I, E, A, O, U> Parser<'a, I, O, E> for Unwrapped> where I: Input<'a>, E: ParserExtra<'a, I>, @@ -758,7 +758,7 @@ where go_extra!(O); } -impl<'a, I, E, A, O> ParserSealed<'a, I, O, E> for Unwrapped> +impl<'a, I, E, A, O> Parser<'a, I, O, E> for Unwrapped> where I: Input<'a>, E: ParserExtra<'a, I>, @@ -787,7 +787,7 @@ pub struct Memoized { } #[cfg(feature = "memoization")] -impl<'a, I, E, A, O> ParserSealed<'a, I, O, E> for Memoized +impl<'a, I, E, A, O> Parser<'a, I, O, E> for Memoized where I: Input<'a>, E: ParserExtra<'a, I>, @@ -853,7 +853,7 @@ impl Clone for Then { } } -impl<'a, I, E, A, B, OA, OB> ParserSealed<'a, I, (OA, OB), E> for Then +impl<'a, I, E, A, B, OA, OB> Parser<'a, I, (OA, OB), E> for Then where I: Input<'a>, E: ParserExtra<'a, I>, @@ -889,7 +889,7 @@ impl Clone for IgnoreThen { } } -impl<'a, I, E, A, B, OA, OB> ParserSealed<'a, I, OB, E> for IgnoreThen +impl<'a, I, E, A, B, OA, OB> Parser<'a, I, OB, E> for IgnoreThen where I: Input<'a>, E: ParserExtra<'a, I>, @@ -925,7 +925,7 @@ impl Clone for ThenIgnore { } } -impl<'a, I, E, A, B, OA, OB> ParserSealed<'a, I, OA, E> for ThenIgnore +impl<'a, I, E, A, B, OA, OB> Parser<'a, I, OA, E> for ThenIgnore where I: Input<'a>, E: ParserExtra<'a, I>, @@ -961,7 +961,7 @@ impl Clone for NestedIn { } } -impl<'a, I, J, E, F, A, B, O> ParserSealed<'a, I, O, E> for NestedIn +impl<'a, I, J, E, F, A, B, O> Parser<'a, I, O, E> for NestedIn where I: Input<'a>, E: ParserExtra<'a, I>, @@ -1020,7 +1020,7 @@ impl Clone for IgnoreWithCtx { } } -impl<'a, I, E, A, B, OA, OB> ParserSealed<'a, I, OB, E> +impl<'a, I, E, A, B, OA, OB> Parser<'a, I, OB, E> for IgnoreWithCtx> where I: Input<'a>, @@ -1038,7 +1038,7 @@ where go_extra!(OB); } -impl<'a, I, E, A, B, OA, OB> IterParserSealed<'a, I, OB, E> +impl<'a, I, E, A, B, OA, OB> IterParser<'a, I, OB, E> for IgnoreWithCtx> where I: Input<'a>, @@ -1093,7 +1093,7 @@ impl Clone for ThenWithCtx { } } -impl<'a, I, E, A, B, OA, OB> ParserSealed<'a, I, (OA, OB), E> +impl<'a, I, E, A, B, OA, OB> Parser<'a, I, (OA, OB), E> for ThenWithCtx> where I: Input<'a>, @@ -1112,7 +1112,7 @@ where go_extra!((OA, OB)); } -impl<'a, I, E, A, B, OA, OB> IterParserSealed<'a, I, OB, E> +impl<'a, I, E, A, B, OA, OB> IterParser<'a, I, OB, E> for ThenWithCtx> where I: Input<'a>, @@ -1164,7 +1164,7 @@ impl Clone for WithCtx { } } -impl<'a, I, O, E, A, Ctx> ParserSealed<'a, I, O, E> for WithCtx +impl<'a, I, O, E, A, Ctx> Parser<'a, I, O, E> for WithCtx where I: Input<'a>, E: ParserExtra<'a, I>, @@ -1195,7 +1195,7 @@ impl Clone for WithState { } } -impl<'a, I, O, E, A, State> ParserSealed<'a, I, O, E> for WithState +impl<'a, I, O, E, A, State> Parser<'a, I, O, E> for WithState where I: Input<'a>, E: ParserExtra<'a, I>, @@ -1231,7 +1231,7 @@ impl Clone for DelimitedBy ParserSealed<'a, I, OA, E> for DelimitedBy +impl<'a, I, E, A, B, C, OA, OB, OC> Parser<'a, I, OA, E> for DelimitedBy where I: Input<'a>, E: ParserExtra<'a, I>, @@ -1269,7 +1269,7 @@ impl Clone for PaddedBy { } } -impl<'a, I, E, A, B, OA, OB> ParserSealed<'a, I, OA, E> for PaddedBy +impl<'a, I, E, A, B, OA, OB> Parser<'a, I, OA, E> for PaddedBy where I: Input<'a>, E: ParserExtra<'a, I>, @@ -1293,7 +1293,7 @@ pub struct Or { pub(crate) choice: crate::primitive::Choice<(A, B)>, } -impl<'a, I, O, E, A, B> ParserSealed<'a, I, O, E> for Or +impl<'a, I, O, E, A, B> Parser<'a, I, O, E> for Or where I: Input<'a>, E: ParserExtra<'a, I>, @@ -1429,7 +1429,7 @@ where } } -impl<'a, I, E, A, OA> ParserSealed<'a, I, (), E> for Repeated +impl<'a, I, E, A, OA> Parser<'a, I, (), E> for Repeated where I: Input<'a>, E: ParserExtra<'a, I>, @@ -1483,7 +1483,7 @@ where go_extra!(()); } -impl<'a, A, O, I, E> IterParserSealed<'a, I, O, E> for Repeated +impl<'a, A, O, I, E> IterParser<'a, I, O, E> for Repeated where I: Input<'a>, E: ParserExtra<'a, I>, @@ -1527,7 +1527,7 @@ where } } -impl<'a, A, O, I, E> ConfigIterParserSealed<'a, I, O, E> for Repeated +impl<'a, A, O, I, E> ConfigIterParser<'a, I, O, E> for Repeated where I: Input<'a>, E: ParserExtra<'a, I>, @@ -1736,7 +1736,7 @@ where } } -impl<'a, I, E, A, B, OA, OB> IterParserSealed<'a, I, OA, E> for SeparatedBy +impl<'a, I, E, A, B, OA, OB> IterParser<'a, I, OA, E> for SeparatedBy where I: Input<'a>, E: ParserExtra<'a, I>, @@ -1817,7 +1817,7 @@ where } } -impl<'a, I, E, A, B, OA, OB> ParserSealed<'a, I, (), E> for SeparatedBy +impl<'a, I, E, A, B, OA, OB> Parser<'a, I, (), E> for SeparatedBy where I: Input<'a>, E: ParserExtra<'a, I>, @@ -1867,7 +1867,7 @@ impl Clone for Enumerate { } } -impl<'a, I, O, E, A> IterParserSealed<'a, I, (usize, O), E> for Enumerate +impl<'a, I, O, E, A> IterParser<'a, I, (usize, O), E> for Enumerate where A: IterParser<'a, I, O, E>, I: Input<'a>, @@ -1922,7 +1922,7 @@ impl Clone for Collect { } } -impl<'a, I, O, E, A, C> ParserSealed<'a, I, C, E> for Collect +impl<'a, I, O, E, A, C> Parser<'a, I, C, E> for Collect where I: Input<'a>, E: ParserExtra<'a, I>, @@ -1981,7 +1981,7 @@ impl Clone for CollectExactly { } } -impl<'a, I, O, E, A, C> ParserSealed<'a, I, C, E> for CollectExactly +impl<'a, I, O, E, A, C> Parser<'a, I, C, E> for CollectExactly where I: Input<'a>, E: ParserExtra<'a, I>, @@ -2029,7 +2029,7 @@ pub struct OrNot { pub(crate) parser: A, } -impl<'a, I, O, E, A> ParserSealed<'a, I, Option, E> for OrNot +impl<'a, I, O, E, A> Parser<'a, I, Option, E> for OrNot where I: Input<'a>, E: ParserExtra<'a, I>, @@ -2050,7 +2050,7 @@ where go_extra!(Option); } -impl<'a, A, O, I, E> IterParserSealed<'a, I, O, E> for OrNot +impl<'a, A, O, I, E> IterParser<'a, I, O, E> for OrNot where I: Input<'a>, E: ParserExtra<'a, I>, @@ -2108,7 +2108,7 @@ impl Clone for Not { } } -impl<'a, I, E, A, OA> ParserSealed<'a, I, (), E> for Not +impl<'a, I, E, A, OA> Parser<'a, I, (), E> for Not where I: ValueInput<'a>, E: ParserExtra<'a, I>, @@ -2160,7 +2160,7 @@ impl Clone for Flatten { } #[cfg(feature = "nightly")] -impl<'a, A, O, I, E> IterParserSealed<'a, I, O::Item, E> for Flatten +impl<'a, A, O, I, E> IterParser<'a, I, O::Item, E> for Flatten where I: Input<'a>, E: ParserExtra<'a, I>, @@ -2234,7 +2234,7 @@ impl Clone for AndIs { } } -impl<'a, I, E, A, B, OA, OB> ParserSealed<'a, I, OA, E> for AndIs +impl<'a, I, E, A, B, OA, OB> Parser<'a, I, OA, E> for AndIs where I: Input<'a>, E: ParserExtra<'a, I>, @@ -2298,7 +2298,7 @@ impl Clone for Foldr { } } -impl<'a, I, F, A, B, O, OA, E> ParserSealed<'a, I, O, E> for Foldr +impl<'a, I, F, A, B, O, OA, E> Parser<'a, I, O, E> for Foldr where I: Input<'a>, A: IterParser<'a, I, OA, E>, @@ -2368,7 +2368,7 @@ impl Clone for FoldrWith { } } -impl<'a, I, F, A, B, O, OA, E> ParserSealed<'a, I, O, E> for FoldrWith +impl<'a, I, F, A, B, O, OA, E> Parser<'a, I, O, E> for FoldrWith where I: Input<'a>, A: IterParser<'a, I, OA, E>, @@ -2441,7 +2441,7 @@ impl Clone for Foldl { } } -impl<'a, I, F, A, B, O, OB, E> ParserSealed<'a, I, O, E> for Foldl +impl<'a, I, F, A, B, O, OB, E> Parser<'a, I, O, E> for Foldl where I: Input<'a>, A: Parser<'a, I, O, E>, @@ -2505,7 +2505,7 @@ impl Clone for FoldlWith { } } -impl<'a, I, F, A, B, O, OB, E> ParserSealed<'a, I, O, E> for FoldlWith +impl<'a, I, F, A, B, O, OB, E> Parser<'a, I, O, E> for FoldlWith where I: Input<'a>, A: Parser<'a, I, O, E>, @@ -2554,7 +2554,7 @@ pub struct Rewind { pub(crate) parser: A, } -impl<'a, I, O, E, A> ParserSealed<'a, I, O, E> for Rewind +impl<'a, I, O, E, A> Parser<'a, I, O, E> for Rewind where I: Input<'a>, E: ParserExtra<'a, I>, @@ -2582,7 +2582,7 @@ pub struct MapErr { pub(crate) mapper: F, } -impl<'a, I, O, E, A, F> ParserSealed<'a, I, O, E> for MapErr +impl<'a, I, O, E, A, F> Parser<'a, I, O, E> for MapErr where I: Input<'a>, E: ParserExtra<'a, I>, @@ -2615,7 +2615,7 @@ where // pub(crate) mapper: F, // } -// impl<'a, I, O, E, A, F> ParserSealed<'a, I, O, E> for MapErrWithSpan +// impl<'a, I, O, E, A, F> Parser<'a, I, O, E> for MapErrWithSpan // where // I: Input<'a>, // E: ParserExtra<'a, I>, @@ -2650,7 +2650,7 @@ pub struct MapErrWithState { pub(crate) mapper: F, } -impl<'a, I, O, E, A, F> ParserSealed<'a, I, O, E> for MapErrWithState +impl<'a, I, O, E, A, F> Parser<'a, I, O, E> for MapErrWithState where I: Input<'a>, E: ParserExtra<'a, I>, @@ -2697,7 +2697,7 @@ impl Clone for Validate { } } -impl<'a, I, OA, U, E, A, F> ParserSealed<'a, I, U, E> for Validate +impl<'a, I, OA, U, E, A, F> Parser<'a, I, U, E> for Validate where I: Input<'a>, E: ParserExtra<'a, I>, @@ -2730,7 +2730,7 @@ where // pub(crate) or_else: F, // } -// impl<'a, I, O, E, A, F> ParserSealed<'a, I, O, E> for OrElse +// impl<'a, I, O, E, A, F> Parser<'a, I, O, E> for OrElse // where // I: Input<'a>, // E: ParserExtra<'a, I>, diff --git a/src/either.rs b/src/either.rs index a24f91fc..b29105ab 100644 --- a/src/either.rs +++ b/src/either.rs @@ -1,14 +1,10 @@ //! A small module that implements the [`Parser`] trait for the //! [`either::Either`](https://docs.rs/either/latest/either/enum.Either.html) type. -use either::Either; +use super::*; +use ::either::Either; -use crate::{ - extra::ParserExtra, prelude::Input, private::ParserSealed, Check, Emit, InputRef, PResult, - Parser, -}; - -impl<'a, L, R, I, O, E> ParserSealed<'a, I, O, E> for Either +impl<'a, L, R, I, O, E> Parser<'a, I, O, E> for Either where I: Input<'a>, E: ParserExtra<'a, I>, diff --git a/src/extension.rs b/src/extension.rs index eea632aa..7402ccef 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -145,7 +145,7 @@ mod current { #[repr(transparent)] pub struct Ext(pub T); - impl<'a, I, O, E, P> ParserSealed<'a, I, O, E> for Ext

+ impl<'a, I, O, E, P> Parser<'a, I, O, E> for Ext

where I: Input<'a>, E: ParserExtra<'a, I>, diff --git a/src/label.rs b/src/label.rs index 15083bbb..be2b81bf 100644 --- a/src/label.rs +++ b/src/label.rs @@ -38,7 +38,7 @@ impl Labelled { } } -impl<'a, I, O, E, A, L> ParserSealed<'a, I, O, E> for Labelled +impl<'a, I, O, E, A, L> Parser<'a, I, O, E> for Labelled where I: Input<'a>, E: ParserExtra<'a, I>, diff --git a/src/lib.rs b/src/lib.rs index 7ce3f0e2..f6789aa7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,11 +24,11 @@ macro_rules! go_extra { ( $O :ty ) => { #[inline(always)] fn go_emit(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult { - ParserSealed::::go::(self, inp) + Parser::::go::(self, inp) } #[inline(always)] fn go_check(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult { - ParserSealed::::go::(self, inp) + Parser::::go::(self, inp) } }; } @@ -41,7 +41,7 @@ macro_rules! go_cfg_extra { inp: &mut InputRef<'a, '_, I, E>, cfg: Self::Config, ) -> PResult { - ConfigParserSealed::::go_cfg::(self, inp, cfg) + ConfigParser::::go_cfg::(self, inp, cfg) } #[inline(always)] fn go_check_cfg( @@ -49,7 +49,7 @@ macro_rules! go_cfg_extra { inp: &mut InputRef<'a, '_, I, E>, cfg: Self::Config, ) -> PResult { - ConfigParserSealed::::go_cfg::(self, inp, cfg) + ConfigParser::::go_cfg::(self, inp, cfg) } }; } @@ -143,10 +143,7 @@ use self::{ inspector::Inspector, prelude::*, primitive::Any, - private::{ - Check, ConfigIterParserSealed, ConfigParserSealed, Emit, IPResult, IterParserSealed, - Located, MaybeUninitExt, Mode, PResult, ParserSealed, Sealed, - }, + private::{Check, Emit, IPResult, Located, MaybeUninitExt, Mode, PResult, Sealed}, recovery::{RecoverWith, Strategy}, span::Span, text::*, @@ -320,8 +317,9 @@ impl ParseResult { /// and returned values or parser state may take advantage of this to borrow tokens or slices of the /// input and hold on to them, if the input supports this. /// -/// You cannot directly implement this trait yourself. If you feel like the built-in parsers are not enough for you, -/// there are several options in increasing order of complexity: +/// This trait is not intended to be implemented by downstream users of `chumsky`. While you can technically implement +/// it, doing so is considered to be outside the stability guarantees of the crate. Your code may break with a future, +/// semver-compatible release! Instead of implementing this trait, you should consider other options: /// /// 1) Try using combinators like [`Parser::try_map`] and [`Parser::validate`] to implement custom error generation /// @@ -330,7 +328,7 @@ impl ParseResult { /// 3) Use chumsky's [`extension`] API to write an extension parser that feels like it's native to chumsky /// /// 4) If you believe you've found a common use-case that's missing from chumsky, you could open a pull request to -/// implement it in chumsky itself. +/// implement it in chumsky itself rather than implementing `Parser` yourself. #[cfg_attr( feature = "nightly", diagnostic::on_unimplemented( @@ -339,9 +337,17 @@ impl ParseResult { note = "You should check that the output types of your parsers are consistent with the combinators you're using", ) )] -pub trait Parser<'a, I: Input<'a>, O, E: ParserExtra<'a, I> = extra::Default>: - ParserSealed<'a, I, O, E> -{ +pub trait Parser<'a, I: Input<'a>, O, E: ParserExtra<'a, I> = extra::Default> { + #[doc(hidden)] + fn go(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult + where + Self: Sized; + + #[doc(hidden)] + fn go_emit(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult; + #[doc(hidden)] + fn go_check(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult; + /// Parse a stream of tokens, yielding an output if possible, and any errors encountered along the way. /// /// If `None` is returned (i.e: parsing failed) then there will *always* be at least one item in the error `Vec`. @@ -2123,7 +2129,9 @@ pub trait Parser<'a, I: Input<'a>, O, E: ParserExtra<'a, I> = extra::Default>: where Self: MaybeSync + Sized + 'a + 'b, { - ParserSealed::boxed(self) + Boxed { + inner: RefC::new(self), + } } /// Use [Pratt parsing](https://en.wikipedia.org/wiki/Operator-precedence_parser#Pratt_parsing) to ergonomically @@ -2171,7 +2179,7 @@ pub trait Parser<'a, I: Input<'a>, O, E: ParserExtra<'a, I> = extra::Default>: } #[cfg(feature = "nightly")] -impl<'a, I, O, E> ParserSealed<'a, I, O, E> for ! +impl<'a, I, O, E> Parser<'a, I, O, E> for ! where I: Input<'a>, E: ParserExtra<'a, I>, @@ -2199,11 +2207,28 @@ where /// /// Not all parsers currently support configuration. If you feel like you need a parser to be configurable /// and it isn't currently, please open an issue on the issue tracker of the main repository. -pub trait ConfigParser<'a, I, O, E>: ConfigParserSealed<'a, I, O, E> +pub trait ConfigParser<'a, I, O, E>: Parser<'a, I, O, E> where I: Input<'a>, E: ParserExtra<'a, I>, { + /// A type describing the configurable aspects of the parser. + type Config: Default; + + #[doc(hidden)] + fn go_cfg(&self, inp: &mut InputRef<'a, '_, I, E>, cfg: Self::Config) -> PResult + where + Self: Sized; + + #[doc(hidden)] + fn go_emit_cfg(&self, inp: &mut InputRef<'a, '_, I, E>, cfg: Self::Config) -> PResult; + #[doc(hidden)] + fn go_check_cfg( + &self, + inp: &mut InputRef<'a, '_, I, E>, + cfg: Self::Config, + ) -> PResult; + /// A combinator that allows configuration of the parser from the current context. Context /// is most often derived from [`Parser::ignore_with_ctx`], [`Parser::then_with_ctx`] or [`map_ctx`], /// and is how chumsky supports parsing things such as indentation-sensitive grammars. @@ -2287,11 +2312,32 @@ where } /// An iterable equivalent of [`Parser`], i.e: a parser that generates a sequence of outputs. -pub trait IterParser<'a, I, O, E = extra::Default>: IterParserSealed<'a, I, O, E> +pub trait IterParser<'a, I, O, E = extra::Default> where I: Input<'a>, E: ParserExtra<'a, I>, { + #[doc(hidden)] + type IterState + where + I: 'a; + + // Determines whether this iter parser is expected to not consume input on each iteration + #[doc(hidden)] + const NONCONSUMPTION_IS_OK: bool = false; + + #[doc(hidden)] + fn make_iter( + &self, + inp: &mut InputRef<'a, '_, I, E>, + ) -> PResult>; + #[doc(hidden)] + fn next( + &self, + inp: &mut InputRef<'a, '_, I, E>, + state: &mut Self::IterState, + ) -> IPResult; + /// Collect this iterable parser into a [`Container`]. /// /// This is commonly useful for collecting parsers that output many values into containers of various kinds: @@ -2554,12 +2600,22 @@ where /// An iterable equivalent of [`ConfigParser`], i.e: a parser that generates a sequence of outputs and /// can be configured at runtime. -pub trait ConfigIterParser<'a, I, O, E = extra::Default>: - ConfigIterParserSealed<'a, I, O, E> +pub trait ConfigIterParser<'a, I, O, E = extra::Default>: IterParser<'a, I, O, E> where I: Input<'a>, E: ParserExtra<'a, I>, { + /// A trait describing the configurable aspects of the iterable parser. + type Config: Default; + + #[doc(hidden)] + fn next_cfg( + &self, + inp: &mut InputRef<'a, '_, I, E>, + state: &mut Self::IterState, + cfg: &Self::Config, + ) -> IPResult; + /// A combinator that allows configuration of the parser from the current context fn configure(self, cfg: F) -> IterConfigure where @@ -2606,7 +2662,7 @@ impl<'a, I: Input<'a>, O, E: ParserExtra<'a, I>> Clone for Boxed<'a, '_, I, O, E } } -impl<'a, I, O, E> ParserSealed<'a, I, O, E> for Boxed<'a, '_, I, O, E> +impl<'a, I, O, E> Parser<'a, I, O, E> for Boxed<'a, '_, I, O, E> where I: Input<'a>, E: ParserExtra<'a, I>, @@ -2627,7 +2683,7 @@ where go_extra!(O); } -impl<'a, I, O, E, T> ParserSealed<'a, I, O, E> for ::alloc::boxed::Box +impl<'a, I, O, E, T> Parser<'a, I, O, E> for ::alloc::boxed::Box where I: Input<'a>, E: ParserExtra<'a, I>, @@ -2644,7 +2700,7 @@ where go_extra!(O); } -impl<'a, I, O, E, T> ParserSealed<'a, I, O, E> for ::alloc::rc::Rc +impl<'a, I, O, E, T> Parser<'a, I, O, E> for ::alloc::rc::Rc where I: Input<'a>, E: ParserExtra<'a, I>, @@ -2661,7 +2717,7 @@ where go_extra!(O); } -impl<'a, I, O, E, T> ParserSealed<'a, I, O, E> for ::alloc::sync::Arc +impl<'a, I, O, E, T> Parser<'a, I, O, E> for ::alloc::sync::Arc where I: Input<'a>, E: ParserExtra<'a, I>, diff --git a/src/number.rs b/src/number.rs index 5672dd0b..16276276 100644 --- a/src/number.rs +++ b/src/number.rs @@ -1,12 +1,8 @@ //! TODO: Add documentation when approved +use super::*; pub use lexical::format; -use crate::extra::ParserExtra; -use crate::input::{InputRef, SliceInput}; -use crate::private::{Check, Emit, Mode, PResult, ParserSealed}; -use crate::EmptyPhantom; - use lexical::parse_partial; use lexical::FromLexical; @@ -30,7 +26,7 @@ pub const fn number() -> Number { } } -impl<'a, const F: u128, I, O, E> ParserSealed<'a, I, O, E> for Number +impl<'a, const F: u128, I, O, E> Parser<'a, I, O, E> for Number where O: FromLexical, I: SliceInput<'a, Cursor = usize>, diff --git a/src/pratt.rs b/src/pratt.rs index 4b94d9ab..22c74f06 100644 --- a/src/pratt.rs +++ b/src/pratt.rs @@ -978,7 +978,7 @@ impl<'a, Atom, Ops> Pratt { } #[allow(unused_variables, non_snake_case)] -impl<'a, I, O, E, Atom, Ops> ParserSealed<'a, I, O, E> for Pratt +impl<'a, I, O, E, Atom, Ops> Parser<'a, I, O, E> for Pratt where I: Input<'a>, E: ParserExtra<'a, I>, diff --git a/src/primitive.rs b/src/primitive.rs index f92ec1ff..7fd0c3bb 100644 --- a/src/primitive.rs +++ b/src/primitive.rs @@ -34,7 +34,7 @@ impl Clone for End { } } -impl<'a, I, E> ParserSealed<'a, I, (), E> for End +impl<'a, I, E> Parser<'a, I, (), E> for End where I: Input<'a>, E: ParserExtra<'a, I>, @@ -72,7 +72,7 @@ impl Clone for Empty { } } -impl<'a, I, E> ParserSealed<'a, I, (), E> for Empty +impl<'a, I, E> Parser<'a, I, (), E> for Empty where I: Input<'a>, E: ParserExtra<'a, I>, @@ -152,7 +152,7 @@ where } } -impl<'a, I, E, T> ParserSealed<'a, I, T, E> for Just +impl<'a, I, E, T> Parser<'a, I, T, E> for Just where I: Input<'a>, E: ParserExtra<'a, I>, @@ -167,7 +167,7 @@ where go_extra!(T); } -impl<'a, I, E, T> ConfigParserSealed<'a, I, T, E> for Just +impl<'a, I, E, T> ConfigParser<'a, I, T, E> for Just where I: Input<'a>, E: ParserExtra<'a, I>, @@ -252,7 +252,7 @@ where } } -impl<'a, I, E, T> ParserSealed<'a, I, I::Token, E> for OneOf +impl<'a, I, E, T> Parser<'a, I, I::Token, E> for OneOf where I: ValueInput<'a>, E: ParserExtra<'a, I>, @@ -326,7 +326,7 @@ where } } -impl<'a, I, E, T> ParserSealed<'a, I, I::Token, E> for NoneOf +impl<'a, I, E, T> Parser<'a, I, I::Token, E> for NoneOf where I: ValueInput<'a>, E: ParserExtra<'a, I>, @@ -393,7 +393,7 @@ where } } -impl<'a, I, O, E, F> ParserSealed<'a, I, O, E> for Custom +impl<'a, I, O, E, F> Parser<'a, I, O, E> for Custom where I: Input<'a>, E: ParserExtra<'a, I>, @@ -445,7 +445,7 @@ where } } -impl<'a, I, O, E, F> ParserSealed<'a, I, O, E> for Select +impl<'a, I, O, E, F> Parser<'a, I, O, E> for Select where I: ValueInput<'a>, I::Token: Clone + 'a, @@ -502,7 +502,7 @@ where } } -impl<'a, I, O, E, F> ParserSealed<'a, I, O, E> for SelectRef +impl<'a, I, O, E, F> Parser<'a, I, O, E> for SelectRef where I: BorrowInput<'a>, I::Token: 'a, @@ -541,7 +541,7 @@ impl Clone for Any { } } -impl<'a, I, E> ParserSealed<'a, I, I::Token, E> for Any +impl<'a, I, E> Parser<'a, I, I::Token, E> for Any where I: ValueInput<'a>, E: ParserExtra<'a, I>, @@ -596,7 +596,7 @@ impl Clone for AnyRef { } } -impl<'a, I, E> ParserSealed<'a, I, &'a I::Token, E> for AnyRef +impl<'a, I, E> Parser<'a, I, &'a I::Token, E> for AnyRef where I: BorrowInput<'a>, E: ParserExtra<'a, I>, @@ -660,7 +660,7 @@ impl Clone for MapCtx { } } -impl<'a, I, O, E, EI, A, F> ParserSealed<'a, I, O, E> for MapCtx +impl<'a, I, O, E, EI, A, F> Parser<'a, I, O, E> for MapCtx where I: Input<'a>, E: ParserExtra<'a, I>, @@ -794,7 +794,7 @@ pub fn todo<'a, I: Input<'a>, O, E: ParserExtra<'a, I>>() -> Todo { } } -impl<'a, I, O, E> ParserSealed<'a, I, O, E> for Todo +impl<'a, I, O, E> Parser<'a, I, O, E> for Todo where I: Input<'a>, E: ParserExtra<'a, I>, @@ -874,7 +874,7 @@ macro_rules! impl_choice_for_tuple { }; (~ $Head:ident $($X:ident)+) => { #[allow(unused_variables, non_snake_case)] - impl<'a, I, E, $Head, $($X),*, O> ParserSealed<'a, I, O, E> for Choice<($Head, $($X,)*)> + impl<'a, I, E, $Head, $($X),*, O> Parser<'a, I, O, E> for Choice<($Head, $($X,)*)> where I: Input<'a>, E: ParserExtra<'a, I>, @@ -906,7 +906,7 @@ macro_rules! impl_choice_for_tuple { } }; (~ $Head:ident) => { - impl<'a, I, E, $Head, O> ParserSealed<'a, I, O, E> for Choice<($Head,)> + impl<'a, I, E, $Head, O> Parser<'a, I, O, E> for Choice<($Head,)> where I: Input<'a>, E: ParserExtra<'a, I>, @@ -924,7 +924,7 @@ macro_rules! impl_choice_for_tuple { impl_choice_for_tuple!(A_ B_ C_ D_ E_ F_ G_ H_ I_ J_ K_ L_ M_ N_ O_ P_ Q_ R_ S_ T_ U_ V_ W_ X_ Y_ Z_); -impl<'a, A, I, O, E> ParserSealed<'a, I, O, E> for Choice<&[A]> +impl<'a, A, I, O, E> Parser<'a, I, O, E> for Choice<&[A]> where A: Parser<'a, I, O, E>, I: Input<'a>, @@ -952,7 +952,7 @@ where go_extra!(O); } -impl<'a, A, I, O, E> ParserSealed<'a, I, O, E> for Choice> +impl<'a, A, I, O, E> Parser<'a, I, O, E> for Choice> where A: Parser<'a, I, O, E>, I: Input<'a>, @@ -965,7 +965,7 @@ where go_extra!(O); } -impl<'a, A, I, O, E, const N: usize> ParserSealed<'a, I, O, E> for Choice<[A; N]> +impl<'a, A, I, O, E, const N: usize> Parser<'a, I, O, E> for Choice<[A; N]> where A: Parser<'a, I, O, E>, I: Input<'a>, @@ -992,7 +992,7 @@ pub const fn group(parsers: T) -> Group { Group { parsers } } -impl<'a, I, O, E, P, const N: usize> ParserSealed<'a, I, [O; N], E> for Group<[P; N]> +impl<'a, I, O, E, P, const N: usize> Parser<'a, I, [O; N], E> for Group<[P; N]> where I: Input<'a>, E: ParserExtra<'a, I>, @@ -1053,7 +1053,7 @@ macro_rules! impl_group_for_tuple { }; (~ $($X:ident $O:ident)*) => { #[allow(unused_variables, non_snake_case)] - impl<'a, I, E, $($X),*, $($O),*> ParserSealed<'a, I, ($($O,)*), E> for Group<($($X,)*)> + impl<'a, I, E, $($X),*, $($O),*> Parser<'a, I, ($($O,)*), E> for Group<($($X,)*)> where I: Input<'a>, E: ParserExtra<'a, I>, diff --git a/src/private.rs b/src/private.rs index a4be9268..1e76cdeb 100644 --- a/src/private.rs +++ b/src/private.rs @@ -350,95 +350,6 @@ impl Mode for Check { } } -// TODO: Consider removing these sealed traits in favour of `Sealed`, with the given methods just being on `Parser` -// with doc(hidden) - -#[cfg_attr( - feature = "nightly", - diagnostic::on_unimplemented( - message = "The following is not a parser from `{I}` to `{O}`: `{Self}`", - label = "This parser is not compatible because it does not implement `Parser<{I}, {O}, E>`", - note = "You should check that the output types of your parsers are consistent with the combinators you're using", - ) -)] -pub trait ParserSealed<'a, I: Input<'a>, O, E: ParserExtra<'a, I>> { - fn go(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult - where - Self: Sized; - - fn go_emit(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult; - fn go_check(&self, inp: &mut InputRef<'a, '_, I, E>) -> PResult; - - fn boxed<'b>(self) -> Boxed<'a, 'b, I, O, E> - where - Self: MaybeSync + Sized + 'a + 'b, - { - Boxed { - inner: RefC::new(self), - } - } -} - -pub trait ConfigParserSealed<'a, I, O, E>: ParserSealed<'a, I, O, E> -where - I: Input<'a>, - E: ParserExtra<'a, I>, -{ - type Config: Default; - - fn go_cfg(&self, inp: &mut InputRef<'a, '_, I, E>, cfg: Self::Config) -> PResult - where - Self: Sized; - - fn go_emit_cfg(&self, inp: &mut InputRef<'a, '_, I, E>, cfg: Self::Config) -> PResult; - fn go_check_cfg( - &self, - inp: &mut InputRef<'a, '_, I, E>, - cfg: Self::Config, - ) -> PResult; -} - -pub trait IterParserSealed<'a, I, O, E> -where - I: Input<'a>, - E: ParserExtra<'a, I>, -{ - type IterState - where - I: 'a; - - // Determines whether this iter parser is expected to not consume input on each iteration - const NONCONSUMPTION_IS_OK: bool = false; - - #[doc(hidden)] - fn make_iter( - &self, - inp: &mut InputRef<'a, '_, I, E>, - ) -> PResult>; - #[doc(hidden)] - fn next( - &self, - inp: &mut InputRef<'a, '_, I, E>, - state: &mut Self::IterState, - ) -> IPResult; -} - -pub trait ConfigIterParserSealed<'a, I, O, E>: IterParserSealed<'a, I, O, E> -where - I: Input<'a>, - E: ParserExtra<'a, I>, -{ - type Config: Default; - - #[doc(hidden)] - fn next_cfg( - &self, - inp: &mut InputRef<'a, '_, I, E>, - state: &mut Self::IterState, - cfg: &Self::Config, - ) -> IPResult; -} - // TODO: Remove this when MaybeUninit transforms to/from arrays stabilize in any form pub(crate) trait MaybeUninitExt: Sized { /// Identical to the unstable [`MaybeUninit::uninit_array`] diff --git a/src/recovery.rs b/src/recovery.rs index fcf771b0..b683d41e 100644 --- a/src/recovery.rs +++ b/src/recovery.rs @@ -59,7 +59,7 @@ pub struct RecoverWith { pub(crate) strategy: S, } -impl<'a, I, O, E, A, S> ParserSealed<'a, I, O, E> for RecoverWith +impl<'a, I, O, E, A, S> Parser<'a, I, O, E> for RecoverWith where I: Input<'a>, E: ParserExtra<'a, I>, diff --git a/src/recursive.rs b/src/recursive.rs index c47a3171..4e038f71 100644 --- a/src/recursive.rs +++ b/src/recursive.rs @@ -179,7 +179,7 @@ pub(crate) fn recurse R>(f: F) -> R { f() } -impl<'a, I, O, E> ParserSealed<'a, I, O, E> for Recursive> +impl<'a, I, O, E> Parser<'a, I, O, E> for Recursive> where I: Input<'a>, E: ParserExtra<'a, I>, @@ -201,7 +201,7 @@ where go_extra!(O); } -impl<'a, I, O, E> ParserSealed<'a, I, O, E> for Recursive> +impl<'a, I, O, E> Parser<'a, I, O, E> for Recursive> where I: Input<'a>, E: ParserExtra<'a, I>, diff --git a/src/regex.rs b/src/regex.rs index d4744064..5920c80c 100644 --- a/src/regex.rs +++ b/src/regex.rs @@ -27,7 +27,7 @@ pub fn regex(pattern: &str) -> Regex { } } -impl<'a, C, I, E> ParserSealed<'a, I, &'a C::Str, E> for Regex +impl<'a, C, I, E> Parser<'a, I, &'a C::Str, E> for Regex where C: Char, I: StrInput<'a, C>, diff --git a/src/text.rs b/src/text.rs index a0e560ea..b4f9ce1e 100644 --- a/src/text.rs +++ b/src/text.rs @@ -131,7 +131,7 @@ pub struct Padded { pub(crate) parser: A, } -impl<'a, I, O, E, A> ParserSealed<'a, I, O, E> for Padded +impl<'a, I, O, E, A> Parser<'a, I, O, E> for Padded where I: ValueInput<'a>, E: ParserExtra<'a, I>,