aldrin_parser/ast/
array_len.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use super::{LitPosInt, NamedRef};
use crate::error::{
    ConstIntNotFound, ExpectedConstIntFoundService, ExpectedConstIntFoundString,
    ExpectedConstIntFoundType, ExpectedConstIntFoundUuid, InvalidArrayLen,
};
use crate::grammar::Rule;
use crate::validate::Validate;
use crate::Span;
use pest::iterators::Pair;

#[derive(Debug, Clone)]
pub struct ArrayLen {
    span: Span,
    value: ArrayLenValue,
}

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

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

        let mut pairs = pair.into_inner();
        let pair = pairs.next().unwrap();
        let value = ArrayLenValue::parse(pair);

        Self { span, value }
    }

    pub(crate) fn validate(&self, validate: &mut Validate) {
        InvalidArrayLen::validate(self, validate);

        self.value.validate(validate);
    }

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

    pub fn value(&self) -> &ArrayLenValue {
        &self.value
    }
}

#[derive(Debug, Clone)]
pub enum ArrayLenValue {
    Literal(LitPosInt),
    Ref(NamedRef),
}

impl ArrayLenValue {
    fn parse(pair: Pair<Rule>) -> Self {
        match pair.as_rule() {
            Rule::lit_pos_int => Self::Literal(LitPosInt::parse(pair)),
            Rule::named_ref => Self::Ref(NamedRef::parse(pair)),
            _ => unreachable!(),
        }
    }

    fn validate(&self, validate: &mut Validate) {
        match self {
            Self::Literal(_) => {}

            Self::Ref(ty) => {
                ConstIntNotFound::validate(ty, validate);
                ExpectedConstIntFoundService::validate(ty, validate);
                ExpectedConstIntFoundString::validate(ty, validate);
                ExpectedConstIntFoundType::validate(ty, validate);
                ExpectedConstIntFoundUuid::validate(ty, validate);

                ty.validate(validate);
            }
        }
    }
}