More types stuff (#5901)
* parse union and fancy array types Signed-off-by: Nick Cameron <nrc@ncameron.org> * type aliases Signed-off-by: Nick Cameron <nrc@ncameron.org> * Treat Helix and Face as primitive types Signed-off-by: Nick Cameron <nrc@ncameron.org> * code motion: factor our execution::types module Signed-off-by: Nick Cameron <nrc@ncameron.org> * Tests for type coercion and subtyping Signed-off-by: Nick Cameron <nrc@ncameron.org> * Add Point2D/3D to std Signed-off-by: Nick Cameron <nrc@ncameron.org> * Rebasing and fixes Signed-off-by: Nick Cameron <nrc@ncameron.org> --------- Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
@ -194,9 +194,21 @@ impl Type {
|
||||
hasher.update(b"FnArgType::Primitive");
|
||||
hasher.update(prim.compute_digest())
|
||||
}
|
||||
Type::Array(prim) => {
|
||||
Type::Array { ty, len } => {
|
||||
hasher.update(b"FnArgType::Array");
|
||||
hasher.update(prim.compute_digest())
|
||||
hasher.update(ty.compute_digest());
|
||||
match len {
|
||||
crate::execution::types::ArrayLen::None => {}
|
||||
crate::execution::types::ArrayLen::NonEmpty => hasher.update(usize::MAX.to_ne_bytes()),
|
||||
crate::execution::types::ArrayLen::Known(n) => hasher.update(n.to_ne_bytes()),
|
||||
}
|
||||
}
|
||||
Type::Union { tys } => {
|
||||
hasher.update(b"FnArgType::Union");
|
||||
hasher.update(tys.len().to_ne_bytes());
|
||||
for t in tys.iter_mut() {
|
||||
hasher.update(t.compute_digest());
|
||||
}
|
||||
}
|
||||
Type::Object { properties } => {
|
||||
hasher.update(b"FnArgType::Object");
|
||||
@ -300,6 +312,9 @@ impl TypeDeclaration {
|
||||
hasher.update(a.compute_digest());
|
||||
}
|
||||
}
|
||||
if let Some(alias) = &mut slf.alias {
|
||||
hasher.update(alias.compute_digest());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -25,7 +25,7 @@ pub use crate::parsing::ast::types::{
|
||||
use crate::{
|
||||
docs::StdLibFn,
|
||||
errors::KclError,
|
||||
execution::{annotations, KclValue, Metadata, TagIdentifier},
|
||||
execution::{annotations, types::ArrayLen, KclValue, Metadata, TagIdentifier},
|
||||
parsing::{ast::digest::Digest, token::NumericSuffix, PIPE_OPERATOR},
|
||||
source_range::SourceRange,
|
||||
ModuleId,
|
||||
@ -150,7 +150,7 @@ impl<T> Node<T> {
|
||||
self.start <= pos && pos <= self.end
|
||||
}
|
||||
|
||||
pub fn map<U>(self, f: fn(T) -> U) -> Node<U> {
|
||||
pub fn map<U>(self, f: impl Fn(T) -> U) -> Node<U> {
|
||||
Node {
|
||||
inner: f(self.inner),
|
||||
start: self.start,
|
||||
@ -1895,6 +1895,7 @@ pub struct TypeDeclaration {
|
||||
pub args: Option<NodeList<Identifier>>,
|
||||
#[serde(default, skip_serializing_if = "ItemVisibility::is_default")]
|
||||
pub visibility: ItemVisibility,
|
||||
pub alias: Option<Node<Type>>,
|
||||
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
#[ts(optional)]
|
||||
@ -3024,7 +3025,14 @@ pub enum Type {
|
||||
/// A primitive type.
|
||||
Primitive(PrimitiveType),
|
||||
// An array of a primitive type.
|
||||
Array(PrimitiveType),
|
||||
Array {
|
||||
ty: PrimitiveType,
|
||||
len: ArrayLen,
|
||||
},
|
||||
// Union/enum types
|
||||
Union {
|
||||
tys: NodeList<PrimitiveType>,
|
||||
},
|
||||
// An object type.
|
||||
Object {
|
||||
properties: Vec<Parameter>,
|
||||
@ -3035,7 +3043,22 @@ impl fmt::Display for Type {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Type::Primitive(primitive_type) => primitive_type.fmt(f),
|
||||
Type::Array(primitive_type) => write!(f, "[{primitive_type}]"),
|
||||
Type::Array { ty, len } => {
|
||||
write!(f, "[{ty}")?;
|
||||
match len {
|
||||
ArrayLen::None => {}
|
||||
ArrayLen::NonEmpty => write!(f, "; 1+")?,
|
||||
ArrayLen::Known(n) => write!(f, "; {n}")?,
|
||||
}
|
||||
write!(f, "]")
|
||||
}
|
||||
Type::Union { tys } => {
|
||||
write!(
|
||||
f,
|
||||
"{}",
|
||||
tys.iter().map(|t| t.to_string()).collect::<Vec<_>>().join(" | ")
|
||||
)
|
||||
}
|
||||
Type::Object { properties } => {
|
||||
write!(f, "{{")?;
|
||||
let mut first = true;
|
||||
@ -3624,11 +3647,17 @@ const cylinder = startSketchOn('-XZ')
|
||||
assert_eq!(params.len(), 3);
|
||||
assert_eq!(
|
||||
params[0].type_.as_ref().unwrap().inner,
|
||||
Type::Array(PrimitiveType::Number(NumericSuffix::None))
|
||||
Type::Array {
|
||||
ty: PrimitiveType::Number(NumericSuffix::None),
|
||||
len: ArrayLen::None
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
params[1].type_.as_ref().unwrap().inner,
|
||||
Type::Array(PrimitiveType::String)
|
||||
Type::Array {
|
||||
ty: PrimitiveType::String,
|
||||
len: ArrayLen::None
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
params[2].type_.as_ref().unwrap().inner,
|
||||
@ -3656,7 +3685,10 @@ const cylinder = startSketchOn('-XZ')
|
||||
assert_eq!(params.len(), 3);
|
||||
assert_eq!(
|
||||
params[0].type_.as_ref().unwrap().inner,
|
||||
Type::Array(PrimitiveType::Number(NumericSuffix::None))
|
||||
Type::Array {
|
||||
ty: PrimitiveType::Number(NumericSuffix::None),
|
||||
len: ArrayLen::None
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
params[1].type_.as_ref().unwrap().inner,
|
||||
@ -3692,7 +3724,15 @@ const cylinder = startSketchOn('-XZ')
|
||||
56,
|
||||
module_id,
|
||||
),
|
||||
type_: Some(Node::new(Type::Array(PrimitiveType::String), 59, 65, module_id)),
|
||||
type_: Some(Node::new(
|
||||
Type::Array {
|
||||
ty: PrimitiveType::String,
|
||||
len: ArrayLen::None
|
||||
},
|
||||
59,
|
||||
65,
|
||||
module_id
|
||||
)),
|
||||
default_value: None,
|
||||
labeled: true,
|
||||
digest: None
|
||||
@ -3773,7 +3813,15 @@ const cylinder = startSketchOn('-XZ')
|
||||
34,
|
||||
module_id,
|
||||
),
|
||||
type_: Some(Node::new(Type::Array(PrimitiveType::String), 37, 43, module_id)),
|
||||
type_: Some(Node::new(
|
||||
Type::Array {
|
||||
ty: PrimitiveType::String,
|
||||
len: ArrayLen::None
|
||||
},
|
||||
37,
|
||||
43,
|
||||
module_id
|
||||
)),
|
||||
default_value: None,
|
||||
labeled: true,
|
||||
digest: None
|
||||
@ -3946,7 +3994,7 @@ startSketchOn('XY')"#;
|
||||
|
||||
assert_eq!(
|
||||
meta_settings.default_length_units,
|
||||
crate::execution::kcl_value::UnitLen::Inches
|
||||
crate::execution::types::UnitLen::Inches
|
||||
);
|
||||
}
|
||||
|
||||
@ -3962,13 +4010,13 @@ startSketchOn('XY')"#;
|
||||
|
||||
assert_eq!(
|
||||
meta_settings.default_length_units,
|
||||
crate::execution::kcl_value::UnitLen::Inches
|
||||
crate::execution::types::UnitLen::Inches
|
||||
);
|
||||
|
||||
// Edit the ast.
|
||||
let new_program = program
|
||||
.change_meta_settings(crate::execution::MetaSettings {
|
||||
default_length_units: crate::execution::kcl_value::UnitLen::Mm,
|
||||
default_length_units: crate::execution::types::UnitLen::Mm,
|
||||
..Default::default()
|
||||
})
|
||||
.unwrap();
|
||||
@ -3977,10 +4025,7 @@ startSketchOn('XY')"#;
|
||||
assert!(result.is_some());
|
||||
let meta_settings = result.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
meta_settings.default_length_units,
|
||||
crate::execution::kcl_value::UnitLen::Mm
|
||||
);
|
||||
assert_eq!(meta_settings.default_length_units, crate::execution::types::UnitLen::Mm);
|
||||
|
||||
let formatted = new_program.recast(&Default::default(), 0);
|
||||
|
||||
@ -4003,7 +4048,7 @@ startSketchOn('XY')
|
||||
// Edit the ast.
|
||||
let new_program = program
|
||||
.change_meta_settings(crate::execution::MetaSettings {
|
||||
default_length_units: crate::execution::kcl_value::UnitLen::Mm,
|
||||
default_length_units: crate::execution::types::UnitLen::Mm,
|
||||
..Default::default()
|
||||
})
|
||||
.unwrap();
|
||||
@ -4012,10 +4057,7 @@ startSketchOn('XY')
|
||||
assert!(result.is_some());
|
||||
let meta_settings = result.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
meta_settings.default_length_units,
|
||||
crate::execution::kcl_value::UnitLen::Mm
|
||||
);
|
||||
assert_eq!(meta_settings.default_length_units, crate::execution::types::UnitLen::Mm);
|
||||
|
||||
let formatted = new_program.recast(&Default::default(), 0);
|
||||
|
||||
|
@ -19,6 +19,7 @@ use super::{
|
||||
use crate::{
|
||||
docs::StdLibFn,
|
||||
errors::{CompilationError, Severity, Tag},
|
||||
execution::types::ArrayLen,
|
||||
parsing::{
|
||||
ast::types::{
|
||||
Annotation, ArrayExpression, ArrayRangeExpression, BinaryExpression, BinaryOperator, BinaryPart, BodyItem,
|
||||
@ -2215,7 +2216,7 @@ fn ty_decl(i: &mut TokenSlice) -> PResult<BoxNode<TypeDeclaration>> {
|
||||
let name = identifier(i)?;
|
||||
let mut end = name.end;
|
||||
|
||||
let args = if peek(open_paren).parse_next(i).is_ok() {
|
||||
let args = if peek((opt(whitespace), open_paren)).parse_next(i).is_ok() {
|
||||
ignore_whitespace(i);
|
||||
open_paren(i)?;
|
||||
ignore_whitespace(i);
|
||||
@ -2228,11 +2229,28 @@ fn ty_decl(i: &mut TokenSlice) -> PResult<BoxNode<TypeDeclaration>> {
|
||||
None
|
||||
};
|
||||
|
||||
let alias = if peek((opt(whitespace), equals)).parse_next(i).is_ok() {
|
||||
ignore_whitespace(i);
|
||||
equals(i)?;
|
||||
ignore_whitespace(i);
|
||||
let ty = argument_type(i)?;
|
||||
|
||||
ParseContext::warn(CompilationError::err(
|
||||
ty.as_source_range(),
|
||||
"Type aliases are experimental, likely to change in the future, and likely to not work properly.",
|
||||
));
|
||||
|
||||
Some(ty)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let module_id = name.module_id;
|
||||
let result = Node::boxed(
|
||||
TypeDeclaration {
|
||||
name,
|
||||
args,
|
||||
alias,
|
||||
visibility,
|
||||
digest: None,
|
||||
},
|
||||
@ -2336,21 +2354,7 @@ impl TryFrom<Token> for Node<TagDeclarator> {
|
||||
format!("Cannot assign a tag to a reserved keyword: {}", token.value.as_str()),
|
||||
)),
|
||||
|
||||
TokenType::Bang
|
||||
| TokenType::At
|
||||
| TokenType::Hash
|
||||
| TokenType::Colon
|
||||
| TokenType::Period
|
||||
| TokenType::Operator
|
||||
| TokenType::DoublePeriod
|
||||
| TokenType::QuestionMark
|
||||
| TokenType::BlockComment
|
||||
| TokenType::Function
|
||||
| TokenType::String
|
||||
| TokenType::Dollar
|
||||
| TokenType::Keyword
|
||||
| TokenType::Unknown
|
||||
| TokenType::LineComment => Err(CompilationError::fatal(
|
||||
_ => Err(CompilationError::fatal(
|
||||
token.as_source_range(),
|
||||
// this is `start with` because if most of these cases are in the middle, it ends
|
||||
// up hitting a different error path(e.g. including a bang) or being valid(e.g. including a comment) since it will get broken up into
|
||||
@ -2617,6 +2621,14 @@ fn colon(i: &mut TokenSlice) -> PResult<Token> {
|
||||
TokenType::Colon.parse_from(i)
|
||||
}
|
||||
|
||||
fn semi_colon(i: &mut TokenSlice) -> PResult<Token> {
|
||||
TokenType::SemiColon.parse_from(i)
|
||||
}
|
||||
|
||||
fn plus(i: &mut TokenSlice) -> PResult<Token> {
|
||||
one_of((TokenType::Operator, "+")).parse_next(i)
|
||||
}
|
||||
|
||||
fn equals(i: &mut TokenSlice) -> PResult<Token> {
|
||||
one_of((TokenType::Operator, "="))
|
||||
.context(expected("the equals operator, ="))
|
||||
@ -2659,6 +2671,12 @@ fn comma_sep(i: &mut TokenSlice) -> PResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Parse a `|`, optionally followed by some whitespace.
|
||||
fn pipe_sep(i: &mut TokenSlice) -> PResult<()> {
|
||||
(opt(whitespace), one_of((TokenType::Operator, "|")), opt(whitespace)).parse_next(i)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Arguments are passed into a function.
|
||||
fn arguments(i: &mut TokenSlice) -> PResult<Vec<Expr>> {
|
||||
separated(0.., expression, comma_sep)
|
||||
@ -2685,7 +2703,15 @@ fn argument_type(i: &mut TokenSlice) -> PResult<Node<Type>> {
|
||||
let type_ = alt((
|
||||
// Object types
|
||||
// TODO it is buggy to treat object fields like parameters since the parameters parser assumes a terminating `)`.
|
||||
(open_brace, parameters, close_brace).map(|(open, params, close)| {
|
||||
(open_brace, parameters, close_brace).try_map(|(open, params, close)| {
|
||||
for p in ¶ms {
|
||||
if p.type_.is_none() {
|
||||
return Err(CompilationError::fatal(
|
||||
p.identifier.as_source_range(),
|
||||
"Missing type for field in record type",
|
||||
));
|
||||
}
|
||||
}
|
||||
Ok(Node::new(
|
||||
Type::Object { properties: params },
|
||||
open.start,
|
||||
@ -2694,12 +2720,21 @@ fn argument_type(i: &mut TokenSlice) -> PResult<Node<Type>> {
|
||||
))
|
||||
}),
|
||||
// Array types
|
||||
(open_bracket, primitive_type, close_bracket).map(|(_, t, _)| Ok(t.map(Type::Array))),
|
||||
// Primitive types
|
||||
primitive_type.map(|t| Ok(t.map(Type::Primitive))),
|
||||
array_type,
|
||||
// Primitive or union types
|
||||
separated(1.., primitive_type, pipe_sep).map(|mut tys: Vec<_>| {
|
||||
if tys.len() == 1 {
|
||||
tys.pop().unwrap().map(Type::Primitive)
|
||||
} else {
|
||||
let start = tys[0].start;
|
||||
let module_id = tys[0].module_id;
|
||||
let end = tys.last().unwrap().end;
|
||||
Node::new(Type::Union { tys }, start, end, module_id)
|
||||
}
|
||||
}),
|
||||
))
|
||||
.parse_next(i)?
|
||||
.map_err(|e: CompilationError| ErrMode::Backtrack(ContextError::from(e)))?;
|
||||
.parse_next(i)?;
|
||||
|
||||
Ok(type_)
|
||||
}
|
||||
|
||||
@ -2721,6 +2756,55 @@ fn primitive_type(i: &mut TokenSlice) -> PResult<Node<PrimitiveType>> {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn array_type(i: &mut TokenSlice) -> PResult<Node<Type>> {
|
||||
fn opt_whitespace(i: &mut TokenSlice) -> PResult<()> {
|
||||
ignore_whitespace(i);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
open_bracket(i)?;
|
||||
let ty = primitive_type(i)?;
|
||||
let len = opt((
|
||||
semi_colon,
|
||||
opt_whitespace,
|
||||
any.try_map(|token: Token| match token.token_type {
|
||||
TokenType::Number => {
|
||||
let value = token.uint_value().ok_or_else(|| {
|
||||
CompilationError::fatal(
|
||||
token.as_source_range(),
|
||||
format!("Expected unsigned integer literal, found: {}", token.value),
|
||||
)
|
||||
})?;
|
||||
|
||||
Ok(value as usize)
|
||||
}
|
||||
_ => Err(CompilationError::fatal(token.as_source_range(), "invalid array length")),
|
||||
}),
|
||||
opt(plus),
|
||||
))
|
||||
.parse_next(i)?;
|
||||
close_bracket(i)?;
|
||||
|
||||
let len = if let Some((tok, _, n, plus)) = len {
|
||||
if plus.is_some() {
|
||||
if n != 1 {
|
||||
return Err(ErrMode::Cut(ContextError::from(CompilationError::fatal(
|
||||
tok.as_source_range(),
|
||||
"Non-empty arrays are specified using `1+`, for a fixed-size array use just an integer",
|
||||
))));
|
||||
} else {
|
||||
ArrayLen::NonEmpty
|
||||
}
|
||||
} else {
|
||||
ArrayLen::Known(n)
|
||||
}
|
||||
} else {
|
||||
ArrayLen::None
|
||||
};
|
||||
|
||||
Ok(ty.map(|ty| Type::Array { ty, len }))
|
||||
}
|
||||
|
||||
fn uom_for_type(i: &mut TokenSlice) -> PResult<NumericSuffix> {
|
||||
any.try_map(|t: Token| t.value.parse()).parse_next(i)
|
||||
}
|
||||
|
@ -367,6 +367,8 @@ pub enum TokenType {
|
||||
QuestionMark,
|
||||
/// The @ symbol.
|
||||
At,
|
||||
/// `;`
|
||||
SemiColon,
|
||||
}
|
||||
|
||||
/// Most KCL tokens correspond to LSP semantic tokens (but not all).
|
||||
@ -396,6 +398,7 @@ impl TryFrom<TokenType> for SemanticTokenType {
|
||||
| TokenType::Hash
|
||||
| TokenType::Dollar
|
||||
| TokenType::At
|
||||
| TokenType::SemiColon
|
||||
| TokenType::Unknown => {
|
||||
anyhow::bail!("unsupported token type: {:?}", token_type)
|
||||
}
|
||||
@ -488,6 +491,18 @@ impl Token {
|
||||
value.parse().ok()
|
||||
}
|
||||
|
||||
pub fn uint_value(&self) -> Option<u32> {
|
||||
if self.token_type != TokenType::Number {
|
||||
return None;
|
||||
}
|
||||
let value = &self.value;
|
||||
let value = value
|
||||
.split_once(|c: char| c == '_' || c.is_ascii_alphabetic())
|
||||
.map(|(s, _)| s)
|
||||
.unwrap_or(value);
|
||||
value.parse().ok()
|
||||
}
|
||||
|
||||
pub fn numeric_suffix(&self) -> NumericSuffix {
|
||||
if self.token_type != TokenType::Number {
|
||||
return NumericSuffix::None;
|
||||
|
@ -88,6 +88,7 @@ pub(super) fn token(i: &mut Input<'_>) -> PResult<Token> {
|
||||
'@' => at,
|
||||
'0'..='9' => number,
|
||||
':' => colon,
|
||||
';' => semi_colon,
|
||||
'.' => alt((number, double_period, period)),
|
||||
'#' => hash,
|
||||
'$' => dollar,
|
||||
@ -282,6 +283,16 @@ fn colon(i: &mut Input<'_>) -> PResult<Token> {
|
||||
))
|
||||
}
|
||||
|
||||
fn semi_colon(i: &mut Input<'_>) -> PResult<Token> {
|
||||
let (value, range) = ';'.with_span().parse_next(i)?;
|
||||
Ok(Token::from_range(
|
||||
range,
|
||||
i.state.module_id,
|
||||
TokenType::SemiColon,
|
||||
value.to_string(),
|
||||
))
|
||||
}
|
||||
|
||||
fn period(i: &mut Input<'_>) -> PResult<Token> {
|
||||
let (value, range) = '.'.with_span().parse_next(i)?;
|
||||
Ok(Token::from_range(
|
||||
@ -689,7 +700,7 @@ const things = "things"
|
||||
#[test]
|
||||
fn test_unrecognized_token() {
|
||||
let module_id = ModuleId::from_usize(1);
|
||||
let actual = lex("12 ; 8", module_id).unwrap();
|
||||
let actual = lex("12 ~ 8", module_id).unwrap();
|
||||
|
||||
use TokenType::*;
|
||||
assert_tokens(&[(Number, 0, 2), (Unknown, 3, 4), (Number, 5, 6)], actual.as_slice());
|
||||
|
Reference in New Issue
Block a user