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
use super::Statement;
use crate::{ast::statement, utility::parsing};
use nom::{bytes::complete::tag, combinator::map, multi::many0, sequence::delimited, IResult};

/// [`Compound`] represents an group of statements wrapped in `{` and `}`
#[derive(Debug, Eq, PartialEq, Clone, Hash)]
pub struct Compound(pub Vec<Statement>);

/// Parse source code to get an [`Compound`].
pub fn parse(code: &str) -> IResult<&str, Compound> {
    map(
        delimited(
            tag("{"),
            many0(parsing::in_multispace(statement::parse)),
            tag("}"),
        ),
        Compound,
    )(code)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn can_parse() {
        let assign = parse(
            r#"{
            a = 1;
            b = 2;
        }"#,
        )
        .unwrap()
        .1;
        assert_eq!(assign.0.len(), 2);
        let assign = parse("{}").unwrap().1;
        assert_eq!(assign.0.len(), 0);
    }
}