aldrin_parser/ast/
const_def.rs

1use super::{Comment, DocString, Ident, LitInt, LitString, LitUuid, Prelude};
2use crate::error::{InvalidConstValue, InvalidEscapeCode};
3use crate::grammar::Rule;
4use crate::validate::Validate;
5use crate::warning::{BrokenDocLink, NonShoutySnakeCaseConst};
6use crate::Span;
7use pest::iterators::Pair;
8
9#[derive(Debug, Clone)]
10pub struct ConstDef {
11    span: Span,
12    comment: Vec<Comment>,
13    doc: Vec<DocString>,
14    name: Ident,
15    value_span: Span,
16    value: ConstValue,
17}
18
19impl ConstDef {
20    pub(crate) fn parse(pair: Pair<Rule>) -> Self {
21        assert_eq!(pair.as_rule(), Rule::const_def);
22
23        let span = Span::from_pair(&pair);
24        let mut pairs = pair.into_inner();
25        let mut prelude = Prelude::regular(&mut pairs);
26
27        pairs.next().unwrap(); // Skip keyword.
28
29        let name = Ident::parse(pairs.next().unwrap());
30
31        pairs.next().unwrap(); // Skip =.
32
33        let value_pair = pairs.next().unwrap();
34        let value_span = Span::from_pair(&value_pair);
35        let value = ConstValue::parse(value_pair);
36
37        Self {
38            span,
39            comment: prelude.take_comment(),
40            doc: prelude.take_doc(),
41            name,
42            value_span,
43            value,
44        }
45    }
46
47    pub(crate) fn validate(&self, validate: &mut Validate) {
48        BrokenDocLink::validate(&self.doc, validate);
49        InvalidEscapeCode::validate(self, validate);
50        NonShoutySnakeCaseConst::validate(self, validate);
51
52        self.name.validate(true, validate);
53        self.value.validate(validate);
54    }
55
56    pub fn span(&self) -> Span {
57        self.span
58    }
59
60    pub fn comment(&self) -> &[Comment] {
61        &self.comment
62    }
63
64    pub fn doc(&self) -> &[DocString] {
65        &self.doc
66    }
67
68    pub fn name(&self) -> &Ident {
69        &self.name
70    }
71
72    pub fn value_span(&self) -> Span {
73        self.value_span
74    }
75
76    pub fn value(&self) -> &ConstValue {
77        &self.value
78    }
79}
80
81#[derive(Debug, Clone)]
82pub enum ConstValue {
83    U8(LitInt),
84    I8(LitInt),
85    U16(LitInt),
86    I16(LitInt),
87    U32(LitInt),
88    I32(LitInt),
89    U64(LitInt),
90    I64(LitInt),
91    String(LitString),
92    Uuid(LitUuid),
93}
94
95impl ConstValue {
96    fn parse(pair: Pair<Rule>) -> Self {
97        assert_eq!(pair.as_rule(), Rule::const_value);
98        let mut pairs = pair.into_inner();
99        let pair = pairs.next().unwrap();
100        let rule = pair.as_rule();
101        pairs.next().unwrap(); // Skip (.
102        let pair = pairs.next().unwrap();
103
104        match rule {
105            Rule::kw_u8 => Self::U8(LitInt::parse(pair)),
106            Rule::kw_i8 => Self::I8(LitInt::parse(pair)),
107            Rule::kw_u16 => Self::U16(LitInt::parse(pair)),
108            Rule::kw_i16 => Self::I16(LitInt::parse(pair)),
109            Rule::kw_u32 => Self::U32(LitInt::parse(pair)),
110            Rule::kw_i32 => Self::I32(LitInt::parse(pair)),
111            Rule::kw_u64 => Self::U64(LitInt::parse(pair)),
112            Rule::kw_i64 => Self::I64(LitInt::parse(pair)),
113            Rule::kw_string => Self::String(LitString::parse(pair)),
114            Rule::kw_uuid => Self::Uuid(LitUuid::parse(pair)),
115            _ => unreachable!(),
116        }
117    }
118
119    fn validate(&self, validate: &mut Validate) {
120        InvalidConstValue::validate(self, validate);
121    }
122}