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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use super::rvalue::RValue;
use nom::{branch::alt, IResult};
use paste::paste;

/// [`BinaryOperatorResult`] represents result of a binary operator.
#[derive(Debug, Eq, PartialEq, Clone, Hash)]
pub struct BinaryOperatorResult {
    /// The operator.
    pub operator: String,
    /// The left hand side of the operator.
    pub lhs: Box<RValue>,
    /// The right hand side of the operator.
    pub rhs: Box<RValue>,
}

mod level3 {
    use super::*;
    use crate::{
        ast::expression::{
            rvalue::RValue,
            unary_operator::{self, higher_than_unary_operator_result},
        },
        utility::parsing,
    };
    use nom::{
        branch::alt, bytes::complete::tag, combinator::map, multi::fold_many0, sequence::pair,
        IResult,
    };

    pub fn higher_than_level3(code: &str) -> IResult<&str, RValue> {
        alt((
            map(unary_operator::parse, RValue::UnaryOperatorResult),
            higher_than_unary_operator_result,
        ))(code)
    }

    pub fn parse(code: &str) -> IResult<&str, BinaryOperatorResult> {
        let (rest, lhs) = higher_than_level3(code)?;
        let (rest, operator) = parsing::in_multispace(alt((tag("*"), tag("/"))))(rest)?;
        let (rest, rhs) = higher_than_level3(rest)?;
        fold_many0(
            pair(
                parsing::in_multispace(alt((tag("*"), tag("/")))),
                higher_than_level3,
            ),
            move || BinaryOperatorResult {
                operator: operator.to_string(),
                lhs: Box::new(lhs.clone()),
                rhs: Box::new(rhs.clone()),
            },
            |lhs, (operator, rhs)| BinaryOperatorResult {
                operator: operator.to_string(),
                lhs: Box::new(RValue::BinaryOperatorResult(lhs)),
                rhs: Box::new(rhs),
            },
        )(rest)
    }
}

/// A macro for generating binary operator parsers.
/// The `level` here are from C's operator precedence.
macro_rules! bin_op_level {
    ($n: expr, $n_minus_1: expr, $($op: expr)*) => {
        paste! {
        mod [<level $n>] {
            use super::*;
            use nom::{
                branch::alt, bytes::complete::tag, combinator::map, multi::fold_many0,
                sequence::pair, IResult,
            };
            use crate::{
                ast::expression::{rvalue::RValue, binary_operator::[<level $n_minus_1>]::{self, [<higher_than_level $n_minus_1>]}},
                utility::parsing,
            };

            pub(in crate::ast::expression) fn [<higher_than_level $n>](
                code: &str,
            ) -> IResult<&str, RValue> {
                alt((
                    map([<level $n_minus_1>]::parse, RValue::BinaryOperatorResult),
                    [<higher_than_level $n_minus_1>],
                ))(code)
            }

            pub fn parse(code: &str) -> IResult<&str, BinaryOperatorResult> {
                let (rest, lhs) = [<higher_than_level $n>](code)?;
                let (rest, operator) = parsing::in_multispace(alt(($(tag($op),)*)))(rest)?;
                let (rest, rhs) = [<higher_than_level $n>](rest)?;
                fold_many0(
                    pair(
                        parsing::in_multispace(alt(($(tag($op),)*))),
                        [<higher_than_level $n>],
                    ),
                    move || BinaryOperatorResult {
                        operator: operator.to_string(),
                        lhs: Box::new(lhs.clone()),
                        rhs: Box::new(rhs.clone()),
                    },
                    |lhs, (operator, rhs)| BinaryOperatorResult {
                        operator: operator.to_string(),
                        lhs: Box::new(RValue::BinaryOperatorResult(lhs)),
                        rhs: Box::new(rhs),
                    },
                )(rest)
            }
        }
    }
    };
}

bin_op_level!(4, 3, "+" "-");
bin_op_level!(5, 4, "<<" ">>");
bin_op_level!(6, 5, "<=" "<" ">=" ">");
bin_op_level!(7, 6, "==" "!=");
bin_op_level!(8, 7, "&" "&");
bin_op_level!(9, 8, "^" "^");
bin_op_level!(10, 9, "|" "|");

/// Parse source code to get a [`BinaryOperatorResult`].
pub fn parse(code: &str) -> IResult<&str, BinaryOperatorResult> {
    alt((
        level10::parse,
        level9::parse,
        level8::parse,
        level7::parse,
        level6::parse,
        level5::parse,
        level4::parse,
        level3::parse,
    ))(code)
}

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

    #[test]
    pub fn can_parse() {
        let bin_op = parse("s.a + s.b").unwrap().1;
        assert_eq!(bin_op.operator, "+");
        let bin_op = parse("a+b*c").unwrap().1;
        assert_eq!(bin_op.operator, "+");
        let bin_op = parse("b*c+d").unwrap().1;
        assert_eq!(bin_op.operator, "+");
        let bin_op = parse("!b+d").unwrap().1;
        assert_eq!(bin_op.operator, "+");
    }
}