pub struct Token<'a, T, F>{ /* private fields */ }Expand description
A token in the tree, one which was added with
Builder::token or its variants.
Tokens have no children.
Implementations§
Source§impl<'a, T, F> Token<'a, T, F>
impl<'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 token with a new one, returning the old value.
Sourcepub fn id(&self) -> F::Pointer
pub fn id(&self) -> F::Pointer
Get the identifier of the token, see Tree::token.
Token ids index the token array of the tree and are separate from node ids. They are not dense: an explicit span which leaves a gap after the previous token takes an internal id for the gap.
Sourcepub fn is_trivia(&self) -> bool
pub fn is_trivia(&self) -> bool
Test if this is a trivia token, inserted with Builder::trivia or
Builder::trivia_with. Iterators skip trivia unless asked to
include it with with_trivia().
§Examples
let mut tree = syntree::Builder::new();
tree.token("ident", 3)?;
tree.trivia("whitespace", 1)?;
let tree = tree.build()?;
let ident = tree.first().and_then(|e| e.into_token()).ok_or("missing ident")?;
let ws = tree.last().and_then(|e| e.into_token()).ok_or("missing whitespace")?;
assert!(!ident.is_trivia());
assert!(ws.is_trivia());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 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.
This is linear in the number of interior nodes which are siblings of
the token. Iterators such as Node::children step in constant time.
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.
This is linear in the number of interior nodes which are siblings of
the token. Iterators such as Node::children step in constant time.
Sourcepub fn ancestors(&self) -> Ancestors<'a, T, F> ⓘ
pub fn ancestors(&self) -> Ancestors<'a, T, F> ⓘ
Get an iterator over this token and its ancestors, starting with the token 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 token and its following siblings, nodes and tokens.
Constructing it is linear in the number of interior nodes which are siblings of the token, stepping it is constant.
See Siblings 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 this token, then continue with what follows it in the tree.
Constructing it is linear in the number of interior nodes which are siblings of the token.
See Walk for documentation.
Sourcepub fn walk_from(&self) -> Walk<'a, T, F> ⓘ
pub fn walk_from(&self) -> Walk<'a, T, F> ⓘ
Walk from this token forwards and upwards through the tree, not including the token itself.
See Walk for documentation.
Sourcepub fn walk_events(&self) -> WalkEvents<'a, T, F> ⓘ
pub fn walk_events(&self) -> WalkEvents<'a, T, F> ⓘ
Walk forward from this token, 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.