mirror of
https://github.com/RGBCube/cstree
synced 2025-07-27 09:07:44 +00:00
Merge pull request #34 from domenicquirl/feature/fold-chunks
This commit is contained in:
commit
ac7c383f74
1 changed files with 23 additions and 9 deletions
|
@ -156,6 +156,8 @@ impl<'n, 'i, I: Resolver + ?Sized, L: Language, D> SyntaxText<'n, 'i, I, L, D> {
|
||||||
/// If `f` returns `Err`, the error is propagated immediately.
|
/// If `f` returns `Err`, the error is propagated immediately.
|
||||||
/// Otherwise, the result of the current call to `f` will be passed to the invocation of `f` on
|
/// Otherwise, the result of the current call to `f` will be passed to the invocation of `f` on
|
||||||
/// the next token, producing a final value if `f` succeeds on all chunks.
|
/// the next token, producing a final value if `f` succeeds on all chunks.
|
||||||
|
///
|
||||||
|
/// See also [`fold_chunks`](SyntaxText::fold_chunks) for folds that always succeed.
|
||||||
pub fn try_fold_chunks<T, F, E>(&self, init: T, mut f: F) -> Result<T, E>
|
pub fn try_fold_chunks<T, F, E>(&self, init: T, mut f: F) -> Result<T, E>
|
||||||
where
|
where
|
||||||
F: FnMut(T, &str) -> Result<T, E>,
|
F: FnMut(T, &str) -> Result<T, E>,
|
||||||
|
@ -165,6 +167,25 @@ impl<'n, 'i, I: Resolver + ?Sized, L: Language, D> SyntaxText<'n, 'i, I, L, D> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Applies the given function to all text chunks (from [`SyntaxToken`]s) that are part of this
|
||||||
|
/// text, starting from the initial value `init`.
|
||||||
|
///
|
||||||
|
/// The result of the current call to `f` will be passed to the invocation of `f` on the next
|
||||||
|
/// token, producing a final value after `f` was called on all chunks.
|
||||||
|
///
|
||||||
|
/// See also [`try_fold_chunks`](SyntaxText::try_fold_chunks), which performs the same operation
|
||||||
|
/// for fallible functions `f`.
|
||||||
|
pub fn fold_chunks<T, F>(&self, init: T, mut f: F) -> T
|
||||||
|
where
|
||||||
|
F: FnMut(T, &str) -> T,
|
||||||
|
{
|
||||||
|
enum Void {}
|
||||||
|
match self.try_fold_chunks(init, |acc, chunk| Ok::<T, Void>(f(acc, chunk))) {
|
||||||
|
Ok(t) => t,
|
||||||
|
Err(void) => match void {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Applies the given function to all text chunks that this text is comprised of, in order,
|
/// Applies the given function to all text chunks that this text is comprised of, in order,
|
||||||
/// as long as `f` completes successfully.
|
/// as long as `f` completes successfully.
|
||||||
///
|
///
|
||||||
|
@ -178,17 +199,10 @@ impl<'n, 'i, I: Resolver + ?Sized, L: Language, D> SyntaxText<'n, 'i, I, L, D> {
|
||||||
|
|
||||||
/// Applies the given function to all text chunks that this text is comprised of, in order.
|
/// Applies the given function to all text chunks that this text is comprised of, in order.
|
||||||
///
|
///
|
||||||
/// See also [`try_fold_chunks`](SyntaxText::try_fold_chunks),
|
/// See also [`fold_chunks`](SyntaxText::fold_chunks),
|
||||||
/// [`try_for_each_chunk`](SyntaxText::try_for_each_chunk).
|
/// [`try_for_each_chunk`](SyntaxText::try_for_each_chunk).
|
||||||
pub fn for_each_chunk<F: FnMut(&str)>(&self, mut f: F) {
|
pub fn for_each_chunk<F: FnMut(&str)>(&self, mut f: F) {
|
||||||
enum Void {}
|
self.fold_chunks((), |(), chunk| f(chunk))
|
||||||
match self.try_for_each_chunk(|chunk| {
|
|
||||||
f(chunk);
|
|
||||||
Ok::<(), Void>(())
|
|
||||||
}) {
|
|
||||||
Ok(()) => (),
|
|
||||||
Err(void) => match void {},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tokens_with_ranges(&self) -> impl Iterator<Item = (&SyntaxToken<L, D>, TextRange)> {
|
fn tokens_with_ranges(&self) -> impl Iterator<Item = (&SyntaxToken<L, D>, TextRange)> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue