Handle errors and format

This commit is contained in:
Niels Mündler 2023-12-08 17:51:13 +01:00 committed by Lucas
parent 772e73ae48
commit b25e82ed36
2 changed files with 15 additions and 10 deletions

View File

@ -232,7 +232,10 @@ peg::parser! {
/ "\\\"" { '\"' } // double quote
/ "\\'" { '\'' } // single quote
/ "\\\\" { '\\' } // backslash
/ "\\x" i:character() i2:character() { hex::decode([i, i2].iter().collect::<String>()).unwrap()[0].into() }
/ "\\x" i:character() i2:character() {? match hex::decode([i,i2].iter().collect::<String>()) {
Ok(res) => {Ok(res[0].into())},
Err(_) => {Err("Invalid hex encoding of escaped byte")},
} } // hex encoded byte
/ [ ^ '"' ]
/ expected!("or any valid ascii character")

View File

@ -273,7 +273,8 @@ 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(
Constant::String(s) => RcDoc::text("\"")
.append(RcDoc::text(
String::from_utf8(
s.as_bytes()
.iter()
@ -281,7 +282,8 @@ impl Constant {
.collect(),
)
.unwrap(),
)) .append(RcDoc::text("\"")),
))
.append(RcDoc::text("\"")),
Constant::Unit => RcDoc::text("()"),
Constant::Bool(b) => RcDoc::text(if *b { "True" } else { "False" }),
Constant::ProtoList(_, items) => RcDoc::text("[")