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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use super::{super::combinator::*, *};
use crate::ast::*;

/// 169 aggregate_initializer = `[` \[ [element] { `,` [element] } \] `]` .
pub fn aggregate_initializer(input: &str) -> ParseResult<Expression> {
    tuple((
        char('['),
        opt(comma_separated(element)).map(|opt| opt.unwrap_or_default()),
        char(']'),
    ))
    .map(|(_open, elements, _close)| Expression::AggregateInitializer { elements })
    .parse(input)
}

/// 203 element = [expression] \[ `:` [repetition] \] .
pub fn element(input: &str) -> ParseResult<Element> {
    tuple((expression, opt(tuple((char(':'), repetition)))))
        .map(|(expr, opt)| Element {
            expr,
            repetition: opt.map(|(_comma, r)| r),
        })
        .parse(input)
}

/// 287 repetition = [numeric_expression] .
pub fn repetition(input: &str) -> ParseResult<Expression> {
    numeric_expression(input)
}

/// 262 numeric_expression = [simple_expression] .
pub fn numeric_expression(input: &str) -> ParseResult<Expression> {
    simple_expression(input)
}

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

    #[test]
    fn aggregate_initializer_empty() {
        let (res, (expr, _remarks)) = super::expression("[]").finish().unwrap();
        dbg!(expr);
        assert_eq!(res, "");
    }

    #[test]
    fn aggregate_initializer() {
        let (res, (expr, _remarks)) = super::expression("[1, 3, 6, 9*8, -12]").finish().unwrap();
        assert_eq!(res, "");
        assert_eq!(
            expr,
            Expression::AggregateInitializer {
                elements: vec![
                    Element {
                        expr: Expression::Literal(Literal::Real(1.0)),
                        repetition: None,
                    },
                    Element {
                        expr: Expression::Literal(Literal::Real(3.0)),
                        repetition: None,
                    },
                    Element {
                        expr: Expression::Literal(Literal::Real(6.0)),
                        repetition: None,
                    },
                    Element {
                        expr: Expression::Binary {
                            op: BinaryOperator::Mul,
                            arg1: Box::new(Expression::Literal(Literal::Real(9.0))),
                            arg2: Box::new(Expression::Literal(Literal::Real(8.0)))
                        },
                        repetition: None,
                    },
                    Element {
                        expr: Expression::Unary {
                            op: UnaryOperator::Minus,
                            arg: Box::new(Expression::Literal(Literal::Real(12.0))),
                        },
                        repetition: None,
                    },
                ]
            }
        );
    }
}