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
use std::fmt;
use std::fmt::{Display, Formatter};

#[derive(Debug, PartialEq)]
pub enum Error {
    IncompleteWordEscape(Option<char>),
    UnterminatedComment,
    UnterminatedRegExp(Option<char>),
    MissingExponent(Option<char>),
    UnterminatedString(Option<char>),
    MissingBinaryDigits,
    MissingOctalDigits,
    MissingHexDigits,
    IllegalChar(char),
    InvalidDigit(char),
    IllegalUnicode(u32),
    IdAfterNumber(char),
    DigitAfterNumber(char)
}

impl Display for Error {
    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
        match self {
            &Error::IncompleteWordEscape(_) => {
                fmt.write_str("incomplete word escape")
            }
            &Error::UnterminatedComment => {
                fmt.write_str("unterminated block comment")
            }
            &Error::UnterminatedRegExp(_) => {
                fmt.write_str("unterminated regexp literal")
            }
            &Error::MissingExponent(_) => {
                fmt.write_str("missing exponent")
            }
            &Error::UnterminatedString(_) => {
                fmt.write_str("unterminated string")
            }
            &Error::MissingBinaryDigits => {
                fmt.write_str("missing binary digits")
            }
            &Error::MissingOctalDigits => {
                fmt.write_str("missing octal digits")
            }
            &Error::MissingHexDigits => {
                fmt.write_str("missing hex digits")
            }
            &Error::IllegalChar(ref ch) => {
                fmt.write_fmt(format_args!("illegal character: {:?}", *ch))
            }
            &Error::InvalidDigit(ref ch) => {
                fmt.write_fmt(format_args!("invalid digit: {:?}", *ch))
            }
            &Error::IllegalUnicode(ref u) => {
                fmt.write_fmt(format_args!("illegal code unit: \\u{{{:04x}}}", u))
            }
            &Error::IdAfterNumber(_) => {
                fmt.write_str("identifier starts immediately after numeric literal")
            }
            &Error::DigitAfterNumber(_) => {
                fmt.write_str("numeric literal starts immediately after previous numeric literal")
            }
        }
    }
}