Skip to main content

Node

Struct Node 

Source
pub struct Node<'t> { /* private fields */ }
Expand description

A node or token of a SyntaxTree: a cheap, copyable view.

Navigation is O(1) per step, except Node::last_child and Node::prev_sibling, which are O(depth) (the arena links forward). In an edited tree too, as long as the step stays inside one of the edits’ runs, so sequential reads cost amortised O(1) per element (see SyntaxTree::is_edited).

Implementations§

Source§

impl<'t> Node<'t>

Source

pub const fn slot(self) -> u32

The element’s slot: a 4-byte name of the element for the tree’s whole life (edits never reuse a slot), resolved with SyntaxTree::node_at_slot. Meant for compact indexes over one tree; unlike a NodeId it does not name its tree, so it is meaningful only with the tree that issued it (and a fork of it, for slots issued before the fork). O(1).

Source

pub fn id(self) -> NodeId

A stable handle to this element.

Source

pub fn preorder_index(self) -> usize

The element’s position in preorder (= source order), 0 for the root. Positions compare elements of one tree: an element comes before everything in its subtree and after its preceding siblings. O(1); O(log R) in an edited tree (R: runs, see SyntaxTree::is_edited).

Source

pub const fn tree(self) -> &'t SyntaxTree

The tree this element belongs to.

Source

pub fn kind(self) -> SyntaxKind

What the element is.

Source

pub fn is_token(self) -> bool

true for a leaf token.

Source

pub fn span(self) -> Span

The source bytes the element covers (UTF-8 byte range); in an edited tree, the range in SyntaxTree::emit’s text. O(1) (a run of nodes opening at one byte aside); edited: O(log R) when the subtree is untouched, else O(depth).

Source

pub fn text(self) -> Cow<'t, str>

The source text the element covers; in an edited tree, its text in SyntaxTree::emit. Borrowed from the tree (always for a parsed tree and for tokens); a node whose text an edit split over several arenas is built from them on each call (O(node size)): the tree keeps no copies of its text.

Source

pub fn parent(self) -> Option<Self>

The enclosing node; None for the root.

Source

pub fn first_child(self) -> Option<Self>

The first child; None for a token or an empty node.

Source

pub fn last_child(self) -> Option<Self>

The last child. O(depth).

Source

pub fn next_sibling(self) -> Option<Self>

The next sibling.

Source

pub fn prev_sibling(self) -> Option<Self>

The previous sibling. O(depth).

Source

pub fn children(self) -> Children<'t> ⓘ

The children, in order.

Source

pub fn ancestors(self) -> Ancestors<'t> ⓘ

The enclosing nodes, innermost first.

Source

pub fn descendants(self) -> Descendants<'t> ⓘ

This element and everything under it, in preorder (= source order).

Source

pub fn walk(self) -> Walk<'t> ⓘ

A preorder walk of this subtree that also reports leaving each element (tokens are entered and left at once).

Source

pub fn diagnostic(self) -> Option<Cow<'t, Diagnostic>>

The diagnostic of an SyntaxKind::Error node (in an edited tree, with its spans moved to the edited text). Past max_diagnostics it is a skeleton, without texts (see SyntaxTree::suppressed_diagnostics).

Source

pub fn anchor(self) -> Option<Anchor<'t>>

A YAML node’s anchor (§6.9.2), read as the event stream reads it.

Source

pub fn tag(self) -> Option<Tag<'t>>

A YAML node’s tag, resolved as the event stream resolves it (%TAG prefix and %XX escapes, §6.9.1).

Cost: O(prefix + suffix) per call; the resolved text copies the %TAG prefix, which may be long. To read many tags, cache by TagSource::key (Node::tag_source) and resolve each distinct tag once.

Source

pub fn tag_source(self) -> Option<TagSource<'t>>

A YAML node’s tag as written, not yet resolved: O(the property’s text), whatever the %TAG prefix’s length. Its span is in the edited text, as Node::tag’s.

Source

pub fn block_header(self) -> Option<BlockHeader>

A block scalar’s header as the event carried it (C5).

Source

pub fn value(self) -> Option<Cow<'t, str>>

A scalar node’s value after escapes, folding, and chomping, decoded by the event stream’s own decoder. Borrowed from the source when it is a slice of it.

Source

pub fn alias_name(self) -> Option<&'t str>

An alias node’s anchor name, without *.

Trait Implementations§

Source§

impl<'t> Clone for Node<'t>

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> Copy for Node<'t>

Source§

impl Debug for Node<'_>

Source§

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

Kind@start..end (not the subtree: see SyntaxTree’s Debug).

Source§

impl Eq for Node<'_>

Source§

impl Hash for Node<'_>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Node<'_>

Source§

fn eq(&self, other: &Self) -> bool

The same element of the same tree instance.

1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<'t> Freeze for Node<'t>

§

impl<'t> RefUnwindSafe for Node<'t>

§

impl<'t> Send for Node<'t>

§

impl<'t> Sync for Node<'t>

§

impl<'t> Unpin for Node<'t>

§

impl<'t> UnsafeUnpin for Node<'t>

§

impl<'t> UnwindSafe for Node<'t>

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.