pub struct DelimitedBy<P, Delim> { /* private fields */ }many only.Expand description
A parser that wraps a repetition driver in a pair of delimiter tokens.
This combinator wraps any of the four repetition builders — Repeated, RepeatedWhile,
Separated, SeparatedWhile, and their bound/leading/trailing option wrappers — in
opening and closing delimiters, parsing constructs like [element element element] or
{item, item, item}.
The delimiter pair is a type, not a pair of classifier closures: delimited::<Delim>()
takes any Delimiter, and delimited_by_brackets / _braces /
_parens / _angles name the built-in pairs.
§Type Parameters
P: The wrapped repetition parser — which is what carries the element parser, the stopping decision, the output type, the lookahead window, the lexer, the context and the languageDelim: The delimiter pair marker (e.g.Bracket), aDelimiterwhoseOpen/Closepunctuators classify the two tokens
§Examples
§Basic Bracketed List
use tokora::{Accumulator, Parse, ParseInput, Parser, while_head};
// Parse: [element element element]
fn items<'a>(inp: &mut InputRef<'a, '_, CharLexer<'a>, Ctx<'a>>) -> Result<Vec<i64>, Error> {
num
.repeated_while(while_head(|t: &Tok| matches!(t, Tok::Num(_))))
.delimited_by_brackets()
.collect()
.parse_input(inp)
}
assert_eq!(Parser::with_parser(items).parse_str("[1 2 3]").unwrap(), vec![1, 2, 3]);
assert_eq!(Parser::with_parser(items).parse_str("[7]").unwrap(), vec![7]);
assert_eq!(Parser::with_parser(items).parse_str("[]").unwrap(), Vec::<i64>::new());§Generic Delimiters
use tokora::{Accumulator, Parse, ParseInput, Parser, while_head};
// Parse: {token token token}
fn items<'a>(inp: &mut InputRef<'a, '_, CharLexer<'a>, Ctx<'a>>) -> Result<Vec<i64>, Error> {
num
.repeated_while(while_head(|t: &Tok| matches!(t, Tok::Num(_))))
.delimited_by_braces()
.collect()
.parse_input(inp)
}
assert_eq!(Parser::with_parser(items).parse_str("{1 2 3}").unwrap(), vec![1, 2, 3]);§Parenthesized Expressions
use tokora::{Accumulator, Parse, ParseInput, Parser, while_head};
// Parse: (expr expr expr)
fn exprs<'a>(inp: &mut InputRef<'a, '_, CharLexer<'a>, Ctx<'a>>) -> Result<Vec<i64>, Error> {
num
.repeated_while(while_head(|t: &Tok| matches!(t, Tok::Num(_))))
.delimited_by_parens()
.collect()
.parse_input(inp)
}
assert_eq!(Parser::with_parser(exprs).parse_str("(1 2 3)").unwrap(), vec![1, 2, 3]);§With Bounds
use tokora::{Accumulator, Parse, ParseInput, Parser, while_head};
// Parse 1-10 elements in brackets.
fn items<'a>(inp: &mut InputRef<'a, '_, CharLexer<'a>, Ctx<'a>>) -> Result<Vec<i64>, Error> {
num
.repeated_while(while_head(|t: &Tok| matches!(t, Tok::Num(_))))
.at_least(1)
.at_most(10)
.delimited_by_brackets()
.collect()
.parse_input(inp)
}
assert_eq!(Parser::with_parser(items).parse_str("[7]").unwrap(), vec![7]);
// Below the minimum: the too-few diagnostic aborts under the fail-fast context.
assert!(Parser::with_parser(items).parse_str("[]").is_err());§How It Works
- Parse opening delimiter: Consume the left delimiter token
- Parse elements: Run the wrapped repetition driver
- Parse closing delimiter: Consume the right delimiter token
- Return: Return the collected elements
§Separated variants
The same wrapper carries a separated driver, so [a, b, c] is
DelimitedBy<SeparatedWhile<..>, Bracket<..>> — built by the identical delimited_by_*
call on a separated/separated_while builder.
| Feature | over a repeated driver | over a separated driver |
|---|---|---|
| Separators | No separators | Elements separated by a separator token |
| Base Parser | Repeated / RepeatedWhile | Separated / SeparatedWhile |
| Example | [a b c] | [a, b, c] |
| Use Case | Consecutive items | Separated lists |
§Performance
- Memory: O(1) for the parser structure
- Runtime: O(n) where n is the number of elements
- Delimiter matching: O(1) per delimiter
§See Also
RepeatedWhile- One of the four repetition drivers this can wrapdelimited- How to create this combinatorCollect- Wrapper for collecting elements into a container
Implementations§
Source§impl<P, Delim> DelimitedBy<P, Delim>
impl<P, Delim> DelimitedBy<P, Delim>
Sourcepub const fn new(parser: P) -> Self
pub const fn new(parser: P) -> Self
Creates a new DelimitedBy combinator wrapping the given parser.
Sourcepub fn map_parser_mut<'a, Q, F>(&'a mut self, f: F) -> DelimitedBy<Q, Delim>
pub fn map_parser_mut<'a, Q, F>(&'a mut self, f: F) -> DelimitedBy<Q, Delim>
Maps the inner parser via a mutable reference, returning a new DelimitedBy.
Trait Implementations§
Source§impl<P: Clone, Delim: Clone> Clone for DelimitedBy<P, Delim>
impl<P: Clone, Delim: Clone> Clone for DelimitedBy<P, Delim>
Source§fn clone(&self) -> DelimitedBy<P, Delim>
fn clone(&self) -> DelimitedBy<P, Delim>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more