iter-tree
This library provide an easy way to transform iterator into tree. This can be useful when building simple parsers to convert a stream of token into a tree of token.
It provide two types of tree:
-
The default one,
Treeis based onVecfrom the standard library. -
The second one is based on
VecDequefrom the standard libray. To get this one you have to activate thedequefeature flag.
In the future, the goal would be to provide other types of Trees, notably some that separate the token that inited and terminated a branch.
The goals for the future of this crate includes but are not limited to :
- Providing other types of Trees, notably some that separate the item that inited and terminated a branch.
- Adding more methods to build Trees such as for example a
tree_mapandtree_deque_mapmethod that would map the item before including it in the Tree.
Usage
The creation of a tree is controlled with the BranchControl enum.
This enum has three variants :
- BranchControl::Start
- Is used to start nesting the items of the iterator into a new branch.
- BranchControl::Continue
- Is used to keep the item in the same branch as the previous ones
- BranchControl::End
- Is used to get back up to the previous branch to put the next items.
Note:
When filling a branch started with
BranchControl::Start, no crash or error will happens if the iterator ends before encountering the correspondingBranchControl::End. Similarly, any unmatchedBranchControl::Endwill simply be ignored.If you want to check for these kind of situations, you can use a trick such as the depth counter showed in the below example.
Example
use *;
let mut depth = 0;
let tree = "a+(b+c)+d"
.chars
.into_iter
.tree
.;
println!;
assert_eq!;
[
)
)
[
)
)
)
)
)
)
)
)
)
To go further
Additionally you can create a struct that implements the Controller trait to replace the closure from the previous example.
Here is an example of how this can be applied :
use *;
let mut controller = default;
let _1 = "< ( < > ) >"
.chars
.tree
.;
assert!;
let mut controller = default;
let _b = "<(>)".chars.tree.;
assert!