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,
tipo: Rc<Type>,
value: String,
base: Base,
},
String {
@ -42,12 +43,14 @@ pub enum TypedExpr {
location: Span,
tipo: Rc<Type>,
bytes: Vec<u8>,
preferred_format: ByteArrayFormatPreference,
},
CurvePoint {
location: Span,
tipo: Rc<Type>,
point: Box<Curve>,
preferred_format: ByteArrayFormatPreference,
},
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> {
let mut printer = tipo::pretty::Printer::new();
name.to_doc()
let doc = name
.to_doc()
.append(": ")
.append(printer.print(&value.tipo()))
// TODO: Show full expression in docs when simple enough
// .append(" = ")
// .append(self.const_expr(value))
.append(printer.print(&value.tipo()));
// NOTE: Only display the full value for simple expressions.
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> {

View File

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

View File

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