Skip to main content

Token

Struct Token 

Source
pub struct Token<'a, T, F>
where T: Copy, F: Flavor,
{ /* 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>
where T: Copy, F: Flavor,

Source

pub fn value(&self) -> T

Access the data associated with the token.

Source

pub fn replace(&self, value: T) -> T

Replace the value of the token with a new one, returning the old value.

Source

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.

Source

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());
Source

pub fn span(&self) -> Span<F::Index>

Get the span of the token.

Source

pub fn range(&self) -> Range<usize> ⓘ

Access the Span of the token as a Range.

Source

pub fn parent(&self) -> Option<Node<'a, T, F>>

Get the parent node, None at the root of the tree.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn find_preceding<P>(&self, predicate: P) -> Option<Element<'a, T, F>>
where P: FnMut(Element<'a, T, F>) -> bool,

Find a preceding element which matches the given predicate.

See Element::find_preceding.

Trait Implementations§

Source§

impl<T, F> Clone for Token<'_, T, F>
where T: Copy, F: Flavor,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, F> Copy for Token<'_, T, F>
where T: Copy, F: Flavor,

Source§

impl<T, F> Debug for Token<'_, T, F>
where T: Copy + Debug, F: Flavor<Index: Debug>,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, F> Eq for Token<'_, T, F>
where T: Copy + Eq, F: Flavor<Index: Eq>,

Source§

impl<T, A, B> PartialEq<Token<'_, T, A>> for Token<'_, T, B>
where T: Copy + PartialEq, A: Flavor, B: Flavor<Index: PartialEq<A::Index>>,

Source§

fn eq(&self, other: &Token<'_, T, A>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<'a, T, F> !RefUnwindSafe for Token<'a, T, F>

§

impl<'a, T, F> !Send for Token<'a, T, F>

§

impl<'a, T, F> !Sync for Token<'a, T, F>

§

impl<'a, T, F> !UnwindSafe for Token<'a, T, F>

§

impl<'a, T, F> Forget for Token<'a, T, F>

§

impl<'a, T, F> Freeze for Token<'a, T, F>
where &'a Tree<T, F>: Freeze, &'a TokenLinks<T, <F as Flavor>::Index, <F as Flavor>::Pointer>: Freeze, <F as Flavor>::Pointer: Freeze,

§

impl<'a, T, F> Move for Token<'a, T, F>

§

impl<'a, T, F> Unpin for Token<'a, T, F>
where &'a Tree<T, F>: Unpin, &'a TokenLinks<T, <F as Flavor>::Index, <F as Flavor>::Pointer>: Unpin, <F as Flavor>::Pointer: Unpin,

§

impl<'a, T, F> UnsafeUnpin for Token<'a, T, F>
where &'a Tree<T, F>: UnsafeUnpin, &'a TokenLinks<T, <F as Flavor>::Index, <F as Flavor>::Pointer>: UnsafeUnpin, <F as Flavor>::Pointer: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.