ok/jj
1
0
Fork 0
forked from mirrors/jj

revsets: use same error variant for imcomplete parse as for syntax error

This commit is contained in:
Martin von Zweigbergk 2021-04-23 16:35:20 -07:00
parent 0b0374d401
commit 9de5f94af6

View file

@ -91,8 +91,6 @@ pub struct RevsetParser;
pub enum RevsetParseError { pub enum RevsetParseError {
#[error("{0}")] #[error("{0}")]
SyntaxError(#[from] pest::error::Error<Rule>), SyntaxError(#[from] pest::error::Error<Rule>),
#[error("{0}")]
IncompleteParse(String),
#[error("Revset function \"{0}\" doesn't exist")] #[error("Revset function \"{0}\" doesn't exist")]
NoSuchFunction(String), NoSuchFunction(String),
#[error("Invalid arguments to revset function \"{name}\": {message}")] #[error("Invalid arguments to revset function \"{name}\": {message}")]
@ -410,11 +408,14 @@ pub fn parse(revset_str: &str) -> Result<RevsetExpression, RevsetParseError> {
let first = pairs.next().unwrap(); let first = pairs.next().unwrap();
assert!(pairs.next().is_none()); assert!(pairs.next().is_none());
if first.as_span().end() != revset_str.len() { if first.as_span().end() != revset_str.len() {
return Err(RevsetParseError::IncompleteParse(format!( let pos = pest::Position::new(revset_str, first.as_span().end()).unwrap();
"Failed to parse revset \"{}\" past position {}", let err = pest::error::Error::new_from_pos(
revset_str, pest::error::ErrorVariant::CustomError {
first.as_span().end() message: "Incomplete parse".to_string(),
))); },
pos,
);
return Err(RevsetParseError::SyntaxError(err));
} }
parse_expression_rule(first.into_inner()) parse_expression_rule(first.into_inner())
@ -889,7 +890,7 @@ mod tests {
// Space is not allowed around prefix operators // Space is not allowed around prefix operators
assert_matches!(parse(" ,, @ "), Err(RevsetParseError::SyntaxError(_))); assert_matches!(parse(" ,, @ "), Err(RevsetParseError::SyntaxError(_)));
// Incomplete parse // Incomplete parse
assert_matches!(parse("foo | :"), Err(RevsetParseError::IncompleteParse(_))); assert_matches!(parse("foo | :"), Err(RevsetParseError::SyntaxError(_)));
// Space is allowed around infix operators and function arguments // Space is allowed around infix operators and function arguments
assert_eq!( assert_eq!(
parse(" description( arg1 , arg2 ) - parents( arg1 ) - all_heads( ) "), parse(" description( arg1 , arg2 ) - parents( arg1 ) - all_heads( ) "),
@ -999,12 +1000,7 @@ mod tests {
RevsetExpression::Parents(Rc::new(RevsetExpression::Symbol("@".to_string()))) RevsetExpression::Parents(Rc::new(RevsetExpression::Symbol("@".to_string())))
))) )))
); );
assert_eq!( assert_matches!(parse("parents(@"), Err(RevsetParseError::SyntaxError(_)));
parse("parents(@"),
Err(RevsetParseError::IncompleteParse(
"Failed to parse revset \"parents(@\" past position 7".to_string()
))
);
assert_eq!( assert_eq!(
parse("parents(@,@)"), parse("parents(@,@)"),
Err(RevsetParseError::InvalidFunctionArguments { Err(RevsetParseError::InvalidFunctionArguments {