From 057102c49129607db76cf05a055c002c51d603d8 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Wed, 5 Apr 2023 14:25:19 +0200 Subject: [PATCH] Add function to uplc::parser for string escape Was originally written as a way to fix a failing property test on the program_builder; but the program builder is now gone. This function is still useful to have around. --- crates/uplc/src/parser.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/crates/uplc/src/parser.rs b/crates/uplc/src/parser.rs index a0ecfde0..370dd5f4 100644 --- a/crates/uplc/src/parser.rs +++ b/crates/uplc/src/parser.rs @@ -55,6 +55,21 @@ fn pair_sub_type(type_info: Option<&Type>) -> Option<(&Type, &Type)> { } } +pub fn escape(string: &str) -> String { + string + .chars() + .flat_map(|c| match c { + '\n' => vec!['\\', c], + '\r' => vec!['\\', c], + '\t' => vec!['\\', c], + '\'' => vec!['\\', c], + '\\' => vec!['\\', c], + '"' => vec!['\\', c], + _ => vec![c], + }) + .collect::() +} + peg::parser! { grammar uplc() for str { pub rule program() -> Program