revsets: allow spaces in most places (but not after prefix operators)

This commit is contained in:
Martin von Zweigbergk 2021-04-18 14:48:47 -07:00
parent c04f418e67
commit 2ac5d1f912
3 changed files with 33 additions and 9 deletions

View file

@ -14,6 +14,7 @@
symbol = @{ (ASCII_ALPHANUMERIC | "@" | "/" | ".")+ } symbol = @{ (ASCII_ALPHANUMERIC | "@" | "/" | ".")+ }
literal_string = { "\"" ~ (!"\"" ~ ANY)+ ~ "\"" } literal_string = { "\"" ~ (!"\"" ~ ANY)+ ~ "\"" }
whitespace = _{ " " }
parents = { ":" } parents = { ":" }
ancestors = { "*:" } ancestors = { "*:" }
@ -35,8 +36,8 @@ function_argument = {
| expression | expression
} }
function_arguments = { function_arguments = {
(function_argument ~ ",")* ~ function_argument (whitespace* ~ function_argument ~ whitespace* ~ ",")* ~ whitespace* ~ function_argument ~ whitespace*
| "" | whitespace*
} }
primary = { primary = {
@ -48,9 +49,9 @@ primary = {
prefix_expression = { prefix_operator* ~ primary } prefix_expression = { prefix_operator* ~ primary }
infix_expression = { infix_expression = {
prefix_expression ~ (infix_operator ~ prefix_expression)* whitespace* ~ prefix_expression ~ whitespace* ~ (infix_operator ~ whitespace* ~ prefix_expression ~ whitespace*)*
} }
expression = { expression = {
infix_expression whitespace* ~ infix_expression ~ whitespace*
} }

View file

@ -343,6 +343,8 @@ fn parse_function_argument_to_string(
} }
pub fn parse(revset_str: &str) -> Result<RevsetExpression, RevsetParseError> { pub fn parse(revset_str: &str) -> Result<RevsetExpression, RevsetParseError> {
// TODO: Return a better error message when parsing fails (such as when the user
// puts whitespace between a prefix operator and the operand)
let mut pairs: Pairs<Rule> = RevsetParser::parse(Rule::expression, revset_str).unwrap(); let mut pairs: Pairs<Rule> = RevsetParser::parse(Rule::expression, revset_str).unwrap();
let first = pairs.next().unwrap(); let first = pairs.next().unwrap();
assert!(pairs.next().is_none()); assert!(pairs.next().is_none());

View file

@ -255,6 +255,27 @@ fn test_parse_revset() {
RevsetExpression::Symbol("@".to_string()) RevsetExpression::Symbol("@".to_string())
))) )))
); );
assert_eq!(
parse(" *:@ "),
Ok(RevsetExpression::Ancestors(Box::new(
RevsetExpression::Symbol("@".to_string())
)))
);
assert_eq!(
parse(" description( arg1 , arg2 ) - parents( arg1 ) - all_heads( ) "),
Ok(RevsetExpression::Difference(
Box::new(RevsetExpression::Difference(
Box::new(RevsetExpression::Description {
needle: "arg1".to_string(),
base_expression: Box::new(RevsetExpression::Symbol("arg2".to_string()))
}),
Box::new(RevsetExpression::Parents(Box::new(
RevsetExpression::Symbol("arg1".to_string())
)))
)),
Box::new(RevsetExpression::AllHeads)
))
);
} }
#[test] #[test]