Struct chumsky::combinator::SeparatedBy [−][src]
pub struct SeparatedBy<A, B, U> { /* fields omitted */ }
Expand description
See Parser::separated_by
.
Implementations
Allow a leading separator to appear before the first item.
Note that even if no items are parsed, a leading separator is permitted.
Examples
let r#enum = text::keyword::<_, _, Simple<char>>("enum")
.padded()
.ignore_then(text::ident()
.padded()
.separated_by(just('|'))
.allow_leading());
assert_eq!(r#enum.parse("enum True | False"), Ok(vec!["True".to_string(), "False".to_string()]));
assert_eq!(r#enum.parse("
enum
| True
| False
"), Ok(vec!["True".to_string(), "False".to_string()]));
Allow a trailing separator to appear after the last item.
Note that if no items are parsed, no leading separator is permitted.
Examples
let numbers = text::int::<_, Simple<char>>(10)
.padded()
.separated_by(just(','))
.allow_trailing()
.delimited_by('(', ')');
assert_eq!(numbers.parse("(1, 2)"), Ok(vec!["1".to_string(), "2".to_string()]));
assert_eq!(numbers.parse("(1, 2,)"), Ok(vec!["1".to_string(), "2".to_string()]));
Require that the pattern appear at least a minimum number of times.
let numbers = just::<_, _, Simple<char>>('-')
.separated_by(just('.'))
.at_least(2);
assert!(numbers.parse("").is_err());
assert!(numbers.parse("-").is_err());
assert_eq!(numbers.parse("-.-"), Ok(vec!['-', '-']));
Require that the pattern appear at most a maximum number of times.
let row_4 = text::int::<_, Simple<char>>(10)
.padded()
.separated_by(just(','))
.at_most(4);
let matrix_4x4 = row_4
.separated_by(just(','))
.at_most(4);
assert_eq!(
matrix_4x4.parse("0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15"),
Ok(vec![
vec!["0".to_string(), "1".to_string(), "2".to_string(), "3".to_string()],
vec!["4".to_string(), "5".to_string(), "6".to_string(), "7".to_string()],
vec!["8".to_string(), "9".to_string(), "10".to_string(), "11".to_string()],
vec!["12".to_string(), "13".to_string(), "14".to_string(), "15".to_string()],
]),
);
Require that the pattern appear exactly the given number of times.
let coordinate_3d = text::int::<_, Simple<char>>(10)
.padded()
.separated_by(just(','))
.exactly(3)
.then_ignore(end());
// Not enough elements
assert!(coordinate_3d.parse("4, 3").is_err());
// Too many elements
assert!(coordinate_3d.parse("7, 2, 13, 4").is_err());
// Just the right number of elements
assert_eq!(coordinate_3d.parse("5, 0, 12"), Ok(vec!["5".to_string(), "0".to_string(), "12".to_string()]));
Trait Implementations
type Error = E
type Error = E
The type of errors emitted by this parser.
Parse a stream of tokens, yielding an output if possible, and any errors encountered along the way. Read more
Parse a stream of tokens, yielding an output if possible, and any errors encountered along the way. Unlike
Parser::parse_recovery
, this function will produce verbose debugging output as it executes. Read more
Parse a stream of tokens, yielding an output or any errors that were encountered along the way. Read more
Include this parser in the debugging output produced by Parser::parse_recovery_verbose
. Read more
Map the output of this parser to another value. Read more
fn map_with_span<U, F>(self, f: F) -> MapWithSpan<Self, F, O> where
Self: Sized,
F: Fn(O, <Self::Error as Error<I>>::Span) -> U,
fn map_with_span<U, F>(self, f: F) -> MapWithSpan<Self, F, O> where
Self: Sized,
F: Fn(O, <Self::Error as Error<I>>::Span) -> U,
Map the output of this parser to another value, making use of the pattern’s span when doing so. Read more
Map the primary error of this parser to another value. Read more
fn map_err_with_span<F>(self, f: F) -> MapErrWithSpan<Self, F> where
Self: Sized,
F: Fn(Self::Error, <Self::Error as Error<I>>::Span) -> Self::Error,
fn map_err_with_span<F>(self, f: F) -> MapErrWithSpan<Self, F> where
Self: Sized,
F: Fn(Self::Error, <Self::Error as Error<I>>::Span) -> Self::Error,
Map the primary error of this parser to another value, making use of the span from the start of the attempted to the point at which the error was encountered. Read more
After a successful parse, apply a fallible function to the output. If the function produces an error, treat it as a parsing error. Read more
Validate an output, producing non-terminal errors if it does not fulfil certain criteria. Read more
Label the pattern parsed by this parser for more useful error messages. Read more
Transform all outputs of this parser to a pretermined value. Read more
Left-fold the output of the parser into a single value. Read more
Right-fold the output of the parser into a single value. Read more
Ignore the output of this parser, yielding ()
as an output instead. Read more
fn collect<C>(self) -> Map<Self, fn(_: O) -> C, O> where
Self: Sized,
O: IntoIterator,
C: FromIterator<O::Item>,
fn collect<C>(self) -> Map<Self, fn(_: O) -> C, O> where
Self: Sized,
O: IntoIterator,
C: FromIterator<O::Item>,
Collect the output of this parser into a type implementing FromIterator
. Read more
Parse one thing and then another thing, yielding a tuple of the two outputs. Read more
fn flatten<T, Inner>(self) -> Map<Self, fn(_: O) -> Vec<T>, O> where
Self: Sized,
O: IntoIterator<Item = Inner>,
Inner: IntoIterator<Item = T>,
fn flatten<T, Inner>(self) -> Map<Self, fn(_: O) -> Vec<T>, O> where
Self: Sized,
O: IntoIterator<Item = Inner>,
Inner: IntoIterator<Item = T>,
Flatten a nested collection. Read more
fn ignore_then<U, P>(self, other: P) -> IgnoreThen<Self, P, O, U> where
Self: Sized,
P: Parser<I, U, Error = Self::Error>,
fn ignore_then<U, P>(self, other: P) -> IgnoreThen<Self, P, O, U> where
Self: Sized,
P: Parser<I, U, Error = Self::Error>,
Parse one thing and then another thing, yielding only the output of the latter. Read more
fn then_ignore<U, P>(self, other: P) -> ThenIgnore<Self, P, O, U> where
Self: Sized,
P: Parser<I, U, Error = Self::Error>,
fn then_ignore<U, P>(self, other: P) -> ThenIgnore<Self, P, O, U> where
Self: Sized,
P: Parser<I, U, Error = Self::Error>,
Parse one thing and then another thing, yielding only the output of the former. Read more
fn padded_by<U, P>(
self,
other: P
) -> ThenIgnore<IgnoreThen<P, Self, U, O>, P, O, U> where
Self: Sized,
P: Parser<I, U, Error = Self::Error> + Clone,
fn padded_by<U, P>(
self,
other: P
) -> ThenIgnore<IgnoreThen<P, Self, U, O>, P, O, U> where
Self: Sized,
P: Parser<I, U, Error = Self::Error> + Clone,
Parse a pattern, but with an instance of another pattern on either end, yielding the output of the inner. Read more
fn delimited_by(self, start: I, end: I) -> DelimitedBy<Self, I> where
Self: Sized,
I: PartialEq,
fn delimited_by(self, start: I, end: I) -> DelimitedBy<Self, I> where
Self: Sized,
I: PartialEq,
Parse the pattern surrounded by the given delimiters. Read more
Parse one thing or, on failure, another thing. Read more
Apply a fallback recovery strategy to this parser should it fail. Read more
Attempt to parse something, but only if it exists. Read more
Parse a pattern any number of times (including zero times). Read more
fn separated_by<U, P>(self, other: P) -> SeparatedBy<Self, P, U> where
Self: Sized,
P: Parser<I, U, Error = Self::Error>,
fn separated_by<U, P>(self, other: P) -> SeparatedBy<Self, P, U> where
Self: Sized,
P: Parser<I, U, Error = Self::Error>,
Parse a pattern, separated by another, any number of times. Read more
Parse a pattern. Afterwards, the input stream will be rewound to its original state, as if parsing had not occurred. Read more
Box the parser, yielding a parser that performs parsing through dynamic dispatch. Read more
Auto Trait Implementations
impl<A, B, U> RefUnwindSafe for SeparatedBy<A, B, U> where
A: RefUnwindSafe,
B: RefUnwindSafe,
U: RefUnwindSafe,
impl<A, B, U> Send for SeparatedBy<A, B, U> where
A: Send,
B: Send,
U: Send,
impl<A, B, U> Sync for SeparatedBy<A, B, U> where
A: Sync,
B: Sync,
U: Sync,
impl<A, B, U> Unpin for SeparatedBy<A, B, U> where
A: Unpin,
B: Unpin,
U: Unpin,
impl<A, B, U> UnwindSafe for SeparatedBy<A, B, U> where
A: UnwindSafe,
B: UnwindSafe,
U: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more