cairo_lang_syntax/node/
ids.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use std::sync::Arc;

use cairo_lang_filesystem::ids::FileId;
use cairo_lang_filesystem::span::TextWidth;
use cairo_lang_utils::{LookupIntern, define_short_id};

use super::SyntaxNode;
use super::db::SyntaxGroup;
use super::green::GreenNode;
use super::kind::SyntaxKind;
use crate::node::stable_ptr::SyntaxStablePtr;

define_short_id!(GreenId, Arc::<GreenNode>, SyntaxGroup, lookup_intern_green, intern_green);
impl GreenId {
    /// Returns the width of the node of this green id.
    pub fn width(&self, db: &dyn SyntaxGroup) -> TextWidth {
        match &self.lookup_intern(db).details {
            super::green::GreenNodeDetails::Token(text) => TextWidth::from_str(text),
            super::green::GreenNodeDetails::Node { width, .. } => *width,
        }
    }
}

define_short_id!(
    SyntaxStablePtrId,
    SyntaxStablePtr,
    SyntaxGroup,
    lookup_intern_stable_ptr,
    intern_stable_ptr
);
impl SyntaxStablePtrId {
    /// Lookups a syntax node using a stable syntax pointer.
    /// Should only be called on the root from which the stable pointer was generated.
    pub fn lookup(&self, db: &dyn SyntaxGroup) -> SyntaxNode {
        let ptr = self.lookup_intern(db);
        match ptr {
            SyntaxStablePtr::Root(file_id, green) => SyntaxNode::new_root(db, file_id, green),
            SyntaxStablePtr::Child { parent, .. } => {
                let parent = parent.lookup(db);
                for child in db.get_children(parent).iter() {
                    if child.stable_ptr() == *self {
                        return child.clone();
                    }
                }
                unreachable!();
            }
        }
    }
    pub fn file_id(&self, db: &dyn SyntaxGroup) -> FileId {
        let ptr = self.lookup_intern(db);
        match ptr {
            SyntaxStablePtr::Root(file_id, _) => file_id,
            SyntaxStablePtr::Child { parent, .. } => parent.file_id(db),
        }
    }
    /// Returns the stable pointer of the parent of this stable pointer.
    /// Assumes that the parent exists (that is, `self` is not the root). Panics otherwise.
    pub fn parent(&self, db: &dyn SyntaxGroup) -> SyntaxStablePtrId {
        let SyntaxStablePtr::Child { parent, .. } = self.lookup_intern(db) else { panic!() };
        parent
    }
    /// Returns the stable pointer of the `n`th parent of this stable pointer.
    /// n = 0: returns itself.
    /// n = 1: return the parent.
    /// n = 2: return the grand parent.
    /// And so on...
    /// Assumes that the `n`th parent exists. Panics otherwise.
    pub fn nth_parent(&self, db: &dyn SyntaxGroup, n: usize) -> SyntaxStablePtrId {
        let mut ptr = *self;
        for _ in 0..n {
            ptr = ptr.parent(db);
        }
        ptr
    }
    /// Returns the kind of this stable pointer.
    /// Assumes that `self` is not the root. Panics otherwise.
    pub fn kind(&self, db: &dyn SyntaxGroup) -> SyntaxKind {
        let SyntaxStablePtr::Child { kind, .. } = self.lookup_intern(db) else { panic!() };
        kind
    }
}