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)] #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct EnumDeclaration { pub struct EnumDeclaration {
pub name: WithPosition<Identifier>, name: WithPosition<Identifier>,
pub type_parameters: Option<Vec<WithPosition<Identifier>>>, type_parameters: Option<Vec<WithPosition<Identifier>>>,
pub variants: Vec<EnumVariant>, 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 { impl Evaluate for EnumDeclaration {

View File

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

View File

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