From b25e82ed3696863ce85a9d6fea6989cca267e429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=BCndler?= Date: Fri, 8 Dec 2023 17:51:13 +0100 Subject: [PATCH] Handle errors and format --- crates/uplc/src/parser.rs | 5 ++++- crates/uplc/src/pretty.rs | 20 +++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/crates/uplc/src/parser.rs b/crates/uplc/src/parser.rs index 2d00aa9c..2a0d3924 100644 --- a/crates/uplc/src/parser.rs +++ b/crates/uplc/src/parser.rs @@ -232,7 +232,10 @@ peg::parser! { / "\\\"" { '\"' } // double quote / "\\'" { '\'' } // single quote / "\\\\" { '\\' } // backslash - / "\\x" i:character() i2:character() { hex::decode([i, i2].iter().collect::()).unwrap()[0].into() } + / "\\x" i:character() i2:character() {? match hex::decode([i,i2].iter().collect::()) { + Ok(res) => {Ok(res[0].into())}, + Err(_) => {Err("Invalid hex encoding of escaped byte")}, + } } // hex encoded byte / [ ^ '"' ] / expected!("or any valid ascii character") diff --git a/crates/uplc/src/pretty.rs b/crates/uplc/src/pretty.rs index 140bb0aa..119bb15e 100644 --- a/crates/uplc/src/pretty.rs +++ b/crates/uplc/src/pretty.rs @@ -273,15 +273,17 @@ impl Constant { match self { Constant::Integer(i) => RcDoc::as_string(i), Constant::ByteString(bs) => RcDoc::text("#").append(RcDoc::text(hex::encode(bs))), - Constant::String(s) => RcDoc::text("\"").append(RcDoc::text( - String::from_utf8( - s.as_bytes() - .iter() - .flat_map(|c| escape_default(*c).collect::>()) - .collect(), - ) - .unwrap(), - )) .append(RcDoc::text("\"")), + Constant::String(s) => RcDoc::text("\"") + .append(RcDoc::text( + String::from_utf8( + s.as_bytes() + .iter() + .flat_map(|c| escape_default(*c).collect::>()) + .collect(), + ) + .unwrap(), + )) + .append(RcDoc::text("\"")), Constant::Unit => RcDoc::text("()"), Constant::Bool(b) => RcDoc::text(if *b { "True" } else { "False" }), Constant::ProtoList(_, items) => RcDoc::text("[")