Re-implement and extend docs formatter for constants

This commit is contained in:
KtorZ 2024-08-17 15:54:20 +02:00
parent 71f90ad49f
commit 6c2e3272da
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
4 changed files with 66 additions and 13 deletions

View File

@ -30,6 +30,7 @@ pub enum TypedExpr {
location: Span, location: Span,
tipo: Rc<Type>, tipo: Rc<Type>,
value: String, value: String,
base: Base,
}, },
String { String {
@ -42,12 +43,14 @@ pub enum TypedExpr {
location: Span, location: Span,
tipo: Rc<Type>, tipo: Rc<Type>,
bytes: Vec<u8>, bytes: Vec<u8>,
preferred_format: ByteArrayFormatPreference,
}, },
CurvePoint { CurvePoint {
location: Span, location: Span,
tipo: Rc<Type>, tipo: Rc<Type>,
point: Box<Curve>, point: Box<Curve>,
preferred_format: ByteArrayFormatPreference,
}, },
Sequence { Sequence {

View File

@ -340,12 +340,53 @@ impl<'comments> Formatter<'comments> {
pub fn docs_const_expr<'a>(&mut self, name: &'a str, value: &'a TypedExpr) -> Document<'a> { pub fn docs_const_expr<'a>(&mut self, name: &'a str, value: &'a TypedExpr) -> Document<'a> {
let mut printer = tipo::pretty::Printer::new(); let mut printer = tipo::pretty::Printer::new();
name.to_doc() let doc = name
.to_doc()
.append(": ") .append(": ")
.append(printer.print(&value.tipo())) .append(printer.print(&value.tipo()));
// TODO: Show full expression in docs when simple enough
// .append(" = ") // NOTE: Only display the full value for simple expressions.
// .append(self.const_expr(value)) let value = self.const_expr(value);
if value.is_empty() {
doc
} else {
doc.append(" = ").append(value)
}
}
pub fn const_expr<'a>(&mut self, value: &'a TypedExpr) -> Document<'a> {
match value {
TypedExpr::UInt { value, base, .. } => self.int(value, base),
TypedExpr::String { value, .. } => self.string(value),
TypedExpr::ByteArray {
bytes,
preferred_format,
..
} => self.bytearray(bytes, None, preferred_format),
TypedExpr::CurvePoint {
point,
preferred_format,
..
} => self.bytearray(
&point.compress(),
Some(point.as_ref().into()),
preferred_format,
),
TypedExpr::Tuple { elems, .. } => {
wrap_args(elems.iter().map(|e| (self.const_expr(e), false))).group()
}
TypedExpr::Pair { fst, snd, .. } => {
let elems = [fst, snd];
"Pair"
.to_doc()
.append(wrap_args(elems.iter().map(|e| (self.const_expr(e), false))).group())
}
TypedExpr::List { elements, .. } => {
wrap_args(elements.iter().map(|e| (self.const_expr(e), false))).group()
}
TypedExpr::Var { name, .. } => name.to_doc(),
_ => Document::Str(""),
}
} }
fn documented_definition<'a>(&mut self, s: &'a UntypedDefinition) -> Document<'a> { fn documented_definition<'a>(&mut self, s: &'a UntypedDefinition) -> Document<'a> {

View File

@ -1,9 +1,8 @@
use chumsky::prelude::*;
use crate::{ use crate::{
expr::UntypedExpr, expr::UntypedExpr,
parser::{error::ParseError, token::Token}, parser::{error::ParseError, token::Token},
}; };
use chumsky::prelude::*;
pub fn parser() -> impl Parser<Token, UntypedExpr, Error = ParseError> { pub fn parser() -> impl Parser<Token, UntypedExpr, Error = ParseError> {
recursive(|sequence| { recursive(|sequence| {

View File

@ -20,6 +20,7 @@ use crate::{
builtins::{from_default_function, BUILTIN}, builtins::{from_default_function, BUILTIN},
expr::{FnStyle, TypedExpr, UntypedExpr}, expr::{FnStyle, TypedExpr, UntypedExpr},
format, format,
parser::token::Base,
tipo::{fields::FieldMap, DefaultFunction, PatternConstructor, TypeVar}, tipo::{fields::FieldMap, DefaultFunction, PatternConstructor, TypeVar},
IdGenerator, IdGenerator,
}; };
@ -434,8 +435,8 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
UntypedExpr::UInt { UntypedExpr::UInt {
location, location,
value, value,
base: _, base,
} => Ok(self.infer_uint(value, location)), } => Ok(self.infer_uint(value, base, location)),
UntypedExpr::Sequence { UntypedExpr::Sequence {
expressions, expressions,
@ -550,8 +551,8 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
UntypedExpr::CurvePoint { UntypedExpr::CurvePoint {
location, location,
point, point,
preferred_format: _, preferred_format,
} => self.infer_curve_point(*point, location), } => self.infer_curve_point(*point, preferred_format, location),
UntypedExpr::RecordUpdate { UntypedExpr::RecordUpdate {
location, location,
@ -592,10 +593,16 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
location, location,
bytes, bytes,
tipo: Type::byte_array(), tipo: Type::byte_array(),
preferred_format,
}) })
} }
fn infer_curve_point(&mut self, curve: Curve, location: Span) -> Result<TypedExpr, Error> { fn infer_curve_point(
&mut self,
curve: Curve,
preferred_format: ByteArrayFormatPreference,
location: Span,
) -> Result<TypedExpr, Error> {
let tipo = match curve { let tipo = match curve {
Curve::Bls12_381(point) => match point { Curve::Bls12_381(point) => match point {
Bls12_381Point::G1(_) => Type::g1_element(), Bls12_381Point::G1(_) => Type::g1_element(),
@ -607,6 +614,7 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
location, location,
point: curve.into(), point: curve.into(),
tipo, tipo,
preferred_format,
}) })
} }
@ -1707,11 +1715,12 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
Ok((args, body, return_type)) Ok((args, body, return_type))
} }
fn infer_uint(&mut self, value: String, location: Span) -> TypedExpr { fn infer_uint(&mut self, value: String, base: Base, location: Span) -> TypedExpr {
TypedExpr::UInt { TypedExpr::UInt {
location, location,
value, value,
tipo: Type::int(), tipo: Type::int(),
base,
} }
} }
@ -2720,6 +2729,7 @@ fn diagnose_expr(expr: TypedExpr) -> TypedExpr {
tipo: Type::byte_array(), tipo: Type::byte_array(),
bytes: vec![], bytes: vec![],
location, location,
preferred_format: ByteArrayFormatPreference::HexadecimalString,
}, },
}, },
], ],