1
0

Add new function for EnumDeclaration

This commit is contained in:
Jeff 2024-06-21 13:56:04 -04:00
parent 880fb7cd1b
commit 578cb6ad16
3 changed files with 34 additions and 24 deletions

View File

@ -10,9 +10,23 @@ use super::{Evaluate, Evaluation, Type, TypeConstructor, WithPosition};
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct EnumDeclaration {
pub name: WithPosition<Identifier>,
pub type_parameters: Option<Vec<WithPosition<Identifier>>>,
pub variants: Vec<EnumVariant>,
name: WithPosition<Identifier>,
type_parameters: Option<Vec<WithPosition<Identifier>>>,
variants: Vec<EnumVariant>,
}
impl EnumDeclaration {
pub fn new(
name: WithPosition<Identifier>,
type_parameters: Option<Vec<WithPosition<Identifier>>>,
variants: Vec<EnumVariant>,
) -> Self {
Self {
name,
type_parameters,
variants,
}
}
}
impl Evaluate for EnumDeclaration {

View File

@ -729,12 +729,8 @@ pub fn parser<'src>(
)
.map_with(|((name, type_parameters), variants), state| {
Statement::EnumDeclaration(
EnumDeclaration {
name,
type_parameters,
variants,
}
.with_position(state.span()),
EnumDeclaration::new(name, type_parameters, variants)
.with_position(state.span()),
)
});

View File

@ -54,10 +54,10 @@ fn enum_declaration() {
assert_eq!(
parse(&lex("enum MyEnum { X, Y }").unwrap()).unwrap()[0],
Statement::EnumDeclaration(
EnumDeclaration {
name: Identifier::new("MyEnum").with_position((5, 11)),
type_parameters: None,
variants: vec![
EnumDeclaration::new(
Identifier::new("MyEnum").with_position((5, 11)),
None,
vec![
EnumVariant {
name: Identifier::new("X").with_position((14, 15)),
content: None
@ -67,7 +67,7 @@ fn enum_declaration() {
content: None
}
],
}
)
.with_position((0, 20))
)
);
@ -78,10 +78,10 @@ fn enum_with_contents() {
assert_eq!(
parse(&lex("enum MyEnum { X(str, int), Y(int) }").unwrap()).unwrap()[0],
Statement::EnumDeclaration(
EnumDeclaration {
name: Identifier::new("MyEnum").with_position((5, 11)),
type_parameters: None,
variants: vec![
EnumDeclaration::new(
Identifier::new("MyEnum").with_position((5, 11)),
None,
vec![
EnumVariant {
name: Identifier::new("X").with_position((14, 15)),
content: Some(vec![
@ -100,7 +100,7 @@ fn enum_with_contents() {
),])
}
]
}
)
.with_position((0, 35))
)
);
@ -111,13 +111,13 @@ fn enum_with_type_parameters() {
assert_eq!(
parse(&lex("enum MyEnum <T, U> { X(T), Y(U) }").unwrap()).unwrap()[0],
Statement::EnumDeclaration(
EnumDeclaration {
name: Identifier::new("MyEnum").with_position((5, 11)),
type_parameters: Some(vec![
EnumDeclaration::new(
Identifier::new("MyEnum").with_position((5, 11)),
Some(vec![
Identifier::new("T").with_position((13, 14)),
Identifier::new("U").with_position((16, 17))
]),
variants: vec![
vec![
EnumVariant {
name: Identifier::new("X").with_position((21, 22)),
content: Some(vec![TypeConstructor::Invokation(
@ -137,7 +137,7 @@ fn enum_with_type_parameters() {
)])
},
]
}
)
.with_position((0, 33))
)
);