cairo_lang_syntax/attribute/
structured.rs

1use std::fmt;
2
3use cairo_lang_debug::DebugWithDb;
4use smol_str::SmolStr;
5
6use crate::node::db::SyntaxGroup;
7use crate::node::{Terminal, TypedSyntaxNode, ast};
8
9/// Easier to digest representation of an [ast::Attribute].
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub struct Attribute {
12    pub stable_ptr: ast::AttributePtr,
13    pub id: SmolStr,
14    pub id_stable_ptr: ast::ExprPathPtr,
15    pub args: Vec<AttributeArg>,
16    pub args_stable_ptr: ast::OptionArgListParenthesizedPtr,
17}
18impl Attribute {
19    /// Checks if the given attribute has a single argument with the given name.
20    pub fn is_single_unnamed_arg(&self, db: &dyn SyntaxGroup, arg_name: &str) -> bool {
21        match &self.args[..] {
22            [arg] => match &arg.variant {
23                AttributeArgVariant::Unnamed(value) => {
24                    value.as_syntax_node().get_text_without_trivia(db) == arg_name
25                }
26                _ => false,
27            },
28            _ => false,
29        }
30    }
31}
32
33/// Easier to digest representation of a single attribute value.
34#[derive(Clone, Debug, PartialEq, Eq)]
35pub struct AttributeArg {
36    pub variant: AttributeArgVariant,
37    pub arg: ast::Arg,
38    pub modifiers: Vec<Modifier>,
39}
40
41/// Variant of [`AttributeArg`].
42#[derive(Clone, Debug, PartialEq, Eq)]
43pub enum AttributeArgVariant {
44    /// Just `value`.
45    Unnamed(ast::Expr),
46    /// `name: value`.
47    Named { value: ast::Expr, name: NameInfo },
48    /// `:name`
49    FieldInitShorthand(NameInfo),
50}
51
52#[derive(Clone, Debug, PartialEq, Eq)]
53/// The data on a name part of an argument.
54pub struct NameInfo {
55    /// The name of the argument.
56    pub text: SmolStr,
57    /// The stable pointer to the name.
58    pub stable_ptr: ast::TerminalIdentifierPtr,
59}
60impl NameInfo {
61    fn from_ast(name: ast::TerminalIdentifier, db: &dyn SyntaxGroup) -> Self {
62        NameInfo { text: name.text(db), stable_ptr: name.stable_ptr(db) }
63    }
64}
65
66/// Easier to digest representation of a [`ast::Modifier`] attached to [`AttributeArg`].
67#[derive(Clone, Debug, PartialEq, Eq)]
68pub struct Modifier {
69    pub text: SmolStr,
70    pub stable_ptr: ast::ModifierPtr,
71}
72
73impl<'a> DebugWithDb<dyn SyntaxGroup + 'a> for Attribute {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>, db: &(dyn SyntaxGroup + 'a)) -> fmt::Result {
75        write!(f, r#"Attribute {{ id: "{}""#, self.id)?;
76        if !self.args.is_empty() {
77            write!(f, ", args: [")?;
78            for arg in &self.args {
79                write!(f, "{:?}, ", arg.arg.as_syntax_node().get_text(db))?;
80            }
81            write!(f, "]")?;
82        }
83        write!(f, " }}")
84    }
85}
86
87pub trait AttributeStructurize {
88    /// Return the structured attribute for the given [ast::Attribute].
89    fn structurize(self, db: &dyn SyntaxGroup) -> Attribute;
90}
91
92impl AttributeStructurize for ast::Attribute {
93    fn structurize(self, db: &dyn SyntaxGroup) -> Attribute {
94        let attr_id = self.attr(db);
95        let attr_args = self.arguments(db);
96
97        Attribute {
98            stable_ptr: self.stable_ptr(db),
99            id: attr_id.as_syntax_node().get_text_without_trivia(db).into(),
100            id_stable_ptr: attr_id.stable_ptr(db),
101            args: match attr_args {
102                ast::OptionArgListParenthesized::ArgListParenthesized(ref attribute_args) => {
103                    attribute_args
104                        .arguments(db)
105                        .elements(db)
106                        .map(|arg| AttributeArg::from_ast(arg, db))
107                        .collect()
108                }
109                ast::OptionArgListParenthesized::Empty(_) => vec![],
110            },
111            args_stable_ptr: attr_args.stable_ptr(db),
112        }
113    }
114}
115
116pub trait AttributeListStructurize {
117    /// Return structured attributes for the given [ast::AttributeList].
118    fn structurize(self, db: &dyn SyntaxGroup) -> Vec<Attribute>;
119}
120
121impl AttributeListStructurize for ast::AttributeList {
122    fn structurize(self, db: &dyn SyntaxGroup) -> Vec<Attribute> {
123        // TODO(ilya): Consider checking for attribute repetitions.
124        self.elements(db).map(|attr| attr.structurize(db)).collect()
125    }
126}
127
128impl AttributeArg {
129    /// Build [`AttributeArg`] from [`ast::Arg`].
130    pub fn from_ast(arg: ast::Arg, db: &dyn SyntaxGroup) -> AttributeArg {
131        let variant = match arg.arg_clause(db) {
132            ast::ArgClause::Unnamed(clause) => AttributeArgVariant::Unnamed(clause.value(db)),
133            ast::ArgClause::Named(clause) => AttributeArgVariant::Named {
134                value: clause.value(db),
135                name: NameInfo::from_ast(clause.name(db), db),
136            },
137            ast::ArgClause::FieldInitShorthand(clause) => AttributeArgVariant::FieldInitShorthand(
138                NameInfo::from_ast(clause.name(db).name(db), db),
139            ),
140        };
141
142        let modifiers =
143            arg.modifiers(db).elements(db).map(|modifier| Modifier::from(modifier, db)).collect();
144
145        AttributeArg { variant, arg, modifiers }
146    }
147
148    pub fn text(&self, db: &dyn SyntaxGroup) -> String {
149        match &self.variant {
150            AttributeArgVariant::Unnamed(value) => {
151                value.as_syntax_node().get_text_without_trivia(db)
152            }
153            AttributeArgVariant::Named { value, name } => {
154                format!("{}: {}", name.text, value.as_syntax_node().get_text_without_trivia(db))
155            }
156            AttributeArgVariant::FieldInitShorthand(name) => {
157                format!(":{}", name.text)
158            }
159        }
160    }
161}
162
163impl Modifier {
164    /// Build [`Modifier`] from [`ast::Modifier`].
165    fn from(modifier: ast::Modifier, db: &dyn SyntaxGroup) -> Modifier {
166        Modifier {
167            stable_ptr: modifier.stable_ptr(db),
168            text: modifier.as_syntax_node().get_text(db).into(),
169        }
170    }
171}