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

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.

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.

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.

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)),
]);

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>>

Determines whether this punctuated sequence is empty, meaning it contains no syntax tree nodes or punctuation.

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.

Borrows the first element in this sequence.

Borrows the last element in this sequence.

Returns an iterator over borrowed syntax tree nodes of type &T.

Returns an iterator over the contents of this sequence as borrowed punctuated pairs.

Determines whether this punctuated sequence ends with a trailing punctuation.

Returns true if either this Punctuated is empty, or it has a trailing punctuation.

Equivalent to punctuated.is_empty() || punctuated.trailing_punct().

Trait Implementations

See the documentation of impl Deref for Separated for why this can’t be AsMut.

Converts this type into a shared reference of the (usually inferred) input type.

See the documentation of impl Deref for Separated for why this can’t be BorrowMut.

Immutably borrows from an owned value. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

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.

The resulting type after dereferencing.
Dereferences the value.
Formats the value using the given formatter. Read more

Extend<(P, T)> can be implemented because it never causes the sequence to become empty, nor does it add any trailing punctuation.

Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more

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.

Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Converts to this type from the input type.
The associated error which can be returned from parsing.
Parses a string s to return a value of this type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Write self to the given TokenStream. Read more
Convert self directly into a TokenStream object. Read more
Convert self directly into a TokenStream object. Read more
The type returned in the event of a conversion error.
Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Returns a Span covering the complete contents of this syntax tree node, or Span::call_site() if this node is empty. Read more

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);

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);
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.