From 739f38beac28b9ad1c8da01ecc3e2e029a75a052 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=BCndler?= Date: Thu, 8 Sep 2022 22:10:12 +0200 Subject: [PATCH] UPLC data parsing support by CBOR notation (#37) * Implement constant data parsing support New notation: (con data #0000) Where #0000 is the CBOR hex representation of a PlutusDatum * Add pretty printing support for data * Format --- crates/uplc/src/parser.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/uplc/src/parser.rs b/crates/uplc/src/parser.rs index 5caee1ca..1bb6d5d7 100644 --- a/crates/uplc/src/parser.rs +++ b/crates/uplc/src/parser.rs @@ -6,6 +6,7 @@ use crate::{ }; use interner::Interner; +use pallas_primitives::{alonzo::PlutusData, Fragment}; use peg::{error::ParseError, str::LineCol}; mod interner; @@ -53,6 +54,7 @@ peg::parser! { / constant_string() / constant_unit() / constant_bool() + / constant_data() ) _* ")" { Term::Constant(con) } @@ -110,6 +112,15 @@ peg::parser! { rule number() -> isize = n:$("-"* ['0'..='9']+) {? n.parse().or(Err("isize")) } + rule constant_data() -> Constant + = "data" _+ "#" i:ident()* { + Constant::Data( + PlutusData::decode_fragment( + hex::decode(String::from_iter(i)).unwrap().as_slice() + ).unwrap() + ) + } + rule name() -> Name = text:ident() { Name { text, unique: 0.into() } }