From 686c1fb522761d8ae2573560ca52297226e0b7da Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Tue, 7 Feb 2023 18:33:26 +0900 Subject: [PATCH] templater: allow trailing comma While rewriting the default log template, I find it's annoying that I have to remove "," from the last line. --- src/commands/mod.rs | 2 +- src/template.pest | 2 +- src/template_parser.rs | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 6b67b7c4d..660ffc30f 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1449,7 +1449,7 @@ fn log_template(settings: &UserSettings) -> String { working_copies, git_head, commit_id.{prefix_format}, - if(conflict, label("conflict", "conflict")) + if(conflict, label("conflict", "conflict")), ) "\n" if(empty, label("empty", "(empty)") " ") diff --git a/src/template.pest b/src/template.pest index 1d0c82d7c..a18e3d2c5 100644 --- a/src/template.pest +++ b/src/template.pest @@ -33,7 +33,7 @@ identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* } function = { identifier ~ "(" ~ function_arguments ~ ")" } function_arguments = { - template ~ ("," ~ template)* + template ~ ("," ~ template)* ~ ("," ~ whitespace*)? | whitespace* } diff --git a/src/template_parser.rs b/src/template_parser.rs index 3d0ebcf52..466f3fe05 100644 --- a/src/template_parser.rs +++ b/src/template_parser.rs @@ -789,6 +789,24 @@ mod tests { }) } + #[test] + fn test_function_call_syntax() { + // Trailing comma isn't allowed for empty argument + assert!(parse(r#" "".first_line() "#).is_ok()); + assert!(parse(r#" "".first_line(,) "#).is_err()); + + // Trailing comma is allowed for the last argument + assert!(parse(r#" "".contains("") "#).is_ok()); + assert!(parse(r#" "".contains("",) "#).is_ok()); + assert!(parse(r#" "".contains("" , ) "#).is_ok()); + assert!(parse(r#" "".contains(,"") "#).is_err()); + assert!(parse(r#" "".contains("",,) "#).is_err()); + assert!(parse(r#" "".contains("" , , ) "#).is_err()); + assert!(parse(r#" label("","") "#).is_ok()); + assert!(parse(r#" label("","",) "#).is_ok()); + assert!(parse(r#" label("",,"") "#).is_err()); + } + #[test] fn test_integer_literal() { let extract = |x: Expression<()>| x.try_into_integer().unwrap().extract(&());