pub enum NodeOrToken<N, K> {
Node(N),
Token(K),
}Expand description
Variants§
Implementations§
Source§impl<N, K> NodeOrToken<N, K>
impl<N, K> NodeOrToken<N, K>
Sourcepub fn into_token(self) -> Option<K>
pub fn into_token(self) -> Option<K>
Get the token, if this is one.
Source§impl<'a, T, F> NodeOrToken<Node<'a, T, F>, Token<'a, T, F>>
impl<'a, T, F> NodeOrToken<Node<'a, T, F>, Token<'a, T, F>>
Sourcepub fn replace(&self, value: T) -> T
pub fn replace(&self, value: T) -> T
Replace the value of the element with a new one, returning the old value.
Sourcepub fn parent(&self) -> Option<Node<'a, T, F>>
pub fn parent(&self) -> Option<Node<'a, T, F>>
Get the parent node, None at the root of the tree.
Sourcepub fn is_trivia(&self) -> bool
pub fn is_trivia(&self) -> bool
Test if the element is a trivia token, see Token::is_trivia. A
node is never trivia.
Sourcepub fn has_children(&self) -> bool
pub fn has_children(&self) -> bool
Check if the element has children, which a token never has.
Sourcepub fn first(&self) -> Option<Element<'a, T, F>>
pub fn first(&self) -> Option<Element<'a, T, F>>
Get the first child, None for a token. See Node::first.
Sourcepub fn last(&self) -> Option<Element<'a, T, F>>
pub fn last(&self) -> Option<Element<'a, T, F>>
Get the last child, None for a token. See Node::last.
Sourcepub fn children(&self) -> Children<'a, T, F> ⓘ
pub fn children(&self) -> Children<'a, T, F> ⓘ
Iterate over the children of the element, which is empty for a token.
See Node::children.
Sourcepub fn ancestors(&self) -> Ancestors<'a, T, F> ⓘ
pub fn ancestors(&self) -> Ancestors<'a, T, F> ⓘ
Get an iterator over this element and its ancestors. See
Node::ancestors.
Sourcepub fn siblings(&self) -> Siblings<'a, T, F> ⓘ
pub fn siblings(&self) -> Siblings<'a, T, F> ⓘ
Get an iterator over this element and its following siblings. See
Node::siblings.
Sourcepub fn walk(&self) -> Walk<'a, T, F> ⓘ
pub fn walk(&self) -> Walk<'a, T, F> ⓘ
Walk the tree forward in depth-first order starting with this element.
See Node::walk.
Sourcepub fn walk_from(&self) -> Walk<'a, T, F> ⓘ
pub fn walk_from(&self) -> Walk<'a, T, F> ⓘ
Walk forwards and upwards from this element, not including it or its
descendants. See Node::walk_from.
Sourcepub fn walk_events(&self) -> WalkEvents<'a, T, F> ⓘ
pub fn walk_events(&self) -> WalkEvents<'a, T, F> ⓘ
Walk forward from this element emitting events. See
Node::walk_events.
Sourcepub fn find_preceding<P>(&self, predicate: P) -> Option<Element<'a, T, F>>
pub fn find_preceding<P>(&self, predicate: P) -> Option<Element<'a, T, F>>
Find a preceding element which matches the given predicate.
A “preceding element” is one whose tokens immediately precede the tokens of the current element. This is found in two steps:
- Starting with the current element and moving through its ancestors,
find the first
prevsibling which matches the predicate. - From that sibling, descend through
lastchildren for as long as each matches the predicate, and return the last match.
Step 2 stops at the first last child which does not match, so a matching element nested below a non-matching last child is not found.
§Examples
let tree = syntree::tree! {
"root" => {
"child1" => {
("token2", 1),
"child2" => {
("token1", 2)
}
},
"child3" => {
"child4" => {
("token1", 4),
}
}
}
};
let node = tree.node_with_range(3..3).ok_or("missing 3")?;
assert_eq!(node.value(), "token1");
let found = node.find_preceding(|n| n.span().end == 3 && n.has_children());
let found = found.ok_or("expected preceding node")?;
assert_eq!(found.value(), "child2");