aldrin_parser/ast/
attribute.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use super::Ident;
use crate::grammar::Rule;
use crate::Span;
use pest::iterators::Pair;

#[derive(Debug, Clone)]
pub struct Attribute {
    span: Span,
    name: Ident,
    options: Vec<Ident>,
}

impl Attribute {
    pub(crate) fn parse(pair: Pair<Rule>) -> Self {
        assert_eq!(pair.as_rule(), Rule::attribute);

        let span = Span::from_pair(&pair);

        let mut pairs = pair.into_inner();
        pairs.next().unwrap(); // Skip #.
        pairs.next().unwrap(); // Skip [.

        let pair = pairs.next().unwrap();
        let name = Ident::parse(pair);

        let mut att = Attribute {
            span,
            name,
            options: Vec::new(),
        };

        for pair in pairs {
            match pair.as_rule() {
                Rule::ident => att.options.push(Ident::parse(pair)),
                Rule::tok_par_open
                | Rule::tok_par_close
                | Rule::tok_comma
                | Rule::tok_squ_close => {}
                _ => unreachable!(),
            }
        }

        att
    }

    pub fn span(&self) -> Span {
        self.span
    }

    pub fn name(&self) -> &Ident {
        &self.name
    }

    pub fn options(&self) -> &[Ident] {
        &self.options
    }
}