From e5211cf7924a823205072e9327c8e2490e7d3626 Mon Sep 17 00:00:00 2001 From: rvcas Date: Sun, 23 Mar 2025 23:11:07 -0400 Subject: [PATCH] feat: add support for capturing spans inside of UntypedExpr::ByteArray and UntypedPattern::ByteArray --- crates/aiken-lang/src/ast.rs | 18 ++++++------- crates/aiken-lang/src/expr.rs | 21 ++++++++++------ .../aiken-lang/src/parser/expr/bytearray.rs | 14 ++++++----- .../src/parser/literal/bytearray.rs | 25 +++++++++++++------ 4 files changed, 47 insertions(+), 31 deletions(-) diff --git a/crates/aiken-lang/src/ast.rs b/crates/aiken-lang/src/ast.rs index 853ce13d..c0d0e471 100644 --- a/crates/aiken-lang/src/ast.rs +++ b/crates/aiken-lang/src/ast.rs @@ -1535,8 +1535,8 @@ impl BinOp { } } -pub type UntypedPattern = Pattern<(), (), Namespace>; -pub type TypedPattern = Pattern, String>; +pub type UntypedPattern = Pattern<(), (), Namespace, (u8, Span)>; +pub type TypedPattern = Pattern, String, u8>; impl TypedPattern { pub fn var(name: &str) -> Self { @@ -1660,7 +1660,7 @@ pub enum Namespace { } #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] -pub enum Pattern { +pub enum Pattern { Int { location: Span, value: String, @@ -1669,7 +1669,7 @@ pub enum Pattern { ByteArray { location: Span, - value: Vec, + value: Vec, preferred_format: ByteArrayFormatPreference, }, @@ -1731,7 +1731,7 @@ pub enum Pattern { }, } -impl Pattern { +impl Pattern { pub fn location(&self) -> Span { match self { Pattern::Assign { pattern, .. } => pattern.location(), @@ -2207,11 +2207,11 @@ impl AssignmentKind { } } -pub type MultiPattern = - Vec>; +pub type MultiPattern = + Vec>; -pub type UntypedMultiPattern = MultiPattern<(), (), Namespace>; -pub type TypedMultiPattern = MultiPattern, String>; +pub type UntypedMultiPattern = MultiPattern<(), (), Namespace, (u8, Span)>; +pub type TypedMultiPattern = MultiPattern, String, u8>; #[derive(Debug, Clone, PartialEq)] pub struct UntypedClause { diff --git a/crates/aiken-lang/src/expr.rs b/crates/aiken-lang/src/expr.rs index 35d9ba83..4f5264e2 100644 --- a/crates/aiken-lang/src/expr.rs +++ b/crates/aiken-lang/src/expr.rs @@ -649,7 +649,7 @@ pub enum UntypedExpr { ByteArray { location: Span, - bytes: Vec, + bytes: Vec<(u8, Span)>, preferred_format: ByteArrayFormatPreference, }, @@ -977,7 +977,7 @@ impl UntypedExpr { location: Span::empty(), value: UntypedExpr::ByteArray { location: Span::empty(), - bytes, + bytes: bytes.into_iter().map(|b| (b, Span::empty())).collect(), preferred_format: ByteArrayFormatPreference::HexadecimalString, }, }], @@ -1001,11 +1001,15 @@ impl UntypedExpr { value: from_pallas_bigint(i).to_string(), }, - PlutusData::BoundedBytes(bytes) => UntypedExpr::ByteArray { - location: Span::empty(), - bytes: bytes.into(), - preferred_format: ByteArrayFormatPreference::HexadecimalString, - }, + PlutusData::BoundedBytes(bytes) => { + let bytes: Vec = bytes.into(); + + UntypedExpr::ByteArray { + location: Span::empty(), + bytes: bytes.into_iter().map(|b| (b, Span::empty())).collect(), + preferred_format: ByteArrayFormatPreference::HexadecimalString, + } + } PlutusData::Array(elems) => UntypedExpr::List { location: Span::empty(), @@ -1113,9 +1117,10 @@ impl UntypedExpr { value: String::from_utf8(bytes.to_vec()).expect("invalid UTF-8 string"), }) } else { + let bytes: Vec = bytes.into(); Ok(UntypedExpr::ByteArray { location: Span::empty(), - bytes: bytes.into(), + bytes: bytes.into_iter().map(|b| (b, Span::empty())).collect(), preferred_format: ByteArrayFormatPreference::HexadecimalString, }) } diff --git a/crates/aiken-lang/src/parser/expr/bytearray.rs b/crates/aiken-lang/src/parser/expr/bytearray.rs index d5f5ce29..8b7b9e02 100644 --- a/crates/aiken-lang/src/parser/expr/bytearray.rs +++ b/crates/aiken-lang/src/parser/expr/bytearray.rs @@ -11,12 +11,14 @@ pub fn parser() -> impl Parser { |bytes, preferred_format, curve, location, emit| match curve { Some(curve @ ast::CurveType::Bls12_381(point)) => { let point = match point { - ast::Bls12_381PointType::G1 => { - blst::blst_p1::uncompress(&bytes).map(ast::Bls12_381Point::G1) - } - ast::Bls12_381PointType::G2 => { - blst::blst_p2::uncompress(&bytes).map(ast::Bls12_381Point::G2) - } + ast::Bls12_381PointType::G1 => blst::blst_p1::uncompress( + &bytes.iter().map(|&(byte, _)| byte).collect::>(), + ) + .map(ast::Bls12_381Point::G1), + ast::Bls12_381PointType::G2 => blst::blst_p2::uncompress( + &bytes.iter().map(|&(byte, _)| byte).collect::>(), + ) + .map(ast::Bls12_381Point::G2), }; let point = point.unwrap_or_else(|_err| { diff --git a/crates/aiken-lang/src/parser/literal/bytearray.rs b/crates/aiken-lang/src/parser/literal/bytearray.rs index 84bfe188..c305a3ed 100644 --- a/crates/aiken-lang/src/parser/literal/bytearray.rs +++ b/crates/aiken-lang/src/parser/literal/bytearray.rs @@ -10,7 +10,7 @@ use crate::{ pub fn parser( into: impl Fn( - Vec, + Vec<(u8, ast::Span)>, ast::ByteArrayFormatPreference, Option, ast::Span, @@ -20,7 +20,13 @@ pub fn parser( choice(( array_of_bytes(), hex_string(), - utf8_string().map(|(p, b)| (None, p, b)), + utf8_string().map(|(p, b)| { + ( + None, + p, + b.into_iter().map(|b| (b, ast::Span::empty())).collect(), + ) + }), )) .validate(move |(curve, preferred_format, bytes), span, emit| { into(bytes, preferred_format, curve, span, emit) @@ -66,7 +72,7 @@ pub fn array_of_bytes() -> impl Parser< ( Option, ast::ByteArrayFormatPreference, - Vec, + Vec<(u8, ast::Span)>, ), Error = ParseError, > { @@ -86,14 +92,14 @@ pub fn array_of_bytes() -> impl Parser< 0 } }; - (byte, base) + (byte, base, span) }) .separated_by(just(Token::Comma)) .allow_trailing() .delimited_by(just(Token::LeftSquare), just(Token::RightSquare)), ) .validate(|(curve, bytes), span, emit| { - let base = bytes.iter().try_fold(None, |acc, (_, base)| match acc { + let base = bytes.iter().try_fold(None, |acc, (_, base, _)| match acc { None => Ok(Some(base)), Some(previous_base) if previous_base == base => Ok(Some(base)), _ => Err(()), @@ -114,7 +120,10 @@ pub fn array_of_bytes() -> impl Parser< ( curve, - bytes.into_iter().map(|(b, _)| b).collect::>(), + bytes + .into_iter() + .map(|(b, _, span)| (b, span)) + .collect::>(), base, ) }) @@ -132,7 +141,7 @@ pub fn hex_string() -> impl Parser< ( Option, ast::ByteArrayFormatPreference, - Vec, + Vec<(u8, ast::Span)>, ), Error = ParseError, > { @@ -153,7 +162,7 @@ pub fn hex_string() -> impl Parser< ( curve, ast::ByteArrayFormatPreference::HexadecimalString, - token, + token.into_iter().map(|b| (b, ast::Span::empty())).collect(), ) }) }