use std::fmt::{ Display, Formatter };
use std::fmt::Error as FormatError;
use read_token::{ ParseNumberError, ParseStringError };
use std::rc::Rc;
use DebugId;
#[derive(Debug, PartialEq)]
pub enum ParseError {
ExpectedWhitespace(DebugId),
ExpectedNewLine(DebugId),
ExpectedSomething(DebugId),
ExpectedNumber(DebugId),
ParseNumberError(ParseNumberError, DebugId),
ExpectedText(DebugId),
EmptyTextNotAllowed(DebugId),
ParseStringError(ParseStringError, DebugId),
ExpectedToken(Rc<String>, DebugId),
DidNotExpectToken(Rc<String>, DebugId),
InvalidRule(&'static str, DebugId),
NoRules,
ExpectedEnd,
}
impl Display for ParseError {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FormatError> {
match self {
&ParseError::ExpectedWhitespace(debug_id) =>
try!(write!(fmt, "#{}, Expected whitespace",
debug_id)),
&ParseError::ExpectedNewLine(debug_id) =>
try!(write!(fmt, "#{}, Expected new line",
debug_id)),
&ParseError::ExpectedSomething(debug_id) =>
try!(write!(fmt, "#{}, Expected something",
debug_id)),
&ParseError::ExpectedNumber(debug_id) =>
try!(write!(fmt, "#{}, Expected number", debug_id)),
&ParseError::ParseNumberError(ref err, debug_id) =>
try!(write!(fmt, "#{}, Invalid number format: {}",
debug_id, err)),
&ParseError::ExpectedToken(ref token, debug_id) =>
try!(write!(fmt, "#{}, Expected: `{}`", debug_id, token)),
&ParseError::DidNotExpectToken(ref token, debug_id) =>
try!(write!(fmt, "#{}, Did not expect: `{}`", debug_id, token)),
&ParseError::ExpectedText(debug_id) =>
try!(write!(fmt, "#{}, Expected text", debug_id)),
&ParseError::EmptyTextNotAllowed(debug_id) =>
try!(write!(fmt, "#{}, Empty text not allowed", debug_id)),
&ParseError::ParseStringError(err, debug_id) =>
try!(write!(fmt, "#{}, Invalid string format: {}",
debug_id, err)),
&ParseError::InvalidRule(msg, debug_id) =>
try!(write!(fmt, "#{}, Invalid rule: {}", debug_id, msg)),
&ParseError::NoRules =>
try!(write!(fmt, "No rules are specified")),
&ParseError::ExpectedEnd =>
try!(write!(fmt, "Expected end")),
}
Ok(())
}
}