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
//! [Infix] Operators are used for creating an
//! infix operation.

// App imports
use crate::*;

#[derive(Clone)]
/// [Infix] operator used to create an infix
/// operation, e.g. `1+1`. This helper is useful
/// because it does not discard the `LHS` result
/// but rather returns it as a `None` value if the
/// `Operator` cannot be found.
pub enum Infix<LHS, Operator, RHS> {
    Found {
        lhs: LHS,
        operator: Operator,
        rhs: RHS,
    },
    None(LHS),
}

impl<LHS, Operator, RHS> ::std::fmt::Debug
    for Infix<LHS, Operator, RHS>
where
    LHS: ::std::fmt::Debug,
    RHS: ::std::fmt::Debug,
    Operator: ::std::fmt::Debug,
{
    fn fmt(
        &self,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        match self {
            Self::Found { lhs, operator, rhs } => f
                .debug_map()
                .entry(&"lhs", lhs)
                .entry(&"operator", operator)
                .entry(&"rhs", rhs)
                .finish(),
            Self::None(lhs) => std::fmt::Debug::fmt(lhs, f),
        }
    }
}

impl<'code, LHS, Operator, RHS> Lexer<'code>
    for Infix<LHS, Operator, RHS>
where
    LHS: Lexer<'code>,
    Operator: Lexer<'code>,
    RHS: Lexer<'code>,
{
    fn lex(
        buffer: &mut Buffer<'code>,
    ) -> Result<Self, Error<'code>> {
        // Clone local buffer
        let mut local_buffer = buffer.clone();

        // Count matched
        let matched = 0usize;

        // Get LHS and update count
        let lhs = <LHS as Lexer>::lex(&mut local_buffer)?;
        let matched = matched + 1;

        match <Operator as Lexer>::lex(&mut local_buffer) {
            Ok(operator) => {
                let matched = matched + 1;

                let rhs =
                    <RHS as Lexer>::lex(&mut local_buffer)
                        .map_err(|mut err| {
                            err.matched = matched;
                            err
                        })?;

                // Write to local buffer
                *buffer = local_buffer;

                Ok(Self::Found { lhs, operator, rhs })
            }
            Err(_none) => {
                // Write back to buffer
                *buffer = local_buffer;
                Ok(Self::None(lhs))
            }
        }
    }
}

impl<'code, LHS, Operator, RHS> Token<'code>
    for Infix<LHS, Operator, RHS>
where
    LHS: Lexer<'code>,
    Operator: Lexer<'code>,
    RHS: Lexer<'code>,
{
    fn buffer(&self) -> Option<Buffer<'code>> {
        match self {
            Infix::Found { lhs, rhs, operator } => {
                let buffers = [
                    <LHS as Token>::buffer(lhs),
                    <Operator as Token>::buffer(operator),
                    <RHS as Token>::buffer(rhs),
                ]
                .into_iter();

                buffers
                    .flatten()
                    .reduce(|acc, current| acc + current)
            }
            Infix::None(lhs) => <LHS as Token>::buffer(lhs),
        }
    }

    const NAME: &'static str = "Infix";
}

#[cfg(test)]
mod tests;