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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
//! flexi-parse is a crate for parsing arbitrary syntax into a syntax tree. It
//! is intended to be more flexible than a parser generator or parser
//! combinator, while still being simple to use.

#![cfg_attr(all(doc, not(doctest)), feature(doc_auto_cfg))]

use std::borrow::Cow;
use std::collections::HashSet;
use std::fmt;
use std::fs;
use std::io;
use std::iter::Extend;
use std::path::Path;
use std::ptr;
use std::result;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::OnceLock;

pub mod error;
use error::Error;
use error::ErrorKind;

pub mod group;

mod lookahead;
pub use lookahead::Lookahead;
pub use lookahead::Peek;

pub mod punctuated;

mod scanner;

mod to_string;

pub mod to_tokens;

pub mod token;
use to_tokens::ToTokens;
use token::Ident;
use token::SingleCharPunct;
use token::Token;
use token::WhiteSpace;

#[cfg(feature = "proc-macro2")]
mod proc_macro;
#[cfg(feature = "proc-macro2")]
pub use proc_macro::ToTokensWrapper;

fn default_source_file<'a>() -> &'a Arc<SourceFile> {
    static DEFAULT_SOURCE_FILE: OnceLock<Arc<SourceFile>> = OnceLock::new();
    DEFAULT_SOURCE_FILE.get_or_init(|| {
        Arc::new(SourceFile {
            name: String::new(),
            path: None,
            contents: String::new(),
        })
    })
}

/// A struct representing a file of source code.
///
/// This type is the input to [`parse_source`].
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SourceFile {
    name: String,
    path: Option<String>,
    contents: String,
}

impl SourceFile {
    /// Reads the file at the given path into a `SourceFile`.
    ///
    /// ## Errors
    /// This function returns an error if the given path is not readable.
    pub fn read(path: &Path) -> io::Result<SourceFile> {
        let name = path
            .file_name()
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "invalid filename"))?
            .to_string_lossy()
            .into_owned();
        let contents = fs::read_to_string(path)?;
        Ok(SourceFile {
            name,
            path: Some(path.to_string_lossy().into_owned()),
            contents,
        })
    }

    /// Creates a new `SourceFile` with the given name and contents.
    pub const fn new(name: String, contents: String) -> SourceFile {
        SourceFile {
            name,
            path: None,
            contents,
        }
    }

    fn id(&self) -> &String {
        self.path.as_ref().unwrap_or(&self.name)
    }
}

impl fmt::Debug for SourceFile {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SourceFile")
            .field("name", &self.name)
            .field("path", &self.path)
            .finish_non_exhaustive()
    }
}

/// A region of source code.
///
/// Note that unlike [`proc_macro::Span`], this struct contains a reference to
/// the file containing it.
///
/// [`proc_macro::Span`]: https://doc.rust-lang.org/stable/proc_macro/struct.Span.html
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Span {
    start: usize,
    end: usize,
    source: Arc<SourceFile>,
}

impl Span {
    const fn new(start: usize, end: usize, source: Arc<SourceFile>) -> Span {
        Span { start, end, source }
    }

    /// Create a new [`Span`] covering no tokens.
    ///
    /// This span has a special 'sentinel' source file which is automatically
    /// overridden when joining with [`Span::across`].
    pub fn empty() -> Span {
        Span {
            start: 0,
            end: 0,
            source: Arc::clone(default_source_file()),
        }
    }

    /// Create a new [`Span`] from the start of `start` to the end of `end`.
    ///
    /// ## Panics
    /// This function will panic if `start` and `end` come from different source
    /// files and neither of them .
    pub fn across(start: &Span, end: &Span) -> Span {
        if &start.source == default_source_file() {
            Span {
                start: start.start,
                end: end.end,
                source: Arc::clone(&end.source),
            }
        } else {
            assert_eq!(
                start.source, end.source,
                "both inputs to `across` must come from the same source file"
            );
            Span {
                start: start.start,
                end: end.end,
                source: Arc::clone(&start.source),
            }
        }
    }

    #[doc(hidden)]
    pub const fn source(&self) -> &Arc<SourceFile> {
        &self.source
    }

    /// Returns the start line and start column.
    fn start_location(&self) -> (usize, usize) {
        let mut newlines = 0;
        let mut last_newline = 0;
        for (i, char) in self.source.contents[..self.start].chars().enumerate() {
            if char == '\n' {
                newlines += 1;
                last_newline = i;
            }
        }

        (newlines + 1, self.start - last_newline + 1)
    }

    /// Returns true if `self` covers no tokens.
    pub const fn is_empty(&self) -> bool {
        self.start == self.end
    }
}

/// Parsing interface for types with a default parsing method.
pub trait Parse: Sized {
    /// Parses the input into this type.
    ///
    /// ## Errors
    /// This function returns an error if `source` doesn't contain a valid instance
    /// of `T`.
    fn parse(input: ParseStream) -> Result<Self>;
}

impl<T: Parse> Parse for Box<T> {
    fn parse(input: ParseStream) -> Result<Self> {
        Ok(Box::new(T::parse(input)?))
    }
}

/// A parser that can parse a stream of tokens into a syntax tree node.
pub trait Parser: Sized {
    /// The return type of this parser.
    type Output;

    /// Parses a [`TokenStream`] into the relevant syntax tree node.
    ///
    /// ## Errors
    /// This function returns an error if `source` doesn't contain a valid
    /// instance of `T`.
    fn parse(self, tokens: TokenStream) -> Result<Self::Output>;
}

impl<F: FnOnce(ParseStream) -> Result<T>, T> Parser for F {
    type Output = T;

    fn parse(self, tokens: TokenStream) -> Result<Self::Output> {
        let cursor = Cursor {
            stream: Cow::Borrowed(tokens.tokens.as_slice()),
            offset: AtomicUsize::new(0),
            last: tokens.tokens.len() - 1,
        };
        self(&ParseBuffer::new(
            cursor,
            Arc::clone(tokens.source.as_ref().unwrap()),
        ))
    }
}

/// Parses the given tokens into the syntax tree node `T`.
///
/// This function ignores all whitespace.
///
/// ## Errors
/// Forwards any error from `T::parse`.
pub fn parse<T: Parse>(mut tokens: TokenStream) -> Result<T> {
    tokens.remove_whitespace();
    Parser::parse(T::parse, tokens)
}

/// Scans and parses the given source file into the syntax tree node `T`.
///
/// This function ignores all whitespace.
///
/// ## Errors
/// Forwards any errors from `T::parse`.
pub fn parse_source<T: Parse>(source: Arc<SourceFile>) -> Result<T> {
    let (tokens, error) = scanner::scan(source, 0, None);
    match parse(tokens) {
        Ok(value) => error.map_or(Ok(value), Err),
        Err(err) => {
            if let Some(error) = error {
                Err(error.with(err))
            } else {
                Err(err)
            }
        }
    }
}

/// Scans and parses the given string into the syntax tree node `T`.
///
/// This function ignores all whitespace.
///
/// ## Errors
/// Forwards any error from `T::parse`.
pub fn parse_string<T: Parse>(source: String) -> Result<T> {
    let source = Arc::new(SourceFile {
        name: "str".to_string(),
        path: None,
        contents: source,
    });
    parse_source(source)
}

/// Attempts to repeatedly parse `input` into the given syntax tree node,
/// using `T`'s default parsing implementation, and continuing until `input` is
/// exhausted.
///
/// Note that this function doesn't perform any error recovery.
///
/// ## Errors
/// Forwards any errors from `T::parse`.
pub fn parse_repeated<T: Parse>(input: ParseStream) -> Result<Vec<T>> {
    let mut items = vec![];

    while !input.is_empty() {
        items.push(input.parse()?);
    }

    Ok(items)
}

/// Gets the `Ok` value, panicking with a formatted error message if the value
/// is `Err`.
///
/// ## Panics
/// Panics if the contained value is `Err`.
#[cfg(feature = "ariadne")]
pub fn pretty_unwrap<T>(result: Result<T>) -> T {
    result.unwrap_or_else(|err| {
        let mut buf = vec![];
        for report in err.to_reports() {
            report.write(&mut buf).unwrap();
        }
        String::from_utf8(buf).map_or_else(
            |_| {
                err.eprint().unwrap();
                panic!("failed due to above errors");
            },
            |s| panic!("{s}"),
        )
    })
}

/// A sequence of tokens.
///
/// This is the return type of
/// [`Group::token_stream`][group::Group::into_token_stream], and can be created
/// from a [`proc_macro::TokenStream`][proc-macro] or
/// [`proc_macro2::TokenStream`][proc-macro2].
///
/// [proc-macro]: https://doc.rust-lang.org/proc_macro/struct.TokenStream.html
/// [proc-macro2]: https://docs.rs/proc-macro2/latest/proc_macro2/struct.TokenStream.html
#[derive(Debug, Clone, PartialEq, PartialOrd, Hash)]
pub struct TokenStream {
    tokens: Vec<Entry>,
    source: Option<Arc<SourceFile>>,
}

impl TokenStream {
    fn new(tokens: Vec<Entry>, source: Option<Arc<SourceFile>>) -> TokenStream {
        TokenStream { tokens, source }
    }

    fn filter<F: FnMut(&TokenStream) -> Vec<usize>>(&mut self, mut function: F) {
        let mut indices = function(self);
        indices.sort_unstable();
        indices.reverse();
        for index in indices {
            self.tokens.remove(index);
        }
    }

    /// Removes all whitespace that doesn't come at the start of a line.
    ///
    /// Note that the `parse*` functions remove all whitespace.
    pub fn prepare_whitespace(&mut self) {
        self.filter(|tokens| {
            let mut indices = vec![];
            let mut post_newline = true;
            for (index, entry) in tokens.tokens.iter().enumerate() {
                if let Entry::WhiteSpace(whitespace) = entry {
                    if matches!(whitespace, WhiteSpace::NewLine(_)) {
                        post_newline = true;
                    } else if !post_newline {
                        indices.push(index);
                    }
                } else {
                    post_newline = false;
                }
            }
            indices
        });
    }

    /// Removes all non-newline whitespace from `self`.
    ///
    /// Note that the `parse*` functions will remove all whitespace.
    pub fn remove_blank_space(&mut self) {
        self.filter(|tokens| {
            let mut indices = vec![];
            for (index, entry) in tokens.tokens.iter().enumerate() {
                if let Entry::WhiteSpace(whitespace) = entry {
                    if !matches!(whitespace, WhiteSpace::NewLine(_)) {
                        indices.push(index);
                    }
                }
            }
            indices
        });
    }

    /// Removes all whitespace tokens from this stream.
    ///
    /// This method is automatically called by the `parse*` functions.
    pub fn remove_whitespace(&mut self) {
        self.filter(|tokens| {
            let mut indices = vec![];
            for (index, entry) in tokens.tokens.iter().enumerate() {
                if let Entry::WhiteSpace(_) = entry {
                    indices.push(index);
                }
            }
            indices
        });
    }

    /// Returns true if there are no tokens in `self`.
    pub fn is_empty(&self) -> bool {
        self.tokens.len() == 1
    }

    fn push(&mut self, entry: Entry) {
        if self.source.is_none() {
            self.source = Some(Arc::clone(&entry.span().source));
        }
        self.tokens.push(entry);
    }

    /// Add another [`TokenStream`] to the end of `self`.
    pub fn append(&mut self, other: &mut TokenStream) {
        self.tokens.append(&mut other.tokens);
    }
}

impl TryFrom<Arc<SourceFile>> for TokenStream {
    type Error = Error;

    fn try_from(value: Arc<SourceFile>) -> Result<Self> {
        let (tokens, error) = scanner::scan(value, 0, None);
        error.map_or(Ok(tokens), Err)
    }
}

impl<A: ToTokens> Extend<A> for TokenStream {
    fn extend<T: IntoIterator<Item = A>>(&mut self, iter: T) {
        for item in iter {
            self.tokens.append(&mut item.into_token_stream().tokens);
        }
    }
}

/// Creates a new error in the given source file, at the given location, and
/// with the given message and code.
///
/// `location` will accept any type that is `Token`, `Delimiter`, or a `Span`.
pub fn new_error<L: Into<Span>>(message: String, location: L, code: u16) -> Error {
    let span = location.into();
    Error::new(
        Arc::clone(&span.source),
        ErrorKind::Custom {
            message,
            span,
            code,
        },
    )
}

/// A cursor position within a token stream.
pub struct ParseBuffer<'a> {
    cursor: Cursor<'a>,
    source: Arc<SourceFile>,
    error: Mutex<Error>,
}

impl<'a> ParseBuffer<'a> {
    fn new(cursor: Cursor<'a>, source: Arc<SourceFile>) -> ParseBuffer<'a> {
        ParseBuffer {
            cursor,
            source,
            error: Mutex::new(Error::empty()),
        }
    }

    /// Attempts to parse `self` into the given syntax tree node, using `T`'s
    /// default parsing implementation.
    ///
    /// ## Errors
    /// Returns an error if `T`'s `Parse` implementation fails.
    pub fn parse<T: Parse>(&self) -> Result<T> {
        T::parse(self)
    }

    /// Returns true if this stream has been exhausted.
    pub fn is_empty(&self) -> bool {
        self.cursor.eof()
    }

    /// Creates a new error at the given location with the given message and
    /// code.
    pub fn new_error<T: Into<Span>>(&self, message: String, location: T, code: u16) -> Error {
        Error::new(
            Arc::clone(&self.source),
            ErrorKind::Custom {
                message,
                span: location.into(),
                code,
            },
        )
    }

    /// Adds a new error to this buffer's storage.
    #[allow(clippy::missing_panics_doc)] // Will not panic.
    pub fn add_error(&self, error: Error) {
        self.error.lock().unwrap().add(error);
    }

    /// Returns an error consisting of all errors from
    /// [`ParseBuffer::add_error`], if it has been called.
    #[allow(clippy::missing_panics_doc)] // Will not panic.
    pub fn get_error(&self) -> Option<Error> {
        let error = self.error.lock().unwrap();
        if error.is_empty() {
            None
        } else {
            Some(error.to_owned())
        }
    }

    /// Repeatedly skips tokens until `function` returns true or `self` is
    /// empty.
    pub fn synchronise<F: FnMut(ParseStream<'_>) -> bool>(&self, mut function: F) {
        while !self.is_empty() && !function(self) {
            let _ = self.next();
        }
        let _ = self.next();
    }

    fn try_parse<T: Parse>(&self) -> Result<T> {
        let offset = self.cursor.offset.load(Ordering::SeqCst);
        T::parse(self).map_err(move |err| {
            self.cursor.offset.store(offset, Ordering::SeqCst);
            err
        })
    }

    /// Parses `T1` and `T2`, with no whitespace allowed between them.
    ///
    /// ## Errors
    /// Returns an error if `self` does not start with the required tokens.
    pub fn parse_joint<T1: Token, T2: Token>(&self) -> Result<(T1, T2)> {
        if self.current()?.span().end < self.next()?.span().start {
            return Err(Error::new(
                Arc::clone(&self.source),
                ErrorKind::UnexpectedToken {
                    expected: HashSet::from_iter([T1::display() + &T2::display()]),
                    span: self.current()?.span().to_owned(),
                },
            ));
        }
        let t1 = self.parse()?;
        let t2 = self.parse()?;
        Ok((t1, t2))
    }

    /// Attempts to parse `self` into `Vec<T>`, with no separating punctuation,
    /// fully consuming `self`.
    ///
    /// To parse separated instances of `T`, see
    /// [Punctuated][punctuated::Punctuated].
    ///
    /// ## Errors
    /// Returns an error if `self` is not a valid sequence of `T`.
    pub fn parse_repeated<T: Parse>(&self) -> Result<Vec<T>> {
        let mut items = vec![];

        while !self.is_empty() {
            items.push(self.parse()?);
        }

        Ok(items)
    }

    /// Returns true if the next token is an instance of `T`.
    #[allow(clippy::needless_pass_by_value)]
    pub fn peek<T: Peek>(&self, token: T) -> bool {
        let _ = token;
        self.parse_undo::<T::Token>().is_ok()
    }

    /// Returns true if the next token is an instance of `T`.
    ///
    /// Note that for the purposes of this function, multi-character punctuation
    /// like `+=` is considered to be two tokens, and float literals are
    /// considered to be three tokens (start, `.`, end).
    pub fn peek2<T: Peek>(&self, token: T) -> bool {
        let buffer = self.fork();
        let _ = buffer.next();
        buffer.peek::<T>(token)
    }

    fn parse_undo<T: Parse>(&self) -> Result<T> {
        let offset = self.cursor.offset.load(Ordering::SeqCst);
        let val = T::parse(self);
        self.cursor.offset.store(offset, Ordering::SeqCst);
        val
    }

    fn report_error_tokens(&self) -> Result<()> {
        let mut error = false;
        while let (Entry::Error(_), offset) = self.cursor.next() {
            self.cursor.offset.store(offset, Ordering::SeqCst);
            error = true;
        }
        if error {
            Err(Error::new(Arc::clone(&self.source), ErrorKind::Silent))
        } else {
            Ok(())
        }
    }

    fn next(&'a self) -> Result<&'a Entry> {
        self.report_error_tokens()?;
        if self.cursor.eof() {
            Err(Error::new(
                Arc::clone(&self.source),
                ErrorKind::EndOfFile(self.source.contents.len()),
            ))
        } else {
            Ok(self.next_raw())
        }
    }

    fn next_raw(&'a self) -> &'a Entry {
        let (token, offset) = self.cursor.next();
        self.cursor.offset.store(offset, Ordering::SeqCst);
        token
    }

    fn current(&'a self) -> Result<&'a Entry> {
        self.report_error_tokens()?;
        if self.cursor.eof() {
            Err(Error::new(
                Arc::clone(&self.source),
                ErrorKind::EndOfFile(self.source.contents.len()),
            ))
        } else {
            Ok(self.cursor.current())
        }
    }

    /// Gets the span of the current token.
    ///
    /// ## Errors
    /// Returns an error if `self` is empty.
    pub fn current_span(&self) -> Result<Span> {
        Ok(self.current()?.span().to_owned())
    }

    fn get_relative(&'a self, offset: isize) -> Result<&'a Entry> {
        self.cursor.get_relative(offset).ok_or(Error::new(
            Arc::clone(&self.source),
            ErrorKind::EndOfFile(self.source.contents.len()),
        ))
    }

    /// Creates a new `ParseBuffer` at the same position as `self`.
    ///
    /// Changes to `self` will not affect the fork, and vice versa.
    #[must_use]
    pub fn fork(&self) -> ParseBuffer<'a> {
        ParseBuffer::new(self.cursor.clone(), Arc::clone(&self.source))
    }

    /// Commits a forked buffer into `self`, updating `self` to reflect `fork`.
    ///
    /// ## Panics
    /// This function will panic if `fork` wasn't forked from `self` or if
    /// `self` is further ahead than `fork`.
    pub fn commit(&self, fork: &Self) {
        if !ptr::eq(self.cursor.stream.as_ptr(), fork.cursor.stream.as_ptr()) {
            panic!("cannot commit ParseBuffer that wasn't forked from this buffer");
        } else if fork.cursor.offset.load(Ordering::SeqCst)
            < self.cursor.offset.load(Ordering::SeqCst)
        {
            panic!("cannot commit original ParseBuffer into fork");
        }
        self.cursor
            .offset
            .store(fork.cursor.offset.load(Ordering::SeqCst), Ordering::SeqCst);
    }

    /// Creates an error with the message `Unexpected token` and the given
    /// expected tokens.
    ///
    /// Use of this function is generally discouraged in favour of
    /// [`Lookahead::error`].
    pub fn unexpected_token(&self, expected: HashSet<String>) -> Error {
        let current = match self.current() {
            Ok(current) => current,
            Err(err) => return err,
        };
        Error::new(
            Arc::clone(&self.source),
            ErrorKind::UnexpectedToken {
                expected,
                span: current.span().clone(),
            },
        )
    }

    /// Creates a helper struct for peeking at the next token.
    pub fn lookahead(&self) -> Lookahead<'a> {
        Lookahead::new(self.fork())
    }

    /// Skips over all whitespace tokens before the next non-whitespace token.
    ///
    /// This method will not skip newlines.
    pub fn skip_whitespace(&self) {
        while let (Entry::WhiteSpace(whitespace), offset) = self.cursor.next() {
            if matches!(whitespace, WhiteSpace::NewLine(_)) {
                break;
            }
            self.cursor.offset.store(offset, Ordering::SeqCst);
        }
    }

    /// Creates a new empty Span with this stream's source file.
    pub fn empty_span(&self) -> Span {
        Span {
            start: 0,
            end: 0,
            source: Arc::clone(&self.source),
        }
    }
}

impl fmt::Debug for ParseBuffer<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ParseBuffer")
            .field(
                "tokens",
                &&self.cursor.stream[self.cursor.offset.load(Ordering::SeqCst)..],
            )
            .field("source", &self.source)
            .field("error", &self.error)
            .finish()
    }
}

impl<'a> From<TokenStream> for ParseBuffer<'a> {
    fn from(value: TokenStream) -> Self {
        let last = value.tokens.len() - 1;
        let cursor = Cursor {
            stream: Cow::Owned(value.tokens),
            offset: AtomicUsize::new(0),
            last,
        };
        ParseBuffer::new(
            cursor,
            value
                .source
                .unwrap_or_else(|| Arc::clone(default_source_file())),
        )
    }
}

impl<'a> From<&'a TokenStream> for ParseBuffer<'a> {
    fn from(value: &'a TokenStream) -> Self {
        let last = value.tokens.len() - 1;
        let cursor = Cursor {
            stream: Cow::Borrowed(&value.tokens),
            offset: AtomicUsize::new(0),
            last,
        };
        let source = Arc::clone(value.source.as_ref().unwrap_or_else(default_source_file));
        ParseBuffer::new(cursor, source)
    }
}

/// Returns true if [`ParseBuffer::peek`] would return true for any types
/// passed.
///
/// Accepts a `ParseStream` followed by one or more types.
#[macro_export]
macro_rules! peek_any {
    ( $input:expr, $( $ty:tt ),+ $(,)? ) => {
        $( $input.peek($ty) || )+ false
    };
}

/// Returns true if [`ParseBuffer::peek2`] would return true for any types
/// passed.
///
/// Accepts a `ParseStream` followed by one or more types.
#[macro_export]
macro_rules! peek2_any {
    ( $input:expr, $( $ty:tt ),+ $(,)? ) => {
        $( $input.peek2($ty) || )+ false
    };
}

/// The input type for all parsing functions.
pub type ParseStream<'a> = &'a ParseBuffer<'a>;

#[derive(Debug)]
struct Cursor<'a> {
    stream: Cow<'a, [Entry]>,
    offset: AtomicUsize,
    last: usize,
}

impl<'a> Cursor<'a> {
    fn bump(&self) -> usize {
        let offset = self.offset.load(Ordering::SeqCst);
        if offset == self.last {
            offset
        } else {
            offset + 1
        }
    }

    fn current(&'a self) -> &'a Entry {
        &self.stream[self.offset.load(Ordering::SeqCst)]
    }

    pub fn eof(&self) -> bool {
        self.offset.load(Ordering::SeqCst) == self.last
    }

    fn next(&'a self) -> (&'a Entry, usize) {
        let token_tree = self.current();
        let offset = self.bump();
        (token_tree, offset)
    }

    fn get_relative(&'a self, offset: isize) -> Option<&'a Entry> {
        let current_offset = self.offset.load(Ordering::SeqCst);
        let index = if offset < 0 {
            current_offset - offset.unsigned_abs()
        } else {
            current_offset + offset.unsigned_abs()
        };
        self.stream.get(index)
    }
}

impl<'a> Clone for Cursor<'a> {
    fn clone(&self) -> Self {
        Cursor {
            stream: self.stream.clone(),
            offset: AtomicUsize::new(self.offset.load(Ordering::SeqCst)),
            last: self.last,
        }
    }
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Hash)]
enum Entry {
    Error(Span),
    Ident(Ident),
    Punct(SingleCharPunct),
    WhiteSpace(WhiteSpace),
    End,
}

impl Entry {
    fn span(&self) -> &Span {
        match self {
            Entry::Error(span) => span,
            Entry::Ident(ident) => &ident.span,
            Entry::Punct(punct) => &punct.span,
            Entry::WhiteSpace(whitespace) => whitespace.span(),
            Entry::End => unreachable!(),
        }
    }

    #[cfg(feature = "proc-macro2")]
    fn set_span(&mut self, span: Span) {
        match self {
            Entry::Error(current_span) => *current_span = span,
            Entry::Ident(ident) => ident.span = span,
            Entry::Punct(punct) => punct.span = span,
            Entry::WhiteSpace(whitespace) => whitespace.set_span(span),
            Entry::End => unreachable!(),
        }
    }
}

impl From<Ident> for Entry {
    fn from(value: Ident) -> Self {
        Self::Ident(value)
    }
}

impl From<SingleCharPunct> for Entry {
    fn from(value: SingleCharPunct) -> Self {
        Self::Punct(value)
    }
}

/// The return type of a parsing function.
pub type Result<T> = result::Result<T, Error>;

#[doc(hidden)]
pub mod private {
    pub trait Sealed {}

    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
    pub enum Marker {}
}

#[cfg(test)]
mod tests;