forked from mirrors/jj
templater: extract function arguments to inner node
This should help to represent an empty argument properly.
This commit is contained in:
parent
62def97f58
commit
c44dda55e3
2 changed files with 28 additions and 14 deletions
|
@ -26,7 +26,10 @@ literal = { "\"" ~ (raw_literal | escape)* ~ "\"" }
|
||||||
|
|
||||||
identifier = @{ (ASCII_ALPHANUMERIC | "_")+ }
|
identifier = @{ (ASCII_ALPHANUMERIC | "_")+ }
|
||||||
|
|
||||||
function = { identifier ~ "(" ~ template ~ ("," ~ template)* ~ ")" }
|
function = { identifier ~ "(" ~ function_arguments ~ ")" }
|
||||||
|
function_arguments = {
|
||||||
|
template ~ ("," ~ template)*
|
||||||
|
}
|
||||||
|
|
||||||
maybe_method = { ("." ~ function)* }
|
maybe_method = { ("." ~ function)* }
|
||||||
|
|
||||||
|
|
|
@ -150,9 +150,14 @@ fn parse_method_chain<'a, I: 'a>(
|
||||||
assert_eq!(pair.as_rule(), Rule::maybe_method);
|
assert_eq!(pair.as_rule(), Rule::maybe_method);
|
||||||
for chain in pair.into_inner() {
|
for chain in pair.into_inner() {
|
||||||
assert_eq!(chain.as_rule(), Rule::function);
|
assert_eq!(chain.as_rule(), Rule::function);
|
||||||
let mut args = chain.into_inner();
|
let (name, args) = {
|
||||||
let name = args.next().unwrap();
|
let mut inner = chain.into_inner();
|
||||||
|
let name = inner.next().unwrap();
|
||||||
|
let args_pair = inner.next().unwrap();
|
||||||
assert_eq!(name.as_rule(), Rule::identifier);
|
assert_eq!(name.as_rule(), Rule::identifier);
|
||||||
|
assert_eq!(args_pair.as_rule(), Rule::function_arguments);
|
||||||
|
(name, args_pair.into_inner())
|
||||||
|
};
|
||||||
labels.push(name.as_str().to_owned());
|
labels.push(name.as_str().to_owned());
|
||||||
property = match property {
|
property = match property {
|
||||||
Property::String(property) => parse_string_method(name, args).after(property),
|
Property::String(property) => parse_string_method(name, args).after(property),
|
||||||
|
@ -291,17 +296,23 @@ fn parse_commit_term<'a>(
|
||||||
Box::new(LabelTemplate::new(property.into_template(), labels))
|
Box::new(LabelTemplate::new(property.into_template(), labels))
|
||||||
}
|
}
|
||||||
Rule::function => {
|
Rule::function => {
|
||||||
|
let (name, mut args) = {
|
||||||
let mut inner = expr.into_inner();
|
let mut inner = expr.into_inner();
|
||||||
let name = inner.next().unwrap().as_str();
|
let name = inner.next().unwrap();
|
||||||
match name {
|
let args_pair = inner.next().unwrap();
|
||||||
|
assert_eq!(name.as_rule(), Rule::identifier);
|
||||||
|
assert_eq!(args_pair.as_rule(), Rule::function_arguments);
|
||||||
|
(name, args_pair.into_inner())
|
||||||
|
};
|
||||||
|
match name.as_str() {
|
||||||
"label" => {
|
"label" => {
|
||||||
let label_pair = inner.next().unwrap();
|
let label_pair = args.next().unwrap();
|
||||||
let label_template = parse_commit_template_rule(repo, workspace_id, label_pair);
|
let label_template = parse_commit_template_rule(repo, workspace_id, label_pair);
|
||||||
let arg_template = match inner.next() {
|
let arg_template = match args.next() {
|
||||||
None => panic!("label() requires two arguments"),
|
None => panic!("label() requires two arguments"),
|
||||||
Some(pair) => pair,
|
Some(pair) => pair,
|
||||||
};
|
};
|
||||||
if inner.next().is_some() {
|
if args.next().is_some() {
|
||||||
panic!("label() accepts only two arguments")
|
panic!("label() accepts only two arguments")
|
||||||
}
|
}
|
||||||
let content: Box<dyn Template<Commit> + 'a> =
|
let content: Box<dyn Template<Commit> + 'a> =
|
||||||
|
@ -319,19 +330,19 @@ fn parse_commit_term<'a>(
|
||||||
Box::new(DynamicLabelTemplate::new(content, get_labels))
|
Box::new(DynamicLabelTemplate::new(content, get_labels))
|
||||||
}
|
}
|
||||||
"if" => {
|
"if" => {
|
||||||
let condition_pair = inner.next().unwrap();
|
let condition_pair = args.next().unwrap();
|
||||||
let condition_template = condition_pair.into_inner().next().unwrap();
|
let condition_template = condition_pair.into_inner().next().unwrap();
|
||||||
let condition =
|
let condition =
|
||||||
parse_boolean_commit_property(repo, workspace_id, condition_template);
|
parse_boolean_commit_property(repo, workspace_id, condition_template);
|
||||||
|
|
||||||
let true_template = match inner.next() {
|
let true_template = match args.next() {
|
||||||
None => panic!("if() requires at least two arguments"),
|
None => panic!("if() requires at least two arguments"),
|
||||||
Some(pair) => parse_commit_template_rule(repo, workspace_id, pair),
|
Some(pair) => parse_commit_template_rule(repo, workspace_id, pair),
|
||||||
};
|
};
|
||||||
let false_template = inner
|
let false_template = args
|
||||||
.next()
|
.next()
|
||||||
.map(|pair| parse_commit_template_rule(repo, workspace_id, pair));
|
.map(|pair| parse_commit_template_rule(repo, workspace_id, pair));
|
||||||
if inner.next().is_some() {
|
if args.next().is_some() {
|
||||||
panic!("if() accepts at most three arguments")
|
panic!("if() accepts at most three arguments")
|
||||||
}
|
}
|
||||||
Box::new(ConditionalTemplate::new(
|
Box::new(ConditionalTemplate::new(
|
||||||
|
|
Loading…
Reference in a new issue