aldrin_parser/ast/
attribute.rs

1use super::Ident;
2use crate::grammar::Rule;
3use crate::Span;
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            match pair.as_rule() {
41                Rule::ident => att.options.push(Ident::parse(pair)),
42
43                Rule::tok_par_open
44                | Rule::tok_par_close
45                | Rule::tok_comma
46                | Rule::tok_squ_close => {}
47
48                _ => unreachable!(),
49            }
50        }
51
52        att
53    }
54
55    pub fn span(&self) -> Span {
56        self.span
57    }
58
59    pub fn name(&self) -> &Ident {
60        &self.name
61    }
62
63    pub fn options(&self) -> &[Ident] {
64        &self.options
65    }
66}