cairo_lang_syntax/node/
green.rs

1use cairo_lang_filesystem::ids::SmolStrId;
2use cairo_lang_filesystem::span::TextWidth;
3use cairo_lang_proc_macros::HeapSize;
4use salsa::Database;
5
6use super::ids::GreenId;
7use super::kind::SyntaxKind;
8
9#[derive(Clone, Debug, Hash, PartialEq, Eq, salsa::Update, HeapSize)]
10pub enum GreenNodeDetails<'a> {
11    Token(SmolStrId<'a>),
12    Node { children: Vec<GreenId<'a>>, width: TextWidth },
13}
14/// Green node. Underlying untyped representation of the syntax tree.
15#[derive(Clone, Debug, Hash, PartialEq, Eq, HeapSize)]
16pub struct GreenNode<'a> {
17    pub kind: SyntaxKind,
18    pub details: GreenNodeDetails<'a>,
19}
20impl<'a> GreenNode<'a> {
21    pub fn width(&self, db: &'a dyn Database) -> TextWidth {
22        match &self.details {
23            GreenNodeDetails::Token(text) => TextWidth::from_str(text.long(db)),
24            GreenNodeDetails::Node { width, .. } => *width,
25        }
26    }
27    pub fn children(&self) -> &[GreenId<'a>] {
28        match &self.details {
29            GreenNodeDetails::Token(_text) => &[],
30            GreenNodeDetails::Node { children, .. } => children,
31        }
32    }
33}