2019-03-15 15:11:31 +00:00
|
|
|
use crate::value::Value;
|
2019-03-15 17:19:59 +00:00
|
|
|
use token::PartialToken;
|
2016-11-16 16:12:26 +00:00
|
|
|
|
2019-03-15 15:11:31 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum Error {
|
|
|
|
WrongArgumentAmount {
|
|
|
|
expected: usize,
|
|
|
|
actual: usize,
|
|
|
|
},
|
|
|
|
ExpectedNumber {
|
|
|
|
actual: Value,
|
|
|
|
},
|
2019-03-15 17:19:59 +00:00
|
|
|
ExpectedBoolean {
|
|
|
|
actual: Value,
|
|
|
|
},
|
2016-11-16 16:12:26 +00:00
|
|
|
|
2019-03-15 15:11:31 +00:00
|
|
|
/// The given expression is empty
|
|
|
|
EmptyExpression,
|
2016-11-16 16:12:26 +00:00
|
|
|
|
2019-03-15 15:11:31 +00:00
|
|
|
/// Tried to evaluate the root node.
|
|
|
|
/// The root node should only be used as dummy node.
|
|
|
|
EvaluatedRootNode,
|
|
|
|
|
|
|
|
/// Tried to append a child to a leaf node.
|
|
|
|
/// Leaf nodes cannot have children.
|
|
|
|
AppendedToLeafNode,
|
|
|
|
|
|
|
|
/// Tried to append a child to a node such that the precedence of the child is not higher.
|
|
|
|
PrecedenceViolation,
|
|
|
|
|
2019-03-18 17:21:07 +00:00
|
|
|
/// A `VariableIdentifier` operation did not find its value in the configuration.
|
|
|
|
VariableIdentifierNotFound(String),
|
|
|
|
|
|
|
|
/// A `FunctionIdentifier` operation did not find its value in the configuration.
|
|
|
|
FunctionIdentifierNotFound(String),
|
2019-03-15 15:40:38 +00:00
|
|
|
|
2019-03-19 10:22:06 +00:00
|
|
|
/// A value has the wrong type. Only use this if there is no other error that describes the expected and provided types in more detail.
|
2019-03-15 15:40:38 +00:00
|
|
|
TypeError,
|
2019-03-15 16:27:10 +00:00
|
|
|
|
|
|
|
/// An opening brace without a matching closing brace was found.
|
|
|
|
UnmatchedLBrace,
|
|
|
|
|
|
|
|
/// A closing brace without a matching opening brace was found.
|
|
|
|
UnmatchedRBrace,
|
2019-03-15 17:19:59 +00:00
|
|
|
|
|
|
|
UnmatchedPartialToken {
|
|
|
|
first: PartialToken,
|
|
|
|
second: Option<PartialToken>,
|
|
|
|
},
|
2019-03-15 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Error {
|
|
|
|
pub fn wrong_argument_amount(actual: usize, expected: usize) -> Self {
|
|
|
|
Error::WrongArgumentAmount { actual, expected }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expected_number(actual: Value) -> Self {
|
|
|
|
Error::ExpectedNumber { actual }
|
|
|
|
}
|
2019-03-15 17:19:59 +00:00
|
|
|
|
|
|
|
pub fn expected_boolean(actual: Value) -> Self {
|
|
|
|
Error::ExpectedBoolean { actual }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unmatched_partial_token(first: PartialToken, second: Option<PartialToken>) -> Self {
|
2019-03-15 17:22:14 +00:00
|
|
|
Error::UnmatchedPartialToken { first, second }
|
2019-03-15 17:19:59 +00:00
|
|
|
}
|
2019-03-15 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expect_argument_amount(actual: usize, expected: usize) -> Result<(), Error> {
|
|
|
|
if actual == expected {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(Error::wrong_argument_amount(actual, expected))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 15:40:38 +00:00
|
|
|
pub fn expect_number(actual: &Value) -> Result<(), Error> {
|
2019-03-15 15:11:31 +00:00
|
|
|
match actual {
|
2019-03-15 15:40:38 +00:00
|
|
|
Value::Float(_) | Value::Int(_) => Ok(()),
|
2019-03-15 15:11:31 +00:00
|
|
|
_ => Err(Error::expected_number(actual.clone())),
|
2016-11-16 16:12:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-15 17:19:59 +00:00
|
|
|
|
|
|
|
pub fn expect_boolean(actual: &Value) -> Result<bool, Error> {
|
|
|
|
match actual {
|
|
|
|
Value::Boolean(boolean) => Ok(*boolean),
|
|
|
|
_ => Err(Error::expected_boolean(actual.clone())),
|
|
|
|
}
|
2019-03-15 17:22:14 +00:00
|
|
|
}
|