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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/// A lexer for utf-8 encoded json data
pub struct Lexer<I: IntoIterator<Item = u8>> {
    chars: I::IntoIter,
    next_byte: Option<u8>,
    cursor: u64,
    buffer_type: BufferType,
}

#[derive(Debug, PartialEq, Clone)]
pub enum TokenType {
    /// `{`
    CurlyOpen,
    /// `}`
    CurlyClose,

    /// `[`
    BracketOpen,
    /// `]`
    BracketClose,

    /// `:`
    Colon,
    /// `,`
    Comma,

    /// A json string , like `"foo"`
    String,
    /// `true`
    BooleanTrue,
    /// `false`
    BooleanFalse,
    /// A Number, like `1.1234` or `123` or `-0.0` or `-1` or `.0` or `.`
    Number,

    /// any json number, like `1.24123` or `123`
    // NOTE: We can't do numbers with our simplified lexer as it would require
    // us to read a byte just to see that it's not a number and thus the previous
    // tokens are to be returned. But we cannot peek without drastically complicating
    // our so far quite speedy implementation.
    // Number,
    /// `null`
    Null,

    /// The type of the token could not be identified.
    /// Should be removed if this lexer is ever to be feature complete
    Invalid,
}

impl AsRef<str> for TokenType {
    fn as_ref(&self) -> &str {
        match *self {
            TokenType::CurlyOpen => "{",
            TokenType::CurlyClose => "}",
            TokenType::BracketOpen => "[",
            TokenType::BracketClose => "]",
            TokenType::Colon => ":",
            TokenType::Comma => ",",
            TokenType::BooleanTrue => "true",
            TokenType::BooleanFalse => "false",
            TokenType::Null => "null",

            TokenType::Invalid => panic!("Cannot convert invalid TokenType"),
            _ => panic!("Cannot convert variant TokenTypes"),
        }
    }
}

/// A pair of indices into the byte stream returned by our source
/// iterator.
/// It is an exclusive range.
#[derive(Debug, PartialEq, Clone, Default)]
pub struct Span {
    /// Index of the first the byte
    pub first: u64,
    /// Index one past the last byte
    pub end: u64,
}

/// A lexical token, identifying its kind and span.
#[derive(Debug, PartialEq, Clone)]
pub struct Token {
    /// The exact type of the token
    pub kind: TokenType,

    /// A buffer representing the bytes of this Token.
    pub buf: Buffer,
}

/// Representation of a buffer containing items making up a `Token`.
///
/// It's either always `Span`, or one of the `*Byte` variants.
#[derive(Debug, PartialEq, Clone)]
pub enum Buffer {
    /// Multiple bytes making up a token. Only set for `TokenType::String` and
    /// `TokenType::Number`.
    MultiByte(Vec<u8>),
    /// The span allows to reference back into the source byte stream
    /// to obtain the string making up the token.
    /// Please note that for control characters, booleans and null (i.e
    /// anything that is not `Buffer::MultiByte` you should use
    /// `<TokenType as AsRef<str>>::as_ref()`)
    Span(Span),
}

/// The type of `Buffer` you want in each `Token`
#[derive(Debug, PartialEq, Clone)]
pub enum BufferType {
    /// Use a `Buffer::MultiByte` were appropriate. Initialize it with the
    /// given capcity (to obtain higher performance when pushing charcters)
    Bytes(usize),
    Span,
}

impl<I> Lexer<I>
where
    I: IntoIterator<Item = u8>,
{
    /// Returns a new Lexer from a given byte iterator.
    pub fn new(chars: I, buffer_type: BufferType) -> Lexer<I> {
        Lexer {
            chars: chars.into_iter(),
            next_byte: None,
            cursor: 0,
            buffer_type: buffer_type,
        }
    }

    pub fn into_inner(self) -> I::IntoIter {
        self.chars
    }

    fn put_back(&mut self, c: u8) {
        debug_assert!(self.next_byte.is_none());
        self.next_byte = Some(c);
        self.cursor -= 1;
    }

    fn next_byte(&mut self) -> Option<u8> {
        match self.next_byte.take() {
            Some(c) => {
                self.cursor += 1;
                Some(c)
            }
            None => {
                let res = self.chars.next();
                match res {
                    None => None,
                    Some(_) => {
                        self.cursor += 1;
                        res
                    }
                }
            }
        }
    }
}

// Identifies the state of the lexer
enum Mode {
    // String parse mode: bool = ignore_next, usize = ignore_digits
    String(bool, usize),
    // `null` parse mode: buf, buf-index
    Null([u8; 4], usize),
    // `true` parse mode
    True([u8; 4], usize),
    // `false` parse mode
    False([u8; 5], usize),
    // `Number` parse mode
    Number,
    SlowPath,
}

impl<I> Iterator for Lexer<I>
where
    I: IntoIterator<Item = u8>,
{
    type Item = Token;

    /// Lex the underlying bytte stream to generate tokens
    fn next(&mut self) -> Option<Token> {
        let mut t: Option<TokenType> = None;

        let mut first = 0;
        let mut state = Mode::SlowPath;
        let last_cursor = self.cursor;
        let mut buf = match self.buffer_type {
            BufferType::Bytes(capacity) => Some(Vec::<u8>::with_capacity(capacity)),
            BufferType::Span => None,
        };

        while let Some(c) = self.next_byte() {
            let mut set_cursor = |cursor| {
                first = cursor - 1;
            };

            match state {
                Mode::String(ref mut ign_next, ref mut ign_digits) => {
                    if let Some(ref mut v) = buf {
                        v.push(c);
                    }
                    if *ign_next {
                        match c {
                            b'"' | b'\\' | b'/' | b'b' | b'f' | b'n' | b'r' | b't' => {
                                *ign_next = false;
                                continue;
                            }
                            b'u' => {
                                *ign_next = false;
                                *ign_digits = 4;
                                continue;
                            }
                            _ => {
                                t = Some(TokenType::Invalid);
                                break;
                            }
                        }
                    }
                    if *ign_digits > 0 {
                        match c {
                            b'0'...b'9' | b'A'...b'F' | b'a'...b'f' => {
                                *ign_digits -= 1;
                                continue;
                            }
                            _ => {
                                t = Some(TokenType::Invalid);
                                break;
                            }
                        }
                    }
                    match c {
                        b'"' => {
                            t = Some(TokenType::String);
                            break;
                        }
                        b'\\' => {
                            *ign_next = true;
                            continue;
                        }
                        _ => {
                            continue;
                        }
                    }
                }
                Mode::Null(ref mut b, ref mut i) => {
                    b[*i] = c;
                    if *i == 3 {
                        // we know b[0] is b'n'
                        if b[1] == b'u' && b[2] == b'l' && b[3] == b'l' {
                            t = Some(TokenType::Null);
                        } else {
                            t = Some(TokenType::Invalid);
                        }
                        break;
                    } else {
                        *i += 1;
                        continue;
                    }
                }
                Mode::Number => match c {
                    b'0'...b'9' | b'-' | b'+' | b'.' | b'E' | b'e' => {
                        if let Some(ref mut v) = buf {
                            v.push(c);
                        }
                        continue;
                    }
                    _ => {
                        t = Some(TokenType::Number);
                        self.put_back(c);
                        break;
                    }
                },
                Mode::True(ref mut b, ref mut i) => {
                    b[*i] = c;
                    if *i == 3 {
                        // we know b[0] is b't'
                        if b[1] == b'r' && b[2] == b'u' && b[3] == b'e' {
                            t = Some(TokenType::BooleanTrue);
                        } else {
                            t = Some(TokenType::Invalid);
                        }
                        break;
                    } else {
                        *i += 1;
                        continue;
                    }
                }
                Mode::False(ref mut b, ref mut i) => {
                    b[*i] = c;
                    if *i == 4 {
                        // we know b[0] is b'f'
                        if b[1] == b'a' && b[2] == b'l' && b[3] == b's' && b[4] == b'e' {
                            t = Some(TokenType::BooleanFalse);
                        } else {
                            t = Some(TokenType::Invalid);
                        }
                        break;
                    } else {
                        *i += 1;
                        continue;
                    }
                }
                Mode::SlowPath => {
                    match c {
                        b'{' => {
                            t = Some(TokenType::CurlyOpen);
                            set_cursor(self.cursor);
                            break;
                        }
                        b'}' => {
                            t = Some(TokenType::CurlyClose);
                            set_cursor(self.cursor);
                            break;
                        }
                        b'"' => {
                            state = Mode::String(false, 0);
                            if let Some(ref mut v) = buf {
                                v.push(c);
                            } else {
                                set_cursor(self.cursor);
                                // it starts at invalid, and once we know it closes, it's a string
                                t = Some(TokenType::Invalid);
                            }
                        }
                        b'n' => {
                            state = Mode::Null([c, b'x', b'x', b'x'], 1);
                            set_cursor(self.cursor);
                        }
                        b'0'...b'9' | b'-' | b'.' => {
                            state = Mode::Number;
                            if let Some(ref mut v) = buf {
                                v.push(c);
                            } else {
                                set_cursor(self.cursor);
                            }
                        }
                        b't' => {
                            state = Mode::True([c, b'x', b'x', b'x'], 1);
                            set_cursor(self.cursor);
                        }
                        b'f' => {
                            state = Mode::False([c, b'x', b'x', b'x', b'x'], 1);
                            set_cursor(self.cursor);
                        }
                        b'[' => {
                            t = Some(TokenType::BracketOpen);
                            set_cursor(self.cursor);
                            break;
                        }
                        b']' => {
                            t = Some(TokenType::BracketClose);
                            set_cursor(self.cursor);
                            break;
                        }
                        b':' => {
                            t = Some(TokenType::Colon);
                            set_cursor(self.cursor);
                            break;
                        }
                        b',' => {
                            t = Some(TokenType::Comma);
                            set_cursor(self.cursor);
                            break;
                        }
                        b'\\' => {
                            // invalid
                            t = Some(TokenType::Invalid);
                            set_cursor(self.cursor);
                            break;
                        }
                        _ => {}
                    } // end single byte match
                } // end case SlowPath
            } // end match state
        } // end for each byte

        match t {
            None => None,
            Some(t) => {
                if self.cursor == last_cursor {
                    None
                } else {
                    let buf = match (&t, buf) {
                        (&TokenType::String, Some(b)) | (&TokenType::Number, Some(b)) => Buffer::MultiByte(b),
                        _ => Buffer::Span(Span {
                            first: first,
                            end: self.cursor,
                        }),
                    };
                    Some(Token { kind: t, buf: buf })
                }
            }
        }
    }
}