cairo_lang_syntax/node/
green.rs

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