Error if not all the input is consumed when parsing expressions

This commit is contained in:
Oliver Hamlet
2019-01-26 09:44:33 +00:00
parent 3777c067a4
commit 529e4fc6d1
3 changed files with 25 additions and 1 deletions
+1
View File
@@ -23,6 +23,7 @@ pub fn handle_error(err: Error) -> c_int {
fn map_error(err: &Error) -> c_int { fn map_error(err: &Error) -> c_int {
match err { match err {
Error::ParsingIncomplete => LCI_ERROR_PARSING_ERROR, Error::ParsingIncomplete => LCI_ERROR_PARSING_ERROR,
Error::UnconsumedInput(_) => LCI_ERROR_PARSING_ERROR,
Error::GenericParsingError(_, _) => LCI_ERROR_PARSING_ERROR, Error::GenericParsingError(_, _) => LCI_ERROR_PARSING_ERROR,
Error::CustomParsingError(_, _) => LCI_ERROR_PARSING_ERROR, Error::CustomParsingError(_, _) => LCI_ERROR_PARSING_ERROR,
Error::PeParsingError(_, _) => LCI_ERROR_PE_PARSING_ERROR, Error::PeParsingError(_, _) => LCI_ERROR_PE_PARSING_ERROR,
+7
View File
@@ -10,6 +10,8 @@ use regex;
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
ParsingIncomplete, ParsingIncomplete,
// The string is the input that was not parsed.
UnconsumedInput(String),
/// The first string is the expression parsed, the second is a tag describing the parser that failed. /// The first string is the expression parsed, the second is a tag describing the parser that failed.
GenericParsingError(String, String), GenericParsingError(String, String),
/// The string is the expression parsed. /// The string is the expression parsed.
@@ -41,6 +43,11 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
Error::ParsingIncomplete => write!(f, "More input was expected by the parser"), Error::ParsingIncomplete => write!(f, "More input was expected by the parser"),
Error::UnconsumedInput(i) => write!(
f,
"The parser did not consume the following input: \"{}\"",
i
),
Error::GenericParsingError(i, e) => write!( Error::GenericParsingError(i, e) => write!(
f, f,
"An error was encountered in the parser \"{}\" while parsing the expression \"{}\"", "An error was encountered in the parser \"{}\" while parsing the expression \"{}\"",
+17 -1
View File
@@ -161,8 +161,14 @@ impl str::FromStr for Expression {
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
parse_expression(nom::types::CompleteStr(s)) parse_expression(nom::types::CompleteStr(s))
.map(|(_, expression)| expression)
.map_err(Error::from) .map_err(Error::from)
.and_then(|(remaining_input, expression)| {
if remaining_input.is_empty() {
Ok(expression)
} else {
Err(Error::UnconsumedInput(remaining_input.to_string()))
}
})
} }
} }
@@ -502,6 +508,16 @@ mod tests {
} }
} }
#[test]
fn expression_parse_should_error_if_it_does_not_consume_the_whole_input() {
let error = Expression::from_str("file(\"Cargo.toml\") foobar").unwrap_err();
assert_eq!(
"The parser did not consume the following input: \" foobar\"",
error.to_string()
);
}
#[test] #[test]
fn compound_condition_parse_should_handle_a_single_condition() { fn compound_condition_parse_should_handle_a_single_condition() {
let result = CompoundCondition::parse("file(\"Cargo.toml\")".into()) let result = CompoundCondition::parse("file(\"Cargo.toml\")".into())