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
use super::*;

use kg_display::ListDisplay;


#[derive(Debug, Eq, PartialEq, PartialOrd, Ord, Clone)]
pub enum Input {
    Byte(u8),
    Char(char),
    Custom(String),
}

impl std::fmt::Display for Input {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            Input::Byte(b) => write!(f, "byte 0x{:02X}", b),
            Input::Char(c) => write!(f, "character {:?}", c),
            Input::Custom(ref s) => write!(f, "{}", s),
        }
    }
}


#[derive(Debug, Eq, PartialEq, PartialOrd, Ord, Clone)]
pub enum Expected {
    Byte(u8),
    ByteRange(u8, u8),
    Char(char),
    CharRange(char, char),
    Custom(String),
    OneOf(Vec<Expected>),
    Or(Box<Expected>, Box<Expected>),
}

impl Expected {
    pub fn one_of(mut elems: Vec<Expected>) -> Expected {
        if elems.len() == 1 {
            elems.pop().unwrap()
        } else {
            elems.sort();
            Expected::OneOf(elems)
        }
    }
}

impl std::fmt::Display for Expected {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            Expected::Byte(b) => write!(f, "0x{:02X}", b),
            Expected::ByteRange(a, b) => write!(f, "[0x{:02X}-0x{:02X}]", a, b),
            Expected::Char(c) => write!(f, "{:?}", c),
            Expected::CharRange(a, b) => write!(f, "[{:?}-{:?}]", a, b),
            Expected::Custom(ref s) => write!(f, "{}", s),
            Expected::OneOf(ref e) => write!(f, "one of: {}", ListDisplay(e)),
            Expected::Or(ref a, ref b) => write!(f, "{} or {}", a, b),
        }
    }
}


#[derive(Display, Debug, Clone, Copy)]
pub enum NumericalErrorKind {
    #[display("overflow")]
    Overflow(f64),
    #[display("underflow")]
    Underflow(f64),
    #[display("invalid format error")]
    Invalid,
}

impl NumericalErrorKind {
    pub fn has_float(&self) -> bool {
        match *self {
            NumericalErrorKind::Overflow(n) | NumericalErrorKind::Underflow(n) => !n.is_nan(),
            NumericalErrorKind::Invalid => false,
        }
    }

    pub fn as_float(&self) -> f64 {
        match *self {
            NumericalErrorKind::Overflow(n) | NumericalErrorKind::Underflow(n) => n,
            NumericalErrorKind::Invalid => std::f64::NAN,
        }
    }
}

impl PartialEq for NumericalErrorKind {
    fn eq(&self, other: &Self) -> bool {
        match (*self, *other) {
            (NumericalErrorKind::Overflow(_), NumericalErrorKind::Overflow(_)) => true,
            (NumericalErrorKind::Underflow(_), NumericalErrorKind::Underflow(_)) => true,
            (NumericalErrorKind::Invalid, NumericalErrorKind::Invalid) => true,
            _ => false,
        }
    }
}

impl Eq for NumericalErrorKind {}


#[derive(Debug, Eq, PartialEq, Clone)]
pub enum ParseErrorDetail {
    Io(IoErrorDetail),
    UnexpectedEof {
        pos: Position,
        expected: Option<Expected>,
        task: String,
    },
    UnexpectedInput {
        pos: Position,
        found: Option<Input>,
        expected: Option<Expected>,
        task: String,
    },
    Numerical {
        span: Span,
        kind: NumericalErrorKind,
    }
}

impl Detail for ParseErrorDetail {
    fn code(&self) -> u32 {
        match *self {
            ParseErrorDetail::Io(ref err) => err.code(),
            ParseErrorDetail::UnexpectedEof { .. } => 40,
            ParseErrorDetail::UnexpectedInput { .. } => 41,
            ParseErrorDetail::Numerical { .. } => 42,
        }
    }
}

impl std::fmt::Display for ParseErrorDetail {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            ParseErrorDetail::Io(ref err) => {
                return std::fmt::Display::fmt(err, f);
            }
            ParseErrorDetail::UnexpectedEof { pos, ref expected, ref task } => {
                write!(f, "unexpected <EOF> at {} while {}", pos, task)?;
                if let Some(e) = expected {
                    write!(f, ", expecting {}", e)?;
                }
            }
            ParseErrorDetail::UnexpectedInput { pos, ref found, ref expected, ref task } => {
                if let Some(ref input) = found {
                    write!(f, "unexpected {} at {} while {}", input, pos, task)?;
                } else {
                    write!(f, "unexpected input at {} while {}", pos, task)?;
                }
                if let Some(e) = expected {
                    write!(f, ", expecting {}", e)?;
                }
            }
            ParseErrorDetail::Numerical { span, kind } => {
                write!(f, "{} while converting number literal at {}", kind, span)?;
            }
        }
        Ok(())
    }
}

impl From<IoErrorDetail> for ParseErrorDetail {
    fn from(err: IoErrorDetail) -> Self {
        ParseErrorDetail::Io(err)
    }
}