spl_frontend/ast/
error_container.rs

1use super::*;
2use crate::ErrorContainer;
3use crate::SplError;
4
5impl ErrorContainer for AstInfo {
6    fn errors(&self) -> Vec<SplError> {
7        self.errors.clone()
8    }
9}
10
11impl ErrorContainer for IntLiteral {
12    fn errors(&self) -> Vec<SplError> {
13        self.info.errors()
14    }
15}
16
17impl ErrorContainer for Identifier {
18    fn errors(&self) -> Vec<SplError> {
19        self.info.errors()
20    }
21}
22
23impl ErrorContainer for ArrayAccess {
24    fn errors(&self) -> Vec<SplError> {
25        let mut errors = self.info.errors();
26        errors.extend(self.array.errors());
27        if let Some(index) = &self.index {
28            errors.extend(index.errors().shift(index.offset))
29        }
30        errors
31    }
32}
33
34impl ErrorContainer for Variable {
35    fn errors(&self) -> Vec<SplError> {
36        match self {
37            Self::ArrayAccess(a) => a.errors(),
38            Self::NamedVariable(n) => n.errors(),
39        }
40    }
41}
42
43impl ErrorContainer for BinaryExpression {
44    fn errors(&self) -> Vec<SplError> {
45        let mut errors = self.info.errors();
46        errors.extend(self.lhs.errors());
47        errors.extend(self.rhs.errors());
48        errors
49    }
50}
51
52impl ErrorContainer for BracketedExpression {
53    fn errors(&self) -> Vec<SplError> {
54        let mut errors = self.info.errors();
55        errors.extend(self.expr.errors());
56        errors
57    }
58}
59
60impl ErrorContainer for UnaryExpression {
61    fn errors(&self) -> Vec<SplError> {
62        let mut errors = self.info.errors();
63        errors.extend(self.expr.errors());
64        errors
65    }
66}
67
68impl ErrorContainer for Expression {
69    fn errors(&self) -> Vec<SplError> {
70        match self {
71            Self::Binary(b) => b.errors(),
72            Self::Bracketed(b) => b.errors(),
73            Self::Error(info) => info.errors(),
74            Self::IntLiteral(i) => i.errors(),
75            Self::Variable(v) => v.errors(),
76            Self::Unary(u) => u.errors(),
77        }
78    }
79}
80
81impl ErrorContainer for TypeDeclaration {
82    fn errors(&self) -> Vec<SplError> {
83        let mut errors = self.info.errors();
84        if let Some(name) = &self.name {
85            errors.extend(name.errors());
86        }
87        if let Some(type_expr) = &self.type_expr {
88            errors.extend(type_expr.errors().shift(type_expr.offset));
89        }
90        errors
91    }
92}
93
94impl ErrorContainer for TypeExpression {
95    fn errors(&self) -> Vec<SplError> {
96        match self {
97            Self::NamedType(n) => n.errors(),
98            Self::ArrayType {
99                base_type, info, ..
100            } => {
101                let mut errors = info.errors();
102                if let Some(base_type) = base_type {
103                    errors.extend(base_type.errors().shift(base_type.offset));
104                }
105                errors
106            }
107        }
108    }
109}
110
111impl ErrorContainer for VariableDeclaration {
112    fn errors(&self) -> Vec<SplError> {
113        match self {
114            Self::Error(info) => info.errors(),
115            Self::Valid {
116                name,
117                type_expr,
118                info,
119                ..
120            } => {
121                let mut errors = info.errors();
122                if let Some(name) = name {
123                    errors.extend(name.errors());
124                }
125                if let Some(type_expr) = type_expr {
126                    errors.extend(type_expr.errors().shift(type_expr.offset));
127                }
128                errors
129            }
130        }
131    }
132}
133
134impl ErrorContainer for ParameterDeclaration {
135    fn errors(&self) -> Vec<SplError> {
136        match self {
137            Self::Error(info) => info.errors(),
138            Self::Valid {
139                name,
140                type_expr,
141                info,
142                ..
143            } => {
144                let mut errors = info.errors();
145                if let Some(name) = name {
146                    errors.extend(name.errors());
147                }
148                if let Some(type_expr) = type_expr {
149                    errors.extend(type_expr.errors().shift(type_expr.offset));
150                }
151                errors
152            }
153        }
154    }
155}
156
157impl ErrorContainer for CallStatement {
158    fn errors(&self) -> Vec<SplError> {
159        let mut errors = self.info.errors();
160        errors.extend(self.name.errors());
161        errors.extend(
162            self.arguments
163                .iter()
164                .flat_map(|arg| arg.errors().shift(arg.offset)),
165        );
166        errors
167    }
168}
169
170impl ErrorContainer for Assignment {
171    fn errors(&self) -> Vec<SplError> {
172        let mut errors = self.info.errors();
173        errors.extend(self.variable.errors());
174        if let Some(expr) = &self.expr {
175            errors.extend(expr.errors().shift(expr.offset));
176        }
177        errors
178    }
179}
180
181impl ErrorContainer for IfStatement {
182    fn errors(&self) -> Vec<SplError> {
183        let mut errors = self.info.errors();
184        if let Some(expr) = &self.condition {
185            errors.extend(expr.errors().shift(expr.offset));
186        }
187        if let Some(stmt) = &self.if_branch {
188            errors.extend(stmt.errors().shift(stmt.offset));
189        }
190        if let Some(stmt) = &self.else_branch {
191            errors.extend(stmt.errors().shift(stmt.offset));
192        }
193        errors
194    }
195}
196
197impl ErrorContainer for WhileStatement {
198    fn errors(&self) -> Vec<SplError> {
199        let mut errors = self.info.errors();
200        if let Some(expr) = &self.condition {
201            errors.extend(expr.errors().shift(expr.offset));
202        }
203        if let Some(stmt) = &self.statement {
204            errors.extend(stmt.errors().shift(stmt.offset));
205        }
206        errors
207    }
208}
209
210impl ErrorContainer for BlockStatement {
211    fn errors(&self) -> Vec<SplError> {
212        let mut errors = self.info.errors();
213        errors.extend(
214            self.statements
215                .iter()
216                .flat_map(|stmt| stmt.errors().shift(stmt.offset)),
217        );
218        errors
219    }
220}
221
222impl ErrorContainer for Statement {
223    fn errors(&self) -> Vec<SplError> {
224        match self {
225            Self::Empty(info) => info.errors(),
226            Self::Error(info) => info.errors(),
227            Self::Assignment(a) => a.errors(),
228            Self::Block(b) => b.errors(),
229            Self::Call(c) => c.errors(),
230            Self::If(i) => i.errors(),
231            Self::While(w) => w.errors(),
232        }
233    }
234}
235
236impl ErrorContainer for ProcedureDeclaration {
237    fn errors(&self) -> Vec<SplError> {
238        let mut errors = self.info.errors();
239        if let Some(name) = &self.name {
240            errors.extend(name.errors());
241        }
242        errors.extend(
243            self.parameters
244                .iter()
245                .flat_map(|stmt| stmt.errors().shift(stmt.offset)),
246        );
247        errors.extend(
248            self.variable_declarations
249                .iter()
250                .flat_map(|stmt| stmt.errors().shift(stmt.offset)),
251        );
252        errors.extend(
253            self.statements
254                .iter()
255                .flat_map(|stmt| stmt.errors().shift(stmt.offset)),
256        );
257        errors
258    }
259}
260
261impl ErrorContainer for GlobalDeclaration {
262    fn errors(&self) -> Vec<SplError> {
263        match self {
264            Self::Type(t) => t.errors(),
265            Self::Procedure(p) => p.errors(),
266            Self::Error(info) => info.errors(),
267        }
268    }
269}
270
271impl ErrorContainer for Program {
272    fn errors(&self) -> Vec<SplError> {
273        let mut errors = self.info.errors();
274        errors.extend(
275            self.global_declarations
276                .iter()
277                .flat_map(|gd| gd.errors().shift(gd.offset)),
278        );
279        errors
280    }
281}