templater: disallow empty template in function arguments and parens

Before, "f()" was parsed as a function call with one empty argument. In
practice, this change means "if(divergent,,false_template)" is no longer
valid.
This commit is contained in:
Yuya Nishihara 2023-01-29 12:59:46 +09:00
parent a5bb48d47e
commit 3d965f22ab
2 changed files with 12 additions and 7 deletions

View file

@ -29,6 +29,7 @@ identifier = @{ (ASCII_ALPHANUMERIC | "_")+ }
function = { identifier ~ "(" ~ function_arguments ~ ")" } function = { identifier ~ "(" ~ function_arguments ~ ")" }
function_arguments = { function_arguments = {
template ~ ("," ~ template)* template ~ ("," ~ template)*
| whitespace*
} }
maybe_method = { ("." ~ function)* } maybe_method = { ("." ~ function)* }
@ -46,7 +47,7 @@ list = _{
} }
template = { template = {
whitespace* ~ (list | term | "") ~ whitespace* whitespace* ~ (list | term) ~ whitespace*
} }
program = _{ SOI ~ template ~ EOI } program = _{ SOI ~ (template | whitespace*) ~ EOI }

View file

@ -369,10 +369,10 @@ fn parse_commit_template_rule<'a>(
let mut templates = inner let mut templates = inner
.map(|term| parse_commit_term(repo, workspace_id, term)) .map(|term| parse_commit_term(repo, workspace_id, term))
.collect_vec(); .collect_vec();
match templates.len() { if templates.len() == 1 {
0 => Box::new(Literal(String::new())), templates.pop().unwrap()
1 => templates.pop().unwrap(), } else {
_ => Box::new(ListTemplate(templates)), Box::new(ListTemplate(templates))
} }
} }
@ -383,5 +383,9 @@ pub fn parse_commit_template<'a>(
) -> Box<dyn Template<Commit> + 'a> { ) -> Box<dyn Template<Commit> + 'a> {
let mut pairs: Pairs<Rule> = TemplateParser::parse(Rule::program, template_text).unwrap(); let mut pairs: Pairs<Rule> = TemplateParser::parse(Rule::program, template_text).unwrap();
let first_pair = pairs.next().unwrap(); let first_pair = pairs.next().unwrap();
parse_commit_template_rule(repo, workspace_id, first_pair) if first_pair.as_rule() == Rule::EOI {
Box::new(Literal(String::new()))
} else {
parse_commit_template_rule(repo, workspace_id, first_pair)
}
} }