Struct cstree::syntax::SyntaxNode

source ·
#[repr(transparent)]
pub struct SyntaxNode<S: Syntax, D: 'static = ()> { /* private fields */ }
Expand description

Inner syntax tree node. Syntax nodes can be shared between threads. Every syntax tree is reference counted as a whole and nodes are pointer-sized, so copying individual nodes is relatively cheap.

Implementations§

source§

impl<S: Syntax, D> SyntaxNode<S, D>

source

pub fn write_debug<R>( &self, resolver: &R, target: &mut impl Write, recursive: bool ) -> Resultwhere R: Resolver<TokenKey> + ?Sized,

Writes this node’s Debug representation into the given target. If recursive is true, prints the entire subtree rooted in this node. Otherwise, only this node’s kind and range are written.

source

pub fn debug<R>(&self, resolver: &R, recursive: bool) -> Stringwhere R: Resolver<TokenKey> + ?Sized,

Returns this node’s Debug representation as a string. If recursive is true, prints the entire subtree rooted in this node. Otherwise, only this node’s kind and range are written.

To avoid allocating for every node, see write_debug.

source

pub fn write_display<R>(&self, resolver: &R, target: &mut impl Write) -> Resultwhere R: Resolver<TokenKey> + ?Sized,

Writes this node’s Display representation into the given target.

source

pub fn display<R>(&self, resolver: &R) -> Stringwhere R: Resolver<TokenKey> + ?Sized,

Returns this node’s Display representation as a string.

To avoid allocating for every node, see write_display.

source

pub fn resolver(&self) -> Option<&StdArc<dyn Resolver<TokenKey>>>

If there is a resolver associated with this tree, returns it.

source

pub fn try_resolved(&self) -> Option<&ResolvedNode<S, D>>

Turns this node into a ResolvedNode, but only if there is a resolver associated with this tree.

source

pub fn resolved(&self) -> &ResolvedNode<S, D>

Turns this node into a ResolvedNode.

Panics

If there is no resolver associated with this tree.

source§

impl<S: Syntax, D> SyntaxNode<S, D>

source

pub fn root(&self) -> &SyntaxNode<S, D>

The root of the tree this node belongs to.

If this node is the root, returns self.

source§

impl<S: Syntax, D> SyntaxNode<S, D>

source

pub fn new_root(green: GreenNode) -> Self

Build a new syntax tree on top of a green tree.

Example
let root: SyntaxNode<MySyntax> = SyntaxNode::new_root(green_root);
assert_eq!(root.kind(), Root);
source

pub fn new_root_with_resolver( green: GreenNode, resolver: impl Resolver<TokenKey> + 'static ) -> ResolvedNode<S, D>

Build a new syntax tree on top of a green tree and associate a resolver with the tree to resolve interned Strings.

Example
use cstree::syntax::ResolvedNode;

let mut builder: GreenNodeBuilder<MySyntax> = GreenNodeBuilder::new();
builder.start_node(Root);
builder.token(Identifier, "content");
builder.finish_node();
let (green, cache) = builder.finish();

// We are safe to use `unwrap` here because we created the builder with `new`.
// This created a new interner and cache for us owned by the builder,
// and `finish` always returns these.
let interner = cache.unwrap().into_interner().unwrap();
let root: ResolvedNode<MySyntax> = SyntaxNode::new_root_with_resolver(green, interner);
assert_eq!(root.text(), "content");
source

pub fn set_data(&self, data: D) -> Arc<D>

Stores custom data for this node. If there was previous data associated with this node, it will be replaced.

source

pub fn try_set_data(&self, data: D) -> Result<Arc<D>, D>

Stores custom data for this node, but only if no data was previously set. If it was, the given data is returned unchanged.

source

pub fn get_data(&self) -> Option<Arc<D>>

Returns the data associated with this node, if any.

source

pub fn clear_data(&self)

Removes the data associated with this node.

source

pub fn replace_with(&self, replacement: GreenNode) -> GreenNode

Returns a green tree, equal to the green tree this node belongs two, except with this node substitute. The complexity of operation is proportional to the depth of the tree

source

pub fn syntax_kind(&self) -> RawSyntaxKind

The internal representation of the kind of this node.

source

pub fn kind(&self) -> S

The kind of this node in terms of your language.

source

pub fn text_range(&self) -> TextRange

The range this node covers in the source text, in bytes.

source

pub fn resolve_text<'n, 'i, I>( &'n self, resolver: &'i I ) -> SyntaxText<'n, 'i, I, S, D>where I: Resolver<TokenKey> + ?Sized,

Uses the provided resolver to return an efficient representation of all source text covered by this node, i.e. the combined text of all token leafs of the subtree originating in this node.

source

pub fn green(&self) -> &GreenNode

Returns the unterlying green tree node of this node.

source

pub fn parent(&self) -> Option<&SyntaxNode<S, D>>

The parent node of this node, except if this node is the root.

source

pub fn arity(&self) -> usize

The number of child nodes (!) of this node.

If you want to also consider leafs, see arity_with_tokens.

source

pub fn arity_with_tokens(&self) -> usize

The number of children of this node.

source

pub fn ancestors(&self) -> impl Iterator<Item = &SyntaxNode<S, D>>

Returns an iterator along the chain of parents of this node.

source

pub fn children(&self) -> SyntaxNodeChildren<'_, S, D>

Returns an iterator over all nodes that are children of this node.

If you want to also consider leafs, see children_with_tokens.

source

pub fn children_with_tokens(&self) -> SyntaxElementChildren<'_, S, D>

Returns an iterator over child elements of this node, including tokens.

source

pub fn first_child(&self) -> Option<&SyntaxNode<S, D>>

The first child node of this node, if any.

If you want to also consider leafs, see first_child_or_token.

source

pub fn first_child_or_token(&self) -> Option<SyntaxElementRef<'_, S, D>>

The first child element of this node, if any, including tokens.

source

pub fn last_child(&self) -> Option<&SyntaxNode<S, D>>

The last child node of this node, if any.

If you want to also consider leafs, see last_child_or_token.

source

pub fn last_child_or_token(&self) -> Option<SyntaxElementRef<'_, S, D>>

The last child element of this node, if any, including tokens.

source

pub fn next_child_after( &self, n: usize, offset: TextSize ) -> Option<&SyntaxNode<S, D>>

The first child node of this node starting at the (n + 1)-st, if any. Note that even if this method returns Some, the contained node may not actually be the (n + 1)-st child, but the next child from there that is a node.

If you want to also consider leafs, see next_child_or_token_after.

source

pub fn next_child_or_token_after( &self, n: usize, offset: TextSize ) -> Option<SyntaxElementRef<'_, S, D>>

The first child element of this node starting at the (n + 1)-st, if any. If this method returns Some, the contained node is the (n + 1)-st child of this node.

source

pub fn prev_child_before( &self, n: usize, offset: TextSize ) -> Option<&SyntaxNode<S, D>>

The last child node of this node up to the nth, if any. Note that even if this method returns Some, the contained node may not actually be the (n - 1)-st child, but the previous child from there that is a node.

If you want to also consider leafs, see prev_child_or_token_before.

source

pub fn prev_child_or_token_before( &self, n: usize, offset: TextSize ) -> Option<SyntaxElementRef<'_, S, D>>

The last child node of this node up to the nth, if any. If this method returns Some, the contained node is the (n - 1)-st child.

source

pub fn next_sibling(&self) -> Option<&SyntaxNode<S, D>>

The node to the right of this one, i.e. the next child node (!) of this node’s parent after this node.

If you want to also consider leafs, see next_sibling_or_token.

source

pub fn next_sibling_or_token(&self) -> Option<SyntaxElementRef<'_, S, D>>

The tree element to the right of this one, i.e. the next child of this node’s parent after this node.

source

pub fn prev_sibling(&self) -> Option<&SyntaxNode<S, D>>

The node to the left of this one, i.e. the previous child node (!) of this node’s parent before this node.

If you want to also consider leafs, see prev_sibling_or_token.

source

pub fn prev_sibling_or_token(&self) -> Option<SyntaxElementRef<'_, S, D>>

The tree element to the left of this one, i.e. the previous child of this node’s parent before this node.

source

pub fn first_token(&self) -> Option<&SyntaxToken<S, D>>

Return the leftmost token in the subtree of this node

source

pub fn last_token(&self) -> Option<&SyntaxToken<S, D>>

Return the rightmost token in the subtree of this node

source

pub fn siblings( &self, direction: Direction ) -> impl Iterator<Item = &SyntaxNode<S, D>>

Returns an iterator over all sibling nodes of this node in the given direction, i.e. all of this node’s parent’s child nodes (!) from this node on to the left or the right. The first item in the iterator will always be this node.

If you want to also consider leafs, see siblings_with_tokens.

source

pub fn siblings_with_tokens( &self, direction: Direction ) -> impl Iterator<Item = SyntaxElementRef<'_, S, D>>

Returns an iterator over all siblings of this node in the given direction, i.e. all of this node’s parent’s children from this node on to the left or the right. The first item in the iterator will always be this node.

source

pub fn descendants(&self) -> impl Iterator<Item = &SyntaxNode<S, D>>

Returns an iterator over all nodes (!) in the subtree starting at this node, including this node.

If you want to also consider leafs, see descendants_with_tokens.

source

pub fn descendants_with_tokens( &self ) -> impl Iterator<Item = SyntaxElementRef<'_, S, D>>

Returns an iterator over all elements in the subtree starting at this node, including this node.

source

pub fn preorder(&self) -> impl Iterator<Item = WalkEvent<&SyntaxNode<S, D>>>

Traverse the subtree rooted at the current node (including the current node) in preorder, excluding tokens.

source

pub fn preorder_with_tokens( &self ) -> impl Iterator<Item = WalkEvent<SyntaxElementRef<'_, S, D>>>

Traverse the subtree rooted at the current node (including the current node) in preorder, including tokens.

source

pub fn token_at_offset( &self, offset: TextSize ) -> TokenAtOffset<SyntaxToken<S, D>>

Find a token in the subtree corresponding to this node, which covers the offset. Precondition: offset must be withing node’s range.

source

pub fn covering_element(&self, range: TextRange) -> SyntaxElementRef<'_, S, D>

Return the deepest node or token in the current subtree that fully contains the range. If the range is empty and is contained in two leaf nodes, either one can be returned. Precondition: range must be contained withing the current node

source§

impl<S, D> SyntaxNode<S, D>where S: Syntax,

source

pub fn as_serialize_with_data_with_resolver<'node>( &'node self, resolver: &'node impl Resolver<TokenKey> ) -> impl Serialize + 'nodewhere D: Serialize,

Return an anonymous object that can be used to serialize this node, including the data and by using an external resolver.

source

pub fn as_serialize_with_resolver<'node>( &'node self, resolver: &'node impl Resolver<TokenKey> ) -> impl Serialize + 'node

Return an anonymous object that can be used to serialize this node, which uses the given resolver instead of the resolver inside the tree.

Trait Implementations§

source§

impl<S: Syntax, D> Clone for SyntaxNode<S, D>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<S: Debug + Syntax, D: Debug + 'static> Debug for SyntaxNode<S, D>

source§

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

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

impl<S: Syntax, D> Drop for SyntaxNode<S, D>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<'a, S: Syntax, D> From<&'a SyntaxNode<S, D>> for SyntaxElementRef<'a, S, D>

source§

fn from(node: &'a SyntaxNode<S, D>) -> Self

Converts to this type from the input type.
source§

impl<S: Syntax, D> From<SyntaxNode<S, D>> for SyntaxElement<S, D>

source§

fn from(node: SyntaxNode<S, D>) -> SyntaxElement<S, D>

Converts to this type from the input type.
source§

impl<S: Syntax, D> Hash for SyntaxNode<S, D>

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<S: Syntax, D> PartialEq<SyntaxNode<S, D>> for SyntaxNode<S, D>

source§

fn eq(&self, other: &SyntaxNode<S, D>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<S: Syntax, D> Eq for SyntaxNode<S, D>

source§

impl<S: Syntax, D: 'static> Send for SyntaxNode<S, D>

source§

impl<S: Syntax, D: 'static> Sync for SyntaxNode<S, D>

Auto Trait Implementations§

§

impl<S, D = ()> !RefUnwindSafe for SyntaxNode<S, D>

§

impl<S, D> Unpin for SyntaxNode<S, D>

§

impl<S, D = ()> !UnwindSafe for SyntaxNode<S, D>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
§

impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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.