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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * mini-haskell: light-weight Haskell for fun
 * Copyright (C) 2020  Xie Ruifeng
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

//! Persistent input from a [`std::io::Read`].

use std::cell::UnsafeCell;
use std::rc::Rc;

use crate::rc_view::RcView;

const DEFAULT_BUF_SIZE: usize = 4 * 1024;
const MAXIMUM_RETRY: isize = 5;

/// A "raw" input.
/// - segmented, shared, and immutable back buffer
/// - lazy reading from the input
/// - lightweight cloning
/// - NOT thread-safe
pub struct RawInput<I>(Rc<UnsafeCell<InputSegment<I>>>);

impl<I> Clone for RawInput<I> {
    fn clone(&self) -> Self { RawInput(self.0.clone()) }
}

enum InputSegment<I> {
    EndOfFile {
        io_error: Option<std::io::Error>,
    },
    Cons {
        data: RcView<[u8], str>,
        next: RawInput<I>,
    },
    Invalid {
        data: RcView<[u8], [u8]>,
        next: RawInput<I>,
    },
    Delayed {
        remaining: Option<RcView<[u8], [u8]>>,
        input: I,
    },
}

impl<I> Default for InputSegment<I> {
    fn default() -> Self { InputSegment::EndOfFile { io_error: None } }
}

type DelayedContent<I> = (Option<RcView<[u8], [u8]>>, I);

impl<I> InputSegment<I> {
    fn new(input: I) -> Self {
        InputSegment::Delayed {
            remaining: None,
            input,
        }
    }

    fn is_delayed(&self) -> bool {
        matches!(self, Self::Delayed { .. })
    }

    fn take_delayed(&mut self) -> Option<DelayedContent<I>> {
        match self {
            Self::Delayed { .. } => match std::mem::take(self) {
                Self::Delayed { remaining, input } => Some((remaining, input)),
                _ => unreachable!(),
            },
            _ => None,
        }
    }
}

impl<I> RawInput<I> {
    /// Create a new [`RawInput`] from a [`std::io::Read`].
    pub fn new(input: I) -> Self {
        RawInput(Rc::new(UnsafeCell::new(InputSegment::new(input))))
    }

    fn wrap(segment: InputSegment<I>) -> Self {
        RawInput(Rc::new(UnsafeCell::new(segment)))
    }

    /// Dump out the content of this raw input.
    pub fn dump(&self) {
        let node = unsafe { &mut *self.0.get() };
        match node {
            InputSegment::EndOfFile { .. } => println!("- <EOF>"),
            InputSegment::Delayed { .. } => println!("- <lazy> not yet read"),
            InputSegment::Cons { data, next } => {
                println!("- {:?}", data);
                next.dump()
            }
            InputSegment::Invalid { data, next } => {
                println!("- <invalid> {:?}", data);
                next.dump()
            }
        }
    }
}

impl<I: std::io::Read> RawInput<I> {
    fn prepare(&mut self) {
        let node = unsafe { &mut *self.0.get() };
        let delayed = node.take_delayed();
        if delayed.is_none() { return; }
        let (remaining, mut input) = delayed.unwrap();
        let mut buffer = vec![0u8; DEFAULT_BUF_SIZE];
        let mut to_read = &mut *buffer;
        if let Some(xs) = remaining {
            let n = xs.len();
            let (head, rest) = to_read.split_at_mut(n);
            head.copy_from_slice(&xs);
            to_read = rest;
        }
        let mut retry = MAXIMUM_RETRY;
        let tail = loop {
            match input.read(to_read) {
                Ok(0) if to_read.is_empty() => break InputSegment::new(input),
                Ok(0) => break InputSegment::EndOfFile { io_error: None },
                Ok(n) => to_read = &mut to_read[n..],
                Err(e) => match e.kind() {
                    std::io::ErrorKind::Interrupted if retry > 0 => retry -= 1,
                    _ => break InputSegment::EndOfFile { io_error: Some(e) },
                },
            }
        };
        let n = DEFAULT_BUF_SIZE - to_read.len();
        let buffer = Rc::<[u8]>::from(buffer);
        let to_decode = RcView::new(buffer, |b| &b[..n]);
        *node = Self::decode(to_decode, tail)
    }

    fn decode(to_decode: RcView<[u8], [u8]>, tail: InputSegment<I>) -> InputSegment<I> {
        let rest = &*to_decode;
        if rest.is_empty() { return tail; }
        match std::str::from_utf8(rest) {
            Ok(s) => InputSegment::Cons {
                data: unsafe { to_decode.derive(s) },
                next: RawInput::wrap(tail),
            },
            Err(e) => {
                let n = e.valid_up_to();
                let (valid, rest) = rest.split_at(n);
                let tail = match e.error_len() {
                    None if tail.is_delayed() => match tail {
                        InputSegment::Delayed { remaining, input } => {
                            assert!(matches!(remaining, None));
                            InputSegment::Delayed {
                                remaining: Some(unsafe { to_decode.derive(rest) }),
                                input,
                            }
                        }
                        _ => unreachable!("impossible: no remaining input expected here"),
                    },
                    _ => {
                        let k = e.error_len().unwrap_or_else(|| rest.len());
                        let (invalid, rest) = rest.split_at(k);
                        InputSegment::Invalid {
                            data: unsafe { to_decode.derive(invalid) },
                            next: RawInput::wrap(Self::decode(
                                unsafe { to_decode.derive(rest) }, tail)),
                        }
                    }
                };
                if n == 0 { tail } else {
                    let valid = unsafe { std::str::from_utf8_unchecked(valid) };
                    InputSegment::Cons {
                        data: unsafe { to_decode.derive(valid) },
                        next: RawInput::wrap(tail),
                    }
                }
            }
        }
    }
}

/// Input with the ability to read one character once.
/// Keeping such an iterator will prevent releasing the input resource.
pub struct Input<I> {
    input: RawInput<I>,
    index: usize,
}

impl<I> Clone for Input<I> {
    fn clone(&self) -> Self {
        Self { input: self.input.clone(), index: self.index }
    }
}

impl<I> Input<I> {
    /// Create a new [`Input`] from a [`std::io::Read`].
    pub fn new(input: I) -> Self {
        Input { input: RawInput::new(input), index: 0 }
    }
}

impl<I: std::io::Read> Input<I> {
    /// Get the next character, if any.
    pub fn next(
        mut self,
        mut report: impl FnMut(&[u8]),
    ) -> std::result::Result<(char, Self), impl Into<Option<std::io::Error>>> {
        loop {
            self.input.prepare();
            let head = unsafe { &mut *self.input.0.get() };
            match head {
                InputSegment::EndOfFile { io_error } => {
                    break Err(unsafe { RcView::wrap(self.input.0, io_error) });
                }
                InputSegment::Cons { data, next } => {
                    let mut cs = data[self.index..].chars();
                    match cs.next() {
                        Some(c) => {
                            self.index = data.len() - cs.as_str().len();
                            break Ok((c, self));
                        }
                        None => self = Self { input: next.clone(), index: 0 },
                    }
                }
                InputSegment::Invalid { data, .. } => {
                    report(data);
                    let next = match std::mem::take(head) {
                        InputSegment::Invalid { next, .. } => next,
                        _ => unreachable!("Already pattern matched."),
                    };
                    *head = Rc::try_unwrap(next.0).ok().unwrap().into_inner();
                }
                _ => unreachable!("RawInput::prepare shall not return a Delayed."),
            }
        }
    }

    /// Match on the input, succeed if the input matches the given string.
    pub fn r#match(mut self, s: &str, mut report: impl FnMut(&[u8])) -> Option<Self> {
        let mut s = s.as_bytes();
        loop {
            if s.is_empty() { return Some(self); }
            self.input.prepare();
            let head = unsafe { &mut *self.input.0.get() };
            match head {
                InputSegment::EndOfFile { .. } => break None,
                InputSegment::Cons { data, next } => {
                    let cs = data[self.index..].as_bytes();
                    let n = std::cmp::min(s.len(), cs.len());
                    if s[..n] != cs[..n] { break None; }
                    self.index += n;
                    if cs[n..].is_empty() { self = Self { input: next.clone(), index: 0 }; }
                    s = &s[n..];
                }
                InputSegment::Invalid { data, .. } => {
                    report(data);
                    let next = match std::mem::take(head) {
                        InputSegment::Invalid { next, .. } => next,
                        _ => unreachable!("Already pattern matched."),
                    };
                    *head = Rc::try_unwrap(next.0).ok().unwrap().into_inner();
                }
                _ => unreachable!("RawInput::prepare shall not return a Delayed."),
            }
        }
    }

    /// Dump out the content of this input.
    pub fn dump(&self) {
        println!("Input[index = {}]:", self.index);
        self.input.dump();
    }
}