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
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
use crate::lib::fmt;

#[cfg(feature = "std")]
use std::error::Error as StdError;

use crate::{stream::StreamOnce, ErrorOffset};

use self::ParseResult::*;

pub(crate) trait ResultExt<E, T> {
    fn committed(self) -> ParseResult<E, T>;
}

impl<E, T> ResultExt<E, T> for Result<E, T> {
    fn committed(self) -> ParseResult<E, T> {
        match self {
            Ok(x) => CommitOk(x),
            Err(x) => CommitErr(x),
        }
    }
}

#[macro_export]
#[doc(hidden)]
macro_rules! ctry {
    ($result:expr) => {
        match $result {
            $crate::error::ParseResult::CommitOk(x) => (x, $crate::error::Commit::Commit(())),
            $crate::error::ParseResult::PeekOk(x) => (x, $crate::error::Commit::Peek(())),
            $crate::error::ParseResult::CommitErr(err) => {
                return $crate::error::ParseResult::CommitErr(err.into())
            }
            $crate::error::ParseResult::PeekErr(err) => {
                return $crate::error::ParseResult::PeekErr(err.into())
            }
        }
    };
}

/// Trait for types which can be used to construct error information.
///
/// To call functions expecting this trait, use the wrapper types defined in this module
/// `Token`, `Range`, `Format` or `Static`/`&'static str`
pub trait ErrorInfo<'s, T, R> {
    type Format: fmt::Display;
    fn into_info(&'s self) -> Info<T, R, Self::Format>;
}

impl<'s, 'a, T, R, F> ErrorInfo<'s, T, R> for &'a F
where
    F: ErrorInfo<'s, T, R>,
{
    type Format = F::Format;
    fn into_info(&'s self) -> Info<T, R, Self::Format> {
        (**self).into_info()
    }
}

#[derive(Clone, Debug)]
pub enum Info<T, R, F = &'static str> {
    Token(T),
    Range(R),
    Static(&'static str),
    Format(F),
}

impl<'s, T, R, F> ErrorInfo<'s, T, R> for Info<T, R, F>
where
    T: Clone,
    R: Clone,
    F: fmt::Display + 's,
{
    type Format = &'s F;
    fn into_info(&'s self) -> Info<T, R, <Self as ErrorInfo<'_, T, R>>::Format> {
        match self {
            Info::Token(b) => Info::Token(b.clone()),
            Info::Range(b) => Info::Range(b.clone()),
            Info::Static(b) => Info::Static(*b),
            Info::Format(b) => Info::Format(b),
        }
    }
}

impl<R, F> From<char> for Info<char, R, F> {
    fn from(s: char) -> Self {
        Info::Token(s)
    }
}

impl<'s, R> ErrorInfo<'s, char, R> for char {
    type Format = &'static str;
    fn into_info(&self) -> Info<char, R, Self::Format> {
        Info::Token(*self)
    }
}

impl<T, R, F> From<&'static str> for Info<T, R, F> {
    fn from(s: &'static str) -> Self {
        Info::Static(s)
    }
}

impl<'s, T, R> ErrorInfo<'s, T, R> for &'static str {
    type Format = &'static str;
    fn into_info(&self) -> Info<T, R, Self::Format> {
        Info::Static(*self)
    }
}

impl<R, F> From<u8> for Info<u8, R, F> {
    fn from(s: u8) -> Self {
        Info::Token(s)
    }
}

impl<R> ErrorInfo<'_, Self, R> for u8 {
    type Format = &'static str;
    fn into_info(&self) -> Info<Self, R, Self::Format> {
        Info::Token(*self)
    }
}

/// Newtype which constructs an `Info::Token` through `ErrorInfo`
pub struct Token<T>(pub T);

impl<T, R> From<Token<T>> for Info<T, R, &'static str> {
    fn from(s: Token<T>) -> Self {
        Info::Token(s.0)
    }
}

impl<'s, T, R> ErrorInfo<'s, T, R> for Token<T>
where
    T: Clone,
{
    type Format = &'static str;
    fn into_info(&'s self) -> Info<T, R, Self::Format> {
        Info::Token(self.0.clone())
    }
}

/// Newtype which constructs an `Info::Range` through `ErrorInfo`
pub struct Range<R>(pub R);

impl<T, R> From<Range<R>> for Info<T, R, &'static str> {
    fn from(s: Range<R>) -> Self {
        Info::Range(s.0)
    }
}

impl<'s, T, R> ErrorInfo<'s, T, R> for Range<R>
where
    R: Clone,
{
    type Format = &'static str;
    fn into_info(&'s self) -> Info<T, R, Self::Format> {
        Info::Range(self.0.clone())
    }
}

/// Newtype which constructs an `Info::Static` through `ErrorInfo`
/// A plain `&'static str` can also be used, this exists for consistency.
pub struct Static(&'static str);

impl<T, R, F> From<Static> for Info<T, R, F>
where
    F: fmt::Display,
{
    fn from(s: Static) -> Self {
        Info::Static(s.0)
    }
}

impl<'s, T, R> ErrorInfo<'s, T, R> for Static {
    type Format = &'static str;
    fn into_info(&'s self) -> Info<T, R, Self::Format> {
        Info::Static(self.0)
    }
}

/// Newtype which constructs an `Info::Format` through `ErrorInfo`
pub struct Format<F>(pub F)
where
    F: fmt::Display;

impl<T, R, F> From<Format<F>> for Info<T, R, F>
where
    F: fmt::Display,
{
    fn from(s: Format<F>) -> Self {
        Info::Format(s.0)
    }
}

impl<'s, T, R, F> ErrorInfo<'s, T, R> for Format<F>
where
    F: fmt::Display + 's,
{
    type Format = &'s F;
    fn into_info(&'s self) -> Info<T, R, Self::Format> {
        Info::Format(&self.0)
    }
}

/// Enum used to indicate if a parser committed any items of the stream it was given as an input.
///
/// This is used by parsers such as `or` and `choice` to determine if they should try to parse
/// with another parser as they will only be able to provide good error reporting if the preceding
/// parser did not commit to the parse.
#[derive(Clone, PartialEq, Debug, Copy)]
pub enum Commit<T> {
    /// Constructor indicating that the parser has committed to this parse. If a parser after this fails,
    /// other parser alternatives will not be attempted (`CommitErr` will be returned)
    Commit(T),
    /// Constructor indicating that the parser has not committed to this parse. If a parser after this fails,
    /// other parser alternatives will be attempted (`EmptyErr` will be returned)
    Peek(T),
}

impl<T> AsMut<T> for Commit<T> {
    fn as_mut(&mut self) -> &mut T {
        match *self {
            Commit::Peek(ref mut t) | Commit::Commit(ref mut t) => t,
        }
    }
}

impl<T> AsRef<T> for Commit<T> {
    fn as_ref(&self) -> &T {
        match *self {
            Commit::Peek(ref t) | Commit::Commit(ref t) => t,
        }
    }
}

impl<T> Commit<T> {
    /// Returns true if `self` is peek.
    pub fn is_peek(&self) -> bool {
        match *self {
            Commit::Peek(_) => true,
            Commit::Commit(_) => false,
        }
    }

    /// Extracts the contained value.
    pub fn into_inner(self) -> T {
        match self {
            Commit::Peek(x) | Commit::Commit(x) => x,
        }
    }

    /// Converts `self` into the `Commit` state.
    pub fn into_commit(self) -> Commit<T> {
        Commit::Commit(self.into_inner())
    }

    /// Converts `self` into the `Peek` state.
    pub fn into_peek(self) -> Commit<T> {
        Commit::Peek(self.into_inner())
    }

    /// Maps over the contained value without changing the committed state.
    pub fn map<F, U>(self, f: F) -> Commit<U>
    where
        F: FnOnce(T) -> U,
    {
        match self {
            Commit::Peek(x) => Commit::Peek(f(x)),
            Commit::Commit(x) => Commit::Commit(f(x)),
        }
    }

    pub fn merge(&self, current: Commit<T>) -> Commit<T> {
        match *self {
            Commit::Peek(_) => current,
            Commit::Commit(_) => current.into_commit(),
        }
    }

    /// Combines the `Commit` flags from `self` and the result of `f`.
    ///
    /// ```text
    /// Peek    <> Peek    -> Peek
    /// Commit <> Peek    -> Commit
    /// Peek    <> Commit -> Commit
    /// Commit <> Commit -> Commit
    /// ```
    ///
    /// ```
    /// # extern crate combine as pc;
    /// # use pc::*;
    /// # fn main() {
    /// //Parses a character of string literal and handles the escaped characters \\ and \" as \
    /// //and " respectively
    /// fn char<Input>(input: &mut Input) -> StdParseResult<char, Input>
    ///     where Input: Stream<Token = char>,
    ///           Input::Error: ParseError<Input::Token, Input::Range, Input::Position>,
    /// {
    ///     let (c, committed) = satisfy(|c| c != '"').parse_stream(input).into_result()?;
    ///     match c {
    ///         //Since the `char` parser has already committed some of the input `combine` is used
    ///         //propagate the committed state to the next part of the parser
    ///         '\\' => committed.combine(|_| {
    ///             satisfy(|c| c == '"' || c == '\\')
    ///                 .map(|c| {
    ///                     match c {
    ///                         '"' => '"',
    ///                         '\\' => '\\',
    ///                         c => c
    ///                     }
    ///                 })
    ///                 .parse_stream(input)
    ///                 .into_result()
    ///             }),
    ///         _ => Ok((c, committed))
    ///     }
    /// }
    /// let result = many(parser(char))
    ///     .easy_parse(r#"abc\"\\"#);
    /// assert_eq!(result, Ok((r#"abc"\"#.to_string(), "")));
    /// }
    /// ```
    pub fn combine<F, U, E>(self, f: F) -> StdParseResult2<U, E>
    where
        F: FnOnce(T) -> StdParseResult2<U, E>,
    {
        match self {
            Commit::Commit(x) => match f(x) {
                Ok((v, Commit::Peek(rest))) => Ok((v, Commit::Commit(rest))),
                Err(Commit::Peek(err)) => Err(Commit::Commit(err)),
                y => y,
            },
            Commit::Peek(x) => f(x),
        }
    }
    pub fn combine_commit<F, U, E>(self, f: F) -> ParseResult<U, E>
    where
        F: FnOnce(T) -> ParseResult<U, E>,
    {
        use self::ParseResult::*;

        match self {
            Commit::Commit(x) => match f(x) {
                PeekOk(v) => CommitOk(v),
                PeekErr(err) => CommitErr(err.error),
                y => y,
            },
            Commit::Peek(x) => f(x),
        }
    }
}

/// A type alias over the specific `Result` type used by parsers to indicate whether they were
/// successful or not.
/// `O` is the type that is output on success.
/// `Input` is the specific stream type used in the parser.
pub type StdParseResult<O, Input> =
    Result<(O, Commit<()>), Commit<Tracked<<Input as StreamOnce>::Error>>>;
pub type StdParseResult2<O, E> = Result<(O, Commit<()>), Commit<Tracked<E>>>;

/// `StreamError` represents a single error returned from a `Stream` or a `Parser`.
///
/// Usually multiple instances of `StreamError` is composed into a `ParseError` to build the final
/// error value.
pub trait StreamError<Item, Range>: Sized {
    fn unexpected_token(token: Item) -> Self;
    fn unexpected_range(token: Range) -> Self;
    fn unexpected_format<T>(msg: T) -> Self
    where
        T: fmt::Display;
    fn unexpected<E>(info: E) -> Self
    where
        E: for<'s> ErrorInfo<'s, Item, Range>,
    {
        match info.into_info() {
            Info::Token(b) => Self::unexpected_token(b),
            Info::Range(b) => Self::unexpected_range(b),
            Info::Static(b) => Self::unexpected_static_message(b),
            Info::Format(b) => Self::unexpected_format(b),
        }
    }
    fn unexpected_static_message(msg: &'static str) -> Self {
        Self::unexpected_format(msg)
    }

    fn expected_token(token: Item) -> Self;
    fn expected_range(token: Range) -> Self;
    fn expected_format<T>(msg: T) -> Self
    where
        T: fmt::Display;
    fn expected<E>(info: E) -> Self
    where
        E: for<'s> ErrorInfo<'s, Item, Range>,
    {
        match info.into_info() {
            Info::Token(b) => Self::expected_token(b),
            Info::Range(b) => Self::expected_range(b),
            Info::Static(b) => Self::expected_static_message(b),
            Info::Format(b) => Self::expected_format(b),
        }
    }
    fn expected_static_message(msg: &'static str) -> Self {
        Self::expected_format(msg)
    }

    fn message_token(token: Item) -> Self;
    fn message_range(token: Range) -> Self;
    fn message_format<T>(msg: T) -> Self
    where
        T: fmt::Display;
    fn message_static_message(msg: &'static str) -> Self {
        Self::message_format(msg)
    }
    fn message<E>(info: E) -> Self
    where
        E: for<'s> ErrorInfo<'s, Item, Range>,
    {
        match info.into_info() {
            Info::Token(b) => Self::message_token(b),
            Info::Range(b) => Self::message_range(b),
            Info::Static(b) => Self::message_static_message(b),
            Info::Format(b) => Self::message_format(b),
        }
    }

    #[cfg(feature = "std")]
    fn other<E>(err: E) -> Self
    where
        E: StdError + Send + Sync + 'static,
    {
        Self::message_format(err)
    }

    fn end_of_input() -> Self {
        Self::unexpected_static_message("end of input")
    }

    fn is_unexpected_end_of_input(&self) -> bool;

    /// Converts `self` into a different `StreamError` type.
    ///
    /// This should aim to preserve as much information as possible into the returned `T` value but
    /// if `Self` ignores some information passed to it using one of the constructors that
    /// information is naturally lost.
    fn into_other<T>(self) -> T
    where
        T: StreamError<Item, Range>;
}

/// Trait which defines a combine parse error.
///
/// A parse error is composed of zero or more `StreamError` instances which gets added to it as
/// errors are encountered during parsing.
pub trait ParseError<Item, Range, Position>: Sized + PartialEq {
    type StreamError: StreamError<Item, Range>;

    /// Constructs an empty error.
    ///
    /// An empty error is expected to be cheap to create as it is frequently created and discarded.
    fn empty(position: Position) -> Self;

    /// Creates a `ParseError` from a single `Self::StreamError`
    fn from_error(position: Position, err: Self::StreamError) -> Self {
        let mut errors = Self::empty(position);
        errors.add(err);
        errors
    }

    /// Sets the position of this `ParseError`
    fn set_position(&mut self, position: Position);

    /// Merges two errors. If they exist at the same position the errors of `other` are
    /// added to `self` (using the semantics of `add`). If they are not at the same
    /// position the error furthest ahead are returned, ignoring the other `ParseError`.
    fn merge(self, other: Self) -> Self {
        other
    }

    /// Adds a `StreamError` to `self`.
    ///
    /// It is up to each individual error type to define what adding an error does, some may push
    /// it to a vector while others may only keep `self` or `err` to avoid allocation
    fn add(&mut self, err: Self::StreamError);

    fn add_expected<E>(&mut self, info: E)
    where
        E: for<'s> ErrorInfo<'s, Item, Range>,
    {
        self.add(Self::StreamError::expected(info))
    }

    fn add_unexpected<E>(&mut self, info: E)
    where
        E: for<'s> ErrorInfo<'s, Item, Range>,
    {
        self.add(Self::StreamError::unexpected(info))
    }

    fn add_message<E>(&mut self, info: E)
    where
        E: for<'s> ErrorInfo<'s, Item, Range>,
    {
        self.add(Self::StreamError::message(info))
    }

    /// Sets `info` as the *only* `Expected` error of `self`
    fn set_expected<F>(self_: &mut Tracked<Self>, info: Self::StreamError, f: F)
    where
        F: FnOnce(&mut Tracked<Self>);

    /// Removes any expected errors currently in `self`
    fn clear_expected(&mut self) {}

    fn is_unexpected_end_of_input(&self) -> bool;

    /// Does a best-effort conversion of `self` into another `ParseError`
    fn into_other<T>(self) -> T
    where
        T: ParseError<Item, Range, Position>;
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum UnexpectedParse {
    Eoi,
    Unexpected,
}

impl fmt::Display for UnexpectedParse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

#[cfg(feature = "std")]
impl StdError for UnexpectedParse {
    fn description(&self) -> &str {
        self.as_str()
    }
}

impl UnexpectedParse {
    fn as_str(&self) -> &str {
        use self::UnexpectedParse::*;
        match *self {
            Unexpected => "unexpected parse",
            Eoi => "unexpected end of input",
        }
    }
}

impl<Item, Range> StreamError<Item, Range> for UnexpectedParse {
    #[inline]
    fn unexpected_token(_: Item) -> Self {
        UnexpectedParse::Unexpected
    }
    #[inline]
    fn unexpected_range(_: Range) -> Self {
        UnexpectedParse::Unexpected
    }
    #[inline]
    fn unexpected_format<T>(_: T) -> Self
    where
        T: fmt::Display,
    {
        UnexpectedParse::Unexpected
    }

    #[inline]
    fn expected_token(_: Item) -> Self {
        UnexpectedParse::Unexpected
    }
    #[inline]
    fn expected_range(_: Range) -> Self {
        UnexpectedParse::Unexpected
    }
    #[inline]
    fn expected_format<T>(_: T) -> Self
    where
        T: fmt::Display,
    {
        UnexpectedParse::Unexpected
    }
    #[inline]
    fn message_format<T>(_: T) -> Self
    where
        T: fmt::Display,
    {
        UnexpectedParse::Unexpected
    }
    #[inline]
    fn message_token(_: Item) -> Self {
        UnexpectedParse::Unexpected
    }
    #[inline]
    fn message_range(_: Range) -> Self {
        UnexpectedParse::Unexpected
    }

    #[inline]
    fn end_of_input() -> Self {
        UnexpectedParse::Eoi
    }

    #[inline]
    fn is_unexpected_end_of_input(&self) -> bool {
        *self == UnexpectedParse::Eoi
    }

    #[inline]
    fn into_other<T>(self) -> T
    where
        T: StreamError<Item, Range>,
    {
        match self {
            UnexpectedParse::Unexpected => T::unexpected_static_message("parse"),
            UnexpectedParse::Eoi => T::end_of_input(),
        }
    }
}

impl<Item, Range, Position> ParseError<Item, Range, Position> for UnexpectedParse
where
    Position: Default,
{
    type StreamError = Self;
    #[inline]
    fn empty(_position: Position) -> Self {
        UnexpectedParse::Unexpected
    }

    #[inline]
    fn from_error(_: Position, err: Self::StreamError) -> Self {
        err
    }

    #[inline]
    fn set_position(&mut self, _position: Position) {}

    #[inline]
    fn add(&mut self, err: Self::StreamError) {
        *self = match (*self, err) {
            (UnexpectedParse::Eoi, _) => UnexpectedParse::Eoi,
            (_, err) => err,
        };
    }

    #[inline]
    fn set_expected<F>(self_: &mut Tracked<Self>, info: Self::StreamError, f: F)
    where
        F: FnOnce(&mut Tracked<Self>),
    {
        f(self_);
        self_.error = info;
    }

    fn is_unexpected_end_of_input(&self) -> bool {
        *self == UnexpectedParse::Eoi
    }

    #[inline]
    fn into_other<T>(self) -> T
    where
        T: ParseError<Item, Range, Position>,
    {
        T::from_error(Position::default(), StreamError::into_other(self))
    }
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum StringStreamError {
    UnexpectedParse,
    Eoi,
    CharacterBoundary,
}

pub(crate) const CHAR_BOUNDARY_ERROR_MESSAGE: &str = "unexpected slice on character boundary";

impl fmt::Display for StringStreamError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

#[cfg(feature = "std")]
impl StdError for StringStreamError {
    fn description(&self) -> &str {
        self.as_str()
    }
}

impl StringStreamError {
    fn as_str(&self) -> &str {
        use self::StringStreamError::*;
        match *self {
            UnexpectedParse => "unexpected parse",
            Eoi => "unexpected end of input",
            CharacterBoundary => CHAR_BOUNDARY_ERROR_MESSAGE,
        }
    }
}

impl<Item, Range> StreamError<Item, Range> for StringStreamError {
    #[inline]
    fn unexpected_token(_: Item) -> Self {
        StringStreamError::UnexpectedParse
    }
    #[inline]
    fn unexpected_range(_: Range) -> Self {
        StringStreamError::UnexpectedParse
    }
    #[inline]
    fn unexpected_format<T>(_msg: T) -> Self
    where
        T: fmt::Display,
    {
        StringStreamError::UnexpectedParse
    }

    #[inline]
    fn expected_token(_: Item) -> Self {
        StringStreamError::UnexpectedParse
    }
    #[inline]
    fn expected_range(_: Range) -> Self {
        StringStreamError::UnexpectedParse
    }
    #[inline]
    fn expected_format<T>(_: T) -> Self
    where
        T: fmt::Display,
    {
        StringStreamError::UnexpectedParse
    }
    #[inline]
    fn message_format<T>(_: T) -> Self
    where
        T: fmt::Display,
    {
        StringStreamError::UnexpectedParse
    }
    #[inline]
    fn message_token(_: Item) -> Self {
        StringStreamError::UnexpectedParse
    }
    #[inline]
    fn message_range(_: Range) -> Self {
        StringStreamError::UnexpectedParse
    }
    fn message_static_message(msg: &'static str) -> Self {
        if msg == CHAR_BOUNDARY_ERROR_MESSAGE {
            StringStreamError::CharacterBoundary
        } else {
            StringStreamError::UnexpectedParse
        }
    }
    #[inline]
    fn end_of_input() -> Self {
        StringStreamError::Eoi
    }
    #[inline]
    fn is_unexpected_end_of_input(&self) -> bool {
        *self == StringStreamError::Eoi
    }
    #[inline]
    fn into_other<T>(self) -> T
    where
        T: StreamError<Item, Range>,
    {
        let msg = match self {
            StringStreamError::CharacterBoundary => CHAR_BOUNDARY_ERROR_MESSAGE,
            StringStreamError::UnexpectedParse => "parse",
            StringStreamError::Eoi => return T::end_of_input(),
        };
        T::unexpected_static_message(msg)
    }
}
impl<Item, Range, Position> ParseError<Item, Range, Position> for StringStreamError
where
    Position: Default,
{
    type StreamError = Self;
    #[inline]
    fn empty(_position: Position) -> Self {
        StringStreamError::UnexpectedParse
    }
    #[inline]
    fn from_error(_: Position, err: Self::StreamError) -> Self {
        err
    }

    #[inline]
    fn set_position(&mut self, _position: Position) {}

    #[inline]
    fn add(&mut self, err: Self::StreamError) {
        *self = match (*self, err) {
            (StringStreamError::Eoi, _) => StringStreamError::Eoi,
            (_, err) => err,
        };
    }

    #[inline]
    fn set_expected<F>(self_: &mut Tracked<Self>, info: Self::StreamError, f: F)
    where
        F: FnOnce(&mut Tracked<Self>),
    {
        f(self_);
        self_.error = info;
    }

    fn is_unexpected_end_of_input(&self) -> bool {
        *self == StringStreamError::Eoi
    }

    #[inline]
    fn into_other<T>(self) -> T
    where
        T: ParseError<Item, Range, Position>,
    {
        T::from_error(Position::default(), StreamError::into_other(self))
    }
}

/// Error wrapper which lets parsers track which parser in a sequence of sub-parsers has emitted
/// the error. `Tracked::from` can be used to construct this and it should otherwise be
/// ignored outside of combine.
#[derive(Clone, PartialEq, Debug, Copy)]
pub struct Tracked<E> {
    /// The error returned
    pub error: E,
    #[doc(hidden)]
    pub offset: ErrorOffset,
}

impl<E> From<E> for Tracked<E> {
    fn from(error: E) -> Self {
        Tracked {
            error: error,
            offset: ErrorOffset(1),
        }
    }
}

/// A `Result` type which has the committed status flattened into the result.
/// Conversions to and from `std::result::Result` can be done using `result.into()` or
/// `From::from(result)`
#[derive(Clone, PartialEq, Debug, Copy)]
pub enum ParseResult<T, E> {
    /// The parser has succeeded and has committed to this parse. If a parser after this fails,
    /// other parser alternatives will not be attempted (`CommitErr` will be returned)
    CommitOk(T),
    /// The parser has succeeded and has not committed to this parse. If a parser after this fails,
    /// other parser alternatives will be attempted (`PeekErr` will be returned)
    PeekOk(T),
    /// The parser failed other parse alternatives will not be attempted.
    CommitErr(E),
    /// The parser failed but other parse alternatives may be attempted.
    PeekErr(Tracked<E>),
}

impl<T, E> ParseResult<T, E> {
    #[inline]
    pub fn is_ok(&self) -> bool {
        match *self {
            CommitOk(_) | PeekOk(_) => true,
            CommitErr(_) | PeekErr(_) => false,
        }
    }

    #[inline]
    pub fn is_err(&self) -> bool {
        !self.is_ok()
    }

    pub fn as_ref(&self) -> ParseResult<&T, &E> {
        match *self {
            CommitOk(ref t) => CommitOk(t),
            PeekOk(ref t) => PeekOk(t),
            CommitErr(ref e) => CommitErr(e),
            PeekErr(ref e) => PeekErr(Tracked {
                error: &e.error,
                offset: e.offset,
            }),
        }
    }

    pub fn and_then<F, T2>(self, f: F) -> F::Output
    where
        F: FnOnce(T) -> ParseResult<T2, E>,
    {
        match self {
            CommitOk(t) => match f(t) {
                CommitOk(t2) | PeekOk(t2) => CommitOk(t2),
                PeekErr(e) => CommitErr(e.error),
                CommitErr(e) => CommitErr(e),
            },
            PeekOk(t) => f(t),
            CommitErr(e) => CommitErr(e),
            PeekErr(e) => PeekErr(e),
        }
    }

    pub fn map_err<F, E2>(self, f: F) -> ParseResult<T, F::Output>
    where
        F: FnOnce(E) -> E2,
    {
        match self {
            CommitOk(t) => CommitOk(t),
            PeekOk(t) => PeekOk(t),
            CommitErr(e) => CommitErr(f(e)),
            PeekErr(e) => PeekErr(Tracked {
                error: f(e.error),
                offset: e.offset,
            }),
        }
    }

    pub fn map<F, T2>(self, f: F) -> ParseResult<F::Output, E>
    where
        F: FnOnce(T) -> T2,
    {
        match self {
            CommitOk(t) => CommitOk(f(t)),
            PeekOk(t) => PeekOk(f(t)),
            CommitErr(e) => CommitErr(e),
            PeekErr(e) => PeekErr(e),
        }
    }
}

impl<O, E> ParseResult<O, E> {
    pub fn into_result(self) -> StdParseResult2<O, E> {
        self.into()
    }
}

impl<T, E> Into<Result<Commit<T>, Commit<Tracked<E>>>> for ParseResult<T, E> {
    #[inline]
    fn into(self) -> Result<Commit<T>, Commit<Tracked<E>>> {
        match self {
            CommitOk(t) => Ok(Commit::Commit(t)),
            PeekOk(t) => Ok(Commit::Peek(t)),
            CommitErr(e) => Err(Commit::Commit(e.into())),
            PeekErr(e) => Err(Commit::Peek(e)),
        }
    }
}

impl<O, E> Into<StdParseResult2<O, E>> for ParseResult<O, E> {
    #[inline]
    fn into(self) -> StdParseResult2<O, E> {
        use self::ParseResult::*;

        match self {
            CommitOk(t) => Ok((t, Commit::Commit(()))),
            PeekOk(t) => Ok((t, Commit::Peek(()))),
            CommitErr(e) => Err(Commit::Commit(e.into())),
            PeekErr(e) => Err(Commit::Peek(e)),
        }
    }
}

impl<O, E> From<StdParseResult2<O, E>> for ParseResult<O, E> {
    #[inline]
    fn from(result: StdParseResult2<O, E>) -> ParseResult<O, E> {
        use self::ParseResult::*;

        match result {
            Ok((t, Commit::Commit(()))) => CommitOk(t),
            Ok((t, Commit::Peek(()))) => PeekOk(t),
            Err(Commit::Commit(e)) => CommitErr(e.error),
            Err(Commit::Peek(e)) => PeekErr(e),
        }
    }
}

#[cfg(all(feature = "std", test))]
mod tests_std {

    use crate::Parser;

    #[derive(Clone, PartialEq, Debug)]
    struct CloneOnly {
        s: String,
    }

    #[test]
    fn parse_clone_but_not_copy() {
        // This verifies we can parse slice references with an token type that is Clone but not Copy.
        let input = &[
            CloneOnly { s: "x".to_string() },
            CloneOnly { s: "y".to_string() },
        ][..];
        let result =
            crate::parser::range::take_while(|c: CloneOnly| c.s == "x".to_string()).parse(input);
        assert_eq!(
            result,
            Ok((
                &[CloneOnly { s: "x".to_string() }][..],
                &[CloneOnly { s: "y".to_string() }][..]
            ))
        );
    }
}