Skip to main content

aldrin_parser/ast/
attribute.rs

1use super::Ident;
2use crate::Span;
3use crate::grammar::Rule;
4use pest::iterators::Pair;
5
6#[derive(Debug, Clone)]
7pub struct Attribute {
8    span: Span,
9    name: Ident,
10    options: Vec<Ident>,
11}
12
13impl Attribute {
14    pub(crate) fn parse(pair: Pair<Rule>) -> Self {
15        assert_eq!(pair.as_rule(), Rule::attribute);
16        Self::parse_impl(pair)
17    }
18
19    pub(crate) fn parse_inline(pair: Pair<Rule>) -> Self {
20        assert_eq!(pair.as_rule(), Rule::attribute_inline);
21        Self::parse_impl(pair)
22    }
23
24    fn parse_impl(pair: Pair<Rule>) -> Self {
25        let span = Span::from_pair(&pair);
26
27        let mut pairs = pair.into_inner();
28        pairs.next().unwrap(); // Skip [.
29
30        let pair = pairs.next().unwrap();
31        let name = Ident::parse(&pair);
32
33        let mut att = Self {
34            span,
35            name,
36            options: Vec::new(),
37        };
38
39        for pair in pairs {
40            #[expect(clippy::wildcard_enum_match_arm)]
41            match pair.as_rule() {
42                Rule::ident => att.options.push(Ident::parse(&pair)),
43
44                Rule::tok_par_open
45                | Rule::tok_par_close
46                | Rule::tok_comma
47                | Rule::tok_squ_close => {}
48
49                _ => unreachable!(),
50            }
51        }
52
53        att
54    }
55
56    pub fn span(&self) -> Span {
57        self.span
58    }
59
60    pub fn name(&self) -> &Ident {
61        &self.name
62    }
63
64    pub fn options(&self) -> &[Ident] {
65        &self.options
66    }
67}