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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
//! Lexer used by both DOM and SAX parsers
#![allow(unused_assignments)]
#![allow(unused_variables)]
#![allow(unreachable_code)]

use std::fmt::{Display, Formatter};
use std::io::BufRead;

use crate::lexer::coords::{Coords, Span};
use crate::lexer::errors::{ParserError, ParserErrorDetails, ParserErrorSource, ParserResult};
use crate::lexer::lexer_input::{CharWithCoords, LexerInput};

/// Default lookahead buffer size
const DEFAULT_BUFFER_SIZE: usize = 4096;
/// Pattern to match for null
const NULL_PATTERN: [char; 4] = ['n', 'u', 'l', 'l'];
/// Pattern to match for true
const TRUE_PATTERN: [char; 4] = ['t', 'r', 'u', 'e'];
/// Pattern to match for false
const FALSE_PATTERN: [char; 5] = ['f', 'a', 'l', 's', 'e'];

/// Enumeration of valid JSON tokens
#[derive(Debug, Clone, PartialEq)]
pub enum Token {
    StartObject,
    EndObject,
    StartArray,
    EndArray,
    Colon,
    Comma,
    Str(String),
    Float(f64),
    Integer(i64),
    Null,
    Boolean(bool),
    EndOfInput,
}

impl Display for Token {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Token::StartObject => write!(f, "StartObject"),
            Token::EndObject => write!(f, "EndObject"),
            Token::StartArray => write!(f, "StartArray"),
            Token::EndArray => write!(f, "EndArray"),
            Token::Colon => write!(f, "Colon"),
            Token::Comma => write!(f, "Comma"),
            Token::Str(str) => write!(f, "String(\"{}\")", str),
            Token::Float(num) => write!(f, "Float({})", num),
            Token::Integer(num) => write!(f, "Integer({})", num),
            Token::Null => write!(f, "Null"),
            Token::Boolean(bool) => write!(f, "Boolean({})", bool),
            Token::EndOfInput => write!(f, "EndOfInput"),
        }
    }
}

/// A packed token consists of a [Token] and the [Span] associated with it
pub type PackedToken<'a> = (Token, Span);

/// Convenience macro for packing tokens along with their positional information
macro_rules! packed_token {
    ($t:expr, $s:expr, $e:expr) => {
        Ok(($t, Span { start: $s, end: $e }))
    };
    ($t:expr, $s:expr) => {
        Ok(($t, Span { start: $s, end: $s }))
    };
}

macro_rules! match_zero {
    () => {
        '0'
    };
}

macro_rules! match_minus {
    () => {
        '-'
    };
}

macro_rules! match_plus_minus {
    () => {
        '+' | '-'
    };
}

macro_rules! match_digit {
    () => {
        '0'..='9'
    };
}

macro_rules! match_non_zero_digit {
    () => {
        '1'..='9'
    };
}

macro_rules! match_exponent {
    () => {
        'e' | 'E'
    };
}

macro_rules! match_period {
    () => {
        '.'
    };
}

macro_rules! match_numeric_terminator {
    () => {
        ']' | '}' | ','
    };
}

macro_rules! match_escape {
    () => {
        '\\'
    };
}

macro_rules! match_escape_non_unicode_suffix {
    () => {
        'n' | 't' | 'r' | '\\' | '/' | 'b' | 'f' | '\"'
    };
}

macro_rules! match_escape_unicode_suffix {
    () => {
        'u'
    };
}

macro_rules! match_quote {
    () => {
        '\"'
    };
}

macro_rules! match_newline {
    () => {
        '\n'
    };
}

pub struct Lexer<'a> {
    /// Input coordinate state
    input: LexerInput<'a>,
}

impl<'a> Lexer<'a> {
    pub fn new(chars: &'a mut impl Iterator<Item = char>) -> Self {
        Lexer {
            input: LexerInput::new(chars),
        }
    }

    /// Get the front of the input
    fn front(&self) -> Option<CharWithCoords> {
        self.input.front()
    }

    /// Get the back of the input
    fn back(&self) -> Option<CharWithCoords> {
        self.input.back()
    }

    /// Grab the front character
    fn front_char(&self) -> char {
        self.input.front().unwrap().ch
    }

    /// Grab the back character
    fn back_char(&self) -> char {
        self.input.back().unwrap().ch
    }

    /// Grab the front input coordinates
    fn front_coords(&self) -> Coords {
        self.input.front().unwrap().coords
    }

    /// Grab the back input coordinates
    fn back_coords(&self) -> Coords {
        self.input.back().unwrap().coords
    }

    /// Grab the current absolute input coordinates
    fn absolute_position(&self) -> Coords {
        self.input.position()
    }

    /// Advance the input by one
    fn advance(&mut self, skip_whitespace: bool) -> ParserResult<()> {
        self.input.advance(skip_whitespace)
    }

    /// Advance the input by n
    fn advance_n(&mut self, n: usize, skip_whitespace: bool) -> ParserResult<()> {
        self.input.advance_n(n, skip_whitespace)
    }

    /// Grab the current input string
    fn current_string(&mut self) -> String {
        self.input.buffer_as_string_with_span().str
    }

    /// Grab the current input character array
    fn current_chars(&mut self) -> Vec<char> {
        self.input.buffer_as_char_array()
    }

    /// Grab the current input byte array
    fn current_bytes(&mut self) -> Vec<u8> {
        self.input.buffer_as_byte_array()
    }

    /// Consume the next [Token] from the input
    pub fn consume(&mut self) -> ParserResult<PackedToken> {
        self.input.clear();
        match self.advance(true) {
            Ok(_) => match self.input.front() {
                Some(CharWithCoords { ch: '{', coords }) => {
                    packed_token!(Token::StartObject, coords)
                }
                Some(CharWithCoords { ch: '}', coords }) => packed_token!(Token::EndObject, coords),
                Some(CharWithCoords { ch: '[', coords }) => {
                    packed_token!(Token::StartArray, coords)
                }
                Some(CharWithCoords { ch: ']', coords }) => packed_token!(Token::EndArray, coords),
                Some(CharWithCoords { ch: ':', coords }) => packed_token!(Token::Colon, coords),
                Some(CharWithCoords { ch: ',', coords }) => packed_token!(Token::Comma, coords),
                Some(CharWithCoords { ch: '\"', coords }) => self.match_string(),
                Some(CharWithCoords { ch: 'n', coords }) => self.match_null(),
                Some(CharWithCoords { ch: 't', coords }) => self.match_true(),
                Some(CharWithCoords { ch: 'f', coords }) => self.match_false(),
                Some(CharWithCoords { ch: '-', coords }) => self.match_number(),
                Some(CharWithCoords { ch: d, coords }) if d.is_ascii_digit() => self.match_number(),
                Some(CharWithCoords { ch, coords }) => lexer_error!(
                    ParserErrorDetails::InvalidCharacter(ch.clone()),
                    coords.clone()
                ),
                None => lexer_error!(ParserErrorDetails::EndOfInput),
            },
            Err(err) => match err.details {
                ParserErrorDetails::EndOfInput => {
                    packed_token!(Token::EndOfInput, self.input.position())
                }
                _ => match err.coords {
                    Some(coords) => lexer_error!(err.details, coords),
                    None => lexer_error!(err.details),
                },
            },
        }
    }

    /// Match on a valid Json string.
    fn match_string(&mut self) -> ParserResult<PackedToken> {
        loop {
            match self.advance(false) {
                Ok(_) => match self.front_char() {
                    match_escape!() => match self.input.advance(false) {
                        Ok(_) => match self.front_char() {
                            match_escape_non_unicode_suffix!() => (),
                            match_escape_unicode_suffix!() => self.check_unicode_sequence()?,
                            _ => {
                                return lexer_error!(
                                    ParserErrorDetails::InvalidEscapeSequence(
                                        self.current_string()
                                    ),
                                    self.back_coords()
                                );
                            }
                        },
                        Err(err) => {
                            return lexer_error!(err.details, err.coords.unwrap());
                        }
                    },
                    match_quote!() => {
                        return packed_token!(
                            Token::Str(self.current_string()),
                            self.back_coords(),
                            self.front_coords()
                        );
                    }
                    _ => (),
                },
                Err(err) => return lexer_error!(err.details, err.coords.unwrap()),
            }
        }
    }

    /// Check for a valid unicode escape sequence of the form '\uXXXX'
    fn check_unicode_sequence(&mut self) -> ParserResult<()> {
        let start_position = self.absolute_position();
        for i in 1..=4 {
            match self.advance(false) {
                Ok(_) => {
                    if !self.front_char().is_ascii_hexdigit() {
                        return lexer_error!(
                            ParserErrorDetails::InvalidUnicodeEscapeSequence(self.current_string()),
                            start_position
                        );
                    }
                }
                Err(e) => {
                    return lexer_error!(ParserErrorDetails::EndOfInput, self.absolute_position());
                }
            }
        }
        Ok(())
    }

    /// Match on a valid Json number representation, taking into account valid prefixes allowed
    /// within Json but discarding anything that may be allowed by a more general representations.
    ///
    /// Few rules are applied here, leading to different error conditions:
    /// - All representations must have a valid prefix
    /// - Only a single exponent can be specified
    /// - Only a single decimal point can be specified
    /// - Exponents must be well-formed
    /// - An non-exponent alphabetic found in the representation will result in an error
    /// - Numbers can be terminated by commas, brackets and whitespace only (end of pair, end of array)
    fn match_number(&mut self) -> ParserResult<PackedToken> {
        let mut have_exponent = false;
        let mut have_decimal = false;

        match self.match_valid_number_prefix() {
            Ok(integral) => {
                have_decimal = !integral;
                loop {
                    match self.advance(false) {
                        Ok(_) => match self.front_char() {
                            match_digit!() => (),
                            match_exponent!() => {
                                if !have_exponent {
                                    self.check_following_exponent()?;
                                    have_exponent = true;
                                } else {
                                    return lexer_error!(
                                        ParserErrorDetails::InvalidNumericRepresentation(
                                            self.current_string()
                                        ),
                                        self.back_coords()
                                    );
                                }
                            }
                            match_period!() => {
                                if !have_decimal {
                                    have_decimal = true;
                                } else {
                                    return lexer_error!(
                                        ParserErrorDetails::InvalidNumericRepresentation(
                                            self.current_string()
                                        ),
                                        self.back_coords()
                                    );
                                }
                            }
                            match_numeric_terminator!() => {
                                self.input.pushback();
                                break;
                            }
                            ch if ch.is_ascii_whitespace() => {
                                self.input.pushback();
                                break;
                            }
                            ch if ch.is_alphabetic() => {
                                return lexer_error!(
                                    ParserErrorDetails::InvalidNumericRepresentation(
                                        self.current_string()
                                    ),
                                    self.back_coords()
                                );
                            }
                            _ => {
                                return lexer_error!(
                                    ParserErrorDetails::InvalidNumericRepresentation(
                                        self.current_string()
                                    ),
                                    self.back_coords()
                                );
                            }
                        },
                        Err(err) => {
                            return match err.coords {
                                Some(coords) => lexer_error!(err.details, coords),
                                None => lexer_error!(err.details),
                            };
                        }
                    }
                }
            }
            Err(err) => {
                return match err.coords {
                    Some(coords) => lexer_error!(err.details, coords),
                    None => lexer_error!(err.details),
                }
            }
        }

        self.parse_numeric(!have_decimal)
    }

    fn check_following_exponent(&mut self) -> ParserResult<()> {
        self.advance(false).and_then(|_| {
            return match self.front_char() {
                match_plus_minus!() => Ok(()),
                _ => lexer_error!(
                    ParserErrorDetails::InvalidNumericRepresentation(self.current_string()),
                    self.absolute_position()
                ),
            };
        })
    }

    #[cfg(not(feature = "mixed_numerics"))]
    fn parse_numeric(
        &mut self,
        integral: bool,
        start_coords: Coords,
        end_coords: Coords,
    ) -> ParserResult<PackedToken> {
        packed_token!(
            Token::Float(fast_float::parse(self.input.buffer_as_bytes()).unwrap()),
            back_input_coords!(),
            front_input_coords!()
        )
    }

    #[cfg(feature = "mixed_numerics")]
    fn parse_numeric(&mut self, integral: bool) -> ParserResult<PackedToken> {
        if integral {
            packed_token!(
                Token::Integer(lexical::parse(self.input.buffer_as_byte_array()).unwrap()),
                self.back_coords(),
                self.front_coords()
            )
        } else {
            packed_token!(
                Token::Float(fast_float::parse(self.input.buffer_as_byte_array()).unwrap()),
                self.back_coords(),
                self.front_coords()
            )
        }
    }

    /// Check that a numeric representation is prefixed correctly.
    ///
    /// A few rules here:
    /// - A leading minus must be followed by a digit
    /// - A leading minus must be followed by at most one zero before a period
    /// - Any number > zero can't have a leading zero in the representation
    fn match_valid_number_prefix(&mut self) -> ParserResult<bool> {
        let ch = self.back_char();
        assert!(ch.is_ascii_digit() || ch == '-');
        match ch {
            match_minus!() => self
                .input
                .advance(false)
                .and_then(|_| self.check_following_minus()),
            match_zero!() => self
                .input
                .advance(false)
                .and_then(|_| self.check_following_zero()),
            _ => Ok(true),
        }
    }

    fn check_following_zero(&mut self) -> ParserResult<bool> {
        match self.front_char() {
            match_period!() => Ok(false),
            match_digit!() => lexer_error!(
                ParserErrorDetails::InvalidNumericRepresentation(self.current_string()),
                self.back_coords()
            ),
            match_newline!() => {
                self.input.pushback();
                Ok(true)
            }
            _ => {
                self.input.pushback();
                Ok(true)
            }
        }
    }

    fn check_following_minus(&mut self) -> ParserResult<bool> {
        match self.front_char() {
            match_non_zero_digit!() => Ok(true),
            match_zero!() => self.advance(false).and_then(|_| {
                if self.front_char() != '.' {
                    return lexer_error!(
                        ParserErrorDetails::InvalidNumericRepresentation(self.current_string()),
                        self.back_coords()
                    );
                }
                Ok(false)
            }),
            match_newline!() => {
                self.input.pushback();
                Ok(true)
            }
            _ => lexer_error!(
                ParserErrorDetails::InvalidNumericRepresentation(self.current_string()),
                self.back_coords()
            ),
        }
    }

    /// Match on a null token
    fn match_null(&mut self) -> ParserResult<PackedToken> {
        self.input.advance_n(3, false).and_then(|_| {
            if self.current_chars() == NULL_PATTERN {
                packed_token!(Token::Null, self.back_coords(), self.front_coords())
            } else {
                lexer_error!(
                    ParserErrorDetails::MatchFailed(
                        String::from_iter(NULL_PATTERN.iter()),
                        self.current_string()
                    ),
                    self.back_coords()
                )
            }
        })
    }

    /// Match on a true token
    fn match_true(&mut self) -> ParserResult<PackedToken> {
        self.advance_n(3, false).and_then(|_| {
            if self.current_chars() == TRUE_PATTERN {
                packed_token!(
                    Token::Boolean(true),
                    self.back_coords(),
                    self.front_coords()
                )
            } else {
                lexer_error!(
                    ParserErrorDetails::MatchFailed(
                        String::from_iter(TRUE_PATTERN.iter()),
                        self.current_string()
                    ),
                    self.back_coords()
                )
            }
        })
    }

    /// Match on a false token
    fn match_false(&mut self) -> ParserResult<PackedToken> {
        self.advance_n(4, false).and_then(|_| {
            if self.current_chars() == FALSE_PATTERN {
                packed_token!(
                    Token::Boolean(false),
                    self.back_coords(),
                    self.front_coords()
                )
            } else {
                lexer_error!(
                    ParserErrorDetails::MatchFailed(
                        String::from_iter(FALSE_PATTERN.iter()),
                        self.current_string()
                    ),
                    self.back_coords()
                )
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use std::env;
    use std::fs::File;
    use std::io::{BufRead, BufReader};
    use std::time::Instant;

    use chisel_decoders::utf8::Utf8Decoder;

    use crate::lexer::coords::Span;
    use crate::lexer::errors::{ParserError, ParserResult};
    use crate::lexer::lexer_core::{Lexer, PackedToken, Token};
    use crate::{lines_from_relative_file, reader_from_bytes};

    #[test]
    fn should_parse_basic_tokens() {
        let mut reader = reader_from_bytes!("{}[],:");
        let mut decoder = Utf8Decoder::new(&mut reader);
        let mut lexer = Lexer::new(&mut decoder);
        let mut tokens: Vec<Token> = vec![];
        let mut spans: Vec<Span> = vec![];
        for _ in 1..=7 {
            let token = lexer.consume().unwrap();
            tokens.push(token.0);
            spans.push(token.1);
        }
        assert_eq!(
            tokens,
            [
                Token::StartObject,
                Token::EndObject,
                Token::StartArray,
                Token::EndArray,
                Token::Comma,
                Token::Colon,
                Token::EndOfInput
            ]
        );
    }

    #[test]
    fn should_parse_null_and_booleans() {
        let mut reader = reader_from_bytes!("null true    falsetruefalse");
        let mut decoder = Utf8Decoder::new(&mut reader);
        let mut lexer = Lexer::new(&mut decoder);
        let mut tokens: Vec<Token> = vec![];
        let mut spans: Vec<Span> = vec![];
        for _ in 1..=6 {
            let token = lexer.consume().unwrap();
            tokens.push(token.0);
            spans.push(token.1);
        }
        assert_eq!(
            tokens,
            [
                Token::Null,
                Token::Boolean(true),
                Token::Boolean(false),
                Token::Boolean(true),
                Token::Boolean(false),
                Token::EndOfInput
            ]
        );
    }

    #[test]
    fn should_parse_strings() {
        let lines = lines_from_relative_file!("fixtures/utf-8/strings.txt");
        for l in lines.flatten() {
            if !l.is_empty() {
                let mut reader = reader_from_bytes!(l);
                let mut decoder = Utf8Decoder::new(&mut reader);
                let mut lexer = Lexer::new(&mut decoder);
                let token = lexer.consume().unwrap();
                match token.0 {
                    Token::Str(str) => {
                        assert_eq!(str, l)
                    }
                    _ => panic!(),
                }
            }
        }
    }

    #[test]
    fn should_report_correct_error_char_position() {
        let mut reader = reader_from_bytes!("{\"abc\" : \nd}");
        let mut decoder = Utf8Decoder::new(&mut reader);
        let mut lexer = Lexer::new(&mut decoder);
        let mut results = vec![];
        for _ in 0..4 {
            results.push(lexer.consume())
        }
        assert!(&results[3].is_err());
        let coords = results[3].clone().err().unwrap().coords.unwrap();
        assert_eq!(coords.absolute, 11);
        assert_eq!(coords.line, 2)
    }

    #[test]
    fn should_parse_numerics() {
        let start = Instant::now();
        let lines = lines_from_relative_file!("fixtures/utf-8/numbers.txt");
        for l in lines.flatten() {
            if !l.is_empty() {
                println!("Parsing {}", l);
                let mut reader = reader_from_bytes!(l);
                let mut decoder = Utf8Decoder::new(&mut reader);
                let mut lexer = Lexer::new(&mut decoder);
                let token = lexer.consume().unwrap();
                match token.0 {
                    Token::Integer(_) => {
                        assert_eq!(
                            token.0,
                            Token::Integer(l.replace(',', "").parse::<i64>().unwrap())
                        );
                    }
                    Token::Float(_) => {
                        assert_eq!(
                            token.0,
                            Token::Float(fast_float::parse(l.replace(',', "")).unwrap())
                        );
                    }
                    _ => panic!(),
                }
            }
        }
        println!("Parsed numerics in {:?}", start.elapsed());
    }

    #[test]
    fn should_correctly_handle_invalid_numbers() {
        let lines = lines_from_relative_file!("fixtures/utf-8/invalid_numbers.txt");
        for l in lines.flatten() {
            if !l.is_empty() {
                let mut reader = reader_from_bytes!(l);
                let mut decoder = Utf8Decoder::new(&mut reader);
                let mut lexer = Lexer::new(&mut decoder);
                let token = lexer.consume();
                assert!(token.is_err());
            }
        }
    }

    #[test]
    fn should_correctly_identity_dodgy_strings() {
        let lines = lines_from_relative_file!("fixtures/utf-8/dodgy_strings.txt");
        for l in lines.flatten() {
            if !l.is_empty() {
                let mut reader = reader_from_bytes!(l);
                let mut decoder = Utf8Decoder::new(&mut reader);
                let mut lexer = Lexer::new(&mut decoder);
                let mut error_token: Option<ParserError> = None;
                loop {
                    let token = lexer.consume();
                    match token {
                        Ok(packed) => {
                            if packed.0 == Token::EndOfInput {
                                break;
                            }
                        }
                        Err(err) => {
                            error_token = Some(err.clone());
                            println!("Dodgy string found: {} : {}", l, err.coords.unwrap());
                            break;
                        }
                    }
                }
                assert!(error_token.is_some());
            }
        }
    }

    #[test]
    fn should_correctly_report_errors_for_booleans() {
        let mut reader = reader_from_bytes!("true farse");
        let mut decoder = Utf8Decoder::new(&mut reader);
        let mut lexer = Lexer::new(&mut decoder);
        let mut results: Vec<ParserResult<PackedToken>> = vec![];
        for _ in 1..=2 {
            results.push(lexer.consume());
        }

        // check that we've got the correct types of results
        assert!(results[0].is_ok());
        assert!(results[1].is_err());

        // check that we've located a boolean in the correct position
        if results[0].is_ok() {
            match &results[0] {
                Ok(packed) => {
                    assert_eq!((*packed).1.start.column, 1)
                }
                Err(_) => {}
            }
        }

        // check that the dodgy boolean has been picked up at the correct location
        if results[1].is_err() {
            match &results[1] {
                Ok(_) => {}
                Err(err) => {
                    assert_eq!(err.coords.unwrap().column, 6)
                }
            }
        }

        println!("Parse error: {:?}", results[1]);
    }
}