Skip to main content

Node

Struct Node 

Source
pub struct Node<'a, T, F>
where T: Copy, F: Flavor,
{ /* private fields */ }
Expand description

A node in the tree.

§Type parameters and bounds

The two type parameters of the tree determine the following properties:

  • T is the data stored in the tree.
  • F determines 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>
where T: Copy, F: Flavor,

Source

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

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

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

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

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

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.

Source

pub fn siblings(&self) -> Siblings<'a, T, F> ⓘ

Get an iterator over this node and its following siblings.

Previous siblings are not included.

See Siblings for documentation.

§Examples
let tree = syntree::tree! {
    "root" => {
        "a",
        "b",
        "c",
        "d",
    }
};

let c = tree.first().and_then(|n| n.last()).and_then(|n| n.prev()).ok_or("missing c")?;
assert_eq!(c.value(), "c");

assert_eq!(
    c.siblings().map(|n| n.value()).collect::<Vec<_>>(),
    ["c", "d"]
);
Source

pub fn children(&self) -> Children<'a, T, F> ⓘ

Get an iterator over the children of this node.

See Children for documentation.

Source

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.

Source

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.

Source

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>
where T: Copy, F: Flavor,

Source

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

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

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

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

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

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

Find a preceding node which matches the given predicate.

A “preceding node” is one whose tokens immediately precede the tokens of the current node. This is found in two steps:

  1. Starting with the current node and moving through its ancestors, find the first Node::prev sibling which matches the predicate.
  2. From that sibling, descend through Node::last children for as long as each matches the predicate, and return the last match.

Step 2 stops at the first Node::last child which does not match, so a matching node 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");
Source

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>
where T: Copy, F: Flavor,

Source

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§

Source§

impl<T, F> Clone for Node<'_, 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 Node<'_, T, F>
where T: Copy, F: Flavor,

Source§

impl<T, F> Debug for Node<'_, 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 Node<'_, T, F>
where T: Copy + Eq, F: Flavor<Index: Eq>,

Source§

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

Nodes compare equal when their values and spans are equal, regardless of their position in a tree.

Compare Node::id to check whether two nodes are the same node.

§Examples

let tree = syntree::tree! {
    "x" => {
        ("x", 0)
    }
};

let root = tree.first().ok_or("missing root")?;
let child = root.first().ok_or("missing child")?;

assert_eq!(root, child);
assert_ne!(root.id(), child.id());
Source§

fn eq(&self, other: &Node<'_, 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 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> !UnwindSafe for Node<'a, T, F>

§

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

§

impl<'a, T, F> Freeze for Node<'a, T, F>
where &'a Links<T, <F as Flavor>::Index, <F as Flavor>::Pointer>: Freeze, &'a [Links<T, <F as Flavor>::Index, <F as Flavor>::Pointer>]: Freeze,

§

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

§

impl<'a, T, F> Unpin for Node<'a, T, F>
where &'a Links<T, <F as Flavor>::Index, <F as Flavor>::Pointer>: Unpin, &'a [Links<T, <F as Flavor>::Index, <F as Flavor>::Pointer>]: Unpin,

§

impl<'a, T, F> UnsafeUnpin for Node<'a, T, F>
where &'a Links<T, <F as Flavor>::Index, <F as Flavor>::Pointer>: UnsafeUnpin, &'a [Links<T, <F as Flavor>::Index, <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.