feat: add support for capturing spans

inside of UntypedExpr::ByteArray
      and UntypedPattern::ByteArray
This commit is contained in:
rvcas 2025-03-23 23:11:07 -04:00
parent cf4534be31
commit e5211cf792
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
4 changed files with 47 additions and 31 deletions

View File

@ -1535,8 +1535,8 @@ impl BinOp {
} }
} }
pub type UntypedPattern = Pattern<(), (), Namespace>; pub type UntypedPattern = Pattern<(), (), Namespace, (u8, Span)>;
pub type TypedPattern = Pattern<PatternConstructor, Rc<Type>, String>; pub type TypedPattern = Pattern<PatternConstructor, Rc<Type>, String, u8>;
impl TypedPattern { impl TypedPattern {
pub fn var(name: &str) -> Self { pub fn var(name: &str) -> Self {
@ -1660,7 +1660,7 @@ pub enum Namespace {
} }
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)] #[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Pattern<Constructor, Type, NamespaceKind> { pub enum Pattern<Constructor, Type, NamespaceKind, ByteValue> {
Int { Int {
location: Span, location: Span,
value: String, value: String,
@ -1669,7 +1669,7 @@ pub enum Pattern<Constructor, Type, NamespaceKind> {
ByteArray { ByteArray {
location: Span, location: Span,
value: Vec<u8>, value: Vec<ByteValue>,
preferred_format: ByteArrayFormatPreference, preferred_format: ByteArrayFormatPreference,
}, },
@ -1731,7 +1731,7 @@ pub enum Pattern<Constructor, Type, NamespaceKind> {
}, },
} }
impl<A, B, C> Pattern<A, B, C> { impl<A, B, C, BV> Pattern<A, B, C, BV> {
pub fn location(&self) -> Span { pub fn location(&self) -> Span {
match self { match self {
Pattern::Assign { pattern, .. } => pattern.location(), Pattern::Assign { pattern, .. } => pattern.location(),
@ -2207,11 +2207,11 @@ impl<T: Default> AssignmentKind<T> {
} }
} }
pub type MultiPattern<PatternConstructor, Type, NamespaceKind> = pub type MultiPattern<PatternConstructor, Type, NamespaceKind, ByteValue> =
Vec<Pattern<PatternConstructor, Type, NamespaceKind>>; Vec<Pattern<PatternConstructor, Type, NamespaceKind, ByteValue>>;
pub type UntypedMultiPattern = MultiPattern<(), (), Namespace>; pub type UntypedMultiPattern = MultiPattern<(), (), Namespace, (u8, Span)>;
pub type TypedMultiPattern = MultiPattern<PatternConstructor, Rc<Type>, String>; pub type TypedMultiPattern = MultiPattern<PatternConstructor, Rc<Type>, String, u8>;
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct UntypedClause { pub struct UntypedClause {

View File

@ -649,7 +649,7 @@ pub enum UntypedExpr {
ByteArray { ByteArray {
location: Span, location: Span,
bytes: Vec<u8>, bytes: Vec<(u8, Span)>,
preferred_format: ByteArrayFormatPreference, preferred_format: ByteArrayFormatPreference,
}, },
@ -977,7 +977,7 @@ impl UntypedExpr {
location: Span::empty(), location: Span::empty(),
value: UntypedExpr::ByteArray { value: UntypedExpr::ByteArray {
location: Span::empty(), location: Span::empty(),
bytes, bytes: bytes.into_iter().map(|b| (b, Span::empty())).collect(),
preferred_format: ByteArrayFormatPreference::HexadecimalString, preferred_format: ByteArrayFormatPreference::HexadecimalString,
}, },
}], }],
@ -1001,11 +1001,15 @@ impl UntypedExpr {
value: from_pallas_bigint(i).to_string(), value: from_pallas_bigint(i).to_string(),
}, },
PlutusData::BoundedBytes(bytes) => UntypedExpr::ByteArray { PlutusData::BoundedBytes(bytes) => {
let bytes: Vec<u8> = bytes.into();
UntypedExpr::ByteArray {
location: Span::empty(), location: Span::empty(),
bytes: bytes.into(), bytes: bytes.into_iter().map(|b| (b, Span::empty())).collect(),
preferred_format: ByteArrayFormatPreference::HexadecimalString, preferred_format: ByteArrayFormatPreference::HexadecimalString,
}, }
}
PlutusData::Array(elems) => UntypedExpr::List { PlutusData::Array(elems) => UntypedExpr::List {
location: Span::empty(), location: Span::empty(),
@ -1113,9 +1117,10 @@ impl UntypedExpr {
value: String::from_utf8(bytes.to_vec()).expect("invalid UTF-8 string"), value: String::from_utf8(bytes.to_vec()).expect("invalid UTF-8 string"),
}) })
} else { } else {
let bytes: Vec<u8> = bytes.into();
Ok(UntypedExpr::ByteArray { Ok(UntypedExpr::ByteArray {
location: Span::empty(), location: Span::empty(),
bytes: bytes.into(), bytes: bytes.into_iter().map(|b| (b, Span::empty())).collect(),
preferred_format: ByteArrayFormatPreference::HexadecimalString, preferred_format: ByteArrayFormatPreference::HexadecimalString,
}) })
} }

View File

@ -11,12 +11,14 @@ pub fn parser() -> impl Parser<Token, UntypedExpr, Error = ParseError> {
|bytes, preferred_format, curve, location, emit| match curve { |bytes, preferred_format, curve, location, emit| match curve {
Some(curve @ ast::CurveType::Bls12_381(point)) => { Some(curve @ ast::CurveType::Bls12_381(point)) => {
let point = match point { let point = match point {
ast::Bls12_381PointType::G1 => { ast::Bls12_381PointType::G1 => blst::blst_p1::uncompress(
blst::blst_p1::uncompress(&bytes).map(ast::Bls12_381Point::G1) &bytes.iter().map(|&(byte, _)| byte).collect::<Vec<u8>>(),
} )
ast::Bls12_381PointType::G2 => { .map(ast::Bls12_381Point::G1),
blst::blst_p2::uncompress(&bytes).map(ast::Bls12_381Point::G2) ast::Bls12_381PointType::G2 => blst::blst_p2::uncompress(
} &bytes.iter().map(|&(byte, _)| byte).collect::<Vec<u8>>(),
)
.map(ast::Bls12_381Point::G2),
}; };
let point = point.unwrap_or_else(|_err| { let point = point.unwrap_or_else(|_err| {

View File

@ -10,7 +10,7 @@ use crate::{
pub fn parser<A>( pub fn parser<A>(
into: impl Fn( into: impl Fn(
Vec<u8>, Vec<(u8, ast::Span)>,
ast::ByteArrayFormatPreference, ast::ByteArrayFormatPreference,
Option<ast::CurveType>, Option<ast::CurveType>,
ast::Span, ast::Span,
@ -20,7 +20,13 @@ pub fn parser<A>(
choice(( choice((
array_of_bytes(), array_of_bytes(),
hex_string(), 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| { .validate(move |(curve, preferred_format, bytes), span, emit| {
into(bytes, preferred_format, curve, span, emit) into(bytes, preferred_format, curve, span, emit)
@ -66,7 +72,7 @@ pub fn array_of_bytes() -> impl Parser<
( (
Option<ast::CurveType>, Option<ast::CurveType>,
ast::ByteArrayFormatPreference, ast::ByteArrayFormatPreference,
Vec<u8>, Vec<(u8, ast::Span)>,
), ),
Error = ParseError, Error = ParseError,
> { > {
@ -86,14 +92,14 @@ pub fn array_of_bytes() -> impl Parser<
0 0
} }
}; };
(byte, base) (byte, base, span)
}) })
.separated_by(just(Token::Comma)) .separated_by(just(Token::Comma))
.allow_trailing() .allow_trailing()
.delimited_by(just(Token::LeftSquare), just(Token::RightSquare)), .delimited_by(just(Token::LeftSquare), just(Token::RightSquare)),
) )
.validate(|(curve, bytes), span, emit| { .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)), None => Ok(Some(base)),
Some(previous_base) if previous_base == base => Ok(Some(base)), Some(previous_base) if previous_base == base => Ok(Some(base)),
_ => Err(()), _ => Err(()),
@ -114,7 +120,10 @@ pub fn array_of_bytes() -> impl Parser<
( (
curve, curve,
bytes.into_iter().map(|(b, _)| b).collect::<Vec<u8>>(), bytes
.into_iter()
.map(|(b, _, span)| (b, span))
.collect::<Vec<(u8, ast::Span)>>(),
base, base,
) )
}) })
@ -132,7 +141,7 @@ pub fn hex_string() -> impl Parser<
( (
Option<ast::CurveType>, Option<ast::CurveType>,
ast::ByteArrayFormatPreference, ast::ByteArrayFormatPreference,
Vec<u8>, Vec<(u8, ast::Span)>,
), ),
Error = ParseError, Error = ParseError,
> { > {
@ -153,7 +162,7 @@ pub fn hex_string() -> impl Parser<
( (
curve, curve,
ast::ByteArrayFormatPreference::HexadecimalString, ast::ByteArrayFormatPreference::HexadecimalString,
token, token.into_iter().map(|b| (b, ast::Span::empty())).collect(),
) )
}) })
} }