Skip to main content

cl_ast/desugar/
squash_groups.rs

1//! Squashes group expressions
2use crate::{ast::*, fold::*};
3
4/// Squashes group expressions
5pub struct SquashGroups;
6
7impl<A: AstTypes> Fold<A, A> for SquashGroups {
8    type Error = ();
9    impl_default_fold!(A, A);
10
11    fn fold_at_expr(&mut self, expr: At<Expr<A>, A>) -> Result<At<Expr<A>, A>, Self::Error> {
12        let expr = expr.children(self)?;
13
14        let At(Expr::Op(Op::Group, mut args), span) = expr else {
15            return Ok(expr);
16        };
17
18        if args.len() != 1 {
19            // TODO: should this be an error? Should we match on args.pop() instead?
20            return Ok(Expr::Op(Op::Group, args).at(span));
21        }
22
23        Ok(args.pop().unwrap())
24    }
25}