pub struct Separated<T, P> { /* private fields */ }
Expand description
Parses a given production repeatedly, separated by punctuation. Trailing punctuation is not allowed, and the production must appear at least once in the input. Parsing stops gracefully once there are no more separators left at the head of the input stream.
It can’t be Default
because that would mean an empty sequence.
use parsel::Result;
use parsel::ast::{ident, LitInt, Ident, Separated, Many};
use parsel::ast::token::{Add, Colon2, Comma};
let mut sum: Separated<LitInt, Add> = parsel::parse_quote! {
1 + 2 + 4 + 8 + 16 + 32
};
sum.push_value(LitInt::from(64));
assert_eq!(sum.to_string(), "1 + 2 + 4 + 8 + 16 + 32 + 64");
let good_short: Separated<Ident, Colon2> = parsel::parse_quote!(one);
let good_short: Vec<_> = good_short.into_iter().collect();
let expected_short = vec![ident("one")];
assert_eq!(good_short, expected_short);
let good_long: Separated<Ident, Colon2> = parsel::parse_quote! {
root::module::Item
};
let good_long: Vec<_> = good_long.into_iter().collect();
let expected_long: Many<Ident> = parsel::parse_quote!(root module Item);
let expected_long: Vec<_> = expected_long.into_iter().collect();
assert_eq!(good_long, expected_long);
let bad_trailing: Result<Separated<Ident, Colon2>> = parsel::parse_str(r"
root::module::Item::
");
assert!(bad_trailing.is_err());
let bad_empty: Result<Separated<Ident, Colon2>> = parsel::parse_str("");
assert!(bad_empty.is_err());
Implementations
sourceimpl<T, P> Separated<T, P>
impl<T, P> Separated<T, P>
pub fn len(&self) -> NonZeroUsize
pub fn into_inner(self) -> Punctuated<T, P>
pub fn first(&self) -> &T
pub fn first_mut(&mut self) -> &mut T
pub fn last(&self) -> &T
pub fn last_mut(&mut self) -> &mut T
sourcepub fn insert(&mut self, index: usize, value: T) where
P: Default,
pub fn insert(&mut self, index: usize, value: T) where
P: Default,
insert()
is allowed because it doesn’t ever make the sequence
empty, nor does it add trailing punctuation, so it doesn’t violate
the invariants of the type.
sourcepub fn push(&mut self, punct: P, value: T)
pub fn push(&mut self, punct: P, value: T)
push()
is allowed because it doesn’t ever make the sequence
empty, nor does it add trailing punctuation, so it doesn’t violate
the invariants of the type.
sourcepub fn push_value(&mut self, value: T) where
P: Default,
pub fn push_value(&mut self, value: T) where
P: Default,
push_value()
is allowed because it doesn’t ever make the sequence
empty, nor does it add trailing punctuation, so it doesn’t violate
the invariants of the type.
pub fn iter(&self) -> Iter<'_, T>
pub fn iter_mut(&mut self) -> IterMut<'_, T>
pub fn into_pairs(self) -> IntoPairs<T, P>
pub fn pairs(&self) -> Pairs<'_, T, P>
pub fn pairs_mut(&mut self) -> PairsMut<'_, T, P>
sourcepub fn into_first_rest(self) -> (T, Vec<(P, T)>)
pub fn into_first_rest(self) -> (T, Vec<(P, T)>)
Returns the first value and the remaining (punctuation, value) pairs.
let singleton: Separated<LitInt, Comma> = parsel::parse_quote!(137);
let (first, rest) = singleton.into_first_rest();
assert_eq!(first, LitInt::from(137));
assert_eq!(rest, []);
let triplet: Separated<LitInt, Comma> = parsel::parse_quote!(731, 813, 139);
let (first, rest) = triplet.into_first_rest();
assert_eq!(first, LitInt::from(731));
assert_eq!(rest, [
(Comma::default(), LitInt::from(813)),
(Comma::default(), LitInt::from(139)),
]);
sourcepub fn into_last_rest(self) -> (T, Vec<(T, P)>)
pub fn into_last_rest(self) -> (T, Vec<(T, P)>)
Returns the last value and the preceding (value, punctuation) pairs.
let singleton: Separated<LitChar, Semi> = parsel::parse_quote!('W');
let (last, rest) = singleton.into_last_rest();
assert_eq!(last, LitChar::from('W'));
assert_eq!(rest, []);
let triplet: Separated<LitChar, Semi> = parsel::parse_quote!('"'; 'é'; '\'');
let (last, rest) = triplet.into_last_rest();
assert_eq!(last, LitChar::from('\''));
assert_eq!(rest, [
(LitChar::from('"'), Semi::default()),
(LitChar::from('é'), Semi::default()),
]);
Methods from Deref<Target = Punctuated<T, P>>
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Determines whether this punctuated sequence is empty, meaning it contains no syntax tree nodes or punctuation.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of syntax tree nodes in this punctuated sequence.
This is the number of nodes of type T
, not counting the punctuation of
type P
.
sourcepub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
Returns an iterator over borrowed syntax tree nodes of type &T
.
sourcepub fn pairs(&self) -> Pairs<'_, T, P>
pub fn pairs(&self) -> Pairs<'_, T, P>
Returns an iterator over the contents of this sequence as borrowed punctuated pairs.
sourcepub fn trailing_punct(&self) -> bool
pub fn trailing_punct(&self) -> bool
Determines whether this punctuated sequence ends with a trailing punctuation.
sourcepub fn empty_or_trailing(&self) -> bool
pub fn empty_or_trailing(&self) -> bool
Returns true if either this Punctuated
is empty, or it has a trailing
punctuation.
Equivalent to punctuated.is_empty() || punctuated.trailing_punct()
.
Trait Implementations
sourceimpl<T, P> AsRef<Punctuated<T, P>> for Separated<T, P>
impl<T, P> AsRef<Punctuated<T, P>> for Separated<T, P>
See the documentation of impl Deref for Separated
for why this
can’t be AsMut
.
sourcefn as_ref(&self) -> &Punctuated<T, P>
fn as_ref(&self) -> &Punctuated<T, P>
Converts this type into a shared reference of the (usually inferred) input type.
sourceimpl<T, P> Borrow<Punctuated<T, P>> for Separated<T, P>
impl<T, P> Borrow<Punctuated<T, P>> for Separated<T, P>
See the documentation of impl Deref for Separated
for why this
can’t be BorrowMut
.
sourcefn borrow(&self) -> &Punctuated<T, P>
fn borrow(&self) -> &Punctuated<T, P>
Immutably borrows from an owned value. Read more
sourceimpl<T, P> Deref for Separated<T, P>
impl<T, P> Deref for Separated<T, P>
It can’t be DerefMut
, because then users could .pop()
the last
remaining item, resulting in an empty Separated
. They could push
a trailing separator, too, which also violates internal invariants.
type Target = Punctuated<T, P>
type Target = Punctuated<T, P>
The resulting type after dereferencing.
sourceimpl<T, P> Extend<(P, T)> for Separated<T, P>
impl<T, P> Extend<(P, T)> for Separated<T, P>
Extend<(P, T)>
can be implemented because it never causes the sequence
to become empty, nor does it add any trailing punctuation.
sourcefn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = (P, T)>,
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = (P, T)>,
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<T, P> Extend<T> for Separated<T, P> where
P: Default,
impl<T, P> Extend<T> for Separated<T, P> where
P: Default,
Extend<Pair<T, P>>
is not provided because it requires either an empty
sequence or one with trailing punctuation.
FromIterator
impls are missing because the input iterator could be empty.
sourcefn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iter: I) where
I: IntoIterator<Item = T>,
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Reserves capacity in a collection for the given number of additional elements. Read more
sourceimpl<T, P> From<Separated<T, P>> for Punctuated<T, P>
impl<T, P> From<Separated<T, P>> for Punctuated<T, P>
sourceimpl<'a, T, P> IntoIterator for &'a Separated<T, P>
impl<'a, T, P> IntoIterator for &'a Separated<T, P>
sourceimpl<'a, T, P> IntoIterator for &'a mut Separated<T, P>
impl<'a, T, P> IntoIterator for &'a mut Separated<T, P>
sourceimpl<T, P> IntoIterator for Separated<T, P>
impl<T, P> IntoIterator for Separated<T, P>
sourceimpl<T, P> Parse for Separated<T, P> where
T: Parse,
P: Parse,
impl<T, P> Parse for Separated<T, P> where
T: Parse,
P: Parse,
fn parse(input: ParseStream<'_>) -> Result<Self>
sourceimpl<T, P> ToTokens for Separated<T, P> where
T: ToTokens,
P: ToTokens,
impl<T, P> ToTokens for Separated<T, P> where
T: ToTokens,
P: ToTokens,
sourcefn to_tokens(&self, tokens: &mut TokenStream)
fn to_tokens(&self, tokens: &mut TokenStream)
Write self
to the given TokenStream
. Read more
sourcefn to_token_stream(&self) -> TokenStream
fn to_token_stream(&self) -> TokenStream
Convert self
directly into a TokenStream
object. Read more
sourcefn into_token_stream(self) -> TokenStream
fn into_token_stream(self) -> TokenStream
Convert self
directly into a TokenStream
object. Read more
impl<T: Eq, P: Eq> Eq for Separated<T, P>
impl<T, P> StructuralEq for Separated<T, P>
impl<T, P> StructuralPartialEq for Separated<T, P>
Auto Trait Implementations
impl<T, P> RefUnwindSafe for Separated<T, P> where
P: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, P> Send for Separated<T, P> where
P: Send,
T: Send,
impl<T, P> Sync for Separated<T, P> where
P: Sync,
T: Sync,
impl<T, P> Unpin for Separated<T, P> where
P: Unpin,
T: Unpin,
impl<T, P> UnwindSafe for Separated<T, P> where
P: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Spanned for T where
T: Spanned + ?Sized,
impl<T> Spanned for T where
T: Spanned + ?Sized,
sourcefn span(&self) -> Span
fn span(&self) -> Span
Returns a Span
covering the complete contents of this syntax tree
node, or Span::call_site()
if this node is empty. Read more
sourceimpl<T> SpannedExt for T where
T: Spanned + ?Sized,
impl<T> SpannedExt for T where
T: Spanned + ?Sized,
sourcefn byte_range(&self, source: &str) -> Range<usize>
fn byte_range(&self, source: &str) -> Range<usize>
TODO(H2CO3): a faster, less naive implementation would be great.
We should use the byte offset of start
to compute that of end
,
sparing the double scan of the source up until the start location.
let source = r#"
-3.667
1248 "string ű literal"
"wíőzs"
"#;
let tokens: Many<Lit> = source.parse()?;
assert_eq!(tokens.len(), 4);
assert_eq!(tokens[0].byte_range(source), 4..10);
assert_eq!(tokens[1].byte_range(source), 13..17);
assert_eq!(tokens[2].byte_range(source), 19..38);
assert_eq!(tokens[3].byte_range(source), 45..54);
sourcefn char_range(&self, source: &str) -> Range<usize>
fn char_range(&self, source: &str) -> Range<usize>
TODO(H2CO3): a faster, less naive implementation would be great.
We should use the char offset of start
to compute that of end
,
sparing the double scan of the source up until the start location.
let source = r#"
-3.667
1248 "string ű literal"
"wíőzs"
"#;
let tokens: Many<Lit> = source.parse()?;
assert_eq!(tokens.len(), 4);
assert_eq!(tokens[0].char_range(source), 4..10);
assert_eq!(tokens[1].char_range(source), 13..17);
assert_eq!(tokens[2].char_range(source), 19..37);
assert_eq!(tokens[3].char_range(source), 44..51);