pub struct Node<'a, T, F>{ /* private fields */ }Expand description
An interior node in the tree, one which was opened with
Builder::open or wrapped with
Builder::close_at.
Tokens are Tokens, and an iterator which mixes both
yields Elements. An Element has most of the methods of a node, so a
child can usually be used without first checking what it is.
§Type parameters and bounds
The two type parameters of the tree determine the following properties:
Tis the data stored in the tree.Fdetermines the Flavor of the tree, defining numerical bounds of spans and node identifiers stored in the tree.
A tree built through Builder::new uses
FlavorDefault.
Implementations§
Source§impl<'a, T, F> Node<'a, T, F>
impl<'a, T, F> Node<'a, T, F>
Sourcepub fn value(&self) -> T
pub fn value(&self) -> T
Access the data associated with the node.
§Examples
let tree = syntree::tree! {
"root" => {
("number", 5),
("ident", 3),
}
};
let root = tree.first().and_then(|e| e.into_node()).ok_or("missing root")?;
assert_eq!(root.value(), "root");
let number = root.first().ok_or("missing number")?;
assert_eq!(number.value(), "number");
let ident = number.next().ok_or("missing ident")?;
assert_eq!(ident.value(), "ident");Sourcepub fn replace(&self, value: T) -> T
pub fn replace(&self, value: T) -> T
Replace the value of the node with a new one, returning the old value.
§Examples
let tree = syntree::tree! {
"root" => {
"number" => {},
("ident", 3),
}
};
let root = tree.first().and_then(|e| e.into_node()).ok_or("missing root")?;
assert_eq!(root.value(), "root");
let number = root.first().and_then(|e| e.into_node()).ok_or("missing number")?;
assert_eq!(number.value(), "number");
assert_eq!(number.replace("other"), "number");
assert_eq!(number.value(), "other");
let ident = number.next().ok_or("missing ident")?;
assert_eq!(ident.value(), "ident");Sourcepub fn id(&self) -> F::Pointer
pub fn id(&self) -> F::Pointer
Get the identifier of the node.
Node ids index the interior nodes of the tree and are separate from
token ids, so a node and a token can share an id.
Use Tree::get to get the node back. An id might be re-used across
different trees. This behavior is never unsafe, but is not
well-defined.
This can be used to register a change in a ChangeSet later.
let mut tree = syntree::Builder::new();
let root_id = tree.open("root")?;
let child_id = tree.open("child")?;
tree.close()?;
let child2_id = tree.open("child2")?;
tree.close()?;
tree.close()?;
let tree = tree.build()?;
let root = tree.first().and_then(|e| e.into_node()).ok_or("missing root")?;
let child = root.first().and_then(|e| e.into_node()).ok_or("missing child")?;
let child2 = child.next().and_then(|e| e.into_node()).ok_or("missing child2")?;
assert_eq!(root.id(), root_id);
assert_eq!(child.id(), child_id);
assert_eq!(child2.id(), child2_id);Sourcepub const fn span(&self) -> &Span<F::Index>
pub const fn span(&self) -> &Span<F::Index>
Get the span of the node. The span of a node is the complete span of all its children.
§Examples
use syntree::Span;
let tree = syntree::tree! {
"root" => {
"number" => {
("lit", 5)
},
"ident" => {
("lit", 3)
}
},
"root2" => {
("whitespace", 5)
}
};
let root = tree.first().and_then(|e| e.into_node()).ok_or("missing root")?;
assert_eq!(*root.span(), Span::new(0, 8));
let root2 = root.next().ok_or("missing second root")?;
assert_eq!(root2.span(), Span::new(8, 13));Sourcepub fn range(&self) -> Range<usize> ⓘ
pub fn range(&self) -> Range<usize> ⓘ
Access the Span of the node as a Range.
§Examples
let tree = syntree::tree! {
"root" => {
"number" => {
("lit", 5)
},
"ident" => {
("lit", 3)
}
},
"root2" => {
("whitespace", 5)
}
};
let root = tree.first().ok_or("missing root")?;
assert_eq!(root.range(), 0..8);
let root2 = root.next().ok_or("missing second root")?;
assert_eq!(root2.range(), 8..13);Sourcepub fn has_children(&self) -> bool
pub fn has_children(&self) -> bool
Check if the node has children, tokens or nodes.
§Examples
let tree = syntree::tree! {
"root" => {
("number", 5),
("ident", 3),
}
};
let root = tree.first().ok_or("missing root")?;
assert!(root.has_children());
assert!(root.children().all(|n| !n.has_children()));Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if the node is empty, in that it has no children.
§Examples
let mut tree = syntree::tree! {
"root" => {},
"root2" => {
("token2", 5)
}
};
let first = tree.first().and_then(|e| e.into_node()).ok_or("missing root")?;
let last = tree.last().and_then(|e| e.into_node()).ok_or("missing root2")?;
assert!(first.is_empty());
assert!(!last.is_empty());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.
§Examples
let tree = syntree::tree! {
"root" => {
"number" => {
("lit", 5)
},
"ident" => {
("lit", 3)
}
},
"root2" => {
("whitespace", 5)
}
};
let root = tree.first().ok_or("missing root")?;
assert_eq!(root.value(), "root");
assert!(root.parent().is_none());
let number = root.first().ok_or("missing number")?;
assert_eq!(number.value(), "number");
let root = number.parent().ok_or("missing parent")?;
assert_eq!(root.value(), "root");Sourcepub fn prev(&self) -> Option<Element<'a, T, F>>
pub fn prev(&self) -> Option<Element<'a, T, F>>
Get the previous sibling, a node or a token.
§Examples
let tree = syntree::tree! {
"root" => {
("lit", 5),
"ident" => {
("lit", 3)
}
}
};
let ident = tree.first().and_then(|n| n.last()).ok_or("missing ident")?;
assert_eq!(ident.value(), "ident");
let lit = ident.prev().ok_or("missing lit")?;
assert_eq!(lit.value(), "lit");
assert!(lit.is_token());
assert!(lit.prev().is_none());Sourcepub fn next(&self) -> Option<Element<'a, T, F>>
pub fn next(&self) -> Option<Element<'a, T, F>>
Get the next sibling, a node or a token.
§Examples
let tree = syntree::tree! {
"root" => {
"number" => {
("lit", 5)
},
("ident", 3)
}
};
let root = tree.first().ok_or("missing root")?;
assert_eq!(root.value(), "root");
let number = root.first().ok_or("missing number")?;
assert_eq!(number.value(), "number");
let ident = number.next().ok_or("missing ident")?;
assert_eq!(ident.value(), "ident");
assert!(ident.is_token());Sourcepub fn first(&self) -> Option<Element<'a, T, F>>
pub fn first(&self) -> Option<Element<'a, T, F>>
Get the first child, a node or a token.
§Examples
let tree = syntree::tree! {
"root" => {
"number" => {
("lit", 5)
},
"ident" => {
("lit", 3)
}
},
"root2" => {
("whitespace", 5)
}
};
let root = tree.first().ok_or("missing root")?;
assert_eq!(root.value(), "root");
let number = root.first().ok_or("missing number")?;
assert_eq!(number.value(), "number");Sourcepub fn last(&self) -> Option<Element<'a, T, F>>
pub fn last(&self) -> Option<Element<'a, T, F>>
Get the last child, a node or a token.
§Examples
let tree = syntree::tree! {
"root" => {
"number" => {
("lit", 5)
},
"ident" => {
("lit", 3)
}
},
"root2" => {
("whitespace", 5)
}
};
let root2 = tree.last().ok_or("missing root2")?;
assert_eq!(root2.value(), "root2");
let whitespace = root2.last().ok_or("missing whitespace")?;
assert_eq!(whitespace.value(), "whitespace");Sourcepub fn ancestors(&self) -> Ancestors<'a, T, F> ⓘ
pub fn ancestors(&self) -> Ancestors<'a, T, F> ⓘ
Get an iterator over this node and its ancestors, starting with the node itself and ending with its root.
See Ancestors for documentation.
Sourcepub fn siblings(&self) -> Siblings<'a, T, F> ⓘ
pub fn siblings(&self) -> Siblings<'a, T, F> ⓘ
Get an iterator over this node and its following siblings, nodes and tokens.
Previous siblings are not included.
See Siblings for documentation.
§Examples
let tree = syntree::tree! {
"root" => {
"a",
"b" => {},
"c",
"d",
}
};
let b = tree.first().and_then(|n| n.first()).and_then(|n| n.next()).ok_or("missing b")?;
assert_eq!(b.value(), "b");
assert_eq!(
b.siblings().map(|n| n.value()).collect::<Vec<_>>(),
["b", "c", "d"]
);Sourcepub fn children(&self) -> Children<'a, T, F> ⓘ
pub fn children(&self) -> Children<'a, T, F> ⓘ
Iterate over the children of the node, nodes and tokens in order.
See Children for documentation.
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 the current node.
The walk does not stop at the end of this node’s subtree, it continues
with the following siblings of this node and of its ancestors. Use
Walk::inside to only visit this node and its descendants.
See Walk for documentation.
Sourcepub fn walk_from(&self) -> Walk<'a, T, F> ⓘ
pub fn walk_from(&self) -> Walk<'a, T, F> ⓘ
Walk from the current node forwards and upwards through the tree.
This does not include the current node or its descendants in the walk.
See Walk for documentation.
Sourcepub fn walk_events(&self) -> WalkEvents<'a, T, F> ⓘ
pub fn walk_events(&self) -> WalkEvents<'a, T, F> ⓘ
Walk the node forwards in a depth-first fashion emitting events indicating how the rest of the tree is being traversed.
See WalkEvents for documentation.
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 node. This is found in two steps:
- Starting with the current node 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.
See Element::find_preceding for an example.
Trait Implementations§
impl<T, F> Copy for Node<'_, T, F>
impl<T, F> Eq for Node<'_, T, F>
Source§impl<T, A, B> PartialEq<Node<'_, T, A>> for Node<'_, T, B>
impl<T, A, B> PartialEq<Node<'_, T, A>> for Node<'_, T, B>
Compare Node::id to check whether two nodes are the same node.
§Examples
let tree = syntree::tree! {
"x" => {
"x" => {}
}
};
let root = tree.first().and_then(|e| e.into_node()).ok_or("missing root")?;
let child = root.first().and_then(|e| e.into_node()).ok_or("missing child")?;
assert_eq!(root, child);
assert_ne!(root.id(), child.id());