From c01145a3b4f1875f7d5c44fbf549c60b989ee153 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Sat, 12 Oct 2024 08:59:08 +0200 Subject: [PATCH] feat: chomped string block Resolves: https://github.com/google/jsonnet/issues/289 --- crates/jrsonnet-parser/src/lib.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/crates/jrsonnet-parser/src/lib.rs b/crates/jrsonnet-parser/src/lib.rs index e70fe621..cc5b4381 100644 --- a/crates/jrsonnet-parser/src/lib.rs +++ b/crates/jrsonnet-parser/src/lib.rs @@ -143,12 +143,21 @@ parser! { pub rule whole_line() -> &'input str = str:$((!['\n'][_])* "\n") {str} pub rule string_block() -> String - = "|||" (!['\n']single_whitespace())* "\n" - empty_lines:$(['\n']*) - prefix:[' ' | '\t']+ first_line:whole_line() - lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})* - [' ' | '\t']*<, {prefix.len() - 1}> "|||" - {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l} + = "|||" chomped:"-"? (!['\n']single_whitespace())* "\n" + empty_lines:$(['\n']*) + prefix:[' ' | '\t']+ first_line:whole_line() + lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})* + [' ' | '\t']*<, {prefix.len() - 1}> "|||" + { + let mut l = empty_lines.to_owned(); + l.push_str(first_line); + l.extend(lines); + if chomped.is_some() { + debug_assert!(l.ends_with('\n')); + l.truncate(l.len() - 1); + } + l + } rule hex_char() = quiet! { ['0'..='9' | 'a'..='f' | 'A'..='F'] } / expected!("")