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 / "\\\"" { '\"' } // double quote
/ "\\'" { '\'' } // single quote / "\\'" { '\'' } // single quote
/ "\\\\" { '\\' } // backslash / "\\\\" { '\\' } // 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") / expected!("or any valid ascii character")

View File

@ -273,15 +273,17 @@ impl Constant {
match self { match self {
Constant::Integer(i) => RcDoc::as_string(i), Constant::Integer(i) => RcDoc::as_string(i),
Constant::ByteString(bs) => RcDoc::text("#").append(RcDoc::text(hex::encode(bs))), Constant::ByteString(bs) => RcDoc::text("#").append(RcDoc::text(hex::encode(bs))),
Constant::String(s) => RcDoc::text("\"").append(RcDoc::text( Constant::String(s) => RcDoc::text("\"")
String::from_utf8( .append(RcDoc::text(
s.as_bytes() String::from_utf8(
.iter() s.as_bytes()
.flat_map(|c| escape_default(*c).collect::<Vec<u8>>()) .iter()
.collect(), .flat_map(|c| escape_default(*c).collect::<Vec<u8>>())
) .collect(),
.unwrap(), )
)) .append(RcDoc::text("\"")), .unwrap(),
))
.append(RcDoc::text("\"")),
Constant::Unit => RcDoc::text("()"), Constant::Unit => RcDoc::text("()"),
Constant::Bool(b) => RcDoc::text(if *b { "True" } else { "False" }), Constant::Bool(b) => RcDoc::text(if *b { "True" } else { "False" }),
Constant::ProtoList(_, items) => RcDoc::text("[") Constant::ProtoList(_, items) => RcDoc::text("[")