[go: up one dir, main page]

Struct rowan::SyntaxNode

source ·
pub struct SyntaxNode<T: Types, R: TreeRoot<T>> { /* private fields */ }
Expand description

An immutable lazy constructed syntax tree with offsets and parent pointers.

The design is close to https://github.com/apple/swift/tree/bc3189a2d265bf7728ea0cfeb55f032bfe5beaf1/lib/Syntax

SyntaxNode exists in two flavors:

  • owned (R = OwnedRoot)
  • borrowed (R = RefRoot<’a, T>)

Borrowed SyntaxNode is Copy, but is parametrized over a lifetime, with a corresponding ergonomics hit.

Owned SyntaxNode is Clone (using Arc::clone under the hood) and is not parametrized over a lifetime. Note that because of the parent links SyntaxNode keeps all of its ancestors alive, and not only descendants, so keep an eye on memory leaks.

Methods like parent or children preserve the flavor (borrowed or owned) of nodes, but you can switch between them at any time using .borrowed() and .owned() methods. As a rule of thumb, when processing nodes, use borrowed version to avoid excessive Arc traffic, and, when storing nodes in data structures, use owned variant, to avoid dealing with lifetimes.

SyntaxNode have object identity equality and hash semantics.

Implementations

Creates a new SyntaxNode.

Text of this node if it is a leaf.

All ancestors of the current node, including itself

Traverse the subtree rooted at the current node in preorder.

Returns common ancestor of the two nodes. Precondition: nodes must be from the same tree.

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

Return the deepest node 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

Switch this node to borrowed flavor.

Switch this node to owned flavor.

Get root data.

Get kind of this node.

Get text range, covered by this node.

Get the parent node.

Get iterator over children.

Get first child.

Get last child.

Get next sibling.

Get previous sibling.

Returns true if this node is a leaf node.

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 TODO: naming is unfortunate, the return value is not current node, it is the new root node.

Get the green node for this node

Get the root node but with the children replaced. See replace_with.

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.