Skip to content

Commit

Permalink
feat: capture decorator span
Browse files Browse the repository at this point in the history
  • Loading branch information
rvcas committed Feb 16, 2025
1 parent 5773288 commit ef764ba
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 52 deletions.
8 changes: 7 additions & 1 deletion crates/aiken-lang/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,13 @@ impl TypedDataType {
}

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Decorator {
pub struct Decorator {
pub kind: DecoratorKind,
pub location: Span,
}

#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum DecoratorKind {
Tag { value: String, base: Base },
Len { value: String, base: Base },
Encoding(DecoratorEncoding),
Expand Down
12 changes: 9 additions & 3 deletions crates/aiken-lang/src/parser/definition/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use crate::{
use chumsky::prelude::*;

pub fn decorators() -> impl Parser<Token, Vec<ast::Decorator>, Error = ParseError> {
let tag_value = select! { Token::Int { value, base } => ast::Decorator::Tag { value, base } };
let len_value = select! { Token::Int { value, base } => ast::Decorator::Len { value, base } };
let tag_value =
select! { Token::Int { value, base } => ast::DecoratorKind::Tag { value, base } };
let len_value =
select! { Token::Int { value, base } => ast::DecoratorKind::Len { value, base } };
let encoding_value = select! {
Token::Name { name } if name == "list" => ast::DecoratorEncoding::List,
Token::Name { name } if name == "constr" => ast::DecoratorEncoding::Constr
}
.map(ast::Decorator::Encoding);
.map(ast::DecoratorKind::Encoding);

just(Token::At)
.ignore_then(choice((
Expand All @@ -25,6 +27,10 @@ pub fn decorators() -> impl Parser<Token, Vec<ast::Decorator>, Error = ParseErro
encoding_value.delimited_by(just(Token::LeftParen), just(Token::RightParen)),
),
)))
.map_with_span(|kind, span| ast::Decorator {
kind,
location: span,
})
.repeated()
.or_not()
.map(|t| t.unwrap_or_default())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ DataType(
constructors: [
RecordConstructor {
decorators: [
Tag {
value: "117",
base: Decimal {
numeric_underscore: false,
Decorator {
kind: Tag {
value: "117",
base: Decimal {
numeric_underscore: false,
},
},
location: 21..30,
},
],
location: 21..41,
Expand All @@ -23,11 +26,14 @@ DataType(
},
RecordConstructor {
decorators: [
Tag {
value: "118",
base: Decimal {
numeric_underscore: false,
Decorator {
kind: Tag {
value: "118",
base: Decimal {
numeric_underscore: false,
},
},
location: 45..54,
},
],
location: 45..82,
Expand Down Expand Up @@ -59,11 +65,14 @@ DataType(
},
RecordConstructor {
decorators: [
Tag {
value: "1170",
base: Decimal {
numeric_underscore: false,
Decorator {
kind: Tag {
value: "1170",
base: Decimal {
numeric_underscore: false,
},
},
location: 86..96,
},
],
location: 86..131,
Expand Down Expand Up @@ -108,11 +117,14 @@ DataType(
},
RecordConstructor {
decorators: [
Tag {
value: "1171",
base: Decimal {
numeric_underscore: false,
Decorator {
kind: Tag {
value: "1171",
base: Decimal {
numeric_underscore: false,
},
},
location: 135..145,
},
],
location: 135..183,
Expand All @@ -133,11 +145,14 @@ DataType(
},
RecordConstructorArg {
decorators: [
Len {
value: "32",
base: Decimal {
numeric_underscore: false,
Decorator {
kind: Len {
value: "32",
base: Decimal {
numeric_underscore: false,
},
},
location: 164..172,
},
],
label: None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,42 @@ description: "Code:\n\n@tag(12)\npub type User {\n @len(32)\n name: ByteArray\
DataType(
DataType {
decorators: [
Tag {
value: "12",
base: Decimal {
numeric_underscore: false,
Decorator {
kind: Tag {
value: "12",
base: Decimal {
numeric_underscore: false,
},
},
location: 0..8,
},
],
constructors: [
RecordConstructor {
decorators: [
Tag {
value: "12",
base: Decimal {
numeric_underscore: false,
Decorator {
kind: Tag {
value: "12",
base: Decimal {
numeric_underscore: false,
},
},
location: 0..8,
},
],
location: 23..55,
name: "User",
arguments: [
RecordConstructorArg {
decorators: [
Len {
value: "32",
base: Decimal {
numeric_underscore: false,
Decorator {
kind: Len {
value: "32",
base: Decimal {
numeric_underscore: false,
},
},
location: 27..35,
},
],
label: Some(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ description: "Code:\n\n@encoding(list)\npub type Thing {\n name: ByteArray\n}\n
DataType(
DataType {
decorators: [
Encoding(
List,
),
Decorator {
kind: Encoding(
List,
),
location: 0..15,
},
],
constructors: [
RecordConstructor {
decorators: [
Encoding(
List,
),
Decorator {
kind: Encoding(
List,
),
location: 0..15,
},
],
location: 31..52,
name: "Thing",
Expand Down
4 changes: 2 additions & 2 deletions crates/aiken-lang/src/tipo/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ impl<'a> Environment<'a> {
parameters,
location,
constructors,
decorators,
decorators: _,
doc: _,
typed_parameters: _,
}) => {
Expand Down Expand Up @@ -1138,7 +1138,7 @@ impl<'a> Environment<'a> {
Definition::TypeAlias(TypeAlias {
location,
public,
decorators,
decorators: _,
parameters: args,
alias: name,
annotation: resolved_type,
Expand Down
8 changes: 0 additions & 8 deletions crates/aiken-lang/src/tipo/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,6 @@ fn infer_definition(
.tipo
.clone();

todo!("check decorators");

Ok(Definition::TypeAlias(TypeAlias {
doc,
location,
Expand All @@ -501,8 +499,6 @@ fn infer_definition(
constructors: untyped_constructors,
typed_parameters: _,
}) => {
todo!("check decorators");

let constructors = untyped_constructors
.into_iter()
.map(|constructor| {
Expand All @@ -512,8 +508,6 @@ fn infer_definition(

let preregistered_type = preregistered_fn.tipo.clone();

todo!("check decorators");

let args = preregistered_type.function_types().map_or(
Ok(vec![]),
|(args_types, _return_type)| {
Expand Down Expand Up @@ -542,8 +536,6 @@ fn infer_definition(
Rc::make_mut(&mut parent.tipo).set_opaque(true)
}

todo!("check decorators");

Ok(RecordConstructorArg {
decorators: arg.decorators,
label: arg.label,
Expand Down

0 comments on commit ef764ba

Please sign in to comment.