pub struct Node<'a, T, F>{ /* private fields */ }
Expand description
A node in the tree.
§Type parameters and bounds
The three type parameters of the tree determines the following properties:
T
is the data stored in the tree.F
determines the Flavor of the tree, defining numerical bounds of spans stored in the tree.
To use the default values, use the Builder::new constructor.
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().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", 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");
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 const fn has_children(&self) -> bool
pub const fn has_children(&self) -> bool
Check if the current node has children or not.
Nodes without children are also known as tokens.
§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 const fn span(&self) -> &Span<F::Index>
pub const fn span(&self) -> &Span<F::Index>
Get the span of the current 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().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 const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Check if the current node is empty. In that it doesn’t have any children.
§Examples
let mut tree = syntree::tree! {
"root",
"root2" => {
("token2", 5)
}
};
let first = tree.first().ok_or("missing root")?;
let last = first.next().ok_or("missing root2")?;
assert!(first.is_empty());
assert!(!last.is_empty());
Sourcepub fn ancestors(&self) -> Ancestors<'a, T, F> ⓘ
pub fn ancestors(&self) -> Ancestors<'a, T, F> ⓘ
Get the ancestors of this node.
See Ancestors for documentation.
Sourcepub fn siblings(&self) -> Siblings<'a, T, F> ⓘ
pub fn siblings(&self) -> Siblings<'a, T, F> ⓘ
Get an iterator over the siblings of this node, including itself.
See Siblings for documentation.
Sourcepub fn children(&self) -> Children<'a, T, F> ⓘ
pub fn children(&self) -> Children<'a, T, F> ⓘ
Get an iterator over the children of this node.
See Children for documentation.
Sourcepub fn walk(&self) -> Walk<'a, T, F> ⓘ
pub fn walk(&self) -> Walk<'a, T, F> ⓘ
Walk the subtree forward starting with the first child of the current node.
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 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.
Source§impl<'a, T, F> Node<'a, T, F>
impl<'a, T, F> Node<'a, T, F>
Sourcepub fn parent(&self) -> Option<Node<'a, T, F>>
pub fn parent(&self) -> Option<Node<'a, T, F>>
Get immediate parent to this node.
§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<Node<'a, T, F>>
pub fn prev(&self) -> Option<Node<'a, T, F>>
Get the previous sibling.
§Examples
let tree = syntree::tree! {
"root" => {
"number" => {
("lit", 5)
},
"ident" => {
("lit", 3)
}
}
};
let number = tree.first().and_then(|n| n.first()).ok_or("missing number")?;
assert_eq!(number.value(), "number");
assert!(number.prev().is_none());
let ident = number.next().ok_or("missing ident")?;
assert_eq!(ident.value(), "ident");
let number = ident.prev().ok_or("missing number")?;
assert_eq!(number.value(), "number");
Sourcepub fn next(&self) -> Option<Node<'a, T, F>>
pub fn next(&self) -> Option<Node<'a, T, F>>
Get the next sibling.
§Examples
let tree = syntree::tree! {
"root" => {
"number" => {
("lit", 5)
},
"ident" => {
("lit", 3)
}
}
};
let root = tree.first().ok_or("missing root")?;
assert_eq!(root.value(), "root");
let number = root.first().ok_or("missing second root")?;
assert_eq!(number.value(), "number");
let ident = number.next().ok_or("missing second root")?;
assert_eq!(ident.value(), "ident");
Sourcepub fn first(&self) -> Option<Node<'a, T, F>>
pub fn first(&self) -> Option<Node<'a, T, F>>
Get the first child node.
§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<Node<'a, T, F>>
pub fn last(&self) -> Option<Node<'a, T, F>>
Get the last child node.
§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 find_preceding<P>(&self, predicate: P) -> Option<Node<'a, T, F>>
pub fn find_preceding<P>(&self, predicate: P) -> Option<Node<'a, T, F>>
Find a preceeding node which matches the given predicate.
A “preceeding node” is one which constitutes tokens the immediately
preceedes the ones of the current node, so this function scans first the
parents of the current node for a matching Node::prev
sibling, and
then traverses that matches Node::last
.
§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 0")?;
assert_eq!(node.value(), "child4");
let found = node.find_preceding(|n| n.span().end == 3 && n.has_children());
let found = found.expect("expected preceeding node");
assert_eq!(found.value(), "child2");
Sourcepub fn id(&self) -> F::Pointer
pub fn id(&self) -> F::Pointer
Get the identifier of the current node.
Note that 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().ok_or("missing root")?;
let child = root.first().ok_or("missing child")?;
let child2 = child.next().ok_or("missing child2")?;
assert_eq!(root.id(), root_id);
assert_eq!(child.id(), child_id);
assert_eq!(child2.id(), child2_id);
Source§impl<T, F> Node<'_, T, F>
impl<T, F> Node<'_, T, F>
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);
Trait Implementations§
impl<T, F> Copy for Node<'_, T, F>
impl<T, F> Eq for Node<'_, T, F>
Auto Trait Implementations§
impl<'a, T, F> Freeze for Node<'a, T, F>
impl<'a, T, F> !RefUnwindSafe for Node<'a, T, F>
impl<'a, T, F> !Send for Node<'a, T, F>
impl<'a, T, F> !Sync for Node<'a, T, F>
impl<'a, T, F> Unpin for Node<'a, T, F>
impl<'a, T, F> !UnwindSafe for Node<'a, T, F>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)