Skip to main content

Ast

Struct Ast 

Source
pub struct Ast { /* private fields */ }
Expand description

Arena-backed mutable AST used by transform-oriented code.

Public mutation APIs intentionally avoid exposing &mut Node so parent links and detached subtree tracking cannot be bypassed accidentally.

Implementations§

Source§

impl Ast

Source

pub fn new() -> Self

Create an empty AST with a math-mode root node.

Source

pub fn with_root_mode(mode: ContentMode) -> Self

Create an empty AST whose root uses the given content mode.

Source

pub fn from_syntax_root(node: &SyntaxNode) -> Self

Convert a parsed SyntaxNode tree into a mutable Ast.

Conversion preserves shape:

  • content argument variants are converted directly to the referenced node
  • single-node content is not wrapped in an implicit group
  • Delimiter::Control(&'static str) becomes Delimiter::Control with owned String data
  • SyntaxNode::Error is preserved losslessly as Node::Error; the resulting tree is structurally valid but semantically incomplete
§Panics

Panics if the converted tree violates AST invariants, such as an environment body that is not a group.

Source

pub fn to_syntax_root(&self) -> SyntaxNode

Convert this AST back into a SyntaxNode tree.

Source

pub fn root(&self) -> NodeId

Return the main root node of the AST.

Source

pub fn contains(&self, id: NodeId) -> bool

Check whether id still exists in the arena.

Source

pub fn kind(&self, id: NodeId) -> NodeKind

Return the lightweight node kind for id.

§Panics

Panics if id is invalid.

Source

pub fn node(&self, id: NodeId) -> &Node

Borrow the full node data for id.

§Panics

Panics if id is invalid.

Source

pub fn node_opt(&self, id: NodeId) -> Option<&Node>

Non-panicking node borrow; returns None if id is invalid.

Source

pub fn node_opt_mut(&mut self, id: NodeId) -> Option<&mut Node>

Non-panicking mutable node borrow; returns None if id is invalid.

Direct mutation through this helper must not change a node’s edges. Structural changes must keep using the edge-aware AST methods.

Source

pub fn is_detached_root(&self, id: NodeId) -> bool

Whether id is currently tracked as a detached root.

Source

pub fn subtree_contains_node(&self, root: NodeId, target: NodeId) -> bool

Whether target lies in the subtree rooted at root, inclusive.

Source

pub fn parent(&self, id: NodeId) -> Option<ParentLink>

Return the parent link for id, if the node is attached.

Root, detached roots, and invalid or removed IDs return None. Callers that need to distinguish a valid detached root from an invalid ID should check Ast::contains first.

Source

pub fn parent_id(&self, id: NodeId) -> Option<NodeId>

Return the parent node ID for id, if attached.

See Ast::parent for the None cases.

Source

pub fn slot(&self, id: NodeId) -> Option<Slot>

Return the slot occupied by id in its parent, if attached.

See Ast::parent for the None cases.

Source

pub fn children(&self, id: NodeId) -> &[NodeId]

Return the direct children of a root/group node.

Other node kinds return an empty slice.

§Panics

Panics if id is invalid.

Source

pub fn arg_slots(&self, id: NodeId) -> &[ArgumentSlot]

Return the argument slots owned by a command-like node.

Non-command-like nodes return an empty slice.

§Panics

Panics if id is invalid.

Source

pub fn edges(&self, id: NodeId) -> Vec<(NodeId, Slot)>

Return every direct tree edge of id as (child, slot) pairs.

Returned order matches the AST’s direct traversal order. Only Content argument entries are exposed as argument edges.

§Panics

Panics if id is invalid.

Source

pub fn clone_subtree(&mut self, id: NodeId) -> NodeId

Deep-copy the subtree rooted at id and return the copied detached root.

The cloned tree preserves node shape and scalar argument values, but every copied node receives a fresh NodeId. The returned root is detached, so callers can attach it with Ast::new_node, Ast::replace_node, or group insertion helpers.

§Panics

Panics if id is invalid or points at the main AST root.

Source

pub fn next_sibling(&self, id: NodeId) -> Option<NodeId>

Return the next sibling of id when it is attached as a group child.

Nodes in non-GroupChild slots and invalid or removed IDs return None.

Source

pub fn prev_sibling(&self, id: NodeId) -> Option<NodeId>

Return the previous sibling of id when it is attached as a group child.

Nodes in non-GroupChild slots and invalid or removed IDs return None.

Source

pub fn find<F>(&self, start: NodeId, predicate: F) -> Option<NodeId>
where F: Fn(&Node) -> bool,

Depth-first search starting at start, returning the first matching node.

§Panics

Panics if start is invalid.

Source

pub fn find_all<F>(&self, start: NodeId, predicate: F) -> Vec<NodeId>
where F: Fn(&Node) -> bool,

Collect all matching nodes reachable from start in depth-first order.

The returned vector is a snapshot of NodeIds at collection time. Later mutations may delete or move those nodes, so callers should use Ast::contains and, when necessary, re-check parent / slot state before mutating.

§Panics

Panics if start is invalid.

Source

pub fn is_char(&self, id: NodeId, expected: char) -> bool

Check whether id is a character node matching expected.

§Panics

Panics if id is invalid.

Source

pub fn subtree_contains_command(&self, start: NodeId, name: &str) -> bool

Check whether the subtree rooted at start contains a command named name.

§Panics

Panics if start is invalid.

Source

pub fn new_node(&mut self, node: Node) -> NodeId

Insert a new detached node into the arena and return its NodeId.

If node references child IDs, those children must already exist as detached roots. They are adopted by the new node immediately.

§Panics

These panics indicate a caller-side structural invariant violation. The AST is not guaranteed to be reusable after such a panic.

Panics if:

  • the same child is referenced more than once
  • a referenced child does not exist
  • a referenced child is not a detached root
  • adopting a child would introduce a cycle
  • an environment body is not a group
Source

pub fn append_child(&mut self, parent: NodeId, child: NodeId)

Append child to the end of a root/group child list.

child must currently be a detached root.

§Panics

Panics if:

  • parent is not a root/group node
  • child is invalid
  • child is already attached
  • child is not tracked as a detached root
  • attaching child would introduce a cycle
Source

pub fn insert_child(&mut self, parent: NodeId, index: usize, child: NodeId)

Insert child at index within a root/group child list.

child must currently be a detached root.

§Panics

Panics if:

  • parent is not a root/group node
  • index is out of bounds for Vec::insert
  • child is invalid
  • child is already attached
  • child is not tracked as a detached root
  • attaching child would introduce a cycle
Source

pub fn detach(&mut self, id: NodeId) -> NodeId

Detach id from its parent and return the same node ID.

This version only supports detaching nodes that occupy Slot::GroupChild. Detached nodes remain in the arena and become detached roots until they are reattached or explicitly removed with Ast::remove_detached.

§Panics

Panics if:

  • id is the main root
  • id is already detached
  • id is attached through a non-GroupChild slot
Source

pub fn replace_children( &mut self, parent: NodeId, children: Vec<NodeId>, ) -> Vec<NodeId>

Replace all direct children of a root/group node.

New children may be detached roots or existing direct children of the same parent. Removed children are preserved as detached roots.

Source

pub fn detach_children_range( &mut self, parent: NodeId, range: Range<usize>, ) -> Vec<NodeId>

Detach a contiguous range of direct children from a root/group node.

§Panics

Panics if parent is not a root/group node or range is out of bounds for Vec::drain.

Source

pub fn replace_content_child(&mut self, old: NodeId, replacement: NodeId)

Replace a content child or another single-child slot.

The replacement must be a detached root. The old child becomes detached.

Source

pub fn remove_node(&mut self, id: NodeId)

Remove an attached node and its entire subtree from the arena.

This is implemented as Ast::detach followed by Ast::remove_detached.

§Panics

Panics under the same conditions as Ast::detach or Ast::remove_detached.

Source

pub fn replace_node(&mut self, id: NodeId, new_node: Node) -> Node

Replace the node data stored at id while preserving the same NodeId.

The new node may:

  • reuse direct children already owned by id
  • adopt detached roots

Direct children removed by the replacement are not deleted. They become detached roots so transforms can keep or inspect them explicitly.

§Panics

These panics indicate a caller-side structural invariant violation. The AST is not guaranteed to be reusable after such a panic.

Panics if:

  • id is the main root or invalid
  • the new node references the same child more than once
  • the new node is a root variant
  • a reused child is not a direct child of id
  • a newly introduced child is not a detached root
  • adopting a child would introduce a cycle
  • an environment body is not a group
Source

pub fn append_cloned_math_content( &mut self, out: &mut Vec<NodeId>, source: NodeId, )

Appends cloned math content into out, flattening implicit math groups.

Parser-created content arguments often wrap multiple items in an implicit math group. Flattening that wrapper lets transforms compose output such as \partial f without introducing extra braces around f.

Source

pub fn implicit_math_group(&mut self, children: Vec<NodeId>) -> NodeId

Creates an implicit math group containing children.

Source

pub fn superscript(&mut self, base: NodeId, superscript: NodeId) -> NodeId

Creates a scripted node with only a superscript.

Source

pub fn replace_node_drop_detached_children( &mut self, id: NodeId, replacement: Node, )

Replaces id and removes any old child subtrees detached by the replacement.

Source

pub fn replace_with_math_sequence( &mut self, id: NodeId, before: Vec<NodeId>, replacement: NodeId, after: Vec<NodeId>, )

Replaces a node with a math-mode sequence.

Every node in before, replacement, and after must be a unique detached root. If id is a group child, before and after are inserted as real siblings around the replacement payload, and replacement is consumed into id. In single-child slots, the sequence is wrapped in an implicit math group because those slots cannot hold siblings.

Source

pub fn replace_with_math_sequence_preserving_scripts( &mut self, id: NodeId, before: Vec<NodeId>, first: NodeId, after: Vec<NodeId>, )

Replaces a node with a math-mode sequence, moving scripts from a parent Node::Scripted onto the final emitted node when id is the scripted base.

Every node in before, first, and after must be a unique detached root.

Source

pub fn remove_detached(&mut self, id: NodeId) -> Node

Destroy a detached subtree and return the removed root node value.

The subtree must already be detached from the main tree. All descendants are removed from the arena as well.

§Panics

Panics if:

  • id is the main root or invalid
  • id is still attached
  • id is not tracked as a detached root
Source

pub fn assert_invariants(&self)

Assert all structural invariants of the AST.

Checked conditions include:

  • the main root exists and has no parent
  • every parent link corresponds to a real direct edge
  • every node is reachable from either the root or a detached root
  • every parentless non-root node is tracked in detached roots
  • environment bodies are always groups

This method is intended for tests and debug-time validation.

§Panics

Panics with a descriptive message when any invariant is violated.

Trait Implementations§

Source§

impl Clone for Ast

Source§

fn clone(&self) -> Ast

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 Debug for Ast

Source§

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

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

impl Default for Ast

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Ast

§

impl RefUnwindSafe for Ast

§

impl Send for Ast

§

impl Sync for Ast

§

impl Unpin for Ast

§

impl UnsafeUnpin for Ast

§

impl UnwindSafe for Ast

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<'src, T> IntoMaybe<'src, T> for T
where T: 'src,

Source§

type Proj<U: 'src> = U

Source§

fn map_maybe<R>( self, _f: impl FnOnce(&'src T) -> &'src R, g: impl FnOnce(T) -> R, ) -> <T as IntoMaybe<'src, T>>::Proj<R>
where R: 'src,

Source§

impl<T> OrderedSeq<'_, T> for T
where T: Clone,

Source§

impl<'p, T> Seq<'p, T> for T
where T: Clone,

Source§

type Item<'a> = &'a T where T: 'a

The item yielded by the iterator.
Source§

type Iter<'a> = Once<&'a T> where T: 'a

An iterator over the items within this container, by reference.
Source§

fn seq_iter(&self) -> <T as Seq<'p, T>>::Iter<'_>

Iterate over the elements of the container.
Source§

fn contains(&self, val: &T) -> bool
where T: PartialEq,

Check whether an item is contained within this sequence.
Source§

fn to_maybe_ref<'b>(item: <T as Seq<'p, T>>::Item<'b>) -> Maybe<T, &'p T>
where 'p: 'b,

Convert an item of the sequence into a MaybeRef.
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 = 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 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.