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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use crate::core::cache::PCache;
use crate::core::context::ParserContext;
use crate::core::parser::Parser;
use crate::core::pos::Pos;
use crate::core::presult::PResult::{PErr, POk};
use crate::core::span::Span;
use crate::error::{err_combine, err_combine_opt, ParseError};

#[derive(Clone)]
pub enum PResult<O, E: ParseError> {
    POk(O, Pos, Pos, bool, Option<(E, Pos)>),
    PErr(E, Pos),
}

impl<O, E: ParseError> PResult<O, E> {
    #[inline(always)]
    pub fn new_empty(o: O, pos: Pos) -> Self {
        POk(o, pos, pos, true, None)
    }

    #[inline(always)]
    pub fn new_ok(o: O, start: Pos, end: Pos) -> Self {
        POk(o, start, end, false, None)
    }

    #[inline(always)]
    pub fn new_err(e: E, s: Pos) -> Self {
        PErr(e, s)
    }

    #[inline(always)]
    pub fn map<P>(self, f: impl FnOnce(O) -> P) -> PResult<P, E> {
        match self {
            POk(o, start, end, empty, e) => POk(f(o), start, end, empty, e),
            PErr(err, s) => PErr(err, s),
        }
    }

    #[inline(always)]
    pub fn map_with_span<P>(self, f: impl FnOnce(O, Span) -> P) -> PResult<P, E> {
        match self {
            POk(o, start, end, empty, e) => POk(f(o, start.span_to(end)), start, end, empty, e),
            PErr(err, s) => PErr(err, s),
        }
    }

    #[inline(always)]
    pub fn add_label_explicit(&mut self, l: E::L) {
        match self {
            POk(_, _, _, _, e) => {
                if let Some((e, _)) = e.as_mut() {
                    e.add_label_explicit(l);
                }
            }
            PErr(e, _) => {
                e.add_label_explicit(l);
            }
        }
    }

    #[inline(always)]
    pub fn add_label_implicit(&mut self, l: E::L) {
        match self {
            POk(_, _, _, _, e) => {
                if let Some((e, _)) = e.as_mut() {
                    e.add_label_implicit(l);
                }
            }
            PErr(e, _) => {
                e.add_label_implicit(l);
            }
        }
    }

    #[inline(always)]
    pub fn collapse(self) -> Result<O, E> {
        match self {
            POk(o, _, _, _, _) => Ok(o),
            PErr(e, _) => Err(e),
        }
    }

    #[inline(always)]
    pub fn is_ok(&self) -> bool {
        match self {
            POk(_, _, _, _, _) => true,
            PErr(_, _) => false,
        }
    }

    #[inline(always)]
    pub fn is_err(&self) -> bool {
        match self {
            POk(_, _, _, _, _) => false,
            PErr(_, _) => true,
        }
    }

    #[inline(always)]
    pub fn end_pos(&self) -> Pos {
        match self {
            POk(_, _, s, _, _) => *s,
            PErr(_, s) => *s,
        }
    }

    #[inline(always)]
    pub fn merge_choice(self, other: Self) -> Self {
        match (self, other) {
            // Left ok
            (ok @ POk(_, _, _, _, _), _) => ok,

            // Right ok
            (PErr(ne, ns), POk(s, start, end, empty, be)) => {
                POk(s, start, end, empty, err_combine_opt(Some((ne, ns)), be))
            }

            // If either parsed more input, prioritise that
            (PErr(e1, s1), PErr(e2, s2)) => {
                let (e, s) = err_combine((e1, s1), (e2, s2));
                PErr(e, s)
            }
        }
    }

    #[inline(always)]
    pub fn merge_seq<O2>(self, other: PResult<O2, E>) -> PResult<(O, O2), E> {
        match (self, other) {
            (POk(o1, start1, _, empty1, e1), POk(o2, start2, end2, empty2, e2)) => {
                // If the first result is empty and the second is not, we skip the first
                let start = match (empty1, empty2) {
                    (false, false) => start1,
                    (false, true) => start1,
                    (true, false) => start2,
                    (true, true) => start1,
                };
                POk(
                    (o1, o2),
                    start,
                    end2,
                    empty1 && empty2,
                    err_combine_opt(e1, e2),
                )
            }
            (POk(_, _, _, _, e1), PErr(e2, s2)) => {
                let (e, s) = err_combine_opt(e1, Some((e2, s2))).unwrap();
                PErr(e, s)
            }
            (err @ PErr(_, _), _) => err.map(|_| unreachable!()),
        }
    }

    #[inline(always)]
    pub fn merge_seq_opt<O2>(self, other: PResult<O2, E>) -> PResult<(O, Option<O2>), E> {
        match (self, other) {
            (r1 @ POk(_, _, _, _, _), r2 @ POk(_, _, _, _, _)) => {
                r1.merge_seq(r2).map(|(o1, o2)| (o1, Some(o2)))
            }
            (POk(o1, start, end, empty, e1), PErr(e2, s2)) => POk(
                (o1, None),
                start,
                end,
                empty,
                err_combine_opt(e1, Some((e2, s2))),
            ),
            (err @ PErr(_, _), _) => err.map(|_| unreachable!()),
        }
    }

    #[inline(always)]
    pub fn merge_choice_parser<'grm, 'arn, P: Parser<'arn, 'grm, O, E>>(
        self,
        other: &P,
        stream: Pos,
        cache: &mut PCache<'arn, 'grm, E>,
        context: &ParserContext,
    ) -> Self
    where
        'grm: 'arn,
    {
        //Quick out
        if self.is_ok() {
            return self;
        }

        self.merge_choice(other.parse(stream, cache, context))
    }

    #[inline(always)]
    pub fn merge_seq_parser<'grm, 'arn, O2, P2: Parser<'arn, 'grm, O2, E>>(
        self,
        other: &P2,
        cache: &mut PCache<'arn, 'grm, E>,
        context: &ParserContext,
    ) -> PResult<(O, O2), E>
    where
        'grm: 'arn,
    {
        //Quick out
        if self.is_err() {
            return self.map(|_| unreachable!());
        }

        let pos = self.end_pos();
        self.merge_seq(other.parse(pos, cache, context))
    }

    #[inline(always)]
    pub fn merge_seq_opt_parser<'grm, 'arn, O2, P2: Parser<'arn, 'grm, O2, E>>(
        self,
        other: &P2,
        cache: &mut PCache<'arn, 'grm, E>,
        context: &ParserContext,
    ) -> (PResult<(O, Option<O2>), E>, bool)
    where
        'grm: 'arn,
    {
        //Quick out
        if self.is_err() {
            return (self.map(|_| unreachable!()), false);
        }

        let pos = self.end_pos();
        let other_res = other.parse(pos, cache, context);
        let should_continue = other_res.is_ok();
        (self.merge_seq_opt(other_res), should_continue)
    }

    #[inline(always)]
    pub fn ok(&self) -> Option<&O> {
        match self {
            POk(o, _, _, _, _) => Some(o),
            PErr(_, _) => None,
        }
    }
}