cas_parser/parser/
garbage.rs

1use super::ast::{assign::AssignTarget, expr::Expr, literal::{LitSym, Literal}, paren::Paren};
2
3/// A trait for producing garbage values, useful for recovering from parsing errors.
4///
5/// We could've implemented [`Default`] on types instead, but garbage values are not useful to the
6/// end user, and we don't want to encourage its use due to [`Default`] being implemented.
7pub(crate) trait Garbage {
8    /// Produces a garbage value.
9    fn garbage() -> Self;
10}
11
12/// Implements [`Garbage`] for tuples.
13macro_rules! garbage_tuple {
14    ($($ty:ident),*) => {
15        impl<$($ty: Garbage),*> Garbage for ($($ty,)*) {
16            fn garbage() -> Self {
17                ($($ty::garbage(),)*)
18            }
19        }
20    };
21}
22
23garbage_tuple!(A, B);
24
25impl<T: Garbage, E> Garbage for Result<T, E> {
26    fn garbage() -> Self {
27        Ok(T::garbage())
28    }
29}
30
31impl<T: Garbage> Garbage for Option<T> {
32    fn garbage() -> Self {
33        Some(T::garbage())
34    }
35}
36
37impl Garbage for AssignTarget {
38    fn garbage() -> Self {
39        AssignTarget::Symbol(LitSym::garbage())
40    }
41}
42
43impl Garbage for Expr {
44    fn garbage() -> Self {
45        Expr::Literal(Literal::Symbol(LitSym::garbage()))
46    }
47}
48
49impl Garbage for LitSym {
50    fn garbage() -> Self {
51        Self { name: String::new(), span: 0..0 }
52    }
53}
54
55impl Garbage for Paren {
56    fn garbage() -> Self {
57        Self { expr: Box::new(Expr::garbage()), span: 0..0 }
58    }
59}