cairo_lang_syntax/node/
stable_ptr.rs

1use cairo_lang_filesystem::ids::FileId;
2
3use super::ids::{GreenId, SyntaxStablePtrId};
4use super::kind::SyntaxKind;
5
6/// Stable pointer to a node in the syntax tree.
7///
8/// Has enough information to uniquely define a node in the AST, given the tree.
9/// Has undefined behavior when used with the wrong tree.
10/// This is not a real pointer in the low-level sense, just a representation of the path from the
11/// root to the node.
12/// Stable means that when the AST is changed, pointers of unchanged items tend to stay the same.
13/// For example, if a function is changed, the pointer of an unrelated function in the AST should
14/// remain the same, as much as possible.
15#[derive(Clone, Debug, Hash, PartialEq, Eq)]
16pub enum SyntaxStablePtr {
17    /// The root node of the tree.
18    Root(FileId, GreenId),
19    /// A child node.
20    Child {
21        /// The parent of the node.
22        parent: SyntaxStablePtrId,
23        /// The SyntaxKind of the node.
24        kind: SyntaxKind,
25        /// A list of field values for this node, to index by.
26        /// Which fields are used is determined by each SyntaxKind.
27        /// For example, a function item might use the name of the function.
28        key_fields: Vec<GreenId>,
29        /// Chronological index among all nodes with the same (parent, kind, key_fields).
30        index: usize,
31    },
32}