mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-18 10:07:28 +00:00
revset: leverage SOI/EOI markers to detect incomplete parser input
The error message is still a bit cryptic, but I don't think it's worse than the original "incomplete parse" error. https://pest.rs/book/grammars/syntax.html#start-and-end-of-input
This commit is contained in:
parent
a81ebeb85e
commit
1717690a64
3 changed files with 20 additions and 13 deletions
|
@ -66,3 +66,5 @@ infix_expression = {
|
|||
expression = {
|
||||
whitespace* ~ infix_expression ~ whitespace*
|
||||
}
|
||||
|
||||
program = _{ SOI ~ expression ~ EOI }
|
||||
|
|
|
@ -857,20 +857,8 @@ pub fn parse(
|
|||
revset_str: &str,
|
||||
workspace_ctx: Option<&RevsetWorkspaceContext>,
|
||||
) -> Result<Rc<RevsetExpression>, RevsetParseError> {
|
||||
let mut pairs = RevsetParser::parse(Rule::expression, revset_str)?;
|
||||
let mut pairs = RevsetParser::parse(Rule::program, revset_str)?;
|
||||
let first = pairs.next().unwrap();
|
||||
assert!(pairs.next().is_none());
|
||||
if first.as_span().end() != revset_str.len() {
|
||||
let pos = pest::Position::new(revset_str, first.as_span().end()).unwrap();
|
||||
let err = pest::error::Error::new_from_pos(
|
||||
pest::error::ErrorVariant::CustomError {
|
||||
message: "Incomplete parse".to_string(),
|
||||
},
|
||||
pos,
|
||||
);
|
||||
return Err(RevsetParseError::from(err));
|
||||
}
|
||||
|
||||
parse_expression_rule(first.into_inner(), workspace_ctx)
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,23 @@ use common::TestEnvironment;
|
|||
|
||||
pub mod common;
|
||||
|
||||
#[test]
|
||||
fn test_syntax_error() {
|
||||
let test_env = TestEnvironment::default();
|
||||
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
|
||||
let repo_path = test_env.env_root().join("repo");
|
||||
|
||||
let stderr = test_env.jj_cmd_failure(&repo_path, &["log", "-r", "x &"]);
|
||||
insta::assert_snapshot!(stderr, @r###"
|
||||
Error: Failed to parse revset: --> 1:4
|
||||
|
|
||||
1 | x &
|
||||
| ^---
|
||||
|
|
||||
= expected range_expression
|
||||
"###);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bad_function_call() {
|
||||
let test_env = TestEnvironment::default();
|
||||
|
|
Loading…
Reference in a new issue