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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Copyright (c) Cole Frederick. All rights reserved.
// The use and distribution terms for this software are covered by the
// Eclipse Public License 1.0 (https://opensource.org/licenses/eclipse-1.0.php)
// which can be found in the file epl-v10.html at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by the terms of this license.
// You must not remove this notice, or any other, from this software.

use std::str::from_utf8;
use memory::Unit;
use value::Value;
use handle::Handle;

#[derive(Debug)]
pub enum ReadResult {
    Ok       { bytes_used: u32, value: Unit },
    NeedMore { bytes_not_used: u32 },
    Error    { location: Counter, message: String },
}

#[derive(Copy, Clone, Debug)]
pub struct Counter {
    pub byte: u32,
    pub row: u32,
    pub col: u32,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Pending {
    Vector, // match closing ]
    List,   // match closing )
    Map,    // match closing }
    Mapping, // read next, add mapping to map
    Namespace,
    Set,    // match closing }
    Tagged, // read next, interpret(?) based on tag
    Discard, // read next, retire
    // Meta, // TODO
    // Quote ', Deref @, Syntax Quote `
    String,
}


impl Pending {
    pub fn name(self) -> &'static str {
        match self {
            Pending::Vector => "vector",
            Pending::List => "list",
            Pending::Map => "map",
            Pending::Mapping => "mapping pair",
            Pending::Namespace => "namespace",
            Pending::Set => "set",
            Pending::Tagged => "tagged",
            Pending::Discard => "discard",
            _ => "pending",
        }
    }
}

pub const STACK_SIZE: usize = 20;

pub struct PendingStack {
    pub count: usize,
    pub discards: usize,
    pub labels: [Pending; STACK_SIZE],
    pub boxes:  [Unit;    STACK_SIZE],
}

pub struct EdnReader {
    pub counter: Counter,
    pub pending: PendingStack,
}

pub fn attach_counter(reader: EdnReader, v: Value) -> Value {
    // assign meta counter info
    unimplemented!()
}
// TODO struct contain options like
//  * attach provenance metadata:
//   * base metadata map (file name, request id, etc)
//   * row col, byte byte-count id
//   * scape: collections, symbols, all heap types, everything (nil true false)
//  * read meta literals
//  * read quotes
//  * intern keywords?
//  * collect comment ranges (; single line, and #_(comment form here)
impl EdnReader {
    pub fn new() -> EdnReader {
        EdnReader { counter: Counter::new(), pending: PendingStack::new() }
    }
}
impl Drop for EdnReader {
    fn drop(&mut self) { self.pending.tear_down() }
}

pub struct EdnRdr {
    pub reader: EdnReader,
    pub buf: Vec<u8>,
    pub resume: usize,
}
// TODO drop method, tear down
impl EdnRdr {
    pub fn new() -> EdnRdr { EdnRdr::with_buffer_capacity(0) }
    pub fn with_buffer_capacity(n: usize) -> EdnRdr {
        EdnRdr { reader: EdnReader { counter: Counter::new(), pending: PendingStack::new() },
            buf: Vec::with_capacity(n), resume: 0 }
    }

    pub fn buffer_wilderness(&mut self) -> &mut [u8] {
        use std::slice::from_raw_parts_mut;
        unsafe {
            let b = from_raw_parts_mut(self.buf.as_mut_ptr(), self.buf.capacity());
            &mut b[self.buf.len()..]
        }
    }

    pub fn buffer_consume(&mut self, n: usize) {
        let new_len = self.buf.len() + n;
        assert!(new_len <= self.buf.capacity());
        unsafe { self.buf.set_len(new_len) }
    }

    pub fn clear_buffer(&mut self) {
        self.buf.clear();
        self.resume = 0;
    }

    pub fn align_buffer(&mut self) {
        if self.resume != 0 {
            let byte_count = self.buf.len() - self.resume;
            let b = self.buf.as_mut_slice();
            for i in 0..byte_count {
                b[i] = b[self.resume + i];
            }
            unsafe { self.buf.set_len(byte_count) }
            self.resume = 0;
        }
    }

    pub fn read_bytes(&mut self, s: &[u8]) -> Result<Option<Value>, String> {
        if self.buf.is_empty() {
            let r = super::read(&mut self.reader, s);
            match r {
                ReadResult::Ok { bytes_used, value } => {
                    let remaining = &s[(bytes_used as usize)..];
                    self.buf.reserve(remaining.len());
                    self.buf.extend_from_slice(remaining);
                    Ok(Some(value.handle().value()))
                },
                ReadResult::NeedMore { bytes_not_used } => {
                    let remaining = &s[(s.len() - bytes_not_used as usize)..];
                    self.buf.extend_from_slice(remaining);
                    Ok(None)
                },
                ReadResult::Error { location, message } => {
                    Err(format!("Line {}:{} {}", location.row, location.col, message))
                },
            }
        } else {
            self.buf.reserve(s.len());
            let b = &mut self.buffer_wilderness()[..s.len()];
            b.copy_from_slice(s);
            self.buffer_consume(s.len());
            self.read_again()
        }
    }

    pub fn read_again(&mut self) -> Result<Option<Value>, String> {
        let r = super::read(&mut self.reader, &self.buf.as_slice()[self.resume..]);
        match r {
            ReadResult::Ok { bytes_used, value } => {
                self.resume += bytes_used as usize;
                let remaining = self.buf.len() - self.resume;
                let sixteenth = self.buf.len() >> 4;
                if remaining <= sixteenth { self.align_buffer(); }
                Ok(Some(value.handle().value()))
            },
            ReadResult::NeedMore { bytes_not_used } => {
                self.resume = self.buf.len() - bytes_not_used as usize;
                self.align_buffer();
                Ok(None)
            },
            ReadResult::Error { location, message } => {
                Err(format!("Line {}:{} {}", location.row, location.col, message))
            },
        }
    }
}

impl Counter {
    pub fn new() -> Counter { Counter { byte: 0, row: 1, col: 1 } }
    pub fn count(mut self, s: &str) -> Counter {
        for c in s.chars() {
            if c == '\n' {
                self.row += 1;
                self.col = 1;
            } else {
                self.col += 1;
            }
        }
        self.byte += s.as_bytes().len() as u32;
        self
    }
    pub fn count_ascii(mut self, s: &[u8]) -> Counter {
        for b in s {
            if *b == b'\n' {
                self.row += 1;
                self.col = 1;
            } else {
                self.col += 1;
            }
        }
        self.byte += s.len() as u32;
        self
    }
    pub fn newline(mut self) -> Counter {
        self.byte += 1;
        self.row += 1;
        self.col = 1;
        self
    }
    pub fn add_ascii(mut self, n: u32) -> Counter {
        self.byte += n;
        self.col += n;
        self
    }
}

impl PendingStack {
    pub fn new() -> PendingStack {
        PendingStack { count: 0, discards: 0,
            labels: [Pending::Vector; STACK_SIZE],
            boxes:  [Handle::NIL;     STACK_SIZE] }
    }
    pub fn is_empty(&self)    -> bool { self.count == 0 }
    pub fn no_discards(&self) -> bool { self.discards == 0 }
    pub fn push(&mut self, p: Pending, u: Unit) {
        if self.count == STACK_SIZE { panic!("Overfull reader stack") }
        self.labels[self.count] = p;
        self.boxes[self.count] = u;
        self.count += 1;
    }
    pub fn push_discard(&mut self) {
        self.push(Pending::Discard, Handle::NIL);
        self.discards += 1;
    }
    pub fn pop(&mut self) {
        self.count -= 1;
    }
    pub fn pop_discard(&mut self) {
        self.pop();
        self.discards -= 1;
    }
    pub fn top(&self) -> (Pending, Unit) {
        if self.is_empty() { panic!("Empty reader stack") }
        let t = self.count - 1;
        (self.labels[t], self.boxes[t])
    }
    pub fn top_case(&self) -> Pending {
        if self.is_empty() { panic!("Empty reader stack") }
        self.labels[self.count - 1]
    }
    pub fn top_unit(&self) -> Unit {
        if self.is_empty() { panic!("Empty reader stack") }
        self.boxes[self.count - 1]
    }
    pub fn default_ns(&self) -> Option<Unit> {
        if self.count < 2 { return None; }
        let t = self.count - 2;
        if self.labels[t] == Pending::Namespace {
            Some(self.boxes[t])
        } else {
            None
        }
    }
    pub fn set_top(&mut self, u: Unit) { self.boxes[self.count - 1] = u; }
    pub fn resolve(&mut self, bytes: &[u8]) {
        for i in 0..self.count {
            let lab = self.labels[i];
            if lab == Pending::Namespace || lab == Pending::Tagged {
                let ns_unit = self.boxes[i];
                if !ns_unit.is_even() {
                    let resolved = {
                        let (start, length) = demediate_both(ns_unit);
                        let b = &bytes[start..(start + length)];
                        use string;
                        string::new_from_str(from_utf8(b).unwrap()).unit()
                    };
                    self.boxes[i] = resolved;
                }
            }
        }
    }
    pub fn tear_down(&mut self) {
        for i in 0..self.count {
            self.boxes[i].handle().retire();
        }
        self.count = 0;
        self.discards = 0;
    }
}

pub fn immediate(x: usize) -> Unit { Unit::from((x << 1) | 0x1) }
pub fn demediate(x: Unit)  -> usize { x.u() >> 1 }
pub fn immediate_both(x: usize, y: usize) -> Unit {
    let res = immediate((x << 7) | y);
    assert_eq!(demediate_both(res), (x, y));
    res
}
pub fn demediate_both(x: Unit) -> (usize, usize) {
    let y = demediate(x);
    (y >> 7, y & 0x7F)
}
pub fn reference(name: Unit, bytes: &[u8]) -> &[u8] {
    if name.is_even() {
        if name.handle().is_string() {
            use string::guide::Guide;
            // TODO wrong, should unwrap (in case of metadata on the string)
            let g = Guide::hydrate(name.handle().prism());
            let bs = g.whole_byte_slice();
            use std::slice;
            unsafe { slice::from_raw_parts(bs.as_ptr(), bs.len()) }
        } else { panic!("Referencing non-string.") }
    } else {
        let (start, length) = demediate_both(name);
        &bytes[start..(start + length)]
    }
}