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
/*
Copyright ⓒ 2016 Daniel Keep.

Licensed under the MIT license (see LICENSE or <http://opensource.org
/licenses/MIT>) or the Apache License, Version 2.0 (see LICENSE of
<http://www.apache.org/licenses/LICENSE-2.0>), at your option. All
files in the project carrying such notice may not be copied, modified,
or distributed except according to those terms.
*/
/*!
Defines error types used by the crate.
*/
use std::error::Error;
use std::fmt;
use std::io;
use std::num::{ParseFloatError, ParseIntError};

/**
Represents an error that occurred during scanning.

Depending on what happened, it could represent an actual scanning failure, a problem with the pattern, an underlying IO failure, or something else entirely.
*/
#[derive(Debug)]
pub struct ScanError {
    /**
    The rough cursor position at which this error occurred.  This will typically be the position the input cursor was at when it began trying to scan a particular literal or value.
    */
    pub at: ScanErrorAt,

    /**
    The kind of error that occurred.
    */
    pub kind: ScanErrorKind,

    /**
    Dummy private field to prevent exhaustive deconstruction.
    */
    _priv: (),
}

impl ScanError {
    /**
    Construct a new `ScanError`.
    */
    pub fn new(at: usize, kind: ScanErrorKind) -> Self {
        ScanError {
            at: ScanErrorAt { bytes: at },
            kind: kind,
            _priv: (),
        }
    }

    /**
    Shorthand for constructing an `ExpectedEnd` error.
    */
    pub fn expected_end() -> Self {
        Self::new(0, ScanErrorKind::ExpectedEnd)
    }

    /**
    Shorthand for constructing an `Float` error.
    */
    pub fn float(err: ParseFloatError) -> Self {
        Self::new(0, ScanErrorKind::Float(err))
    }

    /**
    Shorthand for constructing an `Int` error.
    */
    pub fn int(err: ParseIntError) -> Self {
        Self::new(0, ScanErrorKind::Int(err))
    }

    /**
    Shorthand for constructing an `Io` error.
    */
    pub fn io(err: io::Error) -> Self {
        Self::new(0, ScanErrorKind::Io(err))
    }

    /**
    Shorthand for constructing a `LiteralMismatch` error.
    */
    pub fn literal_mismatch() -> Self {
        Self::new(0, ScanErrorKind::LiteralMismatch)
    }

    /**
    Shorthand for constructing a `Syntax` error.
    */
    pub fn syntax(desc: &'static str) -> Self {
        Self::new(0, ScanErrorKind::Syntax(desc))
    }

    /**
    Shorthand for constructing a `SyntaxNoMessage` error.
    */
    pub fn syntax_no_message() -> Self {
        Self::new(0, ScanErrorKind::SyntaxNoMessage)
    }

    /**
    Shorthand for constructing an `Other` error.
    */
    pub fn other<E: Into<Box<Error>>>(err: E) -> Self {
        Self::new(0, ScanErrorKind::from_other(err))
    }

    /**
    Compare two `ScanError`s, and return the one which occurred the furthest into the input cursor.
    */
    pub fn furthest_along(self, other: Self) -> Self {
        if self.at.offset() >= other.at.offset() {
            self
        } else {
            other
        }
    }

    /**
    Adds the given number of `bytes` to the error's position.

    This is used where an error has been generated by trying to scan a subslice of the original input, and the position needs to be corrected.
    */
    pub fn add_offset(self, bytes: usize) -> Self {
        ScanError::new(self.at.offset() + bytes, self.kind)
    }
}

impl<'a> fmt::Display for ScanError {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        try!("scan error: ".fmt(fmt));
        try!(self.kind.fmt(fmt));
        try!(", at offset: ".fmt(fmt));
        try!(self.at.offset().fmt(fmt));
        Ok(())
    }
}

impl Error for ScanError {
    fn cause(&self) -> Option<&Error> {
        self.kind.cause()
    }

    fn description(&self) -> &str {
        self.kind.description()
    }
}

/**
Represents the position at which an error occurred.
*/
/*
This exists because I'm still considering including the input which generated the error, for the sake of nice error messages.

I'm not using `StrCursor`, because I don't want errors tied to a specific input wrapper.
*/
#[derive(Debug)]
pub struct ScanErrorAt {
    /// Offset in bytes.
    bytes: usize,
}

impl ScanErrorAt {
    /**
    Return the offset from the start of input that an error occurred at, in bytes.
    */
    pub fn offset(&self) -> usize {
        self.bytes
    }
}

/**
Indicates the kind of error that occurred during scanning.
*/
#[derive(Debug)]
pub enum ScanErrorKind {
    /// Failed to match a literal pattern term.
    LiteralMismatch,

    /// General syntax error.
    Syntax(&'static str),

    /**
    General syntax error.

    Due to [Rust issue #26448](https://github.com/rust-lang/rust/issues/26448), some scanners which want to return a `Syntax` error *cannot*.
    */
    SyntaxNoMessage,

    /// Expected end-of-input.
    ExpectedEnd,

    /// Floating point parsing failed.
    Float(ParseFloatError),

    /// Integer parsing failed.
    Int(ParseIntError),

    /// An IO error occurred.
    Io(io::Error),

    /// Some other error occurred.
    Other(Box<Error>),

    /// Hidden variant to prevent exhaustive matching.
    #[doc(hidden)]
    __DoNotMatch,
}

impl ScanErrorKind {
    /**
    Construct an `Other` error from some generic error value.
    */
    pub fn from_other<E: Into<Box<Error>>>(err: E) -> Self {
        ScanErrorKind::Other(err.into())
    }
}

impl fmt::Display for ScanErrorKind {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        use self::ScanErrorKind::*;
        match *self {
            LiteralMismatch => "did not match literal".fmt(fmt),
            Syntax(desc) => {
                try!("syntax error: ".fmt(fmt));
                try!(desc.fmt(fmt));
                Ok(())
            },
            SyntaxNoMessage => "unknown syntax error".fmt(fmt),
            ExpectedEnd => "expected end of input".fmt(fmt),
            Float(ref err) => err.fmt(fmt),
            Int(ref err) => err.fmt(fmt),
            Io(ref err) => err.fmt(fmt),
            Other(ref err) => err.fmt(fmt),
            __DoNotMatch => panic!("do not use ScanErrorKind::__DoNotMatch!"),
        }
    }
}

impl Error for ScanErrorKind {
    fn cause(&self) -> Option<&Error> {
        use self::ScanErrorKind::*;
        match *self {
            LiteralMismatch 
            | Syntax(_)
            | SyntaxNoMessage
            | ExpectedEnd
            => None,
            Float(ref err) => err.cause(),
            Int(ref err) => err.cause(),
            Io(ref err) => err.cause(),
            Other(ref err) => err.cause(),
            __DoNotMatch => panic!("do not use ScanErrorKind::__DoNotMatch!"),
        }
    }

    fn description(&self) -> &str {
        use self::ScanErrorKind::*;
        match *self {
            LiteralMismatch => "did not match literal",
            Syntax(_) => "syntax error",
            SyntaxNoMessage => "unknown syntax error",
            ExpectedEnd => "expected end of input",
            Float(ref err) => err.description(),
            Int(ref err) => err.description(),
            Io(ref err) => err.description(),
            Other(ref err) => err.description(),
            __DoNotMatch => panic!("do not use ScanErrorKind::__DoNotMatch!"),
        }
    }
}